找到最大效用函数U(h1,h2)

时间:2017-05-01 17:39:42

标签: matlab loops max

我有一个matlab函数,它返回效用值U(h1, h2),该值取决于两个人h1h2的工作时数。每个人最多可以工作3500小时。

如何以最佳方式编写此代码?对于只有一个人,我只将函数从输入值0到3500循环到矩阵中,并在矩阵中找到最大值。但是现在我有两个值可以取值0到3500,并希望根据这些值找到函数的最大输出。

我会非常感谢任何帮助!

编辑: 对不起,迟到了!到目前为止,我还没有时间研究这个问题。无论如何,这是代码:

function Test

global LocalTaxRate
global StateTax1Rate
global StateTax2Rate

LocalTaxRate = 0.3212; %Average 2017
StateTax1Rate = 0.25;
StateTax2Rate = 0.05;

h = [0 0]; % start value
lb = [0 0]; % lower bound of h
ub = [3500 3500]; % upper bound of h
myFun = @(h) -CalcUFamily(h(1), h(2)); % function to minimize with one input
Uoptimal = fmincon(myFun, [1000 1000], [], [], [], [], lb, ub)

end

function UFamily = CalcUFamily(hh,hw) %h = male, w = wife

(bunch of code until this step, which is the relevant one)

 YearlyWorkIncomeHusband = WageHH * hh;
    C = YearlyWorkIncomeHusband + MonthlyTaxableTransfers * 12 - CalcTaxSwe(YearlyWorkIncomeHusband + MonthlyTaxableTransfers * 12, YearlyWorkIncomeHusband, Age) + MonthlyNonTaxableTransfers * 12; %Netincome husband
    YearlyWorkIncomeWife = WageHW * hw;
    C = C + YearlyWorkIncomeWife + MonthlyTaxableTransfers * 12 - CalcTaxSwe(YearlyWorkIncomeWife + MonthlyTaxableTransfers * 12, YearlyWorkIncomeWife, Age) + MonthlyNonTaxableTransfers * 12; %Netincome husband + wife

    UFamily = alpha1 * log10(C/100000) + alpha11 * (log10(C/100000)^2) ...
        + alpha2 * (log10(T/1000-hh/1000)) + alpha22 * (log10(T/1000-hh/1000))^2 + alpha12 * log10(C/100000) * (log10(T/1000-hh/1000)) * 2 ... %husband
        + alpha3 * (log10(T/1000-hw/1000)) + alpha33 * (log10(T/1000-hw/1000))^2 + alpha13 * log10(C/100000) * (log10(T/1000-hw/1000)) * 2 ... %wife
        + alpha23 * (log10(T/1000-hh/1000)) * (log10(T/1000-hw/1000)) ... %common leisure parameter
        - alpha4 * Psa - bfc * Dw; %Dummies
end

1 个答案:

答案 0 :(得分:1)

您可以使用两种方法来解决您的问题:

  1. 生成2D网格并找到全局最大值

    [h1, h2] = meshgrid(0:100:3500);
    Uoptimal = max(max(U(h1, h2))
    
  2. 使用多变量最小化求解器,例如fmincon

    h = [1000 1000]; % start value
    lb = [0 0]; % lower bound of h
    ub = [3500 3500]; % upper bound of h
    Uoptimal = fmincon(@(h1, h2) -U(h1, h2), h, [], [], [], [], lb, ub);
    

    请注意,我最小化-U,这与最大化U相同。

  3. 方法1将为您提供全局最大值,但是获得准确结果的速度相当慢。方法2非常准确,但可能会陷入局部最大值,具体取决于效用函数U的起始值和凸度。