Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ode45 output

27 views
Skip to first unread message

Susan van Vliet

unread,
Sep 5, 2005, 5:33:03 PM9/5/05
to
Hi,

I have two questions about the ode45 integrator. First, I would like
to export more than just the integrated y-variables. Within the
function I am calculating other variables that I would like to view,
but I don’t know how to output these variables in the function. An
example:

[t,y] = ode45(@model,[0 1000],[1 1 1 1 1],options);
with
function [dy] = model (t,y)

If I change this to
[t,y,var1,var2] = ode45(@model,[0 1000],[1 1 1 1 1],options);
function [dy] = model(t,y,var1,var2)

I do get var1 and var2 as an output, but not for the 1000 timesteps..

Would anybody know an easy solution to this problem?

The second question is about the timestep output. I would like to
get the whole number timestep as an output. In the help menu I found
that I have to change [1 1000] in the above expression to
[0,1,...,1000]. When I do this, everything after the three dots is
commented out. When I enter [0,1,..,1000], it is not commented out,
but I get an error message: ‘Missing variable or function’. When I
enter [0,1, ,1000] I get the error message: ‘ Incomplete or misformed
expression or statement’ and when I enter [0 1 1000] I get only three
output rows for t, one for t=0, t=1 and t=1000. Does anybody have an
idea how to change this?

Thank you very much,
Susan

Steven Lord

unread,
Sep 6, 2005, 10:16:41 AM9/6/05
to

"Susan van Vliet" <questi...@hotmail.com> wrote in message
news:ef132...@webx.raydaftYaTP...

> Hi,
>
> I have two questions about the ode45 integrator. First, I would like
> to export more than just the integrated y-variables. Within the
> function I am calculating other variables that I would like to view,
> but I don't know how to output these variables in the function. An
> example:
>
> [t,y] = ode45(@model,[0 1000],[1 1 1 1 1],options);
> with
> function [dy] = model (t,y)
>
> If I change this to
> [t,y,var1,var2] = ode45(@model,[0 1000],[1 1 1 1 1],options);
> function [dy] = model(t,y,var1,var2)
>
> I do get var1 and var2 as an output, but not for the 1000 timesteps..
>
> Would anybody know an easy solution to this problem?

You're asking one question and giving an example of a different question.
Do you want your ODE function to accept additional input arguments or do you
want it to output additional data at each timestep?

Accepting additional input arguments: if you're using MATLAB 7.0 (R14) or
later, use an anonymous function.

var1 = 1; var2 = 2;
[t, y] = ode45(@(t, y) model(t, y, var1, var2),[0 1000],[1 1 1 1
1],options);

% Given that your ODE function declaration is:
function dy = model(t,y,var1,var2)

If you're using a version of MATLAB prior to 7.0, look in the help for ODE45
for the P1 and P2 parameters and use them to pass in the additional
parameters.

Displaying additional output arguments: one of the easiest ways is to store
the values you want to output in a persistent variable inside your ODE
function, then at the end make one additional call to your ODE function to
extract the saved data. There's an example of how to do this here:

http://www.mathworks.com/support/solutions/data/1-196SD.html?solution=1-196SD

> The second question is about the timestep output. I would like to
> get the whole number timestep as an output. In the help menu I found
> that I have to change [1 1000] in the above expression to
> [0,1,...,1000]. When I do this, everything after the three dots is
> commented out. When I enter [0,1,..,1000], it is not commented out,
> but I get an error message: 'Missing variable or function'. When I
> enter [0,1, ,1000] I get the error message: ' Incomplete or misformed
> expression or statement' and when I enter [0 1 1000] I get only three
> output rows for t, one for t=0, t=1 and t=1000. Does anybody have an
> idea how to change this?

Yes. The usage of ... in the help text indicates that you should fill in
the additional time steps at which you want the solution to be returned --
it does not mean that you should use the ... syntax to represent those
additional time steps. Pass in 0:1000 as your timestep vector -- MATLAB
will expand this to the vector whose first five elements are [0 1 2 3 4],
whose last five elements are [996 997 998 999 1000] and where each element
is 1 greater than the previous element.

--
Steve Lord
sl...@mathworks.com


Bill Rooker

unread,
Nov 2, 2005, 12:47:45 PM11/2/05
to
Hello,

I am more interested in the ways to obtain parameters that depend on
the integration. I followed the steps (i.e. persistent variables,
ASSIGNIN, EVALIN) from Solution Number: 1-196SD, and it appears that
I get output for every call to the function. Since the algorithm
converges to a solution for every time step, I cannot match the final
value to its applicable time step. Is there a way to get the final
value for every time step?

Thanks for any help.

Best regards,

Bill Rooker

0 new messages