Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
nolinear regression model

Subject: nolinear regression model

From: Ender

Date: 3 Jul, 2008 19:50:04

Message: 1 of 21

I am trying to determine the constants for this nolinear
equation. This is my equation:

y = a+b*(1-exp(-t/c));

I have the vectors for y & t;

I want to solve for the constants a, b,c

I looked at the help documentation on my system but I
could not find an example that was similiar to what I am
trying to solve for.

I am unsure how to write the code that will solve this
equation. Any help would be appreciated.

--Ender--

Subject: nolinear regression model

From: junoexpress

Date: 3 Jul, 2008 20:12:14

Message: 2 of 21

On Jul 3, 3:50 pm, "Ender " <jr...@msstate.edu> wrote:
> I am trying to determine the constants for this nolinear
> equation. This is my equation:
>
> y = a+b*(1-exp(-t/c));
>
> I have the vectors for y & t;
>
> I want to solve for the constants a, b,c
>
> I looked at the help documentation on my system but I
> could not find an example that was similiar to what I am
> trying to solve for.
>
> I am unsure how to write the code that will solve this
> equation. Any help would be appreciated.
>
> --Ender--

I believe that if the errors in your system can be modeled as
Gaussian, you can use other approaches that are much easier to
implement. For example, you can use least-squares and simply do a
search to find the parameter values {a,b,c} minimizing the residual.
If you have decent initial estimates for a,b,c (which you should be
able to get in this case: for example a can be estimated by the y-
intercept, which in your case, is simply the measurement at the first
time point you take), the process is not computationally very
cumbersome.


M

Subject: nolinear regression model

From: Bill

Date: 4 Jul, 2008 01:24:03

Message: 3 of 21

Put the equation to fit in an m script. Then use
LSQCURVEFIT to compute the parameter vector. You will need
realistic initial guesses but thats not usually that hard.
This works extremely well with a 5 term version of this
response if you switch to a lightly damped system....

Subject: nolinear regression model

From: Ender

Date: 7 Jul, 2008 16:40:05

Message: 4 of 21

"Bill " <william.nospam.a.cobb@gm.com> wrote in message
<g4ju3j$o3c$1@fred.mathworks.com>...
> Put the equation to fit in an m script. Then use
> LSQCURVEFIT to compute the parameter vector. You will
need
> realistic initial guesses but thats not usually that
hard.
> This works extremely well with a 5 term version of this
> response if you switch to a lightly damped system....

I am using a student version of MATLAB so I do not have
the LSQCURVEFIT function or the Optimization Toolbar. It
was suggested to take the natural log of the function then
solve for the linearized constants. Do you have any easier
suggestions.

I am not very familiar with curve fitting in MATLAB so I
am trying to find the easiest method possible to solve the
function.

--Ender--

Subject: nolinear regression model

From: Peter Perkins

Date: 7 Jul, 2008 18:22:59

Message: 5 of 21

Ender wrote:

> I am using a student version of MATLAB so I do not have
> the LSQCURVEFIT function or the Optimization Toolbar. It
> was suggested to take the natural log of the function then
> solve for the linearized constants. Do you have any easier
> suggestions.

Ender, I believe the student version includes the Statistics Toolbox, does it
not? You may find this demo, which illustrates some differences between
linearizing versus fitting a nonlinear regression using NLINFIT, helpful:

<http://www.mathworks.com/products/statistics/demos.html?file=/products/demos/shipping/stats/xform2lineardemo.html>

Subject: nolinear regression model

From: Miroslav Balda

Date: 7 Jul, 2008 19:17:01

Message: 6 of 21

Peter Perkins <Peter.PerkinsRemoveThis@mathworks.com> wrote
in message <g4tmu3$sid$1@fred.mathworks.com>...
> Ender wrote:
>
> > I am using a student version of MATLAB so I do not have
> > the LSQCURVEFIT function or the Optimization Toolbar. It
> > was suggested to take the natural log of the function then
> > solve for the linearized constants. Do you have any easier
> > suggestions.
>
> Ender, I believe the student version includes the
Statistics Toolbox, does it
> not? You may find this demo, which illustrates some
differences between
> linearizing versus fitting a nonlinear regression using
NLINFIT, helpful:
>
>
<http://www.mathworks.com/products/statistics/demos.html?file=/products/demos/shipping/stats/xform2lineardemo.html>

Hi Peter,

Even without functions from Optimization Toolbox, you may
solve the problem by using some functions from the File
exchange collection of functions. I have built the function
LMFnlsq, which you may find under an identification number
17534, which solves overdetermined systems of nonlinear
equations in trhe least squares sense. You simply bild a
function, say res.m, which evaluates residuals f(x)-y, where
f(x) is your formula with x=[a;b;c] and y is a vector of
measured values.

You may find howto use the function when calling help LMFnlsq.

I hope that it helps.

Mira

Subject: nolinear regression model

From: Ender

Date: 7 Jul, 2008 19:52:01

Message: 7 of 21

"Miroslav Balda" <miroslav.nospam@balda.cz> wrote in
message <g4tq3d$3el$1@fred.mathworks.com>...
> Peter Perkins <Peter.PerkinsRemoveThis@mathworks.com>
wrote
> in message <g4tmu3$sid$1@fred.mathworks.com>...
> > Ender wrote:
> >
> > > I am using a student version of MATLAB so I do not
have
> > > the LSQCURVEFIT function or the Optimization
Toolbar. It
> > > was suggested to take the natural log of the
function then
> > > solve for the linearized constants. Do you have any
easier
> > > suggestions.
> >
> > Ender, I believe the student version includes the
> Statistics Toolbox, does it
> > not? You may find this demo, which illustrates some
> differences between
> > linearizing versus fitting a nonlinear regression using
> NLINFIT, helpful:
> >
> >
> <http://www.mathworks.com/products/statistics/demos.html?
file=/products/demos/shipping/stats/xform2lineardemo.html>
>
> Hi Peter,
>
> Even without functions from Optimization Toolbox, you may
> solve the problem by using some functions from the File
> exchange collection of functions. I have built the
function
> LMFnlsq, which you may find under an identification
number
> 17534, which solves overdetermined systems of nonlinear
> equations in trhe least squares sense. You simply bild a
> function, say res.m, which evaluates residuals f(x)-y,
where
> f(x) is your formula with x=[a;b;c] and y is a vector of
> measured values.
>
> You may find howto use the function when calling help
LMFnlsq.
>
> I hope that it helps.
>
> Mira
>
>

I am still having some trouble with the nonlinear curve
fit.
I went to the example in the optimization toolbox and
tried their example.Once it worked, I put my equation into
the code and tried to plot the results. This is my
equation:

modelFun = @(p,x) (p(1)+p(2)*(-p(2))*exp(x/p(3)))/log(y);
paramEstsLin = [ones(size(x)), x,x] \ log(y);
paramEstsLin = exp(paramEstsLin)

Once again I am trying to model this equation and solve
for the constants.
% y = a+b*(1-exp(-t/c));
or log(a+b-b*exp(-t/c));

I am not sure if I set up the curve fit correctly.

MatLab gave me results values for the constants, then
MATLAB gave me an error when I tried plotting the function.
This is what the error said:


??? Error using ==> mrdivide
Matrix dimensions must agree.

Error in ==> @(p,x)(p(1)+p(2)*(-p(2))*exp(x/p(3)))/log(y)


Error in ==> practice at 11
yyLin = modelFun(paramEstsLin, xx);

I could use some help debugging this code.
Thanks!

-Ender-

Subject: nolinear regression model

From: Ender

Date: 7 Jul, 2008 20:23:02

Message: 8 of 21

"Ender " <jr147@msstate.edu> wrote in message <g4ts51
$ndv$1@fred.mathworks.com>...
> "Miroslav Balda" <miroslav.nospam@balda.cz> wrote in
> message <g4tq3d$3el$1@fred.mathworks.com>...
> > Peter Perkins <Peter.PerkinsRemoveThis@mathworks.com>
> wrote
> > in message <g4tmu3$sid$1@fred.mathworks.com>...
> > > Ender wrote:
> > >
> > > > I am using a student version of MATLAB so I do not
> have
> > > > the LSQCURVEFIT function or the Optimization
> Toolbar. It
> > > > was suggested to take the natural log of the
> function then
> > > > solve for the linearized constants. Do you have
any
> easier
> > > > suggestions.
> > >
> > > Ender, I believe the student version includes the
> > Statistics Toolbox, does it
> > > not? You may find this demo, which illustrates some
> > differences between
> > > linearizing versus fitting a nonlinear regression
using
> > NLINFIT, helpful:
> > >
> > >
> >
<http://www.mathworks.com/products/statistics/demos.html?
>
file=/products/demos/shipping/stats/xform2lineardemo.html>
> >
> > Hi Peter,
> >
> > Even without functions from Optimization Toolbox, you
may
> > solve the problem by using some functions from the File
> > exchange collection of functions. I have built the
> function
> > LMFnlsq, which you may find under an identification
> number
> > 17534, which solves overdetermined systems of nonlinear
> > equations in trhe least squares sense. You simply bild
a
> > function, say res.m, which evaluates residuals f(x)-y,
> where
> > f(x) is your formula with x=[a;b;c] and y is a vector
of
> > measured values.
> >
> > You may find howto use the function when calling help
> LMFnlsq.
> >
> > I hope that it helps.
> >
> > Mira
> >
> >
>
> I am still having some trouble with the nonlinear curve
> fit.
> I went to the example in the optimization toolbox and
> tried their example.Once it worked, I put my equation
into
> the code and tried to plot the results. This is my
> equation:
>
> modelFun = @(p,x) (p(1)+p(2)*(-p(2))*exp(x/p(3)))/log(y);
> paramEstsLin = [ones(size(x)), x,x] \ log(y);
> paramEstsLin = exp(paramEstsLin)
>
> Once again I am trying to model this equation and solve
> for the constants.
> % y = a+b*(1-exp(-t/c));
> or log(a+b-b*exp(-t/c));
>
> I am not sure if I set up the curve fit correctly.
>
> MatLab gave me results values for the constants, then
> MATLAB gave me an error when I tried plotting the
function.
> This is what the error said:
>
>
> ??? Error using ==> mrdivide
> Matrix dimensions must agree.
>
> Error in ==> @(p,x)(p(1)+p(2)*(-p(2))*exp(x/p(3)))/log(y)
>
>
> Error in ==> practice at 11
> yyLin = modelFun(paramEstsLin, xx);
>
> I could use some help debugging this code.
> Thanks!
>
> -Ender-



I also tried the LMFnlsq. examples from the help option. I
could not get the code to work as it is written. I tried
both links

http://www.mathworks.com/matlabcentral/fileexchange/loadFil
e.do?objectId=17534

and the example on this link

http://www.mathworks.com/matlabcentral/fileexchange/loadFil
e.do?objectId=16063

I copied and pasted the examples but each time I got an
different error. This is one of the errors:


??? Attempt to execute SCRIPT LMFsolve as a function:
Error in ==> LMFsolve at 3
[x,ssq,cnt]=LMFsolve(ros,[-1.2,1],'Display',1,'MaxIter',50)

Subject: nolinear regression model

From: John D'Errico

Date: 7 Jul, 2008 23:57:01

Message: 9 of 21

"Ender " <jr147@msstate.edu> wrote in message
<g4tgt5$t4l$1@fred.mathworks.com>...
> "Bill " <william.nospam.a.cobb@gm.com> wrote in message
> <g4ju3j$o3c$1@fred.mathworks.com>...
> > Put the equation to fit in an m script. Then use
> > LSQCURVEFIT to compute the parameter vector. You will
> need
> > realistic initial guesses but thats not usually that
> hard.
> > This works extremely well with a 5 term version of this
> > response if you switch to a lightly damped system....
>
> I am using a student version of MATLAB so I do not have
> the LSQCURVEFIT function or the Optimization Toolbar. It
> was suggested to take the natural log of the function then
> solve for the linearized constants. Do you have any easier
> suggestions.
>
> I am not very familiar with curve fitting in MATLAB so I
> am trying to find the easiest method possible to solve the
> function.
>
> --Ender--

NO. You cannot log this function and
get anything useful. The problem is the
constant term.

Look for ezyfit 2.0 on the file exchange.

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?
objectId=10176&objectType=file

John

Subject: nolinear regression model

From: Per Sundqvist

Date: 8 Jul, 2008 01:42:02

Message: 10 of 21

"Ender " <jr147@msstate.edu> wrote in message
<g4jahc$kle$1@fred.mathworks.com>...
> I am trying to determine the constants for this nolinear
> equation. This is my equation:
>
> y = a+b*(1-exp(-t/c));
>
> I have the vectors for y & t;
>
> I want to solve for the constants a, b,c
>
> I looked at the help documentation on my system but I
> could not find an example that was similiar to what I am
> trying to solve for.
>
> I am unsure how to write the code that will solve this
> equation. Any help would be appreciated.
>
> --Ender--

Hi, I have posted this before, but this question comes up
frequently. There is an analytic solution to the problem, in
terms of multiple integrals. Numerically you use cumtrapz
and the accuracy is ok since the noise is effectively
integrated out in the integrals. Otherwise you can use these
values in lsqcurvefit as quite good start guesses.
Here I use: f=s1+s2*exp(-t/tau), so s1=a+b and s2=-b, or
b=-s2 and a=s1+s2.

%- prepare some data
T=2;
t=linspace(0,T,200);
f=1+2*exp(-t/3);
%- fit
f0=f(1);
J=cumtrapz(t,f);
J0=J(end);
JJ=cumtrapz(t,J);
JJ0=JJ(end);
JJJ=cumtrapz(t,JJ);
JJJ0=JJJ(end);

%estimate tau, s1,s2
%f=s1+s2*exp(-t/tau)
tau=(6*JJJ0-2*JJ0*T)/(-6*JJ0+2*J0*T + f0*T^2)
s1=f0 + (-J0 + f0*T)/(-T + tau - tau/exp(T/tau))
s2=-((-J0 + f0*T)/(-T + tau - tau/exp(T/tau)))
%plot and compare
tt=linspace(0,10,200);
ff=s1+s2*exp(-tt/tau);
figure(1), clf;plot(t,f,'.',tt,ff);axis([0 10 0 f0])

Subject: nolinear regression model

From: Miroslav Balda

Date: 8 Jul, 2008 05:39:02

Message: 11 of 21

"Ender " <jr147@msstate.edu> wrote in message
<g4jahc$kle$1@fred.mathworks.com>...
> I am trying to determine the constants for this nolinear
> equation. This is my equation:
>
> y = a+b*(1-exp(-t/c));
>
> I have the vectors for y & t;
>
> I want to solve for the constants a, b,c
>
> I looked at the help documentation on my system but I
> could not find an example that was similiar to what I am
> trying to solve for.
>
> I am unsure how to write the code that will solve this
> equation. Any help would be appreciated.
>
> --Ender--

Hi Ender

Could you send me some of your data? I'll try to solve it.

Mira

Subject: nolinear regression model

From: Miroslav Balda

Date: 8 Jul, 2008 06:56:02

Message: 12 of 21

"Miroslav Balda" <balda.nospam@cdm.it.cas.cz> wrote in
message <g4uuhm$t79$1@fred.mathworks.com>...
:
SNIP
:
> Hi Ender
>
> Could you send me some of your data? I'll try to solve it.
>
> Mira

Hi

I decided to show you a solution on a simulated example.
Here is the code:

% Ender 2008-07-08
a = 10, b = -8, c = -.2 % Model parameters
t = (0:.1:1)'; % should be a column vector
f = a + b*(1-exp(t/c)) + .2*randn(size(t));
% Column vector of residuals
res = @(x) x(1) + x(2)*(1-exp(t/x(3))) - f;
% solve it with rather bad estimate of parameters
x0 = [5,3,-.1];
[x,ssq,cnt] = LMFnlsq(res,x0)
plot(t,f, t,res(x)+f,'r'), grid

The output from the run has been:
>> Ender
a =
    10
