Introduce a while loop ina ode45 resolution

14 views (last 30 days)
Hello, I am a new user of Matlab and I am facing difficulties while trying to introduce a while loop in a differential resolution. My code is this one :
function [dy] = vdp1(t,y,Vent)
Cx0 = 2;
Cx1 = 0.8;
Syz = 2;
Sxz = 2;
Sxy = 2;
S1 = 75;
tf1 = 4;
m = 15;
M = 80;
Latitude = 69.5;
dy = zeros (6,1);
D = (sqrt(y(1)^2 + y(2)^2 + y(3)^2))/(2*(m+M));
dy(1) = -1.225*Cx0*Syz*D*y(1) + 2*10^(-5)*sind(Latitude)*y(2) - 2*10^(-5)*cosd(Latitude)*y(3);
dy(4)= y(1);
dy(2) = -1.225*Cx0*Sxz*D*y(2) - 2*10^(-5)*sind(Latitude)*y(1);
dy(5) = y(2);
dy(3) = -1.225*(Cx0*Sxy + t*Cx1*S1/tf1)*D*y(3) + 2*10^(-5)*cosd(Latitude)*y(1) - 9.81;
dy(6) = y(3);
end
function [x] = Position(tf1)
x0=[77 0 -2.4 0 0 4000];
t0=0;
tfin=tf1;
[t,x] = ode45(@vdp1,[t0 tfin],x0,[]);
end
I would like to know if it is possible to insert a while loop saying something like : while y(6) is higher than a certain altitude then solve, else the resolution is over.
Thank you for all the help you could give me.

Accepted Answer

Jan
Jan on 13 Jul 2017
Edited: Jan on 13 Jul 2017
This seems to be a job for the event function. See odeset for setting such a funtion. See also https://www.mathworks.com/help/matlab/math/ode-event-location.html.
Note: 2*10^(-5) is an expensive power operation, while 2e-5 is a cheap constant, which saves run time.
  1 Comment
Lancelot Ribot
Lancelot Ribot on 13 Jul 2017
Thank you for your answer !! I am going to read this page carefully
P.S : thanks for the tip

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!