필터 지우기
필터 지우기

ODE45 Event Not Computed When Trying To Solve

조회 수: 1 (최근 30일)
Tyler Bikaun
Tyler Bikaun 2017년 1월 23일
댓글: Tyler Bikaun 2017년 1월 23일
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
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.
Tyler Bikaun
Tyler Bikaun 2017년 1월 23일
Hi Torsten, thanks for the reply.
See below, as the value of z(1) changes I need k_mb to alter periodically. I've been trying to figure out a way to do this for so long, but cannot figure it out.
Any help would be greatly appreciated.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
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.

카테고리

Help CenterFile Exchange에서 Get Started with MuPAD에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by