用最小二乘法求一个形如y=a+bx^2拟合函数去拟合下列数据:x=19,25,31,38,44 y=19.0,32.3,

1个回答

  • x=[19,25,31,38,44];

    y=[19.0,32.3,49.0,73.3,97.8];

    fun1=inline('c(1)+c(2)*x.^2','c','x'); %拟合函数

    c=lsqcurvefit(fun1,[0,0],x,y) %求拟合系数

    ['y=',num2str(c(1)),'+',num2str(c(2)),'x^2']%拟合函数表达式

    y1=c(1)+c(2)*x.^2; %拟合函数表达式值

    plot (x,y,'b*',x,y1,'r-');%绘制x,y点与拟合函数,比较拟合程度

    进行结果:

    fun1 =

    Inline function:

    fun1(c,x) = c(1)+c(2)*x.^2

    Optimization terminated:first-order optimality less than OPTIONS.TolFun,

    and no negative/zero curvature detected in trust region model.

    c =

    0.972578657321613 0.0500351242188565

    ans =

    y=0.97258+0.050035x^2