Howdy, stranger! Ready to join the community? [log in]

To reply on Icrontic, register now.

It only takes 30 seconds.

Have an account? Sign in for less ads.

Forgot?
juice34
Getting settled in
juice34
7 Posts

Please help projectile motion

I have a problem i need to print out a table of time x values and y values for the path of a projectile, but cannot get it anyway i try. Here is my code and i would appreciate if someone could help me.


%Constants and initial values
g = -9.8;
conv = (pi/180);


%Get users inputs in the following order:distance,velocity,angle
try
distance = input ('Please enter the distance to your target: (meters) \n');
m = Distance <= 0;
if m == 1
disp ('::FATAL ERROR: Distance cannot be < or = to zero, goodbye')
break
end
catch ME
end


try
velocity = input ('Please enter initial velocity of the projectile: (m/s) \n');
m = velocity <= 0;
if m == 1
disp ('::FATAL ERROR: Velocity cannot be < or = to zero, goodbye')
break
end
catch ME
end

try
theta = input ('Please enter an angle (degrees) between 0 and 90: \n');
m = theta < 0 | Theta > 90;
if m == 1
disp ('::FATAL ERROR: Angle cannot be < or = to zero, goodbye')
break
end
catch ME
end

%Calculate initial x and y velocitys, time, and range
voy = velocity*sin(theta*conv);
vox = velocity*cos(theta*conv);
time = -2*(voy / g);
range = (vox*time)

%Show how far bag traveled
fprintf ('The bean bag traveled a total of %6.2f meters \n',range)

%Create masks to determine how close projectile was to target
ma = (range-2.5 <= Distance && Distance <= range+2.5);
mb = (range > Distance-2.5);
mc = (range < Distance+2.5);

%Interpret masks
if ma == 1
fprintf ('Your bag hit the target right on!! GOOD AIM')
elseif mb == 1
fprintf ('so your bean bag traveled long %6.2f meters \n', abs(range - distance))
elseif mc == 1
fprintf ('so your bean bag traveled short %6.2f meters \n', abs(distance - range))
end

disp 'Please press enter to see (x,y) coordinates as a funtion of time:'
pause


%TABLE/GRAPH%

xpos = 0; %Set starting location at (x,y)=(0,0)
ypos = 0;

t=0; %Random incrementing variable(represents time).


for t=0:.1:100
t=t+1;

