在matlab中将曲线拟合到具有多个不同系数的一组数据
Fitting a curve to a set of data with multiple different coefficients in matlab
我需要用一条直线拟合一组大约 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)
这就是我现在的代码returns
就像你一样,写下那个公式:
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
参数的比例与其他参数[=15非常不同=]、C
和 E
)... 相反,我不得不从更近的地方开始 [1 1 1000 1]
。
我需要用一条直线拟合一组大约 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)
这就是我现在的代码returns
就像你一样,写下那个公式:
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
参数的比例与其他参数[=15非常不同=]、C
和 E
)... 相反,我不得不从更近的地方开始 [1 1 1000 1]
。