Problem with ODE45

16 views (last 30 days)
yashwant kolluru
yashwant kolluru on 9 Nov 2016
Edited: yashwant kolluru on 9 Nov 2016
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.

Accepted Answer

Torsten
Torsten on 9 Nov 2016
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 Comments
yashwant kolluru
yashwant kolluru on 9 Nov 2016
Edited: yashwant kolluru on 9 Nov 2016
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 on 9 Nov 2016
Edited: yashwant kolluru on 9 Nov 2016
Apologize, I got the answer.I just changed the definition of the event function.
Anyway! Thanks so much for the help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!