Info

This question is closed. Reopen it to edit or answer.

about useing ode23 solving ordinary differential equations

2 views (last 30 days)
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)

Walter Roberson
Walter Roberson on 11 Jul 2015
Edited: Walter Roberson on 12 Jul 2015
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
Walter Roberson
Walter Roberson on 12 Jul 2015
My solution does not appear to work. I will need to investigate further.

Tags

Community Treasure Hunt

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

Start Hunting!