Curve Fitting using Least Squares

10 Ansichten (letzte 30 Tage)
MATLABhelp
MATLABhelp am 25 Jan. 2019
Kommentiert: Star Strider am 25 Jan. 2019
Given a data table with values of x and y and supposed to approximate relationship between x and y. The first case is a parabola with equation y = a0 + a1*x + a2*(x^2)
and the second case is a saturation growth rate equation with the equation y = a0*(x/(a1+x)). Must find the parameters using normal equations formulation of least squares.
Finished the code for the parabola and it is the following
x = [20 40 60 80 100 120 140 160];
y = [13 22 30 36 40 43 45 46];
A = [ones(size(x))' x' (x.^2)'];
b = inv(A'*A)*(A'*y');
s = b(3)*x.^2+b(2)*x+b(1);
plot(x,s,'k')
hold off
How can you modify code for it to run for saturation growth rate equation?
  2 Kommentare
Walter Roberson
Walter Roberson am 25 Jan. 2019
Bearbeitet: Walter Roberson am 25 Jan. 2019
We recommend that you avoid using inv() for numeric stability reasons.
b = (A'*A) \ (A'*y');
but except in case of singular A, that should be the same as
b = A \ y';
MATLABhelp
MATLABhelp am 25 Jan. 2019
Thank you!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 25 Jan. 2019
Try this:
fcn = @(b,x) b(1).*x./(b(2)+x);
x = [20 40 60 80 100 120 140 160];
y = [13 22 30 36 40 43 45 46];
B0 = [50; 50];
B = fminsearch(@(b) norm(y - fcn(b,x)), B0);
s = fcn(B,x);
figure
plot(x, y, 'pg')
hold on
plot(x,s,'k')
hold off
A = [ones(size(x))' x' (x.^2)'];
b = A\y';
s = A*b;
figure
plot(x,y,'pg')
hold on
plot(x,s,'k')
hold off
The first equation (in my code) is nonlinear in the parameters, in that the derivative with respect to the each parameter is a function of the same parameter or other parameters. A linear approach will not provide optimal estimates of the parameters.
The second (in my code) is linear in the parameters, so a linear approach will provide optimal parameter estimates.
  2 Kommentare
MATLABhelp
MATLABhelp am 25 Jan. 2019
Thank you!
Star Strider
Star Strider am 25 Jan. 2019
As always, my pleasure!
Correcting one small error, ‘... the derivative with respect to the each parameter ...’ should be ‘... the partial derivative with respect to the each parameter ...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Linear and Nonlinear Regression finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by