If logic issues inside ode45

When I print the value of the boolean inside of my if statement inside of my ode function, it reports back values of 0, yet it continues to run inside of the if statement. Because the boolean is not true, the if statement is false and therefore should proceed to the else statement, but it is not the case here. Specifically, the if statement is running when x(4) < 0 but (x(3) - x(1)) > deltaXleft or x(4) < 0 but (x(3) - X(1)) > deltaXright. I don't know what to make of this.
function xdot = memsODE(t,x,K)
m = [4.97e-07 5.1289e-06];
b = [1e-6 1e-6]; %N*s/m
P = 5.9295e6; %Pa -> N/m^2 = 860 psi
A = 2.2e-7*10; %m^2
x1closed = 50e-6;
deltaXleft = 950e-6;
deltaXright = -50e-6;
%Find f1
X0 = 49e-6;
A1 = 0;
A2 = 1;
dx = 1e-6;
f1 = (A1 - A2) ./ (1 + exp((X0 - x(1))/dx)) + A2;
%Find f2
if x(1) < x1closed
f2 = 0;
else
f2 = 1;
end
%Equations of Motion
if ((x(4) > 0) && ((x(3) - x(1)) >= deltaXleft)) || (((x(3) - x(1)) <= deltaXright) && (x(4) < 0)))
xdot(1) = x(2);
xdot(2) = -1*((b(1) + b(2))*x(3) + (K(2)*x(3) + K(1)*x(1)) + f2*K(3)*(x(1) - x1closed) - P*A*f1)/(m(1) + m(2));
x(4) = x(2);
xdot(3) = x(4);
xdot(4) = xdot(2);
% print boolean value
((x(4) > 0) && ((x(3) - x(1)) >= deltaXleft)) || (((x(3) - x(1)) <= deltaXright) && (x(4) < 0)))
else
xdot(1) = x(2);
xdot(2) = -1*(b(1)*x(2) + K(1)*x(1) + f2*K(3)*(x(1) - x1closed))/m(1);
xdot(3) = x(4);
xdot(4) = -1*(b(2)*x(4) + K(2)*x(3) - f1*P*A)/m(2);
end

1 Comment

Torsten
Torsten on 19 Oct 2018
Edited: Torsten on 19 Oct 2018
You changed the value of x(4) before evaluating the boolean expression ...

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Asked:

on 18 Oct 2018

Edited:

on 19 Oct 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!