I plan to solve a function matrix x(j), which want each x(j) could lead W(j) to 0. I have written a function about it, but I do not know what code I need to write to solve this function. I will be really appreciate if anyone could take some time to give me some help. Thank you very much
%% function for omega of every time period for OI scheme
function f=function_1_5_1(x) clear
r0=.02;%interest rate sigma=.15;%vatality rate of risky asset mu0=.06;%drift rate of risky asset gamma=5;%risk aversion rate M=10000;%number of trajectories N=55;%time period T=55;%total time period R=40;%time of retirement R_l=T-R;%length of retiment dt=T/N;%each time period t=1:dt:T; omega=x; Rf=exp(r0);
for j=1:N Rs(:,j+1)=exp(mu0+sigma*randn(M,1)); a(:,j)=.5*rand(M,1); end
for j=1:N-1 for i=1:M if j=1 w(:,N-j+1)=omega(N-j+1)*ones(M,1) w(i,N-j)=(Rf+omega(N-j)*a(i,N-j+1)*(Rs(i,N-j+1)-Rf))^(-gamma)*(Rs(i,N-j+1)-Rf); else if j<R_l w(i,N-j)=(a(i,N-j)*(Rf+omega(N-j)*(Rs(i,N-j+1)-Rf))-a(i,N-j+1))^(-gamma)*(Rs(i,N-j+1)-Rf); else w(i,N-j)=(a(i,N-j)*(Rf+omgea(N-j)*(Rs(i,N-j+1)-Rf))+1-a(i,N-j+1))^(-gamma)*(Rs(i,N-j+1)-Rf); end end end W(j)=sum(w(:,j)); end
No products are associated with this question.
It would be more robust if you defined t as linspace(1,T,N) -- the exact length of 1:dt:T can vary by 1 element because of accumulated round-off error. If, that is, you had not happened to define N and T as being identical and thus implicitly defining dT as being exactly 1.
"if j=1" is invalid syntax. You need "if j==1"
Your "else" of j<R_1 references a variable named "omgea" instead of "omega"
Your f output is copied from W and W(j) is the only place W is assigned, so the size of W is the same as the maximum j, and is thus N-1 (i.e., 54)
Your input vector, x, is copied to omega. The last element of omega that you access is omega(N-j+1) for the case that j=1, so that is omega(N) (i.e., omega(55)). The first element of omega that you access is omega(N-j) when j = N-1 [or would once the omgea spelling mistake is corrected], so omega(1); that's okay, but needed to be cross-checked through the logic.
So your input vector, x, must be length 55, but your output vector is length 54. Please check that that is what you want, as it disagrees with your opening statement that each x(j) should lead W(j) to be 0, as that would imply that x(55) should lead W(55) to 0 but there is no W(55) in your calculation.