b =
    -8
c =
   -0.2000
x =
   10.0640
   -7.9915
   -0.1926
ssq =
    0.2140
cnt =
     8
You see that after 8 evaluations of function and Jacobin
matrix, the solution of the example is close to the initial
a, b, and c. Differences are caused by random deviations in
the formula for f.
Hope it helps.
Mira

Subject: nolinear regression model

From: Miroslav Balda

Date: 8 Jul, 2008 08:14:01

Message: 13 of 21

"Miroslav Balda" <balda.nospam@cdm.it.cas.cz> wrote in
message <g4v322$l25$1@fred.mathworks.com>...
> "Miroslav Balda" <balda.nospam@cdm.it.cas.cz> wrote in
> message <g4uuhm$t79$1@fred.mathworks.com>...
> :
> SNIP
> :
> > Hi Ender
> >
> > Could you send me some of your data? I'll try to solve it.
> >
> > Mira
>
> Hi
>
> I decided to show you a solution on a simulated example.
> Here is the code:
>
> % Ender 2008-07-08
> a = 10, b = -8, c = -.2 % Model parameters
> t = (0:.1:1)'; % should be a column vector
> f = a + b*(1-exp(t/c)) + .2*randn(size(t));
> % Column vector of residuals
> res = @(x) x(1) + x(2)*(1-exp(t/x(3))) - f;
> % solve it with rather bad estimate of parameters
> x0 = [5,3,-.1];
> [x,ssq,cnt] = LMFnlsq(res,x0)
> plot(t,f, t,res(x)+f,'r'), grid
>
> The output from the run has been:
> >> Ender
> a =
> 10
> b =
> -8
> c =
> -0.2000
> x =
> 10.0640
> -7.9915
> -0.1926
> ssq =
> 0.2140
> cnt =
> 8
> You see that after 8 evaluations of function and Jacobin
> matrix, the solution of the example is close to the initial
> a, b, and c. Differences are caused by random deviations in
> the formula for f.
> Hope it helps.
> Mira

I am sorry that I've spent my time to try to help you, when
seeing your evaluation of the function LMFnlsq in FEX. The
function is in order, not your ability to use it in proper
way. Why you did not asked me straight by e-mail, when
having som issues? Excuse me that I have offered you
something, which operates well, what you can see above.

Mira

Subject: nolinear regression model

From: Ender

Date: 8 Jul, 2008 15:30:05

Message: 14 of 21

"Miroslav Balda" <balda.nospam@cdm.it.cas.cz> wrote in
message <g4v7k9$792$1@fred.mathworks.com>...
> "Miroslav Balda" <balda.nospam@cdm.it.cas.cz> wrote in
> message <g4v322$l25$1@fred.mathworks.com>...
> > "Miroslav Balda" <balda.nospam@cdm.it.cas.cz> wrote in
> > message <g4uuhm$t79$1@fred.mathworks.com>...
> > :
> > SNIP
> > :
> > > Hi Ender
> > >
> > > Could you send me some of your data? I'll try to
solve it.
> > >
> > > Mira
> >
> > Hi
> >
> > I decided to show you a solution on a simulated
example.
> > Here is the code:
> >
> > % Ender 2008-07-08
> > a = 10, b = -8, c = -.2 % Model parameters
> > t = (0:.1:1)'; % should be a column vector
> > f = a + b*(1-exp(t/c)) + .2*randn(size(t));
> > % Column vector of residuals
> > res = @(x) x(1) + x(2)*(1-exp(t/x(3))) - f;
> > % solve it with rather bad estimate of parameters
> > x0 = [5,3,-.1];
> > [x,ssq,cnt] = LMFnlsq(res,x0)
> > plot(t,f, t,res(x)+f,'r'), grid
> >
> > The output from the run has been:
> > >> Ender
> > a =
> > 10
> > b =
> > -8
> > c =
> > -0.2000
> > x =
> > 10.0640
> > -7.9915
> > -0.1926
> > ssq =
> > 0.2140
> > cnt =
> > 8
> > You see that after 8 evaluations of function and
Jacobin
> > matrix, the solution of the example is close to the
initial
> > a, b, and c. Differences are caused by random
deviations in
> > the formula for f.
> > Hope it helps.
> > Mira
>
> I am sorry that I've spent my time to try to help you,
when
> seeing your evaluation of the function LMFnlsq in FEX.
The
> function is in order, not your ability to use it in
proper
> way. Why you did not asked me straight by e-mail, when
> having som issues? Excuse me that I have offered you
> something, which operates well, what you can see above.
>
> Mira
>
>
What?


 Are you getting upset because I asked you to help me
evalute a function utilizing code that I have never used
before? Had I known how to use the code correctly, I would
have never had the problem with the code in the first
place. Sorry to disappoint you by not knowing how to do
everything.

Subject: nolinear regression model

From: Ender

Date: 8 Jul, 2008 16:08:02

Message: 15 of 21

