function parameter=customfit(f,x,y,startpoint) %用最小二乘法求曲线拟合的程序。 %f是待求函数,x和y是取样点坐标,parameter是待求系数,startpoint是系数初值 f2=@(p)(sum((f(x,p)-y).^2)); parameter=fminsearch(f2,startpoint); 例子 x=[0.01 0.1 0.5 0.75 1 1.25 1.5 1.75 2 2.25 2.5]; y=[2.4 3.3 13.5 18.9 24 27 35 37 41 47 58]; f=@(x,p)(p(1)*x.^2+p(2)*x);%p是待求系数
|