Matlab 是否有一个不同的循环可以用来执行这个,我如何使它成为一个函数?

Matlab 是否有一个不同的循环可以用来执行这个,我如何使它成为一个函数?,matlab,function,optimization,motion,projectile,Matlab,Function,Optimization,Motion,Projectile,足球踢射运动 目标函数是(2*(v0)^2*sind(a)*cosd(a))/g) 使用黄金搜索法 我们必须创建一个程序,通过以下结构为任何用户定义的初始速度选择一个角度,使x距离最大化: 给定a=[theta] 最大化x(a) 以致 0 < theta < 90 x > 0 y > 0 在调用函数之前,应输入初始速度v0,答案应始终显示45作为角度,同时还显示以该角度计算的距离。阅读注释,链接也有助于实现黄金搜索 最大化给定目标的功能 function gold(v

足球踢射运动

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

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

给定
a=[theta]

最大化
x(a)

以致

0 < theta < 90
x > 0
y > 0

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

阅读注释,链接也有助于实现黄金搜索


最大化给定目标的功能

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

结果

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

@学生22如果答案有助于你,请接受它。如何获得答案: