有两件事值得注意:
fmincon
只接受只有1个参数的函数。所以我需要每次只定义一个只有一个参数的新函数。更多详细信息,请参阅http://www.mathworks.com/help/optim/ug/passing-extra-parameters.html function V_t = backward_recursion(W_0_L,W_0_U,Q_L,Q_U,T,grid_point_num,N,M,R_f,K,alpha,omega_0,mu_R,Sigma_R)
grid = create_grid(W_0_L,W_0_U,Q_L,Q_U,T,grid_point_num) ;
v_t_plus_one_list = [0,1,0] ;
for t = T-1:-1:0
grid_t = grid(t+1,:) ;
bellman_right_hand_side_with_max = zeros(1,grid_point_num) ;
for W_index = size(grid_t,1)
W_t = grid_t(W_index,1) ;
disp(['W_t=',num2str(W_t)])
bellman_right_hand_side_with_max_opt = @(omega) bellman_right_hand_side_without_max(omega, N, M, R_f, alpha, W_t, mu_R, Sigma_R, v_t_plus_one_list) ;
[x,fval] = fmincon(bellman_right_hand_side_with_max_opt,omega_0,ones(1,N),1,[],[],[0,0,0],[1,1,1]);
bellman_right_hand_side_with_max(W_index,1) = - fval ;
end
V_t = - log( - bellman_right_hand_side_with_max ) / alpha ;
v_t_plus_one_list = polyfit(grid_t, V_t, K) ;
end
end
我尝试了嵌套功能,但它没有工作
function [x,fval] = runnested(omega_0, N, M, R_f, alpha, W_t, mu_R, Sigma_R, v_t_plus_one_list)
[x,fval] = fmincon(nested_fun,omega_0,ones(1,N),1,[],[],[0,0,0],[1,1,1]) ;
function y = nested_fun(omega)
y = bellman_right_hand_side_without_max(omega, N, M, R_f, alpha, W_t, mu_R, Sigma_R, v_t_plus_one_list) ;
end
end
和
[~,bellman_right_hand_side_with_max_result] = runnested(omega_0, N, M, R_f, alpha, W_t, mu_R, Sigma_R, v_t_plus_one_list) ;
bellman_right_hand_side_with_max(W_index,1) = - bellman_right_hand_side_with_max_result ;
说
Not enough input arguments.
Error in runnested/nested_fun (line 4)
y = bellman_right_hand_side_without_max(omega, N, M, R_f, alpha, W_t, mu_R, Sigma_R, v_t_plus_one_list) ;
Error in runnested (line 2)
[x,fval] = fmincon(nested_fun,omega_0,ones(1,N),1,[],[],[0,0,0],[1,1,1]) ;