How to create a graph

조회 수: 2 (최근 30일)
Abrahim
Abrahim 2022년 9월 23일
댓글: 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
Geoff Hayes 2022년 9월 23일
편집: 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.

Image Analyst
Image Analyst 2022년 9월 23일
편집: 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
Abrahim
Abrahim 2022년 9월 23일
Thank you very much that was very helpful.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 RF Toolbox에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by