MATLAB: How to specifiy time in step size using ODE45

differential equationsodeode45

Hello, I was looking for some help with my homework. Right now my attached code answers the following ode, where the unit step (de) is equal to one degree. I need to make the step size last for a duration of one second. I was wondering how I specify the length of the unit step. For clarification the code is setting up two constant matrices A (4×4) and B(4×1) to take the form: y'=(A*y)+ (B*de). Any help is appreciated. Thanks.
clear,clc
l1 = [-0.0453,0.0363,0,-0.183];
l2 = [-0.3717,-2.0354,1,0];
l3 = [0.3398,-7.0301,-2.9767,0];
l4 = [0,0,1,0];
A = [l1;l2;l3;l4];
B = [0;-0.1609;-11.8674;0];
de = 1;
y0 = [0;0;0;0];
F = @(t,y) A*y+B*de;
[t,y] = ode45(F,[0 200], y0,options);

Best Answer

All of the ode*() routines are variable step. There is no way to set the actual step size, only the minimum step size.
When you specify tspan as a vector with exactly two values, then the ode routines report outputs for a variety of time points between the two, choosing the time points as needed to meet integration tolerances. The time points reported on are often not exactly the same as the time points that the ode function is called upon: the ode routines use the information from the steps actually taken in order to make projections.
When you specify tspan as a vector with more than two values, then the ode routines report outputs only for the values given in the vector. The ode routines will evaluate at a number of time points, choosing the time points as needed to meet integration tolerances, but they will only report about the points in the tspan in this case. The time points in the tspan are not necessarily evaluated at precisely: the ode routines use the information from the steps actually taken in order to make projections.
I am confused by your mention that your time step is "de" and that it is equal to one degree, and that you want your time step to last for the duration of 1 second. You seem to be mixing units there??
Is it possible that what you mean is that you want to evaluate
A*y + B * (de * (floor(t)+1))
? That is, you want B * de for time 0 to 1, and B * de * 2 for time 1 to 2, and B * de * 3 for time 2 to 3, and so on? If that is correct, then because such steps are not differentiable, you would have to use a series of calls:
y_in = y0;
for tstart = 0 : 199
F = @(t,y) A*y+B*de*(tstart+1);
tspan = [tstart tstart+1/2 tstart+1]; %3+ points to avoid reporting on the intermediates
[temp_t, temp_y] = ode45(F,tspan, y_in);
t(tstart+1) = temp_t(end);
y_in = temp_y(end, :);
y(tstart+1, :) = y_in;
end
That is, integrate for the first time step, get the boundary value produced, update the weight for B, use the produced boundary values as the initial conditions, integrate for 1 second; and so on.