Hello all,
I am trying to solve a system of non-linear vector equations in MATLAB using fsolve.
Here is my function code:
function F = ekmodelProto(x)global dni T L theta gam betan = 4;p = ones(1, n);x_ni = ones(n);w = ones(1, n);for i = 1:n p(i) = x(i);endfor i = n+1:n + (n.^2) - 1 for j = 1:n.^2 x_ni(j) = x(i); endendfor i = n+n.^2:2.*n+ n.^2 for j = 1:n w(j) = x(i); endendF = [(((gam.*(((dni.*((((p.^(1-beta)'))*((w.^beta)))))').^(-theta))*T)').^(-1/theta))-p; ((T'.*((1/p)'.*dni.*(((p.^(1-beta)'))*((w.^beta)))').^(-theta))')- x_ni;((((x'*((w.*L)'))./L)')')-w];end
And here is my script for getting the output:
x0 = 0.5.*ones(1, 24);x0 = x0;n = 4;dni = ones(2);T = ones(4,1);L = ones(4,1);theta = 4;beta = 0.5;gam = 1;sol = fsolve(@ekmodelProto, x0);
For context the matrix in function F should be a n+2 x n matrix. For example, if n is 2 I expect to have a 6 x 2. This is because The first row is a vector equation equal to p which is a 1xn. The second row is equal to x_ni, which is an nxn, and the third row is equal to w which is an 1xn. I am getting the following error.
Matrix dimensions must agree.Error in ekmodelProto (line 21)F = [((gam.*(((dni.*((((p.^(1-beta)'))*((w.^beta)))))').^-theta)*T)')-p;Error in fsolve (line 255) fuser = feval(funfcn{3},x,varargin{:});Error in getoutput2 (line 12)sol = fsolve(@ekmodelProto, x0);Caused by: Failure in initial objective function evaluation. FSOLVE cannot continue.
Two errors are puzzling me. First, MATLAB is telling me that my matrix dimensions don't match up. I double checked and I can't find the error. Obviously, MATLAB is'nt lying. Second, I'm not sure why fsolve is having trouble parsing the initial guess. I am assuming it is shaped incorrectly. How should I shape it? When n = 4 I have 24 unknowns (4 p's, 16 x_ni's, and 4 w's). Is something else causing this error? (The way the function is written?)
Best Answer