MATLAB: If statements in ode45 function

if statementMATLABode4ode45

i am using the following signal as an input in a matlab code that i have made.I have put it inside the ode45 function file but the results that i am getting don't make sense. I have tried using the fixed step ode4 solver and the results are what i expect to be, but i don't want to use a fixed step solver as i don't trust them to produce accurate results.
t = linspace(0,100,10000);
Fmax = 10000;
ts1 = 1;
ts2 = 2;
ts3= 0;
tt = ts1+ts2+ts3+ts4;
n = max(size(t));
Fn = zeros(1,n);
for i = 1:n
B = floor(t(i)/(tt));
if t(i) <= (B)*tt+ts1
Fn(i) =Fmax.*((t(i)-B*tt)/ts1);
elseif t(i) > (B)*tt+ts1 && t(i) <= (B)*tt+ts1+ts2
Fn(i) = Fmax;
elseif t(i) > (B)*tt+ts1+ts2 && t(i) <= (B)*tt+ts1+ts2+ts3
Fn(i) = -Fmax.*(t(i)-(ts2+ts1)-B*tt)/abs(ts3-ts2)+Fmax;
elseif t(i) > (B)*tt+ts1+ts2+ts3 && t(i) < (B)*tt+ts1+ts2+ts3+ts4
Fn(i) = 0;
Of course inside ode function i have taken out the ''for'' part and the i.

Best Answer

  • The MATLAB ODE solvers are adaptive-step, not fixed-step solvers. Everything I’ve read about them indicates they’re the most robust algorithms available.
    I don’t understand what you’re doing with your code. As a general rule, if you have different conditions (variable values, and such) for different times in your integration, the best strategy is to stop the integration, use the last values for the initial conditions of the next integration, then restart the integration. Repeat this for multiple discontinuities. The ODE solvers do not integrate well across discontinuities.