0

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.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
student22
  • 3
  • 1

1 Answers1

0

Read through the comments, also this link is useful for implementing golden search.


Function to maximize the given objective

function gold(v0)
% given data
g = 9.81;

Xup = 85;
Xlo = 1;
et = 10^-2;
% since v0 is a user define, it is considered as constant in function func
% minimize the opposite of the objective is equivalent to maximization
func = @(a) -1*((2 * (v0)^2 * sind(a) * cosd(a)) / g);
% make a call to  goldSearch
[angle, negative_dist] = goldSearch(func, Xlo, Xup, et);

% since we minimize the opposite, to find the real value, multiple by -1
distance = -negative_dist;

% display result
fprintf('Angle : %f\nDistance: %f\n',angle,distance);



            % goldSearch minimize the function  
            function [ xMin, fMin] = goldSearch(funcname, a, b, tol) 

            % a is the lower bound, b is the upper bound
            % default tolerance 
            if nargin <4 
                tol=1.0e-6; 
            end

            % golden ratio value
            R=(sqrt(5)-1)/2; 
            C=1 - R; 

            % initial try and error
            x1=R*a+ C*b; 
            x2=C*a +R*b; 

            % get their function evaluation
            f1=feval(funcname,x1); 
            f2=feval(funcname,x2); 


            while (b-a)>tol
               % update upper bound 
               if f1>f2 
                   a = x1; x1 = x2; f1 = f2; 
                   x2 = C*a + R*b; 
                   f2= feval(funcname,x2); 
               else 
               % update lower bound
                   b = x2; x2 = x1; f2 = f1; 
                   x1= R*a + C*b; 
                   f1= feval(funcname,x1); 
               end
            end

                if f1< f2 
                   fMin=f1; xMin=x1; 
                else 
                   fMin=f2; xMin=x2; 
                end 



            end 

end


Testing the function

format short g
v0 = input('input intial velocity:');
gold(v0)


Results

input intial velocity:25
Angle : 45.000154
Distance: 63.710499
Adam
  • 2,726
  • 1
  • 9
  • 22
  • @student22 if the answer helps you kindly accept it. what to do for answers: https://stackoverflow.com/help/someone-answers – Adam Oct 31 '19 at 02:40