且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MATLAB数据拟合到逆二次方程式

更新时间:2023-02-26 16:12:16

您提供的模型可以使用简单的方法来解决:

The model you give can be solved using simple methods:

% model function
f = @(a,b,c,x) 1./(a*x.^2+b*x+c);

% noise function 
noise = @(z) 0.005*randn(size(z));

% parameters to find
a = +3;
b = +4;
c = -8;

% exmample data
x = -2:0.01:2;    x = x + noise(x);
y = f(a,b,c, x);  y = y + noise(y);


% create linear system Ax = b, with 
% A = [x²  x  1]
% x = [a; b; c]
% b = 1/y;
A = bsxfun(@power, x.', 2:-1:0);

A\(1./y.')

结果:

ans = 
 3.035753123094593e+00  % (a)
 4.029749103502019e+00  % (b)
-8.038644874704120e+00  % (c)

之所以可行,是因为您提供的模型是线性模型,在这种情况下,反斜杠运算符将给出解决方案(尽管1./y有点危险...)

This is possible because the model you give is a linear one, in which case the backslash operator will give the solution (the 1./y is a bit dangerous though...)

在拟合非线性线性模型时,请查看lsqcurvefit(优化工具箱),或者您可以使用fmincon(优化工具箱),fminsearchfminunc.

When fitting non-linear models, take a look at lsqcurvefit (optimization toolbox), or you can write your own implementation using fmincon (optimization toolbox), fminsearch or fminunc.

此外,如果碰巧有曲线拟合工具箱,请键入help curvefit并从此处开始.

Also, if you happen to have the curve fitting toolbox, type help curvefit and start there.