Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB Help w/ fsolve

  1. Jan 22, 2007 #1
    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?
     
  2. jcsd
  3. Jan 22, 2007 #2
    It seems as if there isn't a limit.

    Furthermore, it's not listed as a limitation:

    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: Jan 22, 2007
  4. Jan 22, 2007 #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.
     
  5. Jan 22, 2007 #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.
     
  6. Jan 22, 2007 #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);
     
  7. Jan 22, 2007 #6
    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).
     
  8. Jan 22, 2007 #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!!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: MATLAB Help w/ fsolve
  1. FSOLVE in Matlab (Replies: 0)

  2. MATLAB help (Replies: 7)

  3. Matlab help (Replies: 6)

Loading...