about useing ode23 solving ordinary differential equations
Info
This question is closed. Reopen it to edit or answer.
Show older comments
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
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
craftsman
on 11 Jul 2015
Walter Roberson
on 12 Jul 2015
You are correct, I had a couple of typos in invoking the ode23s; I edited the invocation the way you pointed out.
Walter Roberson
on 12 Jul 2015
My solution does not appear to work. I will need to investigate further.
craftsman
on 12 Jul 2015
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!