I'm using Matlab to solve a differential equation. I want to force ode45 to take constant steps, so it always increments 0.01 on the T axis while solving the equation. How do I do this?
ode45 is consistently taking optimized, random steps, and I can't seem to work out how to make it take consistent, small steps of 0.01. Here is the code:
options= odeset('Reltol',0.001,'Stats','on');
%figure(1);
%clf;
init=[xo yo zo]';
tspan=[to tf];
%tspan = t0:0.01:tf;
[T,Y]=ode45(name,tspan,init,options);
in order to obtain a higher accuracy for polynomials of order 4 and above, reduce the "RelTol" and "AbsTol" properties using the "odeset" function.
m. (second order accuracy) it may be preferable to use a higher-order method (they are more efficient) and in particular to use an automatically chosen variable timestep. MATLAB has such a routine built in, called ode45.
Solve the ODE using ode45 . Specify the function handle so that it passes the predefined values for A and B to odefcn . A = 1; B = 2; tspan = [0 5]; y0 = [0 0.01]; [t,y] = ode45(@(t,y) odefcn(t,y,A,B), tspan, y0); Plot the results.
Based on the documentation it doesn't appear that you can control the size of the steps taken internally by ode45
when solving the equation, but you can control the time points at which the output is generated. You can do this as indicated in your commented-out line:
tspan = to:0.01:tf; % Obtain solution at specific times
[T, Y] = ode45(name, tspan, init, options);
With respect to the accuracy of solutions when fixed step sizes are used, refer to this excerpt from the above link:
If
tspan
has more than two elements[t0,t1,t2,...,tf]
, then the solver returns the solution evaluated at the given points. However, the solver does not step precisely to each point specified intspan
. Instead, the solver uses its own internal steps to compute the solution, then evaluates the solution at the requested points intspan
. The solutions produced at the specified points are of the same order of accuracy as the solutions computed at each internal step.Specifying several intermediate points has little effect on the efficiency of computation, but for large systems it can affect memory management.
So, even when you specify that you want the solution at specific time points in the output, the solvers are still internally taking a number of adaptive steps between the time points that you indicate, coming close to the values at those fixed time points.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With