MATLAB executing else block in event locator function even though if condition is true
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2017-1-18
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()?
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!