Got Questions? Get Answers.
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:
Unknown Parameter estimation using fminsearch and ode45 for exp data fitting

Subject: Unknown Parameter estimation using fminsearch and ode45 for exp data fitting

From: PeiYing Ong

Date: 10 Feb, 2014 14:20:08

Message: 1 of 6

dear all,

I have been struggling on this for weeks and I still cant figure out what is the main problem with my codes. I can solved my ODE equation easily using ode45. but it stuck and it seem like can't be proceed once it combines with the parameter estimation using fminsearch!

Basically, there are 2 constants need to be estimated which is p(1) and p(2). My script codes as follow:

1st M-file:
function F=funeqn(t,x,p)
%estimation example p(1)=a,p(2)=b.
F=-p(1)*x*log(x/(p(2)));

2nd M-file:
function R2=fun(p)
global t x;
t=[0;3;6;9;15;18;21;24;27;30;33;39;42;45;48;51;54];
[tode,xode]=ode45(@funeqn,t,0.01,[],p);
%%Sum least square error
xbar=mean(x);
deviation= x-xode;
SST=dot(xode-xbar,xode-xbar);
SSE=dot(deviation,deviation);
R2=(1-(SSE/SST))

3rd M-file:
function nonlinearfit
global t x;
t=[0;3;6;9;15;18;21;24;27;30;33;39;42;45;48;51;54];
x=[0;0;1;1;1.66666666666667;2;2.33333333333333;12.6666666666667;13;25;43;53.3333333333333;113.333333333333;155.333333333333;189.666666666667;259.333333333333;312];
guess=[.068 358];
[p,error]=fminsearch(@fun,guess);
R2=error;
%
figure(1)
plot(tode,xode,'-',t,x,'o');
ylabel('growth rate');
legend('model_Fit','Experimental_data')
fprintf('The regression is R^2 = %9.7f.\n',R2)

can anyone give me some suggestions? thanks in advance!

Subject: Unknown Parameter estimation using fminsearch and ode45 for exp data fitting

From: Torsten

Date: 10 Feb, 2014 14:40:08

Message: 2 of 6

"PeiYing Ong" wrote in message <ldan6o$ifj$1@newscl01ah.mathworks.com>...
> dear all,
>
> I have been struggling on this for weeks and I still cant figure out what is the main problem with my codes. I can solved my ODE equation easily using ode45. but it stuck and it seem like can't be proceed once it combines with the parameter estimation using fminsearch!
>
> Basically, there are 2 constants need to be estimated which is p(1) and p(2). My script codes as follow:
>
> 1st M-file:
> function F=funeqn(t,x,p)
> %estimation example p(1)=a,p(2)=b.
> F=-p(1)*x*log(x/(p(2)));
>
> 2nd M-file:
> function R2=fun(p)
> global t x;
> t=[0;3;6;9;15;18;21;24;27;30;33;39;42;45;48;51;54];
> [tode,xode]=ode45(@funeqn,t,0.01,[],p);
> %%Sum least square error
> xbar=mean(x);
> deviation= x-xode;
> SST=dot(xode-xbar,xode-xbar);
> SSE=dot(deviation,deviation);
> R2=(1-(SSE/SST))
>
> 3rd M-file:
> function nonlinearfit
> global t x;
> t=[0;3;6;9;15;18;21;24;27;30;33;39;42;45;48;51;54];
> x=[0;0;1;1;1.66666666666667;2;2.33333333333333;12.6666666666667;13;25;43;53.3333333333333;113.333333333333;155.333333333333;189.666666666667;259.333333333333;312];
> guess=[.068 358];
> [p,error]=fminsearch(@fun,guess);
> R2=error;
> %
> figure(1)
> plot(tode,xode,'-',t,x,'o');
> ylabel('growth rate');
> legend('model_Fit','Experimental_data')
> fprintf('The regression is R^2 = %9.7f.\n',R2)
>
> can anyone give me some suggestions? thanks in advance!

Your ODE can be integrated analytically. Why don't you use this explicit expression ?

Best wishes
Torsten.

Subject: Unknown Parameter estimation using fminsearch and ode45 for exp data fitting

From: PeiYing Ong

Date: 10 Feb, 2014 15:00:18

Message: 3 of 6

Dear Torsten,

yeah, if i am not mistaken, it can be solved easily by introduced integrated eqn and solved using fminsearch. But i do hope to know what is the correct script to solve it using combination of ode45 and fminsearch because i try to make it as a template for my another ode eqn that is much more complicated than this. could you give me some advise? thank you very much.


"Torsten" wrote in message <ldaoc8$cau$1@newscl01ah.mathworks.com>...
> "PeiYing Ong" wrote in message <ldan6o$ifj$1@newscl01ah.mathworks.com>...
> > dear all,
> >
> > I have been struggling on this for weeks and I still cant figure out what is the main problem with my codes. I can solved my ODE equation easily using ode45. but it stuck and it seem like can't be proceed once it combines with the parameter estimation using fminsearch!
> >
> > Basically, there are 2 constants need to be estimated which is p(1) and p(2). My script codes as follow:
> >
> > 1st M-file:
> > function F=funeqn(t,x,p)
> > %estimation example p(1)=a,p(2)=b.
> > F=-p(1)*x*log(x/(p(2)));
> >
> > 2nd M-file:
> > function R2=fun(p)
> > global t x;
> > t=[0;3;6;9;15;18;21;24;27;30;33;39;42;45;48;51;54];
> > [tode,xode]=ode45(@funeqn,t,0.01,[],p);
> > %%Sum least square error
> > xbar=mean(x);
> > deviation= x-xode;
> > SST=dot(xode-xbar,xode-xbar);
> > SSE=dot(deviation,deviation);
> > R2=(1-(SSE/SST))
> >
> > 3rd M-file:
> > function nonlinearfit
> > global t x;
> > t=[0;3;6;9;15;18;21;24;27;30;33;39;42;45;48;51;54];
> > x=[0;0;1;1;1.66666666666667;2;2.33333333333333;12.6666666666667;13;25;43;53.3333333333333;113.333333333333;155.333333333333;189.666666666667;259.333333333333;312];
> > guess=[.068 358];
> > [p,error]=fminsearch(@fun,guess);
> > R2=error;
> > %
> > figure(1)
> > plot(tode,xode,'-',t,x,'o');
> > ylabel('growth rate');
> > legend('model_Fit','Experimental_data')
> > fprintf('The regression is R^2 = %9.7f.\n',R2)
> >
> > can anyone give me some suggestions? thanks in advance!
>
> Your ODE can be integrated analytically. Why don't you use this explicit expression ?
>
> Best wishes
> Torsten.

Subject: Unknown Parameter estimation using fminsearch and ode45 for exp

From: Alan_Weiss

Date: 10 Feb, 2014 17:21:14

Message: 4 of 6

On 2/10/2014 10:00 AM, PeiYing Ong wrote:
> Dear Torsten,
>
> yeah, if i am not mistaken, it can be solved easily by introduced
> integrated eqn and solved using fminsearch. But i do hope to know what
> is the correct script to solve it using combination of ode45 and
> fminsearch because i try to make it as a template for my another ode
> eqn that is much more complicated than this. could you give me some
> advise? thank you very much.
>
>
> ***snip***

The problem is likely to be that fminsearch does not have a settable
parameter for the size of its initial simplex. It can start, find that
the ODE solution seems the same at all the initial points, and never
proceeds.

For more information about trying to optimize an ODE numerically, see
http://www.mathworks.com/help/optim/ug/optimizing-a-simulation-or-ordinary-differential-equation.html

For an example, see
http://www.mathworks.com/help/gads/optimize-an-ode-in-parallel.html

Good luck,

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: Unknown Parameter estimation using fminsearch and ode45 for exp data fitting

From: Torsten

Date: 11 Feb, 2014 07:55:08

Message: 5 of 6

