2

I used ode45 and ode23 for a set of non-stiff differential equations. However, both methods converge to a slightly different solution. How can I find out which one is correct? See attached plot where blue is ode45, red is ode23. The dashed lines are the final values for each solver. Also, ode15s is a bit different (less than 1 m)...

enter image description here

horchler
  • 18,384
  • 4
  • 37
  • 73
Jurriën
  • 199
  • 1
  • 2
  • 16
  • 1. you could check the asymptotical value of the solution at `t=inf` on paper. 2. are you sure the equation is not stiff? Lot of things going on at the beginning 3. we're talking about 0.1% of an error. 4. Why are the initial values different? 5. why are there so few (sparse) time steps? – Andras Deak -- Слава Україні Jan 27 '16 at 19:19
  • 2
    They're likely both "wrong" as you're using numerical integration. It's going to be hard to help without runnable code to replicate the issue. Also, why the dashed lines for the final values not the same as the actual final values at 10 seconds? – horchler Jan 27 '16 at 19:20
  • The x-axis represents the final values when chosen for a timestep of 10 seconds, 5 seconds, etc. So, x-axis isn't the time. It is just a simple ballistic missile trajectory calculation with air resistance. – Jurriën Jan 27 '16 at 19:24
  • Hmm...what *kind* of resistance? There's a chance you can solve it on paper. – Andras Deak -- Слава Україні Jan 27 '16 at 19:28
  • http://www.usna.edu/Users/mecheng/ratcliff/EM375/labs/07Project/ProjectileTheory.pdf this is the problem i'am trying to solve :) – Jurriën Jan 27 '16 at 19:33
  • 1
    How exactly are you "choosing a time step". Matlab's ODE solvers are adaptive so one specifies tolerances (it's quite hard to set a true fixed step size with them). – horchler Jan 27 '16 at 19:37
  • @horchler That's true, i just found out that when setting a timestep Matlab interpolates its ODE solutions to 'simulate' your own timestep. I have been playing with some tolerance options, but so far no differences at all... Which is strange because i have set (I think...) my tolerance at 'AbsTol' 1e-10.... – Jurriën Jan 27 '16 at 19:41
  • 1
    You need to set the relative tolerance to a smaller value, not the absolute tolerance. Then `ode23` and `ode45` will converge to closer values after the same amount of time. – horchler Jan 27 '16 at 19:52
  • @horchler, please write your last answer below, so I could accept it as the correct answer. Now all ODE's get me the same answer :) – Jurriën Jan 27 '16 at 20:00

1 Answers1

0

Matlab's ODE solvers are adaptive so one specifies tolerances rather than a step size (see also this answer). Given the code in the PDF linked in the comments, if you specify a smaller value for the relative tolerance, the solutions from ode45 and ode23 will converge after the same amount of time. You can use odeset to set 'RelTol':

...
opts = odeset('RelTol', 1e-12);
[t, oput] = ode23(@(t,y)secondode(t,y,C,g), tspan, IC, opts);
...

Note that I also got rid of the global variables used in the linked code (they're bad and inefficient). You also need to change the function definition for secondode to:

function z = secondode(t, indata, C, g)
...
Community
  • 1
  • 1
horchler
  • 18,384
  • 4
  • 37
  • 73