Plot as you go in MATLAB

368 Views Asked by At

I'm self studying some numerical analysis and I'd like to get a feel for how an ODE solver speed varies as you move forward in time. Is it possible to use matlab to numerically solve an equation (using ode45 or similar) and plot the solution points as they are obtained?

1

There are 1 best solutions below

6
On BEST ANSWER

It's unclear whether you want to plot solutions in real time or if you want to evaluate the step size of the method.

In the first case, it doesn't make sense to plot solutions to ode45 in real time; ode45 uses a Dormand-Prince or a Runge-Kutta-Fehlberg 4(5) pair adaptive solver to compute the solutions, depending on the version of MATLAB that you have. In either case, the step size of the $n+1$ step cannot be computed until the $n$th step is computed, which means that unless your adaptation happens in sub-real time, you can't guarantee a real-time view.

However, the purpose of adaptive solvers is to accelerate solutions to be faster than real time while attaining accuracy better than fixed-step solvers. So in this sense it doesn't make much sense to look at real-time solutions, either.

However, if you want to evaluate the adapatation of the solver, you simply need to execute the following code:

[t,y] = ode45(f,tspan y0);
delta_t = t(2:end)-t(1:end-1);
plot(1:length(t)-1, delta_t);