Hey guys, i have a school project that I have been absolutely stunted by for about 8 hours so far and i just cant seem to understand what is wrong, i was wondering if you guys could help
the object of the code is to calculate a projectile motion plot given an array of angles to iterate through, however it is not doing that, and keeps getting stuck in an infinite while loop, the variable height never reaches below 0 for some reason. Any help would be appreciated, thank you.
here is my code:
% Create a program to plot the motion of the ping pong ball with drag.
% The program will take inputs for velocity in m/s and angle of launch.
% note that with a high speed camera I estmated a launch speed for a ping
% pong ball could be 30 m/s.
clear all
velStart=input('input the initial velocity in m/sec: ');
angle=20:5:80;
% convert the degrees into radians so MATLAB likes it
angle=angle*pi./180;
rangeAngle=length(angle);
hold on
%Lines 1 - 14 are fine
for i=1:rangeAngle
% set initial position and time
x=[];
y=[];
time=[];
vel=velStart;
VelocityX=[];
VelocityY=[];
x(1)=0; % meters
y(1)=.01; % meters
time(1)=0; % seconds
mass=.00247; %kg ping pong ball .00247 Kg
g=-9.8; % m/sec^2
c=-0.0005; % coefficient of drag where Re is between 10^3 and 10^5
% so I can load an array for plotting
% start to increment the motion
index=1;
thisAngle=angle(i);
velx=vel*cos(thisAngle);
VelocityX(1)=velx;
vely=vel*sin(thisAngle);
VelocityY(1)=vely;
% set a time step
deltaTime=.001; % seconds
height=y(1);
flag=0;
%Don't see any issues here
while height>=0 % check that the ball has not hit ground yet
index = index+1;
% *******************************************************
% break velocity into its components
velx = vel*cos(thisAngle);
vely = vel*sin(thisAngle);
% *******************************************************
% *******************************************************
% use an if/else statement to check to see if the ball is moving
% down (negative). If it is, then drag has an opposite sign
% as gravity in the acceleration formula. Otherwise gravity
% and drag have the same sign. Calculate the new acceleration in the y.
if vely < 0
adragy = g + (c*velx.^2)/mass;
else
adragy = g - (c*vely.^2)/mass;
end
% *******************************************************
% *******************************************************
% Now calculate the acceleration in the x .
adragx = (-c*velx.^2)/mass;
% *******************************************************
% *****************************************************
% calculate the new velocity at the end of the time step
% this will have X and Y components, so you need a variable
% for each. One is velFinalX and the other is velFinalY.
velFinalX = velx + adragx.*deltaTime;
velFinalY = vely + adragy.*deltaTime;
% *******************************************************
% ******************************************************
% Get a new velocity vector and angle given the X and Y
% The velocity is the variable "vel" and angle is "angle"
vel = sqrt(velFinalX.^2 + velFinalY.^2);
angle = atan(velFinalY/velFinalX);
%*******************************************************
% now save my values at this time step
VelocityX(index)=velFinalX;
VelocityY(index)=velFinalY;
% and distance numbers
distX=VelocityX(index).*deltaTime;
distY=VelocityY(index).*deltaTime;
% save distance values
x(index)=x(index-1)+distX;
y(index)=y(index)+distY;
height=y(index);
time(index)=time(index-1)+deltaTime;
%lines 105-107 are good
if distY<0&&flag<=1
if height<.25
disp(distY)
fprintf('goal height detected at distance %.2f meters.',x(index));
flag =2;
LandingDistance(i)=x(index);
end
end
end
plot(x,y)
title('distance traveled by ping pong ball in meters')
xlabel('horizontal distance traveled (meters)')
ylabel('vertical distance traveled (meters)')
end
hold off

 채택된 답변

Mario Malic
Mario Malic 2020년 3월 6일
편집: Mario Malic 2020년 3월 6일

0 개 추천

y(index)=y(index-1)+distY;
You may want to check your code if this is fine to do. Also, by reading the error shown, you could've figure it out.
After doing so, it reports an error in here, but it still works.
thisAngle=angle(i); % you can change i to 1, but I don't understand the code completely, so up to you

댓글 수: 9

jason harrison
jason harrison 2020년 3월 6일
편집: jason harrison 2020년 3월 6일
So, the variable "thisAngle" is the angle that is going to be used on this loop of the code, so I want it to change everytime it loops through.
jason harrison
jason harrison 2020년 3월 6일
tii dont think i understand, do you mean to rename that "thisAngle"just to a different name but keep its function the same?
"angle" variable, change the variable name everywhere but not at this line.
angle = atan(velFinalY/velFinalX)
I'll post code in half an hour (not on pc) if you don't make it.
jason harrison
jason harrison 2020년 3월 6일
i tried changing the variable names but i dont think it fixed it for me. Also, another wierd thing is if input 7 as velocityStart, it will run and give me 1 curve, but if i input 8, it goes into an infinite loop.
Mario Malic
Mario Malic 2020년 3월 6일
Check how you calculate height, made a comment on while loop.
jason harrison
jason harrison 2020년 3월 6일
i dont see where my height calculation goes wrong. when i went step by step in the code, at line 84 "vel" starts becoming an infinite number. Im sorry if for not understandind this, im really trying, it just does not make any sense to me at all.
Mario Malic
Mario Malic 2020년 3월 6일
편집: Mario Malic 2020년 3월 6일
I cannot remember everything that I've done, but here's what I do:
  • Negative drag coefficient!
  • Pluses/Minuses on formulas, components of x plugged into y
  • Overwriting variables in while loop
Check with the results if this is what you should get.
jason harrison
jason harrison 2020년 3월 6일
oh my god thank you so much you are an absolute life saver, i am giving you the biggest internet hug right now, thank you so much.
Mario Malic
Mario Malic 2020년 3월 6일
Have a great weekend.
P. S. Would be great if you could mark my answer as solution.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

릴리스

R2019a

태그

질문:

2020년 3월 6일

댓글:

2020년 3월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by