Strange problem with tspan in ode45

30 ビュー (過去 30 日間)
VD
VD 2020 年 4 月 8 日
コメント済み: Ameer Hamza 2020 年 4 月 9 日
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!!

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 8 日
編集済み: Ameer Hamza 2020 年 4 月 8 日
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 件のコメント
VD
VD 2020 年 4 月 9 日
Hi,
That was the answer I was looking for. Thanks!
Ameer Hamza
Ameer Hamza 2020 年 4 月 9 日
Glad to be of help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by