// ---------------------------------------------------- // MACROS // ---------------------------------------------------- function [lambda,beta,u_opt]=CutPerAlea(C,A,b,E,alea,Q,R,V,z) // The function computes the parameters beta and lambda depending of // the realisation of the alea at the stock point z // C cost vector // A constraints matrix for u // b constraints vector for u // E constraints matrix for z // V is an approximate of the Bellman function, // stores one hyperplane per line // and is written [[l1;...;ln],beta] where li are line vectors // and beta is a column vector of size nx1 // the argument of the Bellman function is Q.z+R.u+alea // z is a data (stock) vector cout=[C,1,zeros(1,length(z))]; //new cost function Lambdas=V(:,1:$-1); // lambdas matrix B=[b;-V(:,$)-Lambdas*alea]; //new inequality constraints vector A_ext=[A,zeros(size(A)( 1,1),1),E]; A_ext=[A_ext;Lambdas*R,-ones(size(Lambdas)(1,1),1),Lambdas*Q]; //new inequality constraints matrix Aeq=[zeros(length(z),size(A)(1,2)),zeros(length(z),1),eye(length(z))]; //equality constraints matrix LB=-%inf*ones(length(cout),1) //disables the default non negativity constraint in linprog //An external linear solver is called here [u_opt,c_opt,flag,d]=linprog(cout,A_ext,B,Aeq,z,lb=LB); //coordinates of the new plane lambda=d.lambda($-length(z)+1:$,1); beta=c_opt-lambda'*z; endfunction