如何用MATLAB将一组散点数据拟合成一个对数函数

横坐标为x对应纵坐标数据为y,希望将其拟合成误差尽量小的对数函数y=a+blgS.如何在matlab里面实现。希望画出的图形里面既有散点,又有拟合的曲线。哪位高手不吝赐教,谢谢了!
x=[0.06 0.15 0.24 0.32 0.45 0.55 0.67 0.76 0.85 0.95 1]
y=[0.625 0.455 0.41 0.39 0.26 0.15 0.03 0.01 -0.01 -0.05 -0.06]

第1个回答  2010-07-02
xdata=[0.06 0.15 0.24 0.32 0.45 0.55 0.67 0.76 0.85 0.95 1];
ydata=[0.625 0.455 0.41 0.39 0.26 0.15 0.03 0.01 -0.01 -0.05 -0.06];
x0=[1;1];
fun=@(x,xdata) x(1)+x(2)*log(xdata);
x=lsqcurvefit(fun,x0,xdata,ydata);
xn=0:0.01:1;
scatter(xdata,ydata,'X');
hold on
plot(xn,x(1)+x(2)*log(xn),'g');
第2个回答  推荐于2018-05-10
x=[0.06 0.15 0.24 0.32 0.45 0.55 0.67 0.76 0.85 0.95 1];
y=[0.625 0.455 0.41 0.39 0.26 0.15 0.03 0.01 -0.01 -0.05 -0.06];
x=x';y=y';
st_ = [0.1 0.7];
ft_ = fittype('a+b*log(x)' ,...
'dependent',{'y'},'independent',{'x'},...
'coefficients',{'a', 'b'});
cf_ = fit(x,y,ft_ ,'Startpoint',st_)
plot(x,y,'o')
hold on,
plot(cf_,'fit',0.95)本回答被网友采纳
第3个回答  2010-07-02
x=[0.06 0.15 0.24 0.32 0.45 0.55 0.67 0.76 0.85 0.95 1];
y=[0.625 0.455 0.41 0.39 0.26 0.15 0.03 0.01 -0.01 -0.05 -0.06];
ba=[log10(x(:)),ones(size(x(:)))]\y(:); %如果你的lgS是以自然对数为底的话,请用log函数代替log10,下面作图也是一样的.
a=ba(2)
b=ba(1)

plot(x,y,'*',x,a+b*log10(x),'r-')
legend('原始数据','拟合值')
相似回答