Plotting trajectory until it hits a terrain
7 views (last 30 days)
Show older comments
I need to plot the trajectory of a particle, using euler's method, until it either hits the ground or contacts a "terrain", given in a file named "mountains". I also need to animate the trajectory, although i haven't completely gotten to that since i get errors, mostly surrounding how i define when the particle contacts the terrain. It seems to be having an issue in my while loop of "calculate_trajectory_t" My trajectory function is:
function [t,x,y,vx,vy] = calculate_trajectory_t(y1, ang, v1, dt, ter)
% This function uses Euler's method to compute the trajectory of a ball.
%
%
%Initializing values.
load 'mountains' terrain
g=9.81;
v1x=v1*cosd(ang);
v1y=v1*sind(ang);
vx=v1x;
t=0;
x1=0;
x=[0];
t=[0];
y=[y1];
vy=[v1y];
vx=[v1x];
load 'mountains' terrain
ter=[terrain(1,:), terrain(2,:)]; %Set x&y values of ter to terrain rows 1&2 respectively
hold on;
fill(terrain(1,:),terrain(2,:), 'blue'); %Fill in the ploygon created by terrain data
axis([0 max([terrain(1,:), x]) 0 max([terrain(2,:), y])]); %Set
%Compute positions with respect to time.
n=1;
while (y>0)&&~inploygon(x,y,terrain(1,n),ter(2,n));
n=n+1;
t =[ t t(end)+dt];
vx =[ vx v1x];
vy =[ vy vy(end)-g*dt];
x=[ x x(end)+vx(end)*dt];
y=[ y y(end)+vy(end)*dt-(1/2)*g*t^2];
end
And the script which makes use of this function is:
%This program uses the calculate_trajectory_t function, which takes values
%of y1, ang, v1, dt and ter to calculate t, x, y, vx and vy.
clear;
clc;
%Take user input
v1=input('Please enter the initial speed of the ball (m/s): ');
y1=input('Please enter the initial height of the ball (m): ');
ang=input('Please enter the intial launch angle from the horizontal (degrees): ');
dt=input('Please enter the time step (seconds): ');
%Load mountains file to use data for the terrain
load 'mountains' terrain targx targy
ter=[terrain(1,:), terrain(2,:)]; %Set x&y values of ter to terrain rows 1&2 respectively
hold on;
fill(terrain(1,:),terrain(2,:), 'blue'); %Fill in the ploygon created by terrain data
[t,x,y,vx,vy] = calculate_trajectory_t(y1, ang, v1, dt, ter);
%Calculate and print final speed, time, and distances from the target:
finalspeed = sqrt(vx(end)^2 + vy(end)^2);
finaltime = t(end);
xdistance = x(end)-targx;
ydistance = y(end)-targy;
disttot=sqrt(xdistance^2+ydistance^2);
fprintf('Final speed is: %d \n' ,finalspeed);
fprintf('Final time is: %f \n' ,finaltime);
fprintf('Distance from target is: %d \n' ,disttot);
pause(0.15);
drawnow;
plot(x, y, '-', 'go'); %Plot trajectory
hold on;
-Thanks
0 Comments
Answers (1)
Walter Roberson
on 5 Mar 2011
Since we don't have your data files, you will have to tell us explicitly what errors you see.
Note that the syntax for load is not
load 'mountains' terrain
but rather simply
load mountains terrain
or better,
load('moutains', 'terrain')
Your error is probably in the line
while (y>0)&&~inploygon(x,y,terrain(1,n),ter(2,n))
After your first loop iteration, y is a vector of values rather than a single value, so y>0 is a vector of logical conditions. You cannot have a vector (or matrix) of logical conditions on either side of the && operator. Perhaps y(end)>0 is what you want?
Also, have another look at the way you initialize ter and see what size it is. Is it at least 2 by n where n is the number of time steps? If not, then indexing ter(2,n) is not right. Remember, n is the number of time steps, but you might not have reached as far as point number n in your terrain list.
I can see other problems but those together should give you enough clues to get you to think more clearly about what you are doing.
2 Comments
See Also
Categories
Find more on Data Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!