Strange problem with tspan in ode45

30 Ansichten (letzte 30 Tage)
VD
VD am 8 Apr. 2020
Kommentiert: Ameer Hamza am 9 Apr. 2020
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!!

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 8 Apr. 2020
Bearbeitet: Ameer Hamza am 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 Kommentare
VD
VD am 9 Apr. 2020
Hi,
That was the answer I was looking for. Thanks!
Ameer Hamza
Ameer Hamza am 9 Apr. 2020
Glad to be of help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by