Need help with my code

2 views (last 30 days)
Peter Denardo
Peter Denardo on 16 Dec 2021
Answered: Sargondjani on 16 Dec 2021
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
Peter Denardo
Peter Denardo on 16 Dec 2021
v0 is initial velocity. It is the main unknown in this whole thing. I have to create an iterative loop to determine at what initial velocity the return height will equal 8 meters plus or minus .001 m.

Sign in to comment.

Accepted Answer

Sargondjani
Sargondjani on 16 Dec 2021
You need to use a equation solver. I assume it is a non-linear problem, so then use fminsearch or fsolve (or lsqnonlin if you want to use a least squares approach).
Basically what you need to do is:
  • code a function that takes has input v0, and gives the distance d from the target (assuming the distance is one dimensional):
function dd = my_distance(par,v0)
% compute the distance using initial speed v0, and all other parameters
dd = ...
end
  • Make function handle to use it as the objective:
fun_obj = @(v0)my_distance(par,v0);
  • Use the solver, with initial guess v0_ini:
v0_solution = fsolve(fun_obj,v0_ini);

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!