Strange problem with tspan in ode45

34 views (last 30 days)
Hi all,
I'm currently trying to solve an ODE with ode45. I'm having trouble with the solution I find. It seems that by using as tspan = [0 100] instead than linspace(0,100,100) (for example) in the ode45 inputs, the solution I get changes. I know that these two options are not exactly the same, but I've tried to study the MATLAB documentation related to this issue, and I do not find anything convincent enough. Any clue? Which solution is more accurate?
Below you can find two plots; for different initial conditions. The first column is the solution using tspan = [0 100] and the second column is with the linspace option.
It clearly seems that the option of using tspan = [0 100] is more accurate, but I do not really understand why.
Thanks in advance!!

Accepted Answer

Ameer Hamza
Ameer Hamza on 8 Apr 2020
Edited: Ameer Hamza on 8 Apr 2020
This issue is not about accuracy. When you use tspan=[0 100], the t vector returned by ode45 is adaptively spaced. The differences between consecutive t values are not always constant. Note that the returned t vector can have any length, and MATLAB will smartly create vector t such that it captures the variation in your output signal. Now, when you use
t = linspace(0,100,100)
The ode45 still internally solves the equation exactly the same as before, but now it will the value of output signal at the time vector you provided. You can see that elements of linspace(0,100,100) are equally spaced and have no sense of where the function has variations and where it is smooth. To see the difference, just increase the number of elements in vector t
t = linspace(0,100,1000)
and you will see that it approach the output given by ode45.
  2 Comments
VD
VD on 9 Apr 2020
Hi,
That was the answer I was looking for. Thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!