"Miroslav Balda" <balda.nospam@cdm.it.cas.cz> wrote in
message <g4v7k9$792$1@fred.mathworks.com>...
> "Miroslav Balda" <balda.nospam@cdm.it.cas.cz> wrote in
> message <g4v322$l25$1@fred.mathworks.com>...
> > "Miroslav Balda" <balda.nospam@cdm.it.cas.cz> wrote in
> > message <g4uuhm$t79$1@fred.mathworks.com>...
> > :
> > SNIP
> > :
> > > Hi Ender
> > >
> > > Could you send me some of your data? I'll try to
solve it.
> > >
> > > Mira
> >
> > Hi
> >
> > I decided to show you a solution on a simulated
example.
> > Here is the code:
> >
> > % Ender 2008-07-08
> > a = 10, b = -8, c = -.2 % Model parameters
> > t = (0:.1:1)'; % should be a column vector
> > f = a + b*(1-exp(t/c)) + .2*randn(size(t));
> > % Column vector of residuals
> > res = @(x) x(1) + x(2)*(1-exp(t/x(3))) - f;
> > % solve it with rather bad estimate of parameters
> > x0 = [5,3,-.1];
> > [x,ssq,cnt] = LMFnlsq(res,x0)
> > plot(t,f, t,res(x)+f,'r'), grid
> >
> > The output from the run has been:
> > >> Ender
> > a =
> > 10
> > b =
> > -8
> > c =
> > -0.2000
> > x =
> > 10.0640
> > -7.9915
> > -0.1926
> > ssq =
> > 0.2140
> > cnt =
> > 8
> > You see that after 8 evaluations of function and
Jacobin
> > matrix, the solution of the example is close to the
initial
> > a, b, and c. Differences are caused by random
deviations in
> > the formula for f.
> > Hope it helps.
> > Mira
>
> I am sorry that I've spent my time to try to help you,
when
> seeing your evaluation of the function LMFnlsq in FEX.
The
> function is in order, not your ability to use it in
proper
> way. Why you did not asked me straight by e-mail, when
> having som issues? Excuse me that I have offered you
> something, which operates well, what you can see above.
>
> Mira
>
>

I am unfamiliar with this syntax
 [x,ssq,cnt] = LMFnlsq(res,x0)

This looks like a function but it is in the middle of the
MATLAB script. Whenever I run it from its original
position as in the example, I get an error saying:

??? Undefined function or method 'LMFnlsq' for input
arguments of type 'function_handle'.

Error in ==> practice_2 at 11
[x,ssq,cnt] = LMFnlsq(res,x0)

Subject: nolinear regression model

From: Miroslav Balda

Date: 8 Jul, 2008 16:30:19

Message: 16 of 21

"Ender " <jr147@msstate.edu> wrote in message
<g503d2$cfj$1@fred.mathworks.com>...
> "Miroslav Balda" <balda.nospam@cdm.it.cas.cz> wrote in
> message <g4v7k9$792$1@fred.mathworks.com>...
> > "Miroslav Balda" <balda.nospam@cdm.it.cas.cz> wrote in
> > message <g4v322$l25$1@fred.mathworks.com>...
> > > "Miroslav Balda" <balda.nospam@cdm.it.cas.cz> wrote in
> > > message <g4uuhm$t79$1@fred.mathworks.com>...
> > > :
> > > SNIP
> > > :
> > > > Hi Ender
> > > >
> > > > Could you send me some of your data? I'll try to
> solve it.
> > > >
> > > > Mira
> > >
> > > Hi
> > >
> > > I decided to show you a solution on a simulated
> example.
> > > Here is the code:
> > >
> > > % Ender 2008-07-08
> > > a = 10, b = -8, c = -.2 % Model parameters
> > > t = (0:.1:1)'; % should be a column vector
> > > f = a + b*(1-exp(t/c)) + .2*randn(size(t));
> > > % Column vector of residuals
> > > res = @(x) x(1) + x(2)*(1-exp(t/x(3))) - f;
> > > % solve it with rather bad estimate of parameters
> > > x0 = [5,3,-.1];
> > > [x,ssq,cnt] = LMFnlsq(res,x0)
> > > plot(t,f, t,res(x)+f,'r'), grid
> > >

> > I am sorry that I've spent my time to try to help you,
> when
> > seeing your evaluation of the function LMFnlsq in FEX.
> The
> > function is in order, not your ability to use it in
> proper
> > way. Why you did not asked me straight by e-mail, when
> > having som issues? Excuse me that I have offered you
> > something, which operates well, what you can see above.
> >
> > Mira
> >
> >
>
> I am unfamiliar with this syntax
> [x,ssq,cnt] = LMFnlsq(res,x0)
>
> This looks like a function but it is in the middle of the
> MATLAB script. Whenever I run it from its original
> position as in the example, I get an error saying:
>
> ??? Undefined function or method 'LMFnlsq' for input
> arguments of type 'function_handle'.
>
> Error in ==> practice_2 at 11
> [x,ssq,cnt] = LMFnlsq(res,x0)
>
Even that I didn't want to communicate with you any more,
there are several recommendations:

- Did you downloaded function LMFnlsq fro File Exchange? If
not, do it. If yes
- insert statement help LMFnlsq from keyboard. If you get
the help response, you have function LMFnlsq on you matlab
path. If not put the function just in your actual directory.
- Do you use MATLAB of at least version 7.0? If not, don't
continue with anomymous functions, because MATLAB 6.x does
not know them. Use the normal function definition. If yes,
the above script should run.

- Read some documentation on functions. The line
[x,ssq,cnt] = LMFnlsq(res,x0) is the normal call of a
function LMFnlsq with input arguments
res = function handle and
x0 = initial guess of the solution.
The left-hand side of the statement contains a list of
output parameters. (see help of LMFnlsq).

That's all.

Mira

Subject: nolinear regression model

From: Per Sundqvist

Date: 8 Jul, 2008 16:53:03

