MATLAB: How to set Variable-step in ODE15s?

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.

Best Answer

"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.