Error: Unable to perform assignment because the left and right sides have a different number of elements.
1 次查看(过去 30 天)
显示 更早的评论
Hello all,
i wanted to create a loop, but I always get the following error: Unable to perform assignment because the left and right sides have a different number of elements.
Here is my code:
%% Define all variables:
alpha=0.33;
beta=0.96;
n= 5; %gridsize of kgrid
kgrid= [0 0.1 0.2 0.3 0.4] ; % Choose a grid of possibloe values of the capital stock.
%To do to find the steady stae of k and choose a grid with k_steadystate in
%between. You might want to use the linspace command. Try different grid
%sizes n.
%% Make an initial guess of the value function
v0= [1 1 1 1 1]; % What is the size of v0 ?
%% Define matrix U
for j=1:n
for i=1:n
c= kgrid(i)^(alpha)-kgrid(j);%insert budget constraint
if c>0
u(i,j)= log(kgrid(i)^(alpha)-kgrid(j)) ;% for each point in the matrix use a different combination of k and k'
else
u(i,j)=-inf; %make sure that only c>0 is chosen.
end
end
end %what is happening in the loops? What happens if consumption is negative?
%% Now iterate one step to obtain v1
v1=max(u+(beta).*v0'.*ones(n));
%Insert the right characters instead of ?try to understand all digits. Check: Is v1 of the same dimension as v0?
%what we have to do step-by-step:
v2=max(u+(beta).*v1'.*ones(n));
epsilon=max(v1-v2);
v3=max(u+(beta).*v2'.*ones(n));
epsilon2=max(v3-v2);
v4=max(u+(beta).*v3'.*ones(n));
epsilon3=max(v4-v3);
v5=max(u+(beta).*v4'.*ones(n));
epsilon4=max(v5-v4);
v6=max(u+(beta).*v5'.*ones(n));
epsilon5=max(v6-v5);
%% Repeat until convergence
%set initial value
V=v1
for iter=1:10 %insert the number of iterations until convergence you may try different numbers.
%calculate next step
V(iter+1)=max(u+(beta).*V(iter)'.*ones(n))
end
I know what to do manually (see "%what we have to do step-by-step:" in the code), but somehow I don't manage to create the loop.
Maybe someone know where I did a mistake.
Thanks in advance!
0 个评论
采纳的回答
madhan ravi
2018-11-18
编辑:madhan ravi
2018-11-18
The error was because V keeps increasing linearly by 5 so cell is needed to contain the values properly (since they have the capaciy to expand) as below:
%% Repeat until convergence
%set initial value
V={v1}
for iter=1:10-1 %insert the number of iterations until convergence you may try different numbers.
%calculate next step
V{iter+1}=max(u+(beta).*V{iter}'.*ones(n)) ;
end
V = [V{:}]
更多回答(1 个)
shariq khan
2018-11-18
编辑:shariq khan
2018-11-18
Hey!...pay attention to multiplying mattrix dimension - a[mxn] * b[nxr] = c[mxr] - so you cant multiple v0'(5x1) with ones(5)[5x5] matrix because 1 is not equal to 5
check the dimension multiplication at every level - this is what I encountered in whole program
I didnt solve after c because this is same problem in complete program - as what I perceive
check the attachment
try solving again
2 个评论
madhan ravi
2018-11-18
编辑:madhan ravi
2018-11-18
@shariq see https://www.mathworks.com/help/matlab/ref/times.html this is called element wise operation (.*) in matlab not matrix multiplication ( * )
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!