Soccer ball kicked projectile motion
objective function is (2 * (v0)^2 * sind(a) * cosd(a)) / g)
using golden search method we must create a program that maximizes the x-distance by choosing an angle for any user-defined initial velocity by the following structure:
Given a = [theta]
Maximize x(a)
Such that
0 < theta < 90
x > 0
y > 0
Create the function with at least following inputs: v0
, and x(a)
clc
clear
%gravity
g = 9.81;
%objective function
func = @(v0,a) ((2 * (v0)^2 * sind(a) * cosd(a)) / g);
%angle bounds (degrees) 0 < xa < 90
Xup = 85;
Xlo = 1;
%golden ratio = ((1 + sqrt(5))/2
G = 1.618;
%iteration
d = (G - 1)*(Xup - Xlo);
x1 = (Xup - d);
x2 = (Xlo + d);
%error tolerance
et = 10^-2;
%error limit
error = inf;
%v0 is a user input
v0 = input('input intial velocity:')
% at end points
f_up = func(v0,Xup); %-8.1667
f_lo = func(v0,Xlo); %2.0525
%function iterations
f1 = func(v0,x1);
f2 = func(v0,x2);
while error > et
if f1 > f2
Xup = x2;
f_upper = f2;
x2 = x1;
f2 = f1;
d = (G - 1)*(Xup - Xlo);
Xlo = Xup - d;
f1 = func(v0,x1);
elseif f1 < f2
Xlo = x1;
f_lower = f1;
x1 = x2;
f1 = f2;
d = (G - 1)*(Xup - Xlo);
x2 = Xlo + d;
else
Xlo = (x1 + x2)/2;
Xup = Xlo;
end
%calculating absolute error determining convergence
error = abs(Xup - Xlo);
end
a = (x1 + x2)/2
distance = func(v0,a)
Initial velocity v0
should be inputted before calling the function, and the answer should always display 45 as angle, while also displaying distance calculated at that angle.