How to stop a graph going below zero in
    조회 수: 9 (최근 30일)
  
       이전 댓글 표시
    
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
채택된 답변
  David Goodmanson
      
      
 2017년 4월 11일
        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
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

