how I can fix sample time in ode45

46 views Asked by At

I want to fix the sample time (for example, T=0.5) in ode45. How can I do it? If it is not possible, do we have another way to fix the sample time in Matlab?

I tried following code, but this code did not fix the sample time. When I debug it, see ode45 solve it with variable sample time.

T = 0.5;
tspan = 0:T:10;
y0 = [0.2,0.3];
[t, y] = ode45(@(t,y) odefcn(t,y), tspan, y0);
plot(t, y, '-o'), grid on
xlabel('Time')
function dx = odefcn(t,x)
dx = zeros(2,1);
u = [2 -2]*x(1:2) + 1;
dx(1) = x(1) - 2*x(2);
dx(2) = - x(2) + u;
end
1

There are 1 answers

1
James Tursa On

The ode45( ) function has adaptive step sizing and you can't change that behavior. What you can do is specify tolerances that ode45( ) will use during the integration process, which in turn will influence the step sizes chosen. E.g.,

options = odeset('RelTol',1e-8,'AbsTol',1e-10);
[t, y] = ode45(@(t,y) odefcn(t,y), tspan, y0, options);

If you are doing a study to compare results using various fixed step sizes, you will need to write your own RK4 solver code.