Solving System of Non-Linear Equations w/ MATLAB fsolve

  • MATLAB
  • Thread starter cianfro1
  • Start date
  • Tags
    Matlab
In summary, the conversation is about trying to solve a system of non-linear equations using MATLAB's fsolve function. The speaker was successful in solving 5 equations, but encountered an error message when trying to solve for 20 equations. They discuss the possible limitations of fsolve and the need to ensure that the input variables are the correct size and data type. The conversation ends with the realization that the initial guess for the equations was only set for 5 variables instead of 19, leading to the error message.
  • #1
cianfro1
4
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
  • #2
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:
  • #3
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.
 
  • #4
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.
 
  • #5
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);
 
  • #6
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).
 
  • #7
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!
 

What is a non-linear equation?

A non-linear equation is an equation in which the variables do not form a straight line when plotted on a graph. These equations often involve exponents, logarithms, and trigonometric functions.

Why is it important to solve non-linear equations?

Non-linear equations are commonly used to model complex systems in mathematics, physics, engineering, and other sciences. By solving these equations, we can gain a better understanding of these systems and make more accurate predictions.

What is the fsolve function in MATLAB?

The fsolve function in MATLAB is a built-in function that uses a numerical method to solve systems of non-linear equations. It takes in the equations and initial guesses for the variables as inputs and returns the values of the variables that satisfy the equations.

How does fsolve work?

FSOLVE uses a combination of derivative-based and derivative-free methods to find the roots of a system of non-linear equations. It starts with the initial guesses for the variables and iteratively improves the solutions until it reaches a desired level of accuracy.

What are some common errors encountered when using fsolve?

Some common errors encountered when using fsolve include not providing enough initial guesses, having poorly conditioned equations, or having a non-convergent solution. It is important to carefully choose initial guesses and check for any errors or warnings that may arise during the solving process.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • Programming and Computer Science
Replies
1
Views
667
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
Back
Top