Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 求多变量函数的最小值_Matlab_Minimum_Differentiation - Fatal编程技术网

Matlab 求多变量函数的最小值

Matlab 求多变量函数的最小值,matlab,minimum,differentiation,Matlab,Minimum,Differentiation,问题:在窗口[0,2]×[2,4]中找到f(x,y)=x^2+y^2-2*x-6*y+14的最小值,x和y的增量为0.01 我的方法:找到外汇和fy的一阶偏导数。临界点同时满足方程fx(x,y)=0和fy(x,y)=0。求二阶偏导数fxx(x,y)、fyy(x,y)和fxy(x,y)以求D clc clear all syms x y fun=x^2+y^2-2*x-6*y+14; fx=diff(fun,x); fy=diff(fun,y); pt=solve(fx==0,fy==0); so

问题:在窗口[0,2]×[2,4]中找到f(x,y)=x^2+y^2-2*x-6*y+14的最小值,x和y的增量为0.01

我的方法:找到外汇和fy的一阶偏导数。临界点同时满足方程fx(x,y)=0和fy(x,y)=0。求二阶偏导数fxx(x,y)、fyy(x,y)和fxy(x,y)以求D

clc
clear all
syms x y
fun=x^2+y^2-2*x-6*y+14;
fx=diff(fun,x);
fy=diff(fun,y);
pt=solve(fx==0,fy==0);
sol = struct2array(pt)
fxx=diff(fx,x);
fyy=diff(fy,y);
fxy=diff(fx,y);
D=subs(fxx,[x y],[1 3])*subs(fyy,[x y],[1 3])-(subs(fxy,[x y],[1 3]))^2
fxx_val=subs(fxx,[x y],[1 3])
minimum_value=subs(fun,[x y],[1 3])
我对问题的回答是否正确?除此之外,窗口和增量提到了这个问题。任何提示或解决方案都将不胜感激。

提前感谢。

使用功能评估优化方法,而不是梯度


f = @(x,y)x.^2+y.^2-2.*x-6.*y+14;

% x range
x_lb = 0;
x_ub = 2;

% y range
y_lb = 2;
y_ub = 4;

step = 0.01;

% lower bound of x, initial guess as xmin
xmin = x_lb;

% lower bound of y, initial guess as ymin
ymin = y_lb;

% f at the lower bounds, initial  fmin 
fmin = f(xmin, ymin);

for x = x_lb:step:x_ub

    for y = y_lb:step:y_ub
        % function evaluation
        fval = f(x, y);

        %replace fmin if the newly evaluated f is less than the actual fmin
        if fval < fmin
            fmin = fval;

            % save current x and y where f is minimum 
            xmin = x;
            ymin = y;

        end
    end

end
xmin = 1;
ymin = 3;
fmin = 4;

请通读代码


f = @(x,y)x.^2+y.^2-2.*x-6.*y+14;

% x range
x_lb = 0;
x_ub = 2;

% y range
y_lb = 2;
y_ub = 4;

step = 0.01;

% lower bound of x, initial guess as xmin
xmin = x_lb;

% lower bound of y, initial guess as ymin
ymin = y_lb;

% f at the lower bounds, initial  fmin 
fmin = f(xmin, ymin);

for x = x_lb:step:x_ub

    for y = y_lb:step:y_ub
        % function evaluation
        fval = f(x, y);

        %replace fmin if the newly evaluated f is less than the actual fmin
        if fval < fmin
            fmin = fval;

            % save current x and y where f is minimum 
            xmin = x;
            ymin = y;

        end
    end

end
xmin = 1;
ymin = 3;
fmin = 4;


使用函数评估优化方法代替梯度


f = @(x,y)x.^2+y.^2-2.*x-6.*y+14;

% x range
x_lb = 0;
x_ub = 2;

% y range
y_lb = 2;
y_ub = 4;

step = 0.01;

% lower bound of x, initial guess as xmin
xmin = x_lb;

% lower bound of y, initial guess as ymin
ymin = y_lb;

% f at the lower bounds, initial  fmin 
fmin = f(xmin, ymin);

for x = x_lb:step:x_ub

    for y = y_lb:step:y_ub
        % function evaluation
        fval = f(x, y);

        %replace fmin if the newly evaluated f is less than the actual fmin
        if fval < fmin
            fmin = fval;

            % save current x and y where f is minimum 
            xmin = x;
            ymin = y;

        end
    end

end
xmin = 1;
ymin = 3;
fmin = 4;

请通读代码


f = @(x,y)x.^2+y.^2-2.*x-6.*y+14;

% x range
x_lb = 0;
x_ub = 2;

% y range
y_lb = 2;
y_ub = 4;

step = 0.01;

% lower bound of x, initial guess as xmin
xmin = x_lb;

% lower bound of y, initial guess as ymin
ymin = y_lb;

% f at the lower bounds, initial  fmin 
fmin = f(xmin, ymin);

for x = x_lb:step:x_ub

    for y = y_lb:step:y_ub
        % function evaluation
        fval = f(x, y);

        %replace fmin if the newly evaluated f is less than the actual fmin
        if fval < fmin
            fmin = fval;

            % save current x and y where f is minimum 
            xmin = x;
            ymin = y;

        end
    end

end
xmin = 1;
ymin = 3;
fmin = 4;


我建议利用Matlab的能力来计算矩阵。然后,不需要循环

% your function, look up anonymous functions 
func = @(x,y) x.^2 + y.^2 - 2.*x - 6.*y + 14;

% get matrices for you x- and y-window
[xg, yg] = meshgrid(0:.01:2, 2:0.01:4);

% compute all in one call
result = func(xg,yg);

% find total minimum
minimum = min(result(:));

% find the index of the (first) minimum, for other equations, there might
% be more than one
ind = find(result==minimum, 1);

% Output the result
fprintf('The minimum (%d) is located at x: %d, y: %d.\n', minimum,  xg(ind), yg(ind));

我建议利用Matlab的能力来计算矩阵。然后,不需要循环

% your function, look up anonymous functions 
func = @(x,y) x.^2 + y.^2 - 2.*x - 6.*y + 14;

% get matrices for you x- and y-window
[xg, yg] = meshgrid(0:.01:2, 2:0.01:4);

% compute all in one call
result = func(xg,yg);

% find total minimum
minimum = min(result(:));

% find the index of the (first) minimum, for other equations, there might
% be more than one
ind = find(result==minimum, 1);

% Output the result
fprintf('The minimum (%d) is located at x: %d, y: %d.\n', minimum,  xg(ind), yg(ind));

你可以使用Matlab内置的优化工具吗?@Adam是的,先生。允许使用内置优化工具您允许使用Matlab内置优化工具吗?@Adam Yes先生。允许使用内置的优化工具,谢谢。现在我明白了窗口和增量的含义。1天后我会接受你的回答:)再次感谢汉克斯先生。现在我明白了窗口和增量的含义。1天后我会接受你的回答:)再次感谢