Message: 17 of 21

"Ender " <jr147@msstate.edu> wrote in message
<g4jahc$kle$1@fred.mathworks.com>...
> I am trying to determine the constants for this nolinear
> equation. This is my equation:
>
> y = a+b*(1-exp(-t/c));
>
> I have the vectors for y & t;
>
> I want to solve for the constants a, b,c
>
> I looked at the help documentation on my system but I
> could not find an example that was similiar to what I am
> trying to solve for.
>
> I am unsure how to write the code that will solve this
> equation. Any help would be appreciated.
>
> --Ender--

Hi, If you just want to solve this problem I have the code
for it. look below. Besides, what is the benefit of this
LMRxx-code? Is it better than lsqcurvefit?

Copy and save as exp2fit in your work directory. You don't
need any startguess! (its almost analytic).
--------------------

function s=exp2fit(t,f,options)
%exp2fit solves the non-linear least squares problem
% of the specific exponential function:
% ------------------
% f=s1+s2*exp(-t/s3)
% ------------------
%
% Syntax: s=exp2fit(t,f)
% or s=exp2fit(t,f,options), where options are produced
% by optimset, as used in lsqcurvefit.
%
% It uses an analytic formulae using multiple integrals.
% Integral estimations are used as start guess in
% lsqcurvefit.
% Note: For infinite lengths of t, and f, without noise
% the result is exact.
%
% %--- Example:
% t=linspace(1,4,100)*1e-9;
% noise=0*0.02;
% f=0.1+2*exp(-t/3e-9)+noise*randn(size(t));
%
% %--- solve without startguess
% s=exp2fit(t,f);
%
% %--- plot and compare
% fun = @(s,t) s(1)+s(2)*exp(-t/s(3));
% tt=linspace(0,4*s(3),200);
% ff=fun(s,tt);
% figure(1), clf;plot(t,f,'.',tt,ff);
% axis([0 abs(4*s(3)) 0 max(ff)])
% text(abs(4*s(3))/3,max(ff)/2,...
% sprintf(['f=s_1+s_2*exp(-t/s_3)','\n',...
% '------------------','\n',...
% 's_1= ',num2str(s(1),3),'\n','s_2= ',num2str(s(2),3),...
% '\n','s_3= ',num2str(s(3),3)]))
%
%%% By Per Sundqvist juli 2008.

if nargin<3
    options=optimset('TolX',1e-8,'TolFun',1e-8);
end
if length(t)<3
    error(['WARNING!', ...
    'To few data to give correct estimation of 3 parameters!']);
end

%calculate help variables
T=max(t)-min(t);
f0=f(1);
J=cumtrapz(t,f);
J0=J(end);
JJ=cumtrapz(t,J);
JJ0=JJ(end);
JJJ=cumtrapz(t,JJ);
JJJ0=JJJ(end);

%--- estimate tau, s1,s2
%--- f=s1+s2*exp(-t/tau)
tau=(6*JJJ0-2*JJ0*T)/(-6*JJ0+2*J0*T + f0*T^2);
s1=f0 + (-J0 + f0*T)/(-T + tau - tau/exp(T/tau));
s2=-exp(min(t)/tau)*((-J0 + f0*T)/(-T + tau - tau/exp(T/tau)));

%--- use lsqcurvefit
fun = @(s,t) s(1)+s(2)*exp(-t/s(3))
s0=[s1 s2 tau];
cond=1;
while cond
    [s,RESNORM,RESIDUAL,EXIT]=...
    lsqcurvefit(fun,s0,t,f,[],[],options);
    cond=not(not(EXIT==0));
    s0=s;
end
s=s0;

--------------------

Subject: nolinear regression model

From: Ender

Date: 8 Jul, 2008 18:13:01

Message: 18 of 21

"Per Sundqvist" <sunkan@fy.chalmers.se> wrote in message
<g5061f$b6o$1@fred.mathworks.com>...
> "Ender " <jr147@msstate.edu> wrote in message
> <g4jahc$kle$1@fred.mathworks.com>...
> > I am trying to determine the constants for this
nolinear
> > equation. This is my equation:
> >
> > y = a+b*(1-exp(-t/c));
> >
> > I have the vectors for y & t;
> >
> > I want to solve for the constants a, b,c
> >
> > I looked at the help documentation on my system but I
> > could not find an example that was similiar to what I
am
> > trying to solve for.
> >
> > I am unsure how to write the code that will solve this
> > equation. Any help would be appreciated.
> >
> > --Ender--
>
> Hi, If you just want to solve this problem I have the
code
> for it. look below. Besides, what is the benefit of this
> LMRxx-code? Is it better than lsqcurvefit?
>
> Copy and save as exp2fit in your work directory. You
don't
> need any startguess! (its almost analytic).
> --------------------
>
> function s=exp2fit(t,f,options)
> %exp2fit solves the non-linear least squares problem
> % of the specific exponential function:
> % ------------------
> % f=s1+s2*exp(-t/s3)
> % ------------------
> %
> % Syntax: s=exp2fit(t,f)
> % or s=exp2fit(t,f,options), where options are produced
> % by optimset, as used in lsqcurvefit.
> %
> % It uses an analytic formulae using multiple integrals.
> % Integral estimations are used as start guess in
> % lsqcurvefit.
> % Note: For infinite lengths of t, and f, without noise
> % the result is exact.
> %
> % %--- Example:
> % t=linspace(1,4,100)*1e-9;
> % noise=0*0.02;
> % f=0.1+2*exp(-t/3e-9)+noise*randn(size(t));
> %
> % %--- solve without startguess
> % s=exp2fit(t,f);
> %
> % %--- plot and compare
> % fun = @(s,t) s(1)+s(2)*exp(-t/s(3));
> % tt=linspace(0,4*s(3),200);
> % ff=fun(s,tt);
> % figure(1), clf;plot(t,f,'.',tt,ff);
> % axis([0 abs(4*s(3)) 0 max(ff)])
> % text(abs(4*s(3))/3,max(ff)/2,...
> % sprintf(['f=s_1+s_2*exp(-t/s_3)','\n',...
> % '------------------','\n',...
> % 's_1= ',num2str(s(1),3),'\n','s_2= ',num2str(s
(2),3),...
> % '\n','s_3= ',num2str(s(3),3)]))
> %
> %%% By Per Sundqvist juli 2008.
>
> if nargin<3
> options=optimset('TolX',1e-8,'TolFun',1e-8);
> end
> if length(t)<3
> error(['WARNING!', ...
> 'To few data to give correct estimation of 3
parameters!']);
> end
>
> %calculate help variables
> T=max(t)-min(t);
> f0=f(1);
> J=cumtrapz(t,f);
> J0=J(end);
> JJ=cumtrapz(t,J);
> JJ0=JJ(end);
> JJJ=cumtrapz(t,JJ);
> JJJ0=JJJ(end);
>
> %--- estimate tau, s1,s2
> %--- f=s1+s2*exp(-t/tau)
> tau=(6*JJJ0-2*JJ0*T)/(-6*JJ0+2*J0*T + f0*T^2);
> s1=f0 + (-J0 + f0*T)/(-T + tau - tau/exp(T/tau));
> s2=-exp(min(t)/tau)*((-J0 + f0*T)/(-T + tau - tau/exp
(T/tau)));
>
> %--- use lsqcurvefit
> fun = @(s,t) s(1)+s(2)*exp(-t/s(3))
> s0=[s1 s2 tau];
> cond=1;
> while cond
> [s,RESNORM,RESIDUAL,EXIT]=...
> lsqcurvefit(fun,s0,t,f,[],[],options);
> cond=not(not(EXIT==0));
> s0=s;
> end
> s=s0;
>
> --------------------

I don't have the curve fit toolbox so I don't have the
lsqcurvefit command. I copied the script that you wrote,
but I receive an error whenever MATLAB is evaluating
s=exp2fit(t,f)

The error says:

??? Maximum recursion limit of 500 reached. Use set
(0,'RecursionLimit',N)
to change the limit. Be aware that exceeding your
available stack space can
crash MATLAB and/or your computer.

Error in ==> exp2fit at 24
s=exp2fit(t,f);

Subject: nolinear regression model

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 8 Jul, 2008 18:32:20

Message: 19 of 21

In article <g503d2$cfj$1@fred.mathworks.com>, Ender <jr147@msstate.edu> wrote:

>I am unfamiliar with this syntax
> [x,ssq,cnt] = LMFnlsq(res,x0)

That's a call to a function named LMFnlsq that takes two arguments,
res and x0, and returns three distinct values which are to be
assigned to x, ssq, and cnt .


>This looks like a function but it is in the middle of the
>MATLAB script.

It is a function -call-. A function definition would start with
the word 'function'.

> Whenever I run it from its original
>position as in the example, I get an error saying:

>??? Undefined function or method 'LMFnlsq' for input
>arguments of type 'function_handle'.

That suggests that you have not downloaded LMFnlsq from the
Matlab File Exchange and placed it somewhere on your matlab path.
--
   "History is a pile of debris" -- Laurie Anderson

Subject: nolinear regression model

From: Miroslav Balda

Date: 8 Jul, 2008 20:06:02

Message: 20 of 21

"Ender " <jr147@msstate.edu> wrote in message
<g50and$dqf$1@fred.mathworks.com>...
:
SNIP
:
> I don't have the curve fit toolbox so I don't have the
> lsqcurvefit command. I copied the script that you wrote,
> but I receive an error whenever MATLAB is evaluating
> s=exp2fit(t,f)
>
> The error says:
>
> ??? Maximum recursion limit of 500 reached. Use set
> (0,'RecursionLimit',N)
> to change the limit. Be aware that exceeding your
> available stack space can
> crash MATLAB and/or your computer.
>
> Error in ==> exp2fit at 24
> s=exp2fit(t,f);

I guess that you have still problem with placin new
functions within places defined in matlabpath.

I have used Per's function exp2fit for comparisonwith my
results. I cut off the last part optimizing the solution of
the integration. The big advantage of his function is that
it does not require any initial estimate. The following script
% Ender 2008-07-08
disp('---------------------')
disp('LMFnlsq-1')
a = 10, b = -8, c = -.2 % Model parameters
t = (0:.1:1)'; % should be a column vector
f = a + b*(1-exp(t/c)) + 0.3*randn(size(t));
% Column vector of residuals
res = @(x) x(1) + x(2)*(1-exp(t/x(3))) - f;
% solve it with rather bad estimate of parameters
x0 = [5,3,-.1];
[x,ssq,cnt] = LMFnlsq(res,x0)
plot(t,f, t,res(x)+f,'r'), grid,hold on
disp('---------------------')
disp('eps2fit:')
x = exp2fit(t,f);
ae = x(1)+x(2)
be = -x(2)
ce = -x(3)
fe = ae+be*(1-exp(t/ce));
plot(t,fe,'c')
hold off
df = fe-f;
ssq = df'*df
disp('---------------------')
disp('LMFnlsq-2')
x0 = [ae,be,ce];
[x,ssq,cnt] = LMFnlsq(res,x0)
disp('---------------------')

gave results:

>> Ender
---------------------
LMFnlsq-1
a =
    10
b =
    -8
c =
   -0.2000
x =
    9.9241
   -8.0141
   -0.2064
ssq =
    0.5513
cnt =
     8
---------------------
eps2fit:
ae =
    9.9551
be =
   -8.0904
ce =
   -0.2126
ssq =
    0.5704
---------------------
LMFnlsq-2
x =
    9.9241
   -8.0141
   -0.2064
ssq =
    0.5513
cnt =
     3
---------------------
It is obvious, that the final solution obtained by the
section LMFnlsq-2 is just the same like that from LMFnlsq-1.
Both returned better sum of squares ssq than intermediate
results of exp2fit, however the advantage of exp2fit is an
absence of initial guess of the solution. It is great.

Mira

Subject: nolinear regression model

From: elfriede.holle@yahoo.de

Date: 8 Jul, 2008 21:59:29

Message: 21 of 21

On Jul 7, 10:23 pm, "Ender " <jr...@msstate.edu> wrote:
> "Ender " <jr...@msstate.edu> wrote in message <g4ts51
>
> $nd...@fred.mathworks.com>...
>
> > "Miroslav Balda" <miroslav.nos...@balda.cz> wrote in
> > message <g4tq3d$3e...@fred.mathworks.com>...
> > > Peter Perkins <Peter.PerkinsRemoveT...@mathworks.com>
> > wrote
> > > in message <g4tmu3$si...@fred.mathworks.com>...
> > > > Ender wrote:
>
> > > > > I am using a student version of MATLAB so I do not
> > have
> > > > > the LSQCURVEFIT function or the Optimization
> > Toolbar. It
> > > > > was suggested to take the natural log of the
> > function then
> > > > > solve for the linearized constants. Do you have
> any
> > easier
> > > > > suggestions.
>
> > > > Ender, I believe the student version includes the
> > > Statistics Toolbox, does it
> > > > not? You may find this demo, which illustrates some
> > > differences between
> > > > linearizing versusfittinga nonlinear regression
> using
> > > NLINFIT, helpful:
>
> <http://www.mathworks.com/products/statistics/demos.html?
>
> file=/products/demos/shipping/stats/xform2lineardemo.html>
>
>
>
>
>
> > > Hi Peter,
>
> > > Even without functions from Optimization Toolbox, you
> may
> > > solve the problem by using some functions from the File
> > > exchange collection of functions. I have built the
> > function
> > > LMFnlsq, which you may find under an identification
> > number
> > > 17534, which solves overdetermined systems of nonlinear
> > > equations in trheleastsquaressense. You simply bild
> a
> > > function, say res.m, which evaluates residuals f(x)-y,
> > where
> > > f(x) is your formula with x=[a;b;c] and y is a vector
> of
> > > measured values.
>
> > > You may find howto use the function when calling help
> > LMFnlsq.
>
> > > I hope that it helps.
>
> > > Mira
>
> > I am still having some trouble with the nonlinearcurve
> > fit.
> > I went to the example in the optimization toolbox and
> > tried their example.Once it worked, I put my equation
> into
> > the code and tried to plot the results. This is my
> > equation:
>
> > modelFun = @(p,x) (p(1)+p(2)*(-p(2))*exp(x/p(3)))/log(y);
> > paramEstsLin = [ones(size(x)), x,x] \ log(y);
> > paramEstsLin = exp(paramEstsLin)
>
> > Once again I am trying to model this equation and solve
> > for the constants.
> > % y = a+b*(1-exp(-t/c));
> > or log(a+b-b*exp(-t/c));
>
> > I am not sure if I set up thecurvefit correctly.
>
> > MatLab gave me results values for the constants, then
> > MATLAB gave me an error when I tried plotting the
> function.
> > This is what the error said:
>
> > ??? Error using ==> mrdivide
> > Matrix dimensions must agree.
>
> > Error in ==> @(p,x)(p(1)+p(2)*(-p(2))*exp(x/p(3)))/log(y)
>
> > Error in ==> practice at 11
> > yyLin = modelFun(paramEstsLin, xx);
>
> > I could use some help debugging this code.
> > Thanks!
>
> > -Ender-
>
> I also tried the LMFnlsq. examples from the help option. I
> could not get the code to work as it is written. I tried
> both links
>
> http://www.mathworks.com/matlabcentral/fileexchange/loadFil
> e.do?objectId=17534
>
> and the example on this link
>
> http://www.mathworks.com/matlabcentral/fileexchange/loadFil
> e.do?objectId=16063
>
> I copied and pasted the examples but each time I got an
> different error. This is one of the errors:
>
> ??? Attempt to execute SCRIPT LMFsolve as a function:
> Error in ==> LMFsolve at 3
> [x,ssq,cnt]=LMFsolve(ros,[-1.2,1],'Display',1,'MaxIter',50)

looks complicated - where is the advantage of using Matlab ?
why not adapt existing C code ?
e.g. the public-domain Levenberg-Marquardt implementation lmfit,
www.messen-und-deuten.de/lmfit

- Elfi

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us