vy = voy + (g*t);
xpos = xpos + (vox*.1);
ypos = ypos + (voy*.1);
out = [t' xpos' ypos']

fprintf ( 'The x value is %6.2d and y value is %6.2d n\',out(t,)

if( ypos < 0 )
break;
end
end
shwaip
elaborate bot
shwaip
5,728 Posts
as a quick guess, get rid of the t=t+1; line.

also, please re-post your code inside of
PHP Code:
[code]code here[/code
tags.
juice34
Getting settled in
juice34
7 Posts
I just tried it and it still doesnt work. Im new to this forum and dont know where the php is and dont see it in the reply to new thread window.


here is my code again

Code:
%Constants and initial values
g = -9.8;
conv = (pi/180);


%Get users inputs in the following order:distance,velocity,angle
try
  distance = input ('Please enter the distance to your target: (meters) \n');
  m = Distance <= 0;
    if m == 1
     disp ('::FATAL ERROR: Distance cannot be < or = to zero, goodbye')
     return
    end
catch 
end


try
  velocity = input ('Please enter initial velocity of the projectile: (m/s) \n');
  m = velocity <= 0;
  if m == 1

    return
  end
catch 
end

try
  theta = input ('Please enter an angle (degrees) between 0 and 90: \n');
  m = theta < 0 | Theta > 90;
  if m == 1
    disp ('::FATAL ERROR: Angle cannot be < or = to zero, goodbye')
    return
  end
catch 
end

%Calculate initial x and y velocitys, time, and range
voy = velocity*sin(theta*conv);
vox = velocity*cos(theta*conv);
time = -2*(voy / g);
range = (vox*time)

%Show how far bag traveled
fprintf ('The bean bag traveled a total of %6.2f meters \n',range)

%Create masks to determine how close projectile was to target
ma = (range-2.5 <= distance && distance <= range+2.5);
mb = (range > distance-2.5);
mc = (range < distance+2.5);

%Interpret masks
if ma == 1
  fprintf ('Your bag hit the target right on!! GOOD AIM')
elseif mb == 1
  fprintf ('so your bean bag traveled long %6.2f meters \n', abs(range - distance))
elseif mc == 1
  fprintf ('so your bean bag traveled short %6.2f meters \n', abs(distance - range))
end

disp 'Please press enter to see (x,y) coordinates as a funtion of time:'
pause


%TABLE/GRAPH%

xpos = 0; %Set starting location at (x,y)=(0,0)
ypos = 0;

t=0; %Random incrementing variable(represents time).


for t=0:.1:100


  vy = voy + (g*t);
  xpos = xpos + (vox*.1);
  ypos = ypos + (voy*.1);
  out = [t' xpos' ypos']

  fprintf ( 'The x value is %6.2d and y value is %6.2d n\',out(t,:))

  if( ypos < 0 )
    break;
  end
end
shwaip
elaborate bot
shwaip
5,728 Posts
i'll take a look at it more thoroughly when i get to school and have access to matlab.
juice34
Getting settled in
juice34
7 Posts
Thank you i appreciate your help.
Thrax
Cad
Thrax
23,398 Posts
Shwaip, I added the requisite tags for your sanity.
__________________ Robert Hallock
Technical Analyst
Twitter | LinkedIn


CPU: Core 2 Duo E6420 @ 3.5GHz (500*7)
MoBo: DFI LanParty DK P35-T2R/S
RAM: 4GB G.SKILL PC8500
VID: NVIDIA GeForce GTX 275
HSF: Thermalright Ultra-120

juice34
Getting settled in
juice34
7 Posts
THRAX how do i do that, or where are they located.
shwaip
elaborate bot
shwaip
5,728 Posts
when you paste your code, just type the tags around the code like this:



and it will look like this:

Code:
for i=1:10
     i^2
end
Attached Thumbnails
Click image for larger version

Name:	code.png
Views:	177
Size:	39.3 KB
ID:	26140  
shwaip
elaborate bot
shwaip
5,728 Posts
there was a problem with the indexing you were doing here:

Code:
  fprintf ( 'The x value is %6.2d and y value is %6.2d n\',out(t,:))
here's the new code:

Code:
%Constants and initial values
g = -9.8;
conv = (pi/180);


%Get users inputs in the following order:distance,velocity,angle
try
  distance = input ('Please enter the distance to your target: (meters) \n');
  m = Distance <= 0;
    if m == 1
     disp ('::FATAL ERROR: Distance cannot be < or = to zero, goodbye')
     return
    end
catch 
end


try
  velocity = input ('Please enter initial velocity of the projectile: (m/s) \n');
  m = velocity <= 0;
  if m == 1

    return
  end
catch 
end

try
  theta = input ('Please enter an angle (degrees) between 0 and 90: \n');
  m = theta < 0 | Theta > 90;
  if m == 1
    disp ('::FATAL ERROR: Angle cannot be < or = to zero, goodbye')
    return
  end
catch 
end

%Calculate initial x and y velocitys, time, and range
voy = velocity*sin(theta*conv);
vox = velocity*cos(theta*conv);
time = -2*(voy / g);
range = (vox*time)

%Show how far bag traveled
fprintf ('The bean bag traveled a total of %6.2f meters \n',range)

%Create masks to determine how close projectile was to target
ma = (range-2.5 <= distance && distance <= range+2.5);
mb = (range > distance-2.5);
mc = (range < distance+2.5);

%Interpret masks
if ma == 1
  fprintf ('Your bag hit the target right on!! GOOD AIM\n')
elseif mb == 1
  fprintf ('so your bean bag traveled long %6.2f meters \n', abs(range - distance))
elseif mc == 1
  fprintf ('so your bean bag traveled short %6.2f meters \n', abs(distance - range))
end

disp 'Please press enter to see (x,y) coordinates as a funtion of time:'
pause


%TABLE/GRAPH%

xpos = 0; %Set starting location at (x,y)=(0,0)
ypos = 0;

t=0; %Random incrementing variable(represents time).

dt = 0.01;
for t=0:dt:100


  vy = voy + (g*t);
  xpos = xpos + (vox*dt);
  ypos = ypos + (vy*dt);
  
  
  if( ypos < 0 )
    break;
  end
  
  out = [t' xpos' ypos'];

  fprintf ( 'The x value is %6.2d and y value is %6.2d \n',xpos, ypos)

end
juice34
Getting settled in
juice34
7 Posts
Yea, i dont know what is wrong
shwaip
elaborate bot
shwaip
5,728 Posts
(the code i posted works)
the problem you were having is that 't' is not always a positive integer, and therefore cannot be used to index the matrix.
juice34
Getting settled in
juice34
7 Posts
Schwaip i appreciate all your help it makes sense now. YOUR AWSOME!!!!
juice34
Getting settled in
juice34
7 Posts
Sorry i spelled your forum name wrong but your still awsome!!!
Similar Threads
Thread Thread Starter Forum Replies Last Post
PS3 Controller Motion Sensitive Sledgehammer70 Technology Articles 4 9 May 2006 10:17pm
Capturing Motion JPEGS on ZR-70 or ZR-90 J-girl Web & Digital Media 6 30 Apr 2004 11:37pm

Go Back   Icrontic Forums > Tech: Software > General Software > Matlab Help
Jump to
This Thread Search this Thread
Search this Thread:

Advanced Search


Current time: 10:42pm (GMT)
Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Get Vanilla instead. Trust me.