MATLAB executing else block in event locator function even though if condition is true
2 views (last 30 days)
Show older comments
I am trying to simulate a two-tank liquid level problem. One of the 'states' (control engg. terminology) is defined by a differential equation that I am integrating using ode45. The integration stops when the associated event function gets value=1 based on a condition [which is that the difference between the liquid levels in both tanks is less than a certain value(0.001)]. While debugging I see that the if condition evaluates true after certain number of iterations but MATLAB still executes the else block thus making the integration endless.
UPDATE:It looks like it is executing the if block now but the integration is not terminating even though value=1 is executed. Here is the code:
%ODE function
function hdot = q4_ode_func(t, h)
constants;
hdot=zeros(2,1)
h1=h(1); h2=h(2);
diff1=h1-h0;
diff2=h1-h2;
if(diff1>0)
Qv1=1;
else
Qv1=-1;
end
if(diff2>0)
Qv2=1;
else
Qv2=-1;
end
hdot(1) = (1/A)*(-Qv1-Qv2);
hdot(2) = (1/A)*(Qv1+Qv2);
%Event locator function
function [value,isterminal,direction] = q2
constants;
if h(2)<4.8
value=1;
else
value =0;
end
isterminal = 1;
direction = 0;
end
1 Comment
Image Analyst
on 18 Jan 2017
Just how is function q2 supposed to get array h when h is not passed into it via the argument list, nor is it a global variable, nor do you get it via getappdata()?
Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!