首页 > 解决方案 > 我可以使用不同的循环来执行此操作,以及如何使其成为函数?

问题描述

足球踢射弹运动

目标函数是(2 * (v0)^2 * sind(a) * cosd(a)) / g)

使用黄金搜索方法,我们必须创建一个程序,通过以下结构为任何用户定义的初始速度选择一个角度来最大化 x 距离:

给定a = [theta]

最大化x(a)

这样

0 < theta < 90
x > 0
y > 0

使用至少以下输入创建函数:v0x(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)

v0调用函数前应输入初速度,答案应始终显示 45 作为角度,同时还显示以该角度计算的距离。

标签: matlabfunctionoptimizationmotionprojectile

解决方案


通读评论,这个链接对于实现黄金搜索也很有用。


最大化给定目标的函数

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


测试功能

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


结果

input intial velocity:25
Angle : 45.000154
Distance: 63.710499

推荐阅读