Projectile motion Graph error

조회 수: 1 (최근 30일)
Ziad Al Malouf
Ziad Al Malouf 2020년 4월 27일
댓글: Ameer Hamza 2020년 4월 27일
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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 27일
Add this if-block at the end of for loop
grid on
drawnow
% ^ after these lines add
if y < 1
break
end
  댓글 수: 6
Ziad Al Malouf
Ziad Al Malouf 2020년 4월 27일
Thank you sooo much... it worked, mind explaining the updated if statement?..
Ameer Hamza
Ameer Hamza 2020년 4월 27일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Assembly에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by