# How to create a graph

3 次查看（过去 30 天）
Abrahim 2022-9-23

I am new to Matlab and I am trying to create a graph. However, every time I generate a graph, it does not show the points. For my program, I am creating a projctial motion calculator that asks the user for intial velocity and angle. Then it uses these variables to calculate the distance it traveled in the x and y direction. Here is my program:
%This program calculates the projectial motion of a ping pong ball
%Ask user to input inital velocity and angle
vel=input("Input initial velocity in m/sec: ");
angle=input("Input the angle in launch in degrees: ");
%Converts degrees to radian
angle=angle*pi/180;
%Assigning variables to neccessery values
x(1)=0;
y(1)=0.1;
time(1)=0;
mass=0.00247;
g=-9.8;
%Calculates the x velocity with the y velocity
velx=vel*cos(angle);
Velx(1)=velx;
velFinalX=velx;
vely=vel*sin(angle);
Vely(1)=vely;
deltaTime=0.001;
height=y(1);
%Calculates for the distance the ball traveled in the x and y direction
while height<=0
index=index+1;
accelY=g;
velFinalY=Vely(index-1)+(accelY*deltaTime);
Vely(index)=velFinalY;
distx=velx*deltaTime;
disty=Vely(index)*deltaTime;
x(index)=x(index-1)+distx;
y(index)=y(index-1)+disty;
height=y(index);
end
%Display graph
plot(x,y)
title("distance traveled by ping pong ball in meters")
xlabel("horizontal distance traveled (meters)")
ylabel("vertical distance traveled (meters)")

### 回答（2 个）

Geoff Hayes 2022-9-23

@Abrahim - try changing the condition on your while loop so that you execute the code so long as the height is greater than zero
while height>0
With the condition the other way, you never enter this loop.
You will also need to initialize the index variable before you try to use it.
##### 0 个评论显示 -2更早的评论隐藏 -2更早的评论

Image Analyst 2022-9-23

This "works"
% This program calculates the projectile motion of a ping pong ball
% Ask user to input inital velocity and angle
vel=input("Input initial velocity in m/sec: ");
angle=input("Input the angle in launch in degrees: ");
%Converts degrees to radian
angle=angle*pi/180;
% Assigning variables to neccessary values
x(1)=0;
y(1)=0.1;
time(1)=0;
mass=0.00247;
g=-9.8;
% Calculates the x velocity with the y velocity
velx=vel*cos(angle);
Velx(1)=velx;
velFinalX=velx;
vely=vel*sin(angle);
Vely(1)=vely;
deltaTime=0.001;
height=y(1);
% Calculates for the distance the ball traveled in the x and y direction
index = 1;
x(1) = 0;
y(1) = 0;
while height >= 0
index=index+1;
accelY=g;
velFinalY=Vely(index-1)+(accelY*deltaTime);
Vely(index)=velFinalY;
distx=velx*deltaTime;
disty =Vely(index)*deltaTime;
x(index)=x(index-1)+distx;
y(index)=y(index-1)+disty;
height=y(index);
end
% Display graph
plot(x,y, 'b-')
grid on;
title("Distance traveled by ping pong ball in meters", 'FontSize',fontSize)
xlabel("Horizontal distance traveled (meters)", 'FontSize',fontSize)
ylabel("vertical distance traveled (meters)", 'FontSize',fontSize)
However I think your logic is wrong. The vertical distance traveled will never decrease. The height will decrease but the total distance traveled will never decrease even when the ball is falling. You probably need to take the absolute value of the velocity.
By the way, see my attached demo on projectile that commputes just about everything you could possibly want to compute.
##### 1 个评论显示 -1更早的评论隐藏 -1更早的评论
Abrahim 2022-9-23
Thank you very much that was very helpful.

### 类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by