'How to improve speed simulation results with ODE45/23/23s/15s
I want to simulate an ODE system, very fast. I usually use Euler-Method because it's fast, but not the most stable nor accurate. But I want to use a more standardlized method to simulate ODEs.
The problem with this code is that this way to simulate model is very slow and all depends on how large the input vector u is. If u is short, then the simulation will be fast. Also if stepTime is large, e.g 1 or 2, then the simulation will also be slow.
How can I improve the simulation speed by just modify options variable?
My idea is that if I could have fewer steps in the simulation, that would be great.
If we look at this code below.
% User inputs
model = @(t, x, u) [x(2); -0.7*x(1) - 0.2*x(2) + 2*u(1)]; % Second order state space model
u = linspace(5, 5, 100); % Input signal
stepTime = 0.5; % 0.5 seconds step time
x0 = [0;0]; % Initial states
options = odeset(); % Default values
% Savings
L = length(u);
x = zeros(size(x0, 1), L);
t = zeros(1, L);
% Loop
for i = 1:L
% Save time and output
x(:, i) = x0;
t(i) = stepTime*(i-1);
% Simulate ode45
tspan = [stepTime*(i-1) stepTime*i];
[time, output] = ode45(@(t, x) model(t, x, u(:, i)), tspan, x0, options);
% Next state
x0 = output(end, :)';
end
plot(t, x(1, :))
title('Position')
grid on
xlabel('Time')
ylabel('y0');
figure
plot(t, x(2, :))
title('Velocity')
grid on
xlabel('Time')
ylabel('y1');
It will give out this output signals. One is position and the other one is velocity.
The most interesting part is
% Next state
x0 = output(end, :)';
I'm taking the last row of output and call it next initial state.
Here is the problem. If I could reduce output from ode45 etc, then the simulation will become much faster.
How can I do that? Here is my thoughts:
- Is fixed step a solution? Is there any drawbacks from that? What about stability?
- Is it possible to only simulate the last value and avoid the rest of the values in
output?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

