Info

This question is closed. Reopen it to edit or answer.

Error using fzero. Unable to perform assignment because the size of the left side does not equal the right side

1 view (last 30 days)
I am writing a function to solve for a steady state for a complex economics problem set. I understand that the high number of globals can cause issues, but after speaking with my professor, I think this is the best method for solving my problem.
I appreciate any help, especially understanding what the error even means.
The globals are defined in another function, but are all parameters from alpha, delta, & beta that have exact values.
function [ zero ] = multshoot(k2)
% multshoot.m
% This m file solves for the terminal level of capital by solving the 2nd
% order difference equation by making a guess for the second starting value
% for output
% The global statement is used to pass some information into the function
% from the main program. The variable ovecms, which contains the time path
% for output is also passed back into the main program
global a b d kstar kvecms T tauh tauk alpha delta beta
kvecms(2,1) = k2; % Guess for second-period output
% Iterate on difference equation starting from period 3 using starting
% values for the first two periods for output
for t=3:T
kvecms(t,1) = beta.*(1+(1-tauk).*(alpha.*((1-alpha).^a).*(kvecms(t-1,1).^(alpha-1+alpha.*a))-delta)+(((1-alpha).^b))+(1-tauh).*((1-alpha).^(1+d).*kvecms(t-1,1).^(alpha+alpha.*d))).*(((1-alpha).^(1+a).*kvecms(t-1,1).^(alpha+alpha.*a))+(1+kvecms(t-2,1)).*(alpha.*((1-alpha).^a).*(kvecms(t-2,1).^(alpha-1+alpha.*a))-delta)+kvecms(t-1,1))-(((1-alpha).^(1+a).*kvecms(t-1,1).^(alpha+alpha.*a))+(1+kvecms(t-1,1)).*(alpha.*((1-alpha).^a).*(kvecms(t-1,1).^(alpha-1+alpha.*a))-delta));
% the equation is derived from the non-linear second-order difference
% equation from the Euler equation, solved for kt+2. Everything is in terms of kvecms
% The initial starting value is equal to 0
% Likewise, the second starting value, kvecms(2,1), is equal to the
% guess
end
% Examine the difference between the terminal level of output and the
% steady state level of output
zero = kstar - kvecms(T,1);
end
  3 Comments
Caitlin Gust
Caitlin Gust on 5 Apr 2020
Thanks for your response. The exact error is:
Error using fzero (line 306)
FZERO cannot continue because user-supplied function_handle ==> multshoot failed with the error below.
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 0-by-0.
Tommy
Tommy on 5 Apr 2020
Thanks! Somewhere in multshoot, you have some line
A = B
where MATLAB is trying to set A equal to B but can't because B is empty (i.e. []) and A isn't. It might be this line,
kvecms(2,1) = k2;
for example, if the k2 passed into multshoot is empty. Or it could be elsewhere (two other possibilities). It's hard to tell which without having the rest of your code. I would recommend running
dbstop if error
in the command window and re-running your code. MATLAB should then stop and enter debug mode when it reaches the error. For more on debugging: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!