MATLAB Solving System of Non-Linear Equations w/ MATLAB fsolve

  • Thread starter Thread starter cianfro1
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
The discussion centers on solving a system of non-linear equations using MATLAB's fsolve function. The user encountered an error when attempting to solve for more than five equations, specifically an "index out of bounds" error related to the variable 'r'. It was determined that the initial guess array 'x0' must match the number of equations, which the user initially overlooked. The solution involves ensuring that 'x0' has the same number of elements as the equations defined in the function. This oversight was the root cause of the error, highlighting the importance of aligning the initial guess with the number of equations.
cianfro1
Messages
4
Reaction score
0
I'm trying to solve a system of non-linear equations. I was successful in solving 5 equations/5unkowns with the function fsolve. However, when I have more than 5, I get the error message:

? Attempted to access r(17); index out of bounds because numel(r)=5.

How do I increase that size so that I can solve for 20 equations, instead of a mazimum of 5?
 
Physics news on Phys.org
It seems as if there isn't a limit.

For fsolve, the nonlinear system of equations cannot be underdetermined; that is, the number of equations (the number of elements of F returned by fun) must be at least as many as the length of x ...

Furthermore, it's not listed as a limitation:

Limitations
The function to be solved must be continuous. When successful, fsolve only gives one root. fsolve may converge to a nonzero point, in which case, try other starting values.

fsolve only handles real variables. When x has complex variables, the variables must be split into real and imaginary parts.

I'm not familiar with MATLAB... are you sure your "r" variable is the correct size/datatype, to hold a value at index 17? And are you sure that you've correctly specified all 20 some functions?
 
Last edited:
the 'r' variable is not the problem. Even if I do a test system of equations with 6 equations, it has the same problem. There must be some default that will only solve for 5 equations. If that is the case, I can't find out how to change it.
 
As I understand it, the 'r' variable is precisely the problem. There's a read error when trying to access r(17), which means r(17) does not exist. The length of r is 5.

Whether or not that is a restriction imposed by fsolve, I'm not sure. But I'd assume it's not a limitation, since it's not listed as one under the documentation. Furthermore, searching for this problem on Google yields absolutely nothing of any relevance.

If it is a limitation, they should update their documentation. Otherwise, try posting your code here. Maybe someone can spot some error in your code.
 
Actual Code of Problem

function z = wsteady(r,Et,Ti,Ta,Tdp,v,sigma,absorb,Fe,emiss,Wf,Rfb,Rfins,vamb,Dh,L,P,A,Ub,Uins,hi,density,mdot)

z(1) = r(1) - (0.75*r(17) + 0.25*Ta);
z(2) = r(2) - ((Ta-273)*(0.8004 + 0.00396*(Tdp-273))^0.25 + 273);
z(3) = r(3) - (1006+0.05*(r(1)-273));
z(4) = r(4) - ((0.09485*(r(1) - 273) + 13.278)/1000000);
z(5) = r(5) - ((0.02795*(r(1) - 273) + 24.558)/1000);
z(6) = r(6) - (v*Dh/r(4));
z(7) = r(7) - (r(4)*r(3)*density/r(5));
z(8) = r(8) - (sigma*Fe*(r(16)^4-r(18)^4)/(r(16)-r(18)));
z(9) = r(9) -(0.5*sigma*emiss*(r(15) ^4-Ta^4)/(r(15) -Ta));
z(10) = r(10) - (0.3535*sigma*emiss*(r(15) ^4-r(2)^4)/(r(15) -r(2)));
z(11) = r(11) - (0.1465*sigma*emiss*(r(15) ^4-Ta^4)/(r(15) -Ta));
z(12) = r(12) - (2.537*Wf*Rfb*(P*vamb/A)^0.5 + 1.31*(abs(r(15) - Ta))^(1/3));
z(13) = r(13) - (Rfb*r(5)/Dh*(5.4 + 0.00190*(r(6)*r(7)*Dh/L)^1.71/(1 + 0.00563*(r(6)*r(7)*Dh/L)^1.17)));
z(14) = r(14) - (Rfins*r(5)/Dh*(5.4 + 0.00190*(r(6)*r(7)*Dh/L)^1.71/(1 + 0.00563*(r(6)*r(7)*Dh/L)^1.17)));
z(15) = absorb*Et - (r(12)*(r(15) -Ta) + r(9)*(r(15) -Ta) + r(10)*(r(15) -r(2)) + r(11)*(r(15) -Ta) + Ub*(r(15) -r(16))); % + bden*br(3)*bvol*(r(15)-Tref)
z(16) = Ub*(r(15) -r(16)) - (r(13)*(r(16)-r(1)) + r(8)*(r(16)-r(18)));
z(17) = mdot*r(3)*(r(17)-Ta) - (r(13)*(r(16)-r(1)) + r(14)*(r(18)-r(1)));
z(18) = r(8)*(r(16)-r(18)) - (Uins*(r(18)-r(19)) + r(14)*(r(18)-r(1)));
z(19) = Uins*(r(18)-r(19)) - (hi*r(19)-Ti);

I recall the function using the following code:

sigma = 5.67e-8;
absorb = 0.8;
emiss = 0.9;
Fe = 0.82;
Etwinter=900;
Ti = 296;
Tawinter=268;
Tdpwinter=263;
Wf = 1;
vamb=2;
absorb = 0.8;
sig = 0.0000000567;
emiss = 0.9;
Fe = 0.82;
L=1.8288;
W=0.8128;
gap = 0.0254;
A = W*L;
Dh = 2*gap;
P = 2*W + 2*L
Rfb = 1.67;
Rfins = 2.17;
Uins = 0.283;
Ub = 13.4;
hi = 8.29;
vwinter = 0.8;
densitywinter = 288.15/Tawinter*1.2255;
mdotwinter = densitywinter*gap*vwinter;

options=optimset('FunValCheck','on');
x0=[290,287,288,297,296];

winter = fsolve('wsteady',x0,options,Etwinter,Ti,Tawinter,Tdpwinter,vwinter,sigma,absorb,Fe,emiss,Wf,Rfb,Rins,vamb,Dh,L,P,A,Ub,Uins,hi,densitywinter,mdotwinter);
 
cianfro1 said:
function z = wsteady(r,Et,Ti,Ta,Tdp,v,sigma,absorb,Fe,emiss,Wf,Rfb,Rfins,vamb,Dh,L,P,A,Ub,Uins,hi,density,mdot)

<snip>

I recall the function using the following code:

...
x0=[290,287,288,297,296];

winter = fsolve('wsteady',x0,options,Etwinter,Ti,Tawinter,Tdpwinter,vwinter,sigma,absorb,Fe,emiss,Wf,Rfb,Rins,vamb,Dh,L,P,A,Ub,Uins,hi,densitywinter,mdotwinter);

The array x0 has 5 elements and that is passed to the function wsteady as the variable r. So, as Sane said above, the length of array x0 should at least be as long as the variable r used in the function (which is 19 elements I believe).
 
wow... i overlooked the initial guess. The code was edited from 5 equations to 19 and i forgot to change that part! You guys were both right. Thank you very much!
 

Similar threads

Replies
4
Views
1K
Replies
5
Views
2K
Replies
3
Views
4K
Replies
7
Views
8K
Replies
6
Views
4K
Back
Top