查找多元函数的最小值

时间:2019-06-23 18:06:47

标签: matlab minimum differentiation

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

我的方法:找到第一个偏导数fx和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])

我对所问的问题做正确的事情吗?除了关于窗口和增量的问题外,还有那个问题。任何提示或解决方案将不胜感激。
预先感谢。

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:1)

我建议利用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));