【问题标题】:Fitting a curve to a set of data with multiple different coefficients in matlab在matlab中将曲线拟合到具有多个不同系数的一组数据
【发布时间】:2016-08-09 11:22:36
【问题描述】:

我需要将一条线拟合到一组大约 100 个数据点,这组数据遵循 Pacejka 公式,如下所示:

Fy = Dy sin [Cy arctan {By x - Ey (By x - arctan (Byx))}] + Svy

其中 Dy、Cy、By、Ey 和 Svy 是要求解的系数。

我可以让它用这段代码绘制一条线,但它离数据很近。

到目前为止,这是我用来拟合公式的内容。如何将其更改为更接近实际的最佳拟合线?

x = SA;
y = Fy;
expr = 'D * sin(C * atan(B*x - E*(B* x - atan(B*x)))) + A';
ft = fittype(expr, 'independent', 'x');
opts = fitoptions('Method', 'NonlinearLeastSquares');
opts.StartPoint = ones(1,5);
[fitresult, gof] = fit(x, y, ft, opts)
plot(fitresult, x, y)

这是我的代码现在返回的内容

【问题讨论】:

标签: matlab curve-fitting


【解决方案1】:

就像你做的那样,写下那个公式:

expr = 'D * sin(C * atan(B*x - E*(B* x - atan(B*x)))) + A';
ft = fittype(expr, 'independent', 'x');
opts = fitoptions('Method', 'NonlinearLeastSquares');
opts.StartPoint = ones(1,5);  % [A,B,C,D,E]
[fitresult, gof] = fit(x, y, ft, opts)
plot(fitresult, x, y)

编辑:

基于Wikipedia article,下面是一个小例子:

% some data based on equation
B = 0.714;
C = 1.4;
D = 800;
E = -0.2;
f = @(x) D * sin(C * atan(B*(1-E)*x + E*atan(B*x)));
x = linspace(0,10,200)';  %'
y = f(x);

% add noise
yy = y + randn(size(x))*16;

% fit
%expr = 'D * sin(C * atan(B*(1-E)*x + E*atan(B*x)))';
expr = 'D * sin(C * atan(B*x - E*(B* x - atan(B*x))))';
ft = fittype(expr, 'independent', 'x', 'dependent','y');
opts = fitoptions('Method', 'NonlinearLeastSquares');
opts.StartPoint = [1 1 1000 1];  % [B C D E]
[fitresult, gof] = fit(x, y, ft, opts)

% plot
yhat = feval(fitresult, x);
h = plot(x,y,'b-', x,yhat,'r-', x,yy,'g.');
set(h, 'LineWidth',2)
legend({'y', 'yhat', 'y+noise'}, 'Location','SouthEast')
grid on

结果:

fitresult = 
     General model:
     fitresult(x) = D * sin(C * atan(B*x - E*(B* x - atan(B*x))))
     Coefficients (with 95% confidence bounds):
       B =      0.5916  (0.5269, 0.6563)
       C =       1.899  (1.71, 2.089)
       D =       783.7  (770.5, 796.9)
       E =       1.172  (1.136, 1.207)
gof = 
           sse: 6.6568e+04
       rsquare: 0.9834
           dfe: 196
    adjrsquare: 0.9832
          rmse: 18.4291

请注意,使用像[1 1 1 1] 这样的起点远非真正的解决方案,因此拟合将停止而不收敛(D 参数的比例与其他参数 BCE)...相反,我不得不从更接近 [1 1 1000 1] 的地方开始。

【讨论】:

  • 我更新了问题。该代码可以给我一条线,但这条线并不靠近数据。我做错了什么?
  • 您的解决方案可能没有收敛。要么增加最大迭代次数,要么提供更好的起点
  • @SamQL 我添加了一个看起来像您的数据的示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 2016-10-06
  • 2015-06-27
  • 2019-02-20
  • 1970-01-01
相关资源
最近更新 更多