How to stop a graph going below zero in
19 views (last 30 days)
Show older comments
Mitul Dattani
on 10 Apr 2017
Answered: David Goodmanson
on 11 Apr 2017
Hi, ive been given a project to do and im new to matlab, I've written out some code for projectile motion but the issue I have is when I draw the graph the y axis will continually go below zero, how do i stop this?
%======================================================================%
%Arrow is fired from a cliff plot its projectile motion 10m above ground at
%20 m/s
%======================================================================%
clear all %clears workspace
angle = 0.4; %Size of angle it is fired at in radians from cliff
V0 = 20; %inital velocity = 20 m/s
g = -9.81; %Gravity force taking up as +ive
t = 0:0.1:4; %time
%t = zeros(30);
h = 10; %height
%Ax = 0; %Vertical Acceleration
Ay = g; %Horizontal Acceleration
%======================================================================%
%xVelocity = V0 * cos(angle);
yVelocity = V0 * sin(angle); %Vertical Velocity
x = t; %x axis = time
y = h + (yVelocity * t + (1/2) * Ay * t.^2); %s = ut + (0.5)at^2
%======================================================================%
plot(x, y);%+10 BECAUSE 10M ABOVE GROUND
%%%%ADD IN LOOP TO STOP WHEN Y AXIS GOES BELOW ZERO THEN INCREASE
%%%%INCREMENTS
%======================================================================%
caption = sprintf('Angle = %.2f radians', angle); %Title of graph defined
title(caption, 'FontSize', 15); %Title of graph inserted
grid on; %Grid on graph
%======================================================================%
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
%======================================================================%
Also how would I then go about making a GUI so it is then an app and I can input my own different starting heights and angles.
0 Comments
Accepted Answer
David Goodmanson
on 11 Apr 2017
Hello Matui, several ways to do this. If you want to keep the x and y arrays the same length as they are, you could use the first line below and a section of the plot would consist of y=0. If you want to reduce the sizes of x and y you could use the second line. If you want to keep the values of y around for awhile you could use the third line since Matlab does not plot nans.
y(y<0)=0
x(y<0)=[];y(y<0)=[];
x(y<0)=nan;
0 Comments
More Answers (0)
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!