필터 지우기
필터 지우기

How to stop a graph going below zero in

조회 수: 13 (최근 30일)
Mitul Dattani
Mitul Dattani 2017년 4월 10일
답변: David Goodmanson 2017년 4월 11일
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.

채택된 답변

David Goodmanson
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개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by