Projectile motion Graph error

1 view (last 30 days)
Ziad Al Malouf
Ziad Al Malouf on 27 Apr 2020
Commented: Ameer Hamza on 27 Apr 2020
My Graph continues to the right... where i need it to stop at y=0 ( upon landing).. can someone help me resolve this issue.
clc
clear all
close all
format long
V = input('Enter velocity: ') % user enters initial Velocity in m/s
g= input('Enter acceleration due to gravity: ') % user enters gravity acceleration in m/s^2
theta= input('Enter angle: ') % user enters angle in degrees
y0=input('Enter Initial height: ') %user Enters the initial Height in m
Fs = input('Enter constant force spring force= '); %Constant force spring force, Fs (N)
t=0:0.1:5; %Time Vector; When this is changed, the value of x changes.
% please change only to shorten the simulation time.
figure
set(gcf,'position',[50,50,1200,500])
%close all
Vx = V.*cosd(theta) % Component of X value
Vy = V.*sind(theta)
% loop that goes through to get the x and y values and plots them using the
% built in plot function and stops while time(t) is reached
for i= 1:length(t)
%Vy = V.*cosd(theta)-g*t(i)
x=Vx*t(i); %Formula to find X
y=y0+V.*sind(theta)*t(i)-g*t(i)^2; % Formula to find Y
% Find all the indices of y that are less than zero
zeroIdx = find(y < 0);
% If there are indices with altitude less than zero, find the first one and set the X axis limits
if ~isempty(zeroIdx)
finalPosition = y(zeroIdx(1));
if(finalPosition>0)
xlim([0, finalPosition])
else
xlim([finalPosition,0])
end
end
% Set all altitudes less than zero to zero (can't penetrate the ground!)
y(zeroIdx) = 0;
plot(x,y,'o','MarkerFacecolor','b')
hold all
axis equal
xlabel('x in meters')
ylabel('y in meters')
xlim([0 180]) % the second value in the brackets sets the limit for the X axis
ylim([0 100]) % the second value in the brackets sets the limit for the Y axis
grid on
drawnow
end
k=Fs./x;
fprintf("%i",k)
zoom on;
maxh= max(y) % find the max height by finding Y max using the max() function
maxpoint= find(y== max(y(:))) % find the index of max point
xhigh = x(maxpoint) % this is the x value of max height
h = plot(x(maxpoint),y(maxpoint), 'r.'); % plots the max height on the graph and outputs the result
set(h, 'MarkerSize', 20); % plots h and displays dotted positions of the ball

Accepted Answer

Ameer Hamza
Ameer Hamza on 27 Apr 2020
Add this if-block at the end of for loop
grid on
drawnow
% ^ after these lines add
if y < 1
break
end
  6 Comments
Ziad Al Malouf
Ziad Al Malouf on 27 Apr 2020
Thank you sooo much... it worked, mind explaining the updated if statement?..
Ameer Hamza
Ameer Hamza on 27 Apr 2020
I am glad to be of help. I added an if-condition, which breaks the for the loop when the projectile is close to ground (y < 1). In the second code, I added a new condition, then If the projectile start close to the ground (y<1), then It should not stop immediately; it should stop when the projectile is returning to ground.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!