【问题标题】:is there a different loop i can use to execute this, and how do i make it a function?我可以使用不同的循环来执行此操作,以及如何使其成为函数?
【发布时间】:2020-02-13 19:14:24
【问题描述】:

足球踢球运动

目标函数是(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为角度,同时显示以该角度计算的距离。

【问题讨论】:

    标签: matlab function optimization motion projectile


    【解决方案1】:

    通读 cmets,this 链接对于实现 黄金搜索 也很有用。


    最大化给定目标的函数

    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
    

    【讨论】:

    猜你喜欢
    • 2013-05-18
    • 2023-03-28
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 2019-02-14
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多