matrix size error using fsolve

3 views (last 30 days)
Ryan
Ryan on 7 Apr 2014
Commented: Walter Roberson on 7 Apr 2014
I'm trying to use fsolve to solve a nonlinear system of equations but i keep getting the following error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
All the globally defined variables are scalars. Here's my code:
function f = solvefeedgases(m)
global Cb Cc Ce HH2 HCH4 q xH2
f(1) = xH2 - (m(1)/(m(1) + m(2) + Cb + Cc + 2*Ce));
f(2) = m(3) - (m(2)/(m(1) + m(2) + Cb + Cc + 2*Ce));
f(3) = m(3) - m(4)*HCH4;
f(4) = -m(5) + m(1) + 2*Ce + Cc*q;
f(5) = m(6) - m(2)*q;
f(6) = m(6) - m(5)*(.01/.99);
end
I can't seem to figure out how to get rid of this error.
  1 Comment
Ryan
Ryan on 7 Apr 2014
the error is specifically referring to the " f(4) = -m(5) + m(1) + 2*Ce + Cc*q"

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 7 Apr 2014
The variable "q" is not a scalar. Possibly it was not previously initialized.
  2 Comments
Ryan
Ryan on 7 Apr 2014
why do you say that? from my workspace, it shows that q is a scalar.
Walter Roberson
Walter Roberson on 7 Apr 2014
You have the expression
f(4) = -m(5) + m(1) + 2*Ce + Cc*q
m(5) and m(1) and 2 are certain to be scalars or else you would have gotten an error about indexing out of range (if m was empty).So if the expression on the right is producing a non-scalar then Ce or CC or q must be non-scalars, perhaps empty. Ce and CC were used to construct f(1) which did not fail, so they must not be non-scalars. All we are left with as a possibility is q. What does size(q) show ?
dbstop if error
and run the code and check what q is at the time of the error.

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!