## Why is ODE45 using so many intervals?

Thomas Veith

### Thomas Veith (view profile)

さんによって質問されました 2019 年 6 月 17 日

### Jan (view profile)

さんによって 回答されました 2019 年 6 月 17 日
Star Strider

### Star Strider (view profile)

さんの 回答が採用されました
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 (view profile)

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.

Thomas Veith

### Thomas Veith (view profile)

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

### Star Strider (view profile)

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.
Jan

### Jan (view profile)

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 (view profile)

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