ODE45 Event Not Computed When Trying To Solve
1 次查看(过去 30 天)
显示 更早的评论
Hi, I'm trying to implement an event with ODE45 to vary a value (k_mb) based on a value (z(1)) that is integrated within ODE45. I have reviewed the MathWorks documentation and examples within MATLAB, but cannot figure out what is going wrong with my code. Any help would be much appreciated.
function nestttest1
r_p = 10; r_g = 10; I_p = 10; m_p = 10; I_g = 10; m_g = 10;
tspan = [0:100];
z0 = [1,0,0,0,0,0,0,0];
A_or_B = [5,10];
V = [0,5,15,20,30,35,45,50,60];
iters = 0;
options = odeset('Events', @events);
[t,z,ye,ie] = ode45(@odefunc,tspan,z0, options);
[t,z(:,:)]
[ye,ie]
function zdot = odefunc(t,z)
if iters == 0
k_mb = 5;
end
zdot(2) = ((r_p*k_mb)/I_p)*(-r_p*z(1)+r_g*z(3)+z(5)-z(7)); % Eq 1ss
zdot(6) = (k_mb/m_p)*(r_p*z(1)-r_g*z(3)-z(5)+z(7)); % Eq 2ss
zdot(4) = ((r_g*k_mb)/I_g)*(-r_g*z(3)+r_p*z(1)+z(7)-z(5)); % Eq 3ss
zdot(8) = (k_mb/m_g)*(-r_p*z(1)+r_g*z(3)-z(7)+z(5)); % Eq 4ss
zdot(1) = z(2); % Eq 5ss
zdot(3) = z(4); % Eq 6ss
zdot(5) = z(6); % Eq 7ss
zdot(7) = z(8); % Eq 8ss
zdot = zdot';
iters = iters + 1;
k_mb % Used to check whether k_mb is being changed.
end
function [k_mb,isterminal,direction] = events(t,z)
lower_index = find(V < z(1), 1, 'last');
even_or_odd = mod(lower_index,2);
k_mb = A_or_B(2 - even_or_odd);
isterminal = 0;
direction = 0;
end
end
2 个评论
Torsten
2017-1-23
An event is reported when a certain function changes sign.
In your case, I can't see that k_mb could change sign since it is 5 or 10.
Could you explain in your own plain words when your event should happen ?
Best wishes
Torsten.
回答(1 个)
Jan
2017-1-23
Changing a variable in an event function, which is shared using nested functions, is a bad idea. Note that the event function can be called multiple times at the startup, e.g. during the determination of the initial step size. In consequence, I could not estimate the value of k_mb, when odefunc is called for the first valid step.
Changing a parameters in a non-differentiable way inside the function to be integrated is a bad idea also, because Matlab's integertaors handle smooth functions only, see: http://www.mathworks.com/matlabcentral/answers/59582#answer_72047.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with MuPAD 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!