Need help with my code
Show older comments
I am trying do an iterative loop that will input an increasing velocity into 3 different trajectories until the final trajectory gets into tolerance around the initial height. To put it simply, a ball is thrown at a wall, bounces off the wall and hits the ground and then bounces towards the initial throwing point. The loop needs to converge on the initial height plus/minus the tolerance by inputting an increasing velocity until the final height is reached, and then output the number of iterations and the plot of the trajectories. Here is the code I have so far. I am still a newby with loops so not sure how to proceed. The help is appreciated.
% All distances and heights measured in meters, time in seconds, velocity in m/s, angles in degrees and
%%acceleration in m/s^2
H0 = 8;
return_height_tolerance = .001;
theta_0=26;
g=9.81;
dAC=16;
eR=1;
%initial velocities in both directions
v0x=v0*cosd(theta_0) %Trig
v0y=v0*sind(theta_0) %Trig
%Point B = Maximmum height along trajectory path 1
H1=H0+v0y^2/(2*g)
%trajectory 1
H2=H0+dAC*tand(theta_0)-g/(2*v0x^2)*dAC^2 %Trajectory Eq.
%velocities in x and y direction at max height
v1x=v0x %Zero Acceleration
v1y=(sqrt(v0y^2-2*g*(H1-H0)))
%velocity just prior to hitting wall
v2x=v1x
v2y=sqrt(v1y^2+2*g*((H0+H1)-(H0+H2)))
v2=sqrt(v2x^2+v2y^2)
%velocity just after hitting the wall
v3x=v2x*eR
v3y=v2y
v3=sqrt(v3x^2+v3y^2)
theta_3_ref=atand(v3y/v3x)
% Trig: Reference Triangle
theta_3_abs=theta_3_ref+180
% Absolute Angle (Quadrant "3")
% velocity just prior to hitting ground
v4x=v3x
v4y=(sqrt(v3y^2-2*g*(H3-H2)))
v4=sqrt(v4x^2+v4y^2)
theta_4_ref=atand(v4x/v4y)
theta_4_abs=theta_4_ref+180
%velocity just after hitting the ground
v5x=v4x
v5y=v4y*eR
v5=sqrt(v5x^2+v5y^2)
theta_5_ref=atand(v5x/v5y)
theta_5_abs=theta_4_ref+90
%Distance between wall and ground
A=(-g/(2*v3x^2));
B=tand(theta_3_abs);
C=(1/(2*g)*(v4^2-v3^2));
%Quadratic formula for distance between wall and ground
%%based on energy Conservation 2-3 022& Trajectory Eq. 2-3 equations
dCD=(-B+sqrt(B^2-4*A*C))/(2*A)
%trajectory 2
H3=H2+dCD*tand(theta_3_abs)-(g/(2*v3^2*(cosd(theta_3_abs))^2))*dCD^2
%distance between ground and return height
dDA=-dAC-dCD
%trajectory 3
H4=H3+dDA*tand(theta_5_abs)-(g/(2*v4^2*(cosd(theta_5_abs)^2)))*dDA^2
v0(n)=0;
while (H0-return_height_tolerance<H4)<H0+return_height_tolerance
n=0;
n=n+.001;
H2=H0+dAC*tand(theta_0)-g/(2*v0x^2)*dAC^2;
H3=H2+dCD*tand(theta_3_abs)-(g/(2*v3^2*(cosd(theta_3_abs))^2))*dCD^2;
H4=H3+dDA*tand(theta_5_abs)-(g/(2*v4^2*(cosd(theta_5_abs)^2)))*dDA^2;
end
4 Comments
Michael Van de Graaff
on 16 Dec 2021
Edited: Michael Van de Graaff
on 16 Dec 2021
use the formatting toolbar to put your code as code.
Also, what is the problem that you're encountering? is there a particular error? Right now you've just given us the entire problem, and we aren't just going to do the whole problem for you
Peter Denardo
on 16 Dec 2021
Michael Van de Graaff
on 16 Dec 2021
Also, just playing with this, you are using variable before you've defined them all over the place. What is v0?
Peter Denardo
on 16 Dec 2021
Edited: Peter Denardo
on 16 Dec 2021
Accepted Answer
More Answers (0)
Categories
Find more on General Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!