0

I need to fit a line to a set of about 100 data points, the set of data follows the Pacejka formula which looks like this:

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

where Dy, Cy, By, Ey, and Svy are the coefficients being solved for.

I can get it to graph a line with this code but it is no where close to the data.

here is what i have for fitting the formula so far. how do i change it to be closer to the actual line of best fit?

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)

this is what my code returns now enter image description here enter image description here

Vbasic4now
  • 579
  • 3
  • 6
  • 33

1 Answers1

3

Just like you did, just write down that formula:

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)

EDIT:

Based on the Wikipedia article, here's a small example:

% 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

fit

Result:

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

Note that using a starting point like [1 1 1 1] is far from the true solution, so fitting will stop without converging (the scale of D parameter is very different from the other params B, C, and E)... Instead I had to start somewhere closer [1 1 1000 1].

Amro
  • 123,847
  • 25
  • 243
  • 454
  • ive updated the question. that code works to give me a line, but the line is not anywhere close to the data. what am i doing wrong? – Vbasic4now Apr 17 '16 at 21:13
  • your solution probably did not converge.. Either increase the max number of iterations, or provide a better starting point – Amro Apr 17 '16 at 21:41
  • @SamQL I added an example that looks like your data – Amro Apr 17 '16 at 21:50