"PeiYing Ong" wrote in message <ldapi2$785$1@newscl01ah.mathworks.com>...
> Dear Torsten,
>
> yeah, if i am not mistaken, it can be solved easily by introduced integrated eqn and solved using fminsearch. But i do hope to know what is the correct script to solve it using combination of ode45 and fminsearch because i try to make it as a template for my another ode eqn that is much more complicated than this. could you give me some advise? thank you very much.
>
>
> "Torsten" wrote in message <ldaoc8$cau$1@newscl01ah.mathworks.com>...
> > "PeiYing Ong" wrote in message <ldan6o$ifj$1@newscl01ah.mathworks.com>...
> > > dear all,
> > >
> > > I have been struggling on this for weeks and I still cant figure out what is the main problem with my codes. I can solved my ODE equation easily using ode45. but it stuck and it seem like can't be proceed once it combines with the parameter estimation using fminsearch!
> > >
> > > Basically, there are 2 constants need to be estimated which is p(1) and p(2). My script codes as follow:
> > >
> > > 1st M-file:
> > > function F=funeqn(t,x,p)
> > > %estimation example p(1)=a,p(2)=b.
> > > F=-p(1)*x*log(x/(p(2)));
> > >
> > > 2nd M-file:
> > > function R2=fun(p)
> > > global t x;
> > > t=[0;3;6;9;15;18;21;24;27;30;33;39;42;45;48;51;54];
> > > [tode,xode]=ode45(@funeqn,t,0.01,[],p);
> > > %%Sum least square error
> > > xbar=mean(x);
> > > deviation= x-xode;
> > > SST=dot(xode-xbar,xode-xbar);
> > > SSE=dot(deviation,deviation);
> > > R2=(1-(SSE/SST))
> > >
> > > 3rd M-file:
> > > function nonlinearfit
> > > global t x;
> > > t=[0;3;6;9;15;18;21;24;27;30;33;39;42;45;48;51;54];
> > > x=[0;0;1;1;1.66666666666667;2;2.33333333333333;12.6666666666667;13;25;43;53.3333333333333;113.333333333333;155.333333333333;189.666666666667;259.333333333333;312];
> > > guess=[.068 358];
> > > [p,error]=fminsearch(@fun,guess);
> > > R2=error;
> > > %
> > > figure(1)
> > > plot(tode,xode,'-',t,x,'o');
> > > ylabel('growth rate');
> > > legend('model_Fit','Experimental_data')
> > > fprintf('The regression is R^2 = %9.7f.\n',R2)
> > >
> > > can anyone give me some suggestions? thanks in advance!
> >
> > Your ODE can be integrated analytically. Why don't you use this explicit expression ?
> >
> > Best wishes
> > Torsten.

I suggest that you try whether you can fit your parameters using the analytical solution of your ODE first.
If I made no mistake, it is given by
x(t)=p(2)*(x(0)/p(2))^exp(-p(1)*t).
And better use lsqcurvefit instead of fminsearch to fit your parameters.
And call ODE45 as
[tode,xode]=ode45(@(t,x)funeqn(t,x,p),t,0.01);
And use a row vector instead of a column vector for tspan (=t).

Best wishes
Torsten.

Subject: Unknown Parameter estimation using fminsearch and ode45 for exp

From: PeiYing Ong

Date: 11 Feb, 2014 08:27:15

Message: 6 of 6


> > ***snip***
>
> The problem is likely to be that fminsearch does not have a settable
> parameter for the size of its initial simplex. It can start, find that
> the ODE solution seems the same at all the initial points, and never
> proceeds.
>
> For more information about trying to optimize an ODE numerically, see
> http://www.mathworks.com/help/optim/ug/optimizing-a-simulation-or-ordinary-differential-equation.html
>
> For an example, see
> http://www.mathworks.com/help/gads/optimize-an-ode-in-parallel.html
>
> Good luck,
>
> Alan Weiss
> MATLAB mathematical toolbox documentation



Dear Alan,

Yeah! Exactly! It keep showing "busy" status once I run the script!Thank you so much. I will try to look at that. :)

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