My Graph continues to the right… where i need it to stop at y=0 ( upon landing).. can someone help me resolve this issue.
clcclear allclose allformat longV = 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.
figureset(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])elsexlim([finalPosition,0])endend% Set all altitudes less than zero to zero (can't penetrate the ground!)
y(zeroIdx) = 0; plot(x,y,'o','MarkerFacecolor','b')hold allaxis equalxlabel('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 ondrawnowendk=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
Best Answer