help-While and for loops

6 views (last 30 days)
Maria Andersson
Maria Andersson on 31 Jan 2014
Edited: Mischa Kim on 31 Jan 2014
Hi! I have a question about using the for and while loops to simulate a toy rocket path. The rocket will follow three steps, where in the first it is shot up in the air and will accelerate with the force (F) 16N until t=0.15 Seconds, the second step is where the rocket motor will turn of and a parachute will be fold out and the rocket will continue upwards and afterward turn to fall downwards with a constant velocity of 20m/s (-20 m/s) , the third step is when the rocket no longer will accalerate and will start from the point where the speed reaches -20 m/s until the rocket falls to the ground.
here are the three functions to use for the assignment:
v(t)= v+a*(t-t0);
h(t)=h0+v0*(t-t0)+0.5*a*(t-t0)^2
a=((F -m*g)/m
where m=0.05 kg
g=9.81
as step 1 is the startup point:
v0=h0=t0=0
Step 2:
v0 = the final speed for step 1 (v0 is max v for step 1 as step 2 starts when step 1 ends)
h0= the final speed for step 1
t0= 0.15
as no force will be current in this step, F=0 --> a= -9.81
Step 3:
v0= the end speed for step 2 which in this case is -20 m/s
t0= the end time of step 2
h0= the final hight of step 2
as no acceleration is current in this step F=mg --> a=0;
So with this information, the required assignment is to plot the whole path of the rocket by using if-sets and (or) for and while loops.
I have tried to use the for and while loops but I always have problems as I am not sure how to save the speed and height of each step in a matrix, so I really need help and it would be great if anyone could solve this problem for me asap!!
this is how my coding looks like:
m=0.05;
g=9.81;
dt=0.01;
%FOR STEP ONE:
t0=0;
tend=0.15;
h0=0;
v0=0;
F=16;
t=t0:dt:tend;
y=1:length(t);
for i=y
a=acceleration(F,m,g);
a1=a;
v=speed(v0,a1,t,t0);
h=hight(h0,v0,t,t0,a1);
end
plot(t,v,t,h)
hold on
%STEP TWO
Velocity2=[];
hight2=[];
while v>=-20
v0=max(v1);
h0=max(h1);
F=0;
a=acceleration(F,m,g);
t0=0.15;
v=Velocity(v0,a,t,t0);
Velocity2=[Velocity2;v];
h=hight(h0,v0,t,t0,a2);
hight2=[hight2;h];
t=t0+dt;
plot(t,v,t,h)
end
%STEP 3:
while h>=0;
a=acceleration(F,m,g);
v=Velocity(v0,a,t,t0);
h=height(h0,v0,t,t0,a);
t=t0+dt;
end
plot(t,v,t,h);
hold off
Thanks in advance

Answers (1)

Mischa Kim
Mischa Kim on 31 Jan 2014
Edited: Mischa Kim on 31 Jan 2014
Simply save the speed and height data points into vectors:
...
for ii = 1:length(t)
...
v(ii) = speed(v0, a1, t(ii), t0);
h(ii) = hight(h0, v0, t(ii), t0, a1);
...
end
...
asumming that functions speed and hight are defined and working.

Categories

Find more on Audio Processing Algorithm Design 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!