matlab中怎么实现一类非线性函数的最优拟合?

2025-09-27 15:12:47

1、点击“新建脚本”。

得图1所示。

matlab中怎么实现一类非线性函数的最优拟合?

2、首先,编写一段程序如下:

t = (0:.1:2)';

y = [5.8955 3.5639 2.5173 1.9790 1.8990 1.3938 1.1359 1.0096 1.0343 ...

   0.8435 0.6856 0.6100 0.5392 0.3946 0.3903 0.5474 0.3459 0.1370 ...

   0.2211 0.1704 0.2636]';

plot(t,y,'ro')

hold on

h = plot(t,y,'b');

hold off

title('Input data');

ylim([0 6])

3、按“Enter”键。

得图2所示。

matlab中怎么实现一类非线性函数的最优拟合?

4、目标是用两个线性参数和两个非线性参数将以下函数拟合到数据中:

  y =  C(1)*exp(-lambda(1)*t) + C(2)*exp(-lambda(2)*t)

为了适应这个函数,我们创建了一个函数FITFUN。

给定非线性参数(lambda)和数据(t和y),FITFUN计算适合该方程的误差,并更新(h)线。

type fitfun

5、猜测lambda的初始估计(开始),然后调用FMINSEARCH。

通过调整lambda,它使从FITFUN返回的错误最小化。

它返回lambda的最终值。

使用输出函数绘制中间拟合。

start = [1;0];

% We use an anonymous function to pass additional parameters t, y, h to the

% output function.

outputFcn = @(x,optimvalues,state) fitoutputfun(x,optimvalues,state,t,y,h);

options = optimset('OutputFcn',outputFcn,'TolX',0.1);

estimated_lambda = fminsearch(@(x)fitfun(x,t,y),start,options)

6、按“Enter”键。

得图3所示。

matlab中怎么实现一类非线性函数的最优拟合?

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