Nonlinear least square regression

i have (x , y) data
the function between x and y is y = 0.392* (1 - (x / b1) .^ b2
i want to use nonlinear least square regression to obtain the values of b1 and b2
can any one help me with the structure of the Matlab program
thanks in advance

1 Comment

Matt J
Matt J on 27 May 2013
Edited: Matt J on 27 May 2013
You're missing a parenthesis ')' in your regression model. It's important for us to know where it should go.

Sign in to comment.

Answers (1)

If you have the Statistics Toolbox, then you can use the nlinfit() function.
Type
doc nlinfit
in the interface, or see this page for details: http://www.mathworks.com/help/stats/nlinfit.html

8 Comments

ameen
ameen on 27 May 2013
Edited: Matt J on 27 May 2013
thank you for your reply
i put the Matlab program as follows :
>> clear
>> data=[0.5040371 0.261115
0.4485177 0.246058
0.40458907 0.248194
0.29249153 0.272286
0.502260793 0.242385
0.448165094 0.226992
0.402305 0.232877
0.288641 0.266967
0.501377 0.275141
0.447111 0.272572
0.397536 0.272892
0.335926 0.275011
0.500058 0.249249
0.44641 0.24623
0.403158 0.252487
0.341357 0.265364
0 0.39185];
t = data(:,1);
y = data(:,2);
F = @(x,xdata)0.392*(1-(xdata/x(1))).^x(2);
x0 = [0.65 1];
x = nlinfit(t,y,F,x0);
>>
but the values of x is too bigger than what is expected
Matt J
Matt J on 27 May 2013
Edited: Matt J on 27 May 2013
NLINFIT seems to think that y(t) = .392* exp(-t) is a pretty good model for your data. Note that this is the same as
y(t) = lim_n-->inf 0.392*(1+t/n)^n
so the fitting algorithm will obviously look for large x(i) in your proposed model. Your data does look vaguely exponential...
what i want to do is obtaining the values of x1 and x2 from this equation
y = 0.392*(1-(x / x(1) ) ).^x(2)
using nonlinear least square regression
what is the best function ???
lsqnonlin or nlinfit or lsqcurvefit or another function ????
Matt J
Matt J on 27 May 2013
Edited: Matt J on 27 May 2013
As I explained, I think you've already obtained the best x1 and x2. The results nlinfit gave you are correct, in spite of what you expect.
ameen
ameen on 27 May 2013
Edited: ameen on 27 May 2013
Using nlinfit:
data=[0.5040371 0.261115
0.4485177 0.246058
0.40458907 0.248194
0.29249153 0.272286
0.502260793 0.242385
0.448165094 0.226992
0.402305 0.232877
0.288641 0.266967
0.501377 0.275141
0.447111 0.272572
0.397536 0.272892
0.335926 0.275011
0.500058 0.249249
0.44641 0.24623
0.403158 0.252487
0.341357 0.265364
0 0.39185];
t = data(:,1);
y = data(:,2);
F = @(x,xdata)0.392*(1-(xdata/x(1))).^x(2);
x0 = [0.65 1];
beta = nlinfit(t,y,F,x0)
beta =
1.0e+09 *
1.9793 2.0014
Using lsqcurvefit :
data=[0.5040371 0.261115
0.4485177 0.246058
0.40458907 0.248194
0.29249153 0.272286
0.502260793 0.242385
0.448165094 0.226992
0.402305 0.232877
0.288641 0.266967
0.501377 0.275141
0.447111 0.272572
0.397536 0.272892
0.335926 0.275011
0.500058 0.249249
0.44641 0.24623
0.403158 0.252487
0.341357 0.265364
0 0.39185];
t = data(:,1);
y = data(:,2);
F = @(x,xdata)0.392*(1-(xdata/x(1))).^x(2);
x0 = [0.65 1];
[x,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,t,y);
x =
258.1339 261.1441
which one is correct ???
Matt J
Matt J on 27 May 2013
Edited: Matt J on 27 May 2013
which one is correct ???
Both of them. As you can see from running the code below, they both produce virtually identical curves. Again, this is because making your parameters large causes the curve to converge to 0.392*exp(-t).
t=sort(t);
f=@(t,x) 0.392*(1-(t./x(1))).^x(2);
beta=[1.9793 2.0014]*1e9;
x =[258.1339 261.1441];
plot(t,f(t,beta), '*-' ,t,f(t,x),'o--',t,0.392*exp(-t),'d-.')
legend('Using Beta','Using x','Using 0.392*exp(-t)')
Thank you very much Matt
so the problem is with my data ??
Matt J
Matt J on 27 May 2013
Edited: Matt J on 27 May 2013
Or your model. Maybe you should be fitting
y=x(1)*exp(-x(2)*t)

Sign in to comment.

Tags

Asked:

on 27 May 2013

Community Treasure Hunt

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

Start Hunting!