Problem with ODE45

15 ビュー (過去 30 日間)
yashwant kolluru
yashwant kolluru 2016 年 11 月 9 日
編集済み: yashwant kolluru 2016 年 11 月 9 日
Hallo, I wrote code on ODE45. I want to record the event at a condition (pasted in the code). The code is as follows.
y1_init = 0; y2_init = 0; ph_init = 0; vy1=0;vy2=2;wz=0;
x_init = [y1_init; y2_init; ph_init]; v_init= [vy1; vy2; wz]; acc_init = [0; 0; 0];
w_init = [x_init; v_init]; wp_init = [v_init; acc_init];
opts = odeset('RelTol',1e-3,'AbsTol',1e-3,'Events',@events);
[t,y,te,ye,ie] = ode45(@expl,[0 30],w_init,[],Mdash,Qdash,Pdash,hdash,s0dash,opts);
teout = [teout; te];
yeout = [yeout; ye];
ieout = [ieout; ie];
% where Mdash, Qdash,Pdash, hdash,s0dash are matrices I defined earlier.
%Below is the ode function
function dydt = expl(t,y,Mdash,Qdash,Pdash,hdash,s0dash,opts)
dydt = [y(4:6);(Mdash^(-1)*hdash)-(Mdash^(-1)*Qdash*y(1:3))];
end
% below is the vent function
function [value,isterminal,direction] = events(t,y)
value = y(5)-1.9999;
isterminal =1;
direction =0; end
Everything works fine without the definitions of event. But I want to stop the ode at some point (the variable 'value' in event function). But it throws me an error at the main function.
The below is the error.
Error in ode45 (line 78) solver_name = 'ode45';
Output argument "varargout{4}" (and maybe others) not assigned during call to "C:\Program Files\MATLAB\R2012a\toolbox\matlab\funfun\ode45.m>ode45".
Error in Peicewise_ODE_v5 (line 40) [t,y,te,ye,ie] = ode45(@expl,[0 30],w_init,[],Mdash,Qdash,Pdash,hdash,s0dash,opts);
Any Inputs? I don't understand what is the mistake I did.

採用された回答

Torsten
Torsten 2016 年 11 月 9 日
Change the following lines:
[t,y,te,ye,ie] = ode45(@(t,y)expl(t,y,Mdash,Qdash,Pdash,hdash,s0dash),[0 30],w_init,opts);
...
function dydt = expl(t,y,Mdash,Qdash,Pdash,hdash,s0dash)
dydt = [y(4:6);(Mdash^(-1)*hdash)-(Mdash^(-1)*Qdash*y(1:3))];
end
Best wishes
Torsten.
  2 件のコメント
yashwant kolluru
yashwant kolluru 2016 年 11 月 9 日
編集済み: yashwant kolluru 2016 年 11 月 9 日
I did it few minutes back! it works. I still have a short question. I want to compare the value variable (of event function) with an variable from main function.
Something like below
function [value,isterminal,direction] = events(t,y)
value = abs(y(2)-y(1))-s0dash;
isterminal =1;
direction =0;
%Where s0dash is the value from the main and it keeps changing with for loop. I tried passing it to event function but does not work. Can you suggest me, about what can i do know?
yashwant kolluru
yashwant kolluru 2016 年 11 月 9 日
編集済み: yashwant kolluru 2016 年 11 月 9 日
Apologize, I got the answer.I just changed the definition of the event function.
Anyway! Thanks so much for the help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by