How to extract a specific value from a loop.

Hello
I wrote this code to model a simple projectile motion problem. What I would like to do now is output the value for x after 2 seconds but I don't know how to pull just one value out of a for loop.
%Projectile motion
v0= 10;
a= 45;
h= .04;
a=a*pi/180;
g=9.8; %acceleration due to gravity in m/s^2
xmax=v0^2*sin(2*a)/g;
ymax=v0^2*sin(a)^2/(2*g);
td=2*v0*sin(a)/g; %total time
x1=0;
y1=0;
figure('color','white');
for t=0:h:td+h
plot(x1,y1,'bo')
hold all
xlim([0 1.1*xmax]);
ylim([0 1.1*ymax]);
title('Projectile Motion');
xlabel('Distance in meter');
ylabel('Height in meter');
getframe;
x1=x1+h*v0*cos(a); %Euler's method for x
y1=y1+h*(v0*sin(a)-g*t);%Euler's method for y
% x1(2) % this is where I'm having the problem
end

 Respuesta aceptada

M
M el 31 de Ag. de 2018
t is your time variable, so you have to use it to test if it's equal to 2seconds.
if t==2 % assumes your time units is seconds
x_2sec=x1;
end
but in your code, t will never reach 2 seconds.

4 comentarios

%Projectile motion
v0= 10;
a= 45;
h= .04;
a=a*pi/180;
g=9.8; %acceleration due to gravity in m/s^2
xmax=v0^2*sin(2*a)/g;
ymax=v0^2*sin(a)^2/(2*g);
td=2*v0*sin(a)/g; %total time
x1=0;
y1=0;
figure('color','white');
for t=0:h:td+h
plot(x1,y1,'bo')
hold all
xlim([0 1.1*xmax]);
ylim([0 1.1*ymax]);
title('Projectile Motion');
xlabel('Distance in meter');
ylabel('Height in meter');
getframe;
x1=x1+h*v0*cos(a); %Euler's method for x
y1=y1+h*(v0*sin(a)-g*t);%Euler's method for y
% x1(2) % this is where I'm having the problem
if t==1.3 % assumes your time units is seconds
x_2sec=x1
end
end
When I do that nothing outputs. Matlab doesn't seem to recognize that if statement. Am I using it wrong?
Hi, if statement is never true in your code. You can check that by adding the else statement just after that. Shown in below code:
%Projectile motion
v0= 10;
a= 45;
h= .04;
a=a*pi/180;
g=9.8; %acceleration due to gravity in m/s^2
xmax=v0^2*sin(2*a)/g;
ymax=v0^2*sin(a)^2/(2*g);
td=2*v0*sin(a)/g; %total time
x1=0;
y1=0;
figure('color','white');
for t=0:h:td+h
plot(x1,y1,'bo')
hold all
xlim([0 1.1*xmax]);
ylim([0 1.1*ymax]);
title('Projectile Motion');
xlabel('Distance in meter');
ylabel('Height in meter');
getframe;
x1=x1+h*v0*cos(a); %Euler's method for x
y1=y1+h*(v0*sin(a)-g*t);%Euler's method for y
% x1(2) % this is where I'm having the problem
if t==1.3 % assumes your time units is seconds
x_2sec=x1
else % adding else statement tell us that, it is not if is never true
x_2sec=0
end
end
M
M el 3 de Sept. de 2018
When I do that nothing outputs. Matlab doesn't seem to recognize that if statement. Am I using it wrong?
Your time step is equal to 0.4, so your time variable will be succesively equal to t=1.28 and 1.32, and your condition will never be satisfied.
Dennis
Dennis el 3 de Sept. de 2018
To check if t==1.32 would not work either. Due to floating point limitations t will not be exactly 1.32.

Iniciar sesión para comentar.

Más respuestas (1)

GK
GK el 31 de Ag. de 2018

0 votos

Yes you can. 1. If you remove semicolon at the end of expression, MATLAB simply shows it at the command line 2. Alternately, you can also print it in excel file using- xlswrite('x1values.xlsx', x1);

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Preguntada:

el 31 de Ag. de 2018

Comentada:

el 3 de Sept. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by