matrix size error using fsolve
3 views (last 30 days)
Show older comments
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.
Answers (1)
Walter Roberson
on 7 Apr 2014
The variable "q" is not a scalar. Possibly it was not previously initialized.
2 Comments
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.
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!