about useing ode23 solving ordinary differential equations

Hi all,
I have a question about using ode23 or other ode functions in Matlab to solve ordinary differential equation . The call format of ode functions is like [T,Y] = ode23(odefun,tspan,y0). odefun is the differential equations. If I set a break point in the odefun function, I can see the variable and parameter values in the odefun will update every time I press F5 .
My question is can the odefun get the last step variable or parameter values in the current step?
Thanks

Answers (1)

No. If you need to pass values between iterations, add the values to your "y" vector. For example,
prevtime = 0; %getting the right initial conditions is important
prevs14 = 0;
initvals = [y0(:); prevtime; prevs14];
[T,OUTVALS] = ode23s(@odefun,tspan,initvals)
Y = OUTVALS(1:end-2,:); %discard the values carried between generations
with
function outvals = odefun(t, invals)
y = invals(1:end-2); %pull apart the inputs
prevtime = invals(end-1);
prevs14 = invals(end);
s14 = (sum(sin(y).^2) - prevs14)./(t-prevtime); %some intermediate calculation
newy = y * s14; %building outputs
outvals = [newy; t; s14]; %include the information to be passed between iterations
end
Remember, if you want to know what the "previous" values were, then you have the problem of what value to use for the first iteration.
Also remember to take the extra values into account when you are building the jacobian.

4 Comments

Hi Roberson, thanks a lot for your answer. I guess you means the call function of ode23 might be [T,OUTVALS] = ode23s(@odefun,tspan,initvals). I tried your function,but it can't preserve the previous s14 value in the variable prevs14.
you set the output of odefun is [newy; t; s14]. I think this means the derivate of the input variables (jacobin of input variables) in the invals is [newy;t,s14]. Like y = invals(1:end-2) can't be the previous value of y, how can the prevs14 be the previous value of s14? They will both update by iterations, as they are both input variables and outvals is their derivative.
newy is the derivative of y, t is the derivative of prevtime, s14 the derivative of prevs14.
You are correct, I had a couple of typos in invoking the ode23s; I edited the invocation the way you pointed out.
My solution does not appear to work. I will need to investigate further.

This question is closed.

Tags

Asked:

on 10 Jul 2015

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!