Wich is the good soulution My vs. School - curve fitting?

1 view (last 30 days)
Hy, I wonder wich is the good solution for this problem:
Nonlinear least square problem: function: y = x / (a + b.x) linearization: 1/y = a/x + b substitution: v = 1/y , u = 1/x >> model v = b + a.u
What we did in school:
x = [1 2 4 7]; y = [2 1 0.4 0.1];
v=1./y;
u=1./x;
n = length(x);
A=[ones(n,1), u']
xbeta=A\v'
Lbeta=xbeta;
beta0=flipud(Lbeta)
beta=fminsearch('kritfun',beta0)
r = [kritfun(beta0) , kritfun(beta)]
+ kritfun.m
function z=kritfun(beta)
a=beta(1);
b=beta(2);
x = [1 2 4 7];
y = [2 1 0.4 0.1];
error = y - x./(a + b*x );
z = sum((error .^2 ));
ML: xbeta =
7.3658
-8.1692
beta =
1.0e+014 *
-8.2085
4.1043
r =
11.0600 4.1700
but when I try in Curve Fitting tool too check the soulution , I get something else ..according to this video: youtube turtorial video too call CF I typed to prompt >>cftool
When I try this I get the same number like in the Curve fitting tool , the a,b parameters is my beta(1),beta(2) and the SSE number is my r(2) ..what was same when I tried this:
x = [1 2 4 7];
y = [2 1 0.4 0.1];
%u = 1./x; %these u and v I dont use it...but I didnt know when not using substituion is correct the beta and r(2) , but when I didnt use I get same numbers lik in the Curve Fitting tool
%v = 1./y;
n = length(x);
X = [ ones(n,1), x'];
btr = X\y'
beta0 = flipud(btr)'
beta = fminsearch('mnsNLcFUN',beta0)
r = [mnsNLcFUN(beta0), mnsNLcFUN(beta)]
+ mnsNLcFUN.m function is:
function z=mnsNLcFUN(beta)
a=beta(1);
b=beta(2);
x = [1 2 4 7];
y = [2 1 0.4 0.1];
error = y - x./(a+b.*x);
z = sum((error .^2 ));

Accepted Answer

Star Strider
Star Strider on 3 Nov 2014
Don’t linearise! It distorts the errors and results in inaccurate parameter estimates.
Using fminsearch gives a decent fit to the data:
x = [1 2 4 7];
y = [2 1 0.4 0.1];
fy = @(b,x) x./(b(1) + b(2).*x);
CF = @(b) sum((y-fy(b,x)).^2);
[b, fv] = fminsearch(CF, [1;1])
figure(1)
plot(x, y, '*r')
hold on
plot(x, fy(b,x), '-b')
hold off
producing the parameter estimates:
b =
-2.0553e+000
2.5517e+000
where a=b(1) and b=b(2).
  2 Comments
x y
x y on 3 Nov 2014
This code is awesome what You writted, is so simple and short. Is always work when I in fminsearch use start point 1,1 ? In the school and the book is were written in example,...the linearization. When have nonlinear function is doing linearization and after back transformation and after use the backstransformated parameters used in fminsearch to the start point,... So in my problem was I didnt know in this problem I need or how to do back transformation or linearization....
Star Strider
Star Strider on 3 Nov 2014
Thank you!
The starting parameter estimates [1;1] are a guess and could be replaced by any reasonable guess.
I have the Optimization and Statistics Toolboxes, not the Curve Fitting Toolbox, so I cannot replicate your code exactly.
If you plot the results of the linearisation, you will see that it does not produce accurate parameter estimates:
ab = [1./x' ones(size(x'))]\(1./y') % Linearisation
fy = @(b,x) x./(b(1) + b(2).*x);
CF = @(b) sum((y-fy(b,x)).^2);
[b, fv] = fminsearch(CF, [1;1])
figure(1)
plot(x, y, '*r')
hold on
plot(x, fy(b,x), '-b')
plot(x, fy(ab,x), '-g')
hold off
legend('Data', 'Nonlinear Least Squares', 'Linearisation')

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!