Thread Subject: dy/dt value from ode45 solver

Subject: dy/dt value from ode45 solver

From: Md. Shahriar Karim

Date: 18 Aug, 2009 19:58:18

Message: 1 of 11

Hi,

I just used ode45 to solve a set of differential equations and got value of my variables (say, y) plotted. How can I have the "dy/dt" value saved as well for each time step when I use ODE45 solver.

Actually, I am interested to find out the steady-state time using the above approach. I consider "dy/dt" as a slope for the y plot and would like to extract the time value for any given condition on Slope dy/dt.

Could you please suggest the way to save the dy/dt data value for ODE45 sovler?

Thanking you

Md. Shahriar Karim

Subject: dy/dt value from ode45 solver

From: pavant

Date: 18 Aug, 2009 20:21:15

Message: 2 of 11

On Aug 18, 3:58 pm, "Md. Shahriar Karim" <karim.shahr...@gmail.com>
wrote:
> Hi,
>
> I just used ode45 to solve a set of differential equations and got value of my variables (say, y) plotted. How can I have the "dy/dt" value saved as well for each time step when I use ODE45 solver.
>
> Actually, I am interested to find out the steady-state time using the above approach. I consider "dy/dt" as a slope for the y plot and would like to extract the time value for any given condition on Slope dy/dt.
>
> Could you please suggest the way to save the dy/dt data value for ODE45 sovler?
>
> Thanking you
>
> Md. Shahriar Karim

Hi,
One solution is to double the dimension of your system. write down the
eqns for d2y/dt2 also and append them to your state eqns.
ex:

if your original system is:

dy/dt = -y;

then d2y/dt2 = -dy/dt = y;

then your system is:

vdot = [-y; y];

return vdot

Subject: dy/dt value from ode45 solver

From: Steven Lord

Date: 18 Aug, 2009 21:17:36

Message: 3 of 11


"Md. Shahriar Karim" <karim.shahriar@gmail.com> wrote in message
news:h6f14q$7a7$1@fred.mathworks.com...
> Hi,
>
> I just used ode45 to solve a set of differential equations and got value
> of my variables (say, y) plotted. How can I have the "dy/dt" value saved
> as well for each time step when I use ODE45 solver.

Call ODE45 and specify some output arguments (one if you want the SOL
solution struct array, two if you want t and y as individual vectors.) Then
plot the results (using DEVAL if you're using the SOL struct array.)

> Actually, I am interested to find out the steady-state time using the
> above approach. I consider "dy/dt" as a slope for the y plot and would
> like to extract the time value for any given condition on Slope dy/dt.

Write a function to DEVAL the SOL struct array at a given time and subtract
that result from the desired dy/dt, then use FZERO on that function.

--
Steve Lord
slord@mathworks.com

Subject: dy/dt value from ode45 solver

From: Md. Shahriar Karim

Date: 19 Aug, 2009 00:57:03

Message: 4 of 11

Hi Steve,

DEVAL asks for a structure input. I also replaced

[t, y]=ode45('steadystate', [0:0.1:10000], y0); part of my code by
sol=ode45('steadystate', [0:0.1:10000], y0);

but it didn't solve the problem. Again structure problem.

My code works as follows--

.......
y0(1)=70; y0(2)=0; y0(3)=200;y0(4)=0; y0(5)=0;
.......
.......
[t, y]=ode45('steadystate', [0:0.1:10000], y0); % I am calling another function "steadystate" %
or
% sol=ode45('steadystate', [0:0.1:10000], y0);

function dydt = steadystate(t,y)
......
B = y(1); C = y(2); D = y(3);E = y(4); M =y(5);
......
dydt(1)=... (differential Equation for B, C, D, E, M)
dydt(2)=...
.....
.....
dydt=dydt';

My target is to calculate the dy/dt value for variable "C"and once dy/dt value is available, I can put the checking condition for the slope calculation and find out my steady-state time accordingly.

Please suggest something again. Thanks,

Md. Shahriar Karim





"Steven Lord" <slord@mathworks.com> wrote in message <h6f5pa$hct$1@fred.mathworks.com>...
>
> "Md. Shahriar Karim" <karim.shahriar@gmail.com> wrote in message
> news:h6f14q$7a7$1@fred.mathworks.com...
> > Hi,
> >
> > I just used ode45 to solve a set of differential equations and got value
> > of my variables (say, y) plotted. How can I have the "dy/dt" value saved
> > as well for each time step when I use ODE45 solver.
>
> Call ODE45 and specify some output arguments (one if you want the SOL
> solution struct array, two if you want t and y as individual vectors.) Then
> plot the results (using DEVAL if you're using the SOL struct array.)
>
> > Actually, I am interested to find out the steady-state time using the
> > above approach. I consider "dy/dt" as a slope for the y plot and would
> > like to extract the time value for any given condition on Slope dy/dt.
>
> Write a function to DEVAL the SOL struct array at a given time and subtract
> that result from the desired dy/dt, then use FZERO on that function.
>
> --
> Steve Lord
> slord@mathworks.com
>

Subject: dy/dt value from ode45 solver

From: Steven Lord

Date: 19 Aug, 2009 01:00:41

Message: 5 of 11


"Md. Shahriar Karim" <karim.shahriar@gmail.com> wrote in message
news:h6fikv$egv$1@fred.mathworks.com...
> Hi Steve,
>
> DEVAL asks for a structure input. I also replaced
>
> [t, y]=ode45('steadystate', [0:0.1:10000], y0); part of my code by
> sol=ode45('steadystate', [0:0.1:10000], y0);

Try replacing the string input with a function handle to your function and
use the second of those syntaxes..

--
Steve Lord
slord@mathworks.com

Subject: dy/dt value from ode45 solver

From: Md. Shahriar Karim

Date: 19 Aug, 2009 14:22:00

Message: 6 of 11

Hi Steve,

Thanks for your help so far. However, I am totally unsure from the below part--

Write a function to DEVAL the SOL struct array at a given time and subtract
that result from the desired dy/dt, then use FZERO on that function.

 sol=ode45(@steadystate, [0:0.1:10000], y0);

once I do the above, I get my all variables value after solving the ODEs..variables are..B C D ..etc.

Now from this I am told to use the DEVAL on "sol", it gives me the values of my variables B,C,D...but I was needed the "dy/dt...the slope values.

Now, I am supposed to subtract my desired "dy/dt"..say 0.00001 from DEVAL output which is my variables values? Once this is done, I do FZERO on the result I get after subtraction....this is what I was told.

Could you please clarify how am I getting dy/dt out of the whole process? I am quite confused at this stage. Thanking you,

Md. Shahriar Karim



"Steven Lord" <slord@mathworks.com> wrote in message <h6firi$red$1@fred.mathworks.com>...
>
> "Md. Shahriar Karim" <karim.shahriar@gmail.com> wrote in message
> news:h6fikv$egv$1@fred.mathworks.com...
> > Hi Steve,
> >
> > DEVAL asks for a structure input. I also replaced
> >
> > [t, y]=ode45('steadystate', [0:0.1:10000], y0); part of my code by
> > sol=ode45('steadystate', [0:0.1:10000], y0);
>
> Try replacing the string input with a function handle to your function and
> use the second of those syntaxes..
>
> --
> Steve Lord
> slord@mathworks.com
>

Subject: dy/dt value from ode45 solver

From: Yi Cao

Date: 19 Aug, 2009 18:39:03

Message: 7 of 11

"Md. Shahriar Karim" <karim.shahriar@gmail.com> wrote in message <h6h1q8$sp5$1@fred.mathworks.com>...
> Hi Steve,
>
> Thanks for your help so far. However, I am totally unsure from the below part--
>
> Write a function to DEVAL the SOL struct array at a given time and subtract
> that result from the desired dy/dt, then use FZERO on that function.
>
> sol=ode45(@steadystate, [0:0.1:10000], y0);
>
> once I do the above, I get my all variables value after solving the ODEs..variables are..B C D ..etc.
>
> Now from this I am told to use the DEVAL on "sol", it gives me the values of my variables B,C,D...but I was needed the "dy/dt...the slope values.
>
> Now, I am supposed to subtract my desired "dy/dt"..say 0.00001 from DEVAL output which is my variables values? Once this is done, I do FZERO on the result I get after subtraction....this is what I was told.
>
> Could you please clarify how am I getting dy/dt out of the whole process? I am quite confused at this stage. Thanking you,
>
> Md. Shahriar Karim
>
>
>
> "Steven Lord" <slord@mathworks.com> wrote in message <h6firi$red$1@fred.mathworks.com>...
> >
> > "Md. Shahriar Karim" <karim.shahriar@gmail.com> wrote in message
> > news:h6fikv$egv$1@fred.mathworks.com...
> > > Hi Steve,
> > >
> > > DEVAL asks for a structure input. I also replaced
> > >
> > > [t, y]=ode45('steadystate', [0:0.1:10000], y0); part of my code by
> > > sol=ode45('steadystate', [0:0.1:10000], y0);
> >
> > Try replacing the string input with a function handle to your function and
> > use the second of those syntaxes..
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> >

Actually, after you got the solution to the ODE, just call the function again you can get dy/dt, i.e.

[t, y]=ode45('steadystate', [0:0.1:10000], y0);
dydt = steadystate(t,y); %if your function is vectorized. Alternatively

dydt=y;
for k=1:numel(t)
 dydt(k,:)=steadystate(t(k),y(k,:));
end

HTH
Yi

Subject: dy/dt value from ode45 solver

From: Md. Shahriar Karim

Date: 20 Aug, 2009 04:09:03

Message: 8 of 11

Hi Cao,

Could you please explain the logic working here? Once I call the function again how does it give the dydt value?

is it that if I call the function again, it's going to give us the value of dy2dt2?

Thanks,

Md. Shahriar Karim


"Yi Cao" <y.cao@cranfield.ac.uk> wrote in message <h6hgs7$9uf$1@fred.mathworks.com>...
> "Md. Shahriar Karim" <karim.shahriar@gmail.com> wrote in message <h6h1q8$sp5$1@fred.mathworks.com>...
> > Hi Steve,
> >
> > Thanks for your help so far. However, I am totally unsure from the below part--
> >
> > Write a function to DEVAL the SOL struct array at a given time and subtract
> > that result from the desired dy/dt, then use FZERO on that function.
> >
> > sol=ode45(@steadystate, [0:0.1:10000], y0);
> >
> > once I do the above, I get my all variables value after solving the ODEs..variables are..B C D ..etc.
> >
> > Now from this I am told to use the DEVAL on "sol", it gives me the values of my variables B,C,D...but I was needed the "dy/dt...the slope values.
> >
> > Now, I am supposed to subtract my desired "dy/dt"..say 0.00001 from DEVAL output which is my variables values? Once this is done, I do FZERO on the result I get after subtraction....this is what I was told.
> >
> > Could you please clarify how am I getting dy/dt out of the whole process? I am quite confused at this stage. Thanking you,
> >
> > Md. Shahriar Karim
> >
> >
> >
> > "Steven Lord" <slord@mathworks.com> wrote in message <h6firi$red$1@fred.mathworks.com>...
> > >
> > > "Md. Shahriar Karim" <karim.shahriar@gmail.com> wrote in message
> > > news:h6fikv$egv$1@fred.mathworks.com...
> > > > Hi Steve,
> > > >
> > > > DEVAL asks for a structure input. I also replaced
> > > >
> > > > [t, y]=ode45('steadystate', [0:0.1:10000], y0); part of my code by
> > > > sol=ode45('steadystate', [0:0.1:10000], y0);
> > >
> > > Try replacing the string input with a function handle to your function and
> > > use the second of those syntaxes..
> > >
> > > --
> > > Steve Lord
> > > slord@mathworks.com
> > >
>
> Actually, after you got the solution to the ODE, just call the function again you can get dy/dt, i.e.
>
> [t, y]=ode45('steadystate', [0:0.1:10000], y0);
> dydt = steadystate(t,y); %if your function is vectorized. Alternatively
>
> dydt=y;
> for k=1:numel(t)
> dydt(k,:)=steadystate(t(k),y(k,:));
> end
>
> HTH
> Yi

Subject: dy/dt value from ode45 solver

From: Steven Lord

Date: 20 Aug, 2009 16:55:24

Message: 9 of 11


"Md. Shahriar Karim" <karim.shahriar@gmail.com> wrote in message
news:h6h1q8$sp5$1@fred.mathworks.com...
> Hi Steve,
>
> Thanks for your help so far. However, I am totally unsure from the below
> part--
>
> Write a function to DEVAL the SOL struct array at a given time and
> subtract
> that result from the desired dy/dt, then use FZERO on that function.
>
> sol=ode45(@steadystate, [0:0.1:10000], y0);
>
> once I do the above, I get my all variables value after solving the
> ODEs..variables are..B C D ..etc.
>
> Now from this I am told to use the DEVAL on "sol", it gives me the
> values of my variables B,C,D...but I was needed the "dy/dt...the slope
> values.
>
> Now, I am supposed to subtract my desired "dy/dt"..say 0.00001 from DEVAL
> output which is my variables values? Once this is done, I do FZERO on the
> result I get after subtraction....this is what I was told.
>
> Could you please clarify how am I getting dy/dt out of the whole process?
> I am quite confused at this stage. Thanking you,

After reading Yi's response, I think I misread your question and I _think_
it's simpler than I'm making it out to be, but I'm going to ask a
clarification question to make sure I understand what you're asking for.

    Are you trying to evaluate dy/dt at the times t and solution values y
returned by ODE45?

If so, just call your ODE function with those t and y inputs. Depending on
how you wrote your ODE function, you may need to loop over the elements of
the time vector and the rows of the solution vector. Remember, ODE45
requires a function that accepts t and y values and returns dy/dt evaluated
at those values -- which is precisely what I think you're looking for.

If that's not what you're looking for, please explain in a bit more depth
what you're trying to do.

--
Steve Lord
slord@mathworks.com

Subject: dy/dt value from ode45 solver

From: Md. Shahriar Karim

Date: 20 Aug, 2009 17:23:21

Message: 10 of 11

Hi Steve,

Thanks.

I need dy/dt value from a set of ODE. y value is not mandatory for me. however, it's okay if I have those. I have coded it in following manner--
***********************************************************
[t, y]=ode45('steadystate', [0:0.1:10000], y0);
% dydt=steadystate(t,y);
plot(t, y)
xlabel('TIME'); ylabel('Concentration of Species in Molecules');
dydt=y;
for k=1:numel(t)
    dydt(k,:)=steadystate(t(k),y(k,:));
end
save dydt;
slopeC=dydt(:,2);
for i=1:numel(t)
    if (slopeC(i)<0.000001)
        time=t(i)
        break;
    end
************************************************

But I am not yet clear how the calling of steadystate function is giving me the data dy/dt? Could you please explain a little bit?

Thanks,

Shahriar


"Steven Lord" <slord@mathworks.com> wrote in message <h6jv5k$mfq$1@fred.mathworks.com>...
>
> "Md. Shahriar Karim" <karim.shahriar@gmail.com> wrote in message
> news:h6h1q8$sp5$1@fred.mathworks.com...
> > Hi Steve,
> >
> > Thanks for your help so far. However, I am totally unsure from the below
> > part--
> >
> > Write a function to DEVAL the SOL struct array at a given time and
> > subtract
> > that result from the desired dy/dt, then use FZERO on that function.
> >
> > sol=ode45(@steadystate, [0:0.1:10000], y0);
> >
> > once I do the above, I get my all variables value after solving the
> > ODEs..variables are..B C D ..etc.
> >
> > Now from this I am told to use the DEVAL on "sol", it gives me the
> > values of my variables B,C,D...but I was needed the "dy/dt...the slope
> > values.
> >
> > Now, I am supposed to subtract my desired "dy/dt"..say 0.00001 from DEVAL
> > output which is my variables values? Once this is done, I do FZERO on the
> > result I get after subtraction....this is what I was told.
> >
> > Could you please clarify how am I getting dy/dt out of the whole process?
> > I am quite confused at this stage. Thanking you,
>
> After reading Yi's response, I think I misread your question and I _think_
> it's simpler than I'm making it out to be, but I'm going to ask a
> clarification question to make sure I understand what you're asking for.
>
> Are you trying to evaluate dy/dt at the times t and solution values y
> returned by ODE45?
>
> If so, just call your ODE function with those t and y inputs. Depending on
> how you wrote your ODE function, you may need to loop over the elements of
> the time vector and the rows of the solution vector. Remember, ODE45
> requires a function that accepts t and y values and returns dy/dt evaluated
> at those values -- which is precisely what I think you're looking for.
>
> If that's not what you're looking for, please explain in a bit more depth
> what you're trying to do.
>
> --
> Steve Lord
> slord@mathworks.com
>

Subject: dy/dt value from ode45 solver

From: Steven Lord

Date: 20 Aug, 2009 17:28:10

Message: 11 of 11


"Md. Shahriar Karim" <karim.shahriar@gmail.com> wrote in message
news:h6k0q9$9f5$1@fred.mathworks.com...
> Hi Steve,
>
> Thanks.
>
> I need dy/dt value from a set of ODE. y value is not mandatory for me.
> however, it's okay if I have those. I have coded it in following manner--
> ***********************************************************
> [t, y]=ode45('steadystate', [0:0.1:10000], y0);
> % dydt=steadystate(t,y);
> plot(t, y)
> xlabel('TIME'); ylabel('Concentration of Species in Molecules');
> dydt=y;
> for k=1:numel(t)
> dydt(k,:)=steadystate(t(k),y(k,:));
> end

*snip*

> But I am not yet clear how the calling of steadystate function is giving
> me the data dy/dt? Could you please explain a little bit?

Remember, as stated in the documentation for ODE45, the first input argument
that it requires is a function that accepts two inputs, t and y, and returns
the right-hand side of the system of differential equations y' = dy/dt =
f(t, y). Thus the values from that function _are_ the dy/dt values you're
looking for.

--
Steve Lord
slord@mathworks.com

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
dydt from ode45... Md. Shahriar Karim 20 Aug, 2009 13:24:12
dydt value calc... Md. Shahriar Karim 20 Aug, 2009 00:09:23
deval Md. Shahriar Karim 18 Aug, 2009 20:59:22
ode 45 Md. Shahriar Karim 18 Aug, 2009 20:59:22
steadystate Md. Shahriar Karim 18 Aug, 2009 20:59:22
steadystate time Md. Shahriar Karim 18 Aug, 2009 15:59:06
dydt Md. Shahriar Karim 18 Aug, 2009 15:59:06
ode45 Md. Shahriar Karim 18 Aug, 2009 15:59:06
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com