How to set Variable-step in ODE15s?

I am trying to solve ODE using ODE15s, but when I change the Variable-step, the result changes. So how to set the step? Thanks.
[t,N]=ode15s(odefun,[0:5:2000],NN0);
The resulet is like the first image. When I changed the step to 10,the result is like the second image.

4 Comments

Stephen23
Stephen23 on 5 Oct 2018
Edited: Stephen23 on 5 Oct 2018
"The resulet is like the first image. When I changed the step to 10,the result is like the second image."
They both look fine to me. When you change the requested t values, of course the outputs will be different. And for a curve with such a sharp peak like that, of course the curves might look slightly different. I don't see what the problem is.
Plot them on the same axes, or set the Y limits to be the same, and you will see that they are the same curve.
Yes, the two curves look the same, but the values are different. that means different step results in different result. So which result is right and which step is better? How to set a better step?
plot both the curves on the same using hold on then you will get know.....use different steps and plot..at one time step..your values get converged..this would be the required time step.
Yes, you are alos right. Thanks a lot.

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 5 Oct 2018
Edited: Stephen23 on 5 Oct 2018
"Yes, the two curves look the same"
Because they are the same, within the precision of the solver and for the steps that you picked.
"...but the values are different."
I suspect your confusion is caused by the different peak value causing the Y axis to have different range. I told you two ways to compare the curves: did you try either of them? Because your curves are actually the same (just the peak is different, which is to be expected for such a sharp curve sampled at different X values).
To make a better comparison, try one of these:
  • plotting on the same axes (use hold on).
  • setting the Y limits to be the same.
  • using ginput or similar to get the X,Y values for points on the curves.
Lets compare the two imagess that you provided:
What is the Y-value for X=200? The both look about the same to me, with both curves showing about Y=0.16. If they have the same value of Y=0.16 at X=200, why do you say that these curves are different? Can you show where they are different (apart from the peak, which is expected)?
"That means different step results in different result."
Sure, in the sense that you will get two different sets of values. But those sets of values represent exactly the same curve, sampled as best as the solver can.
"So which result is right and which step is better? How to set a better step?"
They are both equally good. Pick whichever one you prefer.

More Answers (1)

Bruno Luong
Bruno Luong on 5 Oct 2018
Edited: Bruno Luong on 5 Oct 2018
[t,N]=ode15s(odefun,[0:5:2000],NN0);
Don't be confuse, the vector in tspan does not specify the solver step, just the solution values to be output at intermediate points you have specified.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!