繁体   English   中英

MATLAB优化功能

[英]MATLAB optimization of the function

我想优化此功能:

function Y_res = GetY( Y, P )
    cnt = length(P);
    denom = 0;
    for i = 1:cnt
        Y(i) = rand;
        denom = denom + P(i) / (1 - Y(i));
    end
    Y_res = 1 - 1 / denom;
    Y
    Y_res
end

该函数接收Y和P。P是一个常数数组。 我需要优化Y数组值。 我试图做到这一点:

Y = [0.3 0.2 0.1];     % need to be optimized
P = [0.65 0.2 0.15];   % constant array 

fhnd_GetY = @(x) GetY(Y, P);

options = optimset('TolX', 1e-3);
optimal_x = fminbnd(fhnd_GetY, 0.2, 0.4, options);

但是, optimal_x变量的结果不等于Y_res ,您可以在GetY函数的迭代过程中在屏幕上看到它。 为什么? 例如,我需要优化Y_res在0.2和0.4之间。 因此,结果是我需要接收Y_res介于0.2和0.4之间的Y_res和具有值的Y数组,通过它们我可以接收Y_res

假设您只打算优化函数GetY并考虑到要在其中创建Y的事实,因此不需要在外部输入Y ,请查看此修改后的函数是否已为您进行了充分优化-

function [Y_res,Y] = GetY( P )

found = false;
while ~found
    Y = rand(size(P));
    Y_res = 1-1./sum(P./(1-Y));

    if Y_res>0.2 & Y_res<0.4
        found = true;
    end
end

return;

请注意,如果不需要Y作为输出,则可以改用Yres = GetY([0.65 0.2 0.15]) ,但很可能您会需要(我的猜测)。

很少有样品运行-

>> [Yres,Y] = GetY([0.65 0.2 0.15])
Yres =
    0.3711
Y =
    0.4243    0.2703    0.1971
>> [Yres,Y] = GetY([0.65 0.2 0.15])
Yres =
    0.2723
Y =
    0.1897    0.4950    0.1476
>> [Yres,Y] = GetY([0.65 0.2 0.15])
Yres =
    0.3437
Y =
    0.3624    0.0495    0.4896

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM