How do I include a whileloop in the Ode45?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hey!
I´m trying to solve a set of equation that describes the motion of a body in a ballistic trajectory. I'm interested of knowing at what downrange the body will hit the ground so I want to set a limit for the altitidue that H>0.
How can I include a whileloop in the ode45 function to set that limit.
Here is the code:
function [yp] = FS1(t,y)
ms1 = 698390; %structure mass for the first stage
[T, a, P, rho] = atmosisa(y(2));
g0=9.8; %Gravity at sea level [N/m^2]
Re=6371000; %Earth radius [m]
g=g0*(Re/(Re+y(2)))^2; %Gravitational variation
A=10; % Reference area
Cd=0; % Drag coefficient
D=0.5*rho*y(3)^2*Cd*A;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%The four first order differential equations yp=zeros(4,1);
yp(1)=y(3)*cos(y(4));
yp(2)=y(3)*sin(y(4));
yp(3)= -D/ms1 -g*sin(y(4)) +(((y(3)*(cos(y(4))))^2)*sin(y(4)))/(Re+y(2));
yp(4)= (-cos(y(4))*g)/y(3) +((y(3)*cos(y(4)))^2*cos(y(4)))/(y(3)*(Re+y(2)));
%%%%%%%%%%%%%%%%%%%%%%%%%%
y0 = zeros(4,1);
y0(1,1) = XR1(end); %Initial downrange, can be set to zero
y0(2,1) = HR1(end); %initial altitude, can be set to 7.6730e+004
y0(3,1) = VR1(end); %initial velocity, can be set to 2.7684e+003
y0(4,1) = gammaR1(end); %initial angel of attack, can be set to 0.5599
tspan = [0 500];
[t,y] = ode45(@FS1,tspan,y0);
XF1 = y(:,1);
HF1 = y(:,2);
VF1 = y(:,3);
gammaF1 = y(:,4);
figure(3)
plot(XF1,HF1)
The initial values is not that important, you can put the values you like. I will be very thankfull for any help.
6 comentarios
Star Strider
el 23 de Sept. de 2012
@Hassan — Thank you for your clarification. The problem is that without the atmosisa function, I cannot run your code and try my ‘Events’ option with it.
In any event, if you have solved your problem (as you seem to have in your comment to Azzi's answer), then any solution I can come up with is likely unnecessary anyway.
Respuestas (1)
Azzi Abdelmalek
el 23 de Sept. de 2012
Editada: Azzi Abdelmalek
el 23 de Sept. de 2012
correted code
y0 = zeros(4,1);
y0(1,1) = XR1(end);
y0(2,1) = HR1(end);
y0(3,1) = VR1(end);
y0(4,1) = gammaR1(end);
tf=500
test=1
while test>0
tf=tf+100
tspan = [0 tf];
[t,y] = ode45(@FS1,tspan,y0);
HF1 = y(:,2);
idx=find(HF1<0,1)
if ~isempty(idx)
test=0
end
end
XF1 = y(1:idx,1);
HF1 = y(1:idx,2);
VF1 = y(1:idx,3);
gammaF1 = y(1:idx,4);
figure(3)
plot(XF1,HF1)
2 comentarios
Azzi Abdelmalek
el 23 de Sept. de 2012
maby I did'nt understand your question try the corrected code above
Ver también
Categorías
Más información sobre Ordinary Differential Equations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!