MATLAB: ODE45 code running indefinitely

ode45

My code continues to run indefinitely. I'm trying to use ode45 to output time and two different x values. I'm just not sure what is going on/why it takes so long. Any thoughts?
My main file:
time = (0:0.1:10);
initial = [0 0 0 0];
[T,X] = ode45('memsODE',time,initial);
My ode function file:
function xdot = memsODE(t,x)
m = [4*10^(-11) 4*10^(-9)];
k = [0.25 0.05];
b = 1*10^(-10);
P = 13790;
A = 4*10^(-8);
delta = 100*10^(-6);
c = [4266666666.66667 -640000 21.3333333333333];
f1 = c(1)*x(1).^3+c(2)*x(1).^2+c(3)*x(1);
if (x(1)<(delta/2))
f2 = 1;
else
f2 = -1;
end
xdot(1) = x(2);
xdot(2) = (-f1-2*k(1)*(x(1)-x(3)))/m(1);
xdot(3) = x(4);
xdot(4) = (-b*x(4)-2*k(2)*x(3)+2*k(1)*(x(1)-x(3))+P*A*f2)/m(2);
xdot = xdot';

Best Answer

The ode* routines cannot handle discontinuities. You need to create an event function to detect when x(1) switches between less than delta/2 or not, and assert termination when it does. Then you restart the computation from the last time of the previous run, using the last output of the previous run as the initial conditions for the new run.
I am working on an adjusted version.