MATLAB Answers


Why is ODE45 using so many intervals?

Thomas Veith さんによって質問されました 2019 年 6 月 17 日
最新アクティビティ Jan
さんによって 回答されました 2019 年 6 月 17 日
Hi all,
I'm using the code below to solve an ODE. It's not problematic per se, but I'm just wondering if there is a way to force it to use less intervals? As it is written below, I get 22,237 entries for both x and y in my sol structure.
options=odeset('RelTol', 1e-8, 'AbsTol', 1e-8);
tspan=[0 50];
ODE = @(t,x,gamma,psi,beta) [-gamma*x(1) + psi*x(1); beta*x(2) - x(1)*x(2)];
sol = ode45(@(t,x) ODE(t,x,0.4116,0.6026,0.7505), tspan, [1 29], options);

  0 件のコメント

サインイン to comment.


2 件の回答

Star Strider
回答者: Star Strider
2019 年 6 月 17 日

It will use as many intervals as it needs to.
If you want it to output fewer intervals, create ‘tspan’ as a vector of more than two elements, for example:
tspan = linspace(0, 50, 25);
will result in the result having 25 values for the time vector and a (25xN) matrix for the integrated solution.

  3 件のコメント

Thomas Veith 2019 年 6 月 17 日
Thank you for your quick reply! However, even if I use linspace as suggested, it still spits out a 1x22237 for sol.x and 2x22237 for sol.y. Perhaps there is another way?
Star Strider
2019 年 6 月 17 日
My pleasure.
I always just do this:
tspan = linspace(0, 50, 50);
[t,y] = ode45(@(t,x) ODE(t,x,0.4116,0.6026,0.7505), tspan, [1 29], options);
I have no idea why it fails to follow the elements of tspan when using the structure output, however, I was able to reproduce that behaviour. If you want to force the integrator to output its results using the elements of tspan, use the convention I use here, rather than the structure output.
2019 年 6 月 17 日
If the output of ode45 is caught as a struct, a vector valued tspan is ignored. I still consider this as a bug, reported it some years ago, but MathWorks thinks, this is the wanted behaviour. You need just to change one line in ode45.m to get the expected time steps in the output.

サインイン to comment.

回答者: Jan
2019 年 6 月 17 日

Do you want to reduce the number of evaluated steps to reduce the processing time, or is it enough to reduce the size of teh output, because you need less points?
For the later you can either use interp1 or a faster metod from the FileExchange, or use the two outputs [t,y] instead of the struct (see my comment above).
To get less evaluated steps, either increase the tolerances, or use another integrator. Did you check if the problem is stiff?

  0 件のコメント

サインイン to comment.

Translated by