Matlab fsolve: Solving a System of Non-Linear Equations - Need Help!"

  • MATLAB
  • Thread starter duff8000
  • Start date
  • Tags
    Matlab
In summary, the conversation is about a person seeking help with solving a system of 29 non-linear equations using MATLAB. They have created a function with variables for each "guess/starting point" and another function with all the equations. They are encountering errors and are looking for help and also wondering about the syntax/code for using fsolve. They have also made updates to their code but are unsure if they have made much progress.
  • #1
duff8000
3
0
Hey all, new here, lookin for some help with matlab, pretty much no experience.
Gotta solve a system of 29 non-linear equations so here is what i was thinkin. I made a m-file with func which has each "guess/starting point" set to a variable. Then this function puts all of these into a matrix just so its easier to change and move around etc. Then my other m-file (as shown) has all the equations. Dont know if you can take a matrix as "IN" or what I am doing wrong but keeps thrown errors so any help would be much appreciated...thanks in advance (Also what is the syntax/code for fsolve once i get this workn)?

I had it set so i would call the other function, create a matrix with all those value and pass it to the one below (prolly not best most efficient but I am not too good at all this)



function f = HWFive (A)
m1=A(1);
m2=A(2);
m8=A(3);
m10L=A(4);
m10V=A(5);
m14=A(6);
T2=A(7);
T4=A(8);
T5=A(9);
T6=A(10);
T7=A(11);
T10=A(12);
T12=A(13);
T13=A(14);
cp4=A(15);
cp13=A(16);
P4=A(17);
P13=A(18);
h5=A(19);
h6=A(20);
h6g=A(21);
h7=A(22);
h8=A(23);
h10L=A(24);
h10V=A(25);
h11=A(26);
h12=A(27);
mpropane=A(28);
methylene=A(29);

%Well Here It Goes...


% T-Joint No.1
f(1) = m1-m2+m14;
f(2) = m1*290-m2*T2+m14*320;
%Compressor No.1
f(3) = (P4/1000)-7.5+150.5*((m2*T2^0.5)/1000);
%Propane Evaporator
f(4) = (230 - T4)/100 + exp((-2.64/(m2*cp4)));
f(5) = cp4-2.114-0.000188*P4+(1.76*10^-8)*P4^2;
%Heat Exchanger No.1
f(6) = m2*cp4*(T4-T5)-m14*cp13*(T13-T12);
f(7) = ((T13 - T12)/(T4 - T12)) - ((1 - exp((-4.22/(m14*cp13)*(1 - ((cp13*m14)/(cp4*m2))))))/(1 - ((cp13*m14)/(cp4*m2))*exp((-4.22/(m14*cp13)*(1 - ((cp13*m14)/(cp4*m2)))))));
f(8) = cp13 - 2.114 - 0.000188*P13 + (1.76*10^(-8))*(P13)^2;
%Compressor No.2
f(9) = (1000/P13) - 5.6 + ((142.8*m14*T13^0.5)/P13);
%Heat Exchanger No.2
f(10) = m2*(h6 - h7) - m14*(h12 - h11);
f(11) = 1 - exp((-5.275*(T6 - T7))/(m2*(h6 - h7))) - ((T6 - T7)/(T6 - T12));
f(12) = h6 + 219.6 - 7.025*T6 + (0.034965*(T6)^2) - (0.00011321*(T6^3));
f(13) = h7 + 219.6 - 7.025*T7 + (0.034965*(T7)^2) - (0.00011321*(T7^3));
f(14) = h12 - 790.13 + 4.0137*T12 - (0.058268*(T12)^2) + (0.00019465*(T12^3));
f(15) = log(P13) + (1026/T12) - 13.8;
%Storage Tank
f(16) = m10L + m10V - m8;
f(17) = m8*h8 - m10L*h10L - m10V*h10V;
f(18) = h10L + 219.6 - 7.025*T10 + (0.034965*(T10^2)) - (0.00011321*(T10^3));
f(19) = h10V - 790.13 + 4.0137*T10 - (0.058268*(T10^2)) + (0.00019465*(T10^3));
f(20) = log(101) + (1026/T10) - 13.8;
%T-Joint No.2
f(21) = m2 - m14 - m8;
f(22) = h11 - h7;
f(23) = h8 - h7;
%Ethylene Evaporator
f(24) = m2*(h5 - h6) - 42.2*(T6 - 170);
f(25) = h5 - h6g - cp4*(T5 - T6);
f(26) = h6g - 790.13 + 4.0137*T6 - (0.058268*(T6^2)) + (0.00019465*(T6^3));
f(27) = log(P4) + (1026/T6) - 13.8;
%Other Mass Flow Rates
f(28) = (mpropane)*427.8 - (m2*cp4*(T2 - T4));
f(29) = (methylene)*800.1 - m2*(h5 - h6);
 
Physics news on Phys.org
  • #2
In order to help you were going to need to know what errors its giving you. Matlab is usually pretty good about telling you what is wrong and why when errors occur. Also check Matlab Central or the file exchange. A lot of your work may already be done for you.
 
  • #3
Alrite was workn on it a bit newest code is posted below (not really sure if i changed much)

function f = HWFivexx (m1,m2,m8,m10L,m10V,m14,T2,T4,T5,T6,T7,T10,T12,T13,cp4,cp13,P4,P13,h5,h6,h6g,h7,h8,h10L,h10V,h11,h12,mpropane,methylene)

%Well Here It Goes...


% T-Joint No.1
f(1) = m1-m2+m14;
f(2) = m1*290-m2*T2+m14*320;
%Compressor No.1
f(3) = (P4/1000)-7.5+150.5*((m2*T2^0.5)/1000);
%Propane Evaporator
f(4) = (230 - T4)/100 + exp((-2.64/(m2*cp4)));
f(5) = cp4-2.114-0.000188*P4+(1.76*10^-8)*P4^2;
%Heat Exchanger No.1
f(6) = m2*cp4*(T4-T5)-m14*cp13*(T13-T12);
f(7) = ((T13 - T12)/(T4 - T12)) - ((1 - exp((-4.22/(m14*cp13)*(1 - ((cp13*m14)/(cp4*m2))))))/(1 - ((cp13*m14)/(cp4*m2))*exp((-4.22/(m14*cp13)*(1 - ((cp13*m14)/(cp4*m2)))))));
f(8) = cp13 - 2.114 - 0.000188*P13 + (1.76*10^(-8))*(P13)^2;
%Compressor No.2
f(9) = (1000/P13) - 5.6 + ((142.8*m14*T13^0.5)/P13);
%Heat Exchanger No.2
f(10) = m2*(h6 - h7) - m14*(h12 - h11);
f(11) = 1 - exp((-5.275*(T6 - T7))/(m2*(h6 - h7))) - ((T6 - T7)/(T6 - T12));
f(12) = h6 + 219.6 - 7.025*T6 + (0.034965*(T6)^2) - (0.00011321*(T6^3));
f(13) = h7 + 219.6 - 7.025*T7 + (0.034965*(T7)^2) - (0.00011321*(T7^3));
f(14) = h12 - 790.13 + 4.0137*T12 - (0.058268*(T12)^2) + (0.00019465*(T12^3));
f(15) = log(P13) + (1026/T12) - 13.8;
%Storage Tank
f(16) = m10L + m10V - m8;
f(17) = m8*h8 - m10L*h10L - m10V*h10V;
f(18) = h10L + 219.6 - 7.025*T10 + (0.034965*(T10^2)) - (0.00011321*(T10^3));
f(19) = h10V - 790.13 + 4.0137*T10 - (0.058268*(T10^2)) + (0.00019465*(T10^3));
f(20) = log(101) + (1026/T10) - 13.8;
%T-Joint No.2
f(21) = m2 - m14 - m8;
f(22) = h11 - h7;
f(23) = h8 - h7;
%Ethylene Evaporator
f(24) = m2*(h5 - h6) - 42.2*(T6 - 170);
f(25) = h5 - h6g - cp4*(T5 - T6);
f(26) = h6g - 790.13 + 4.0137*T6 - (0.058268*(T6^2)) + (0.00019465*(T6^3));
f(27) = log(P4) + (1026/T6) - 13.8;
%Other Mass Flow Rates
f(28) = (mpropane)*427.8 - (m2*cp4*(T2 - T4));
f(29) = (methylene)*800.1 - m2*(h5 - h6);

In the command window i first define all the vars, then call the function

answer=fsolve('HWFivexx',m1,m2,m8,m10L,m10V,m14,T2,T4,T5,T6,T7,T10,T12,T13,cp4,cp13,P4,P13,h5,h6,h6g,h7,h8,h10L,h10V,h11,h12,mpropane,methylene)

It tells me ---
"
Error in ==> HWFivexx at 47
f(29) = (methylene)*800.1 - m2*(h5 - h6);

Error in ==> fsolve at 193
fuser = feval(funfcn{3},x,varargin{:});
"

So i thought i had a problem with missing a variable when declaring it or something I thought I had Checked Pretty good for this ... is that what this error means for sure?
 
  • #4
Oh yea forgot to say that in order to check, i type " methylene " in the command window and it pops out with the correct value that I assigned...dont kno if this means anything tho...
 
  • #5
Error in ==> fsolve at 193
fuser = feval(funfcn{3},x,varargin{:});

Its telling you that there is an error with the command "fsolve" which is the source of the problem. Look up the help document for this command ("help fsolve") and read its requirements. Chances are there is something wrong with your syntax or your asking it to do something that it can't.
 

1. How does fsolve work in Matlab?

Fsolve is a built-in function in Matlab that uses the Newton's method to solve a system of non-linear equations. It takes in an initial guess for the solution and iteratively improves it until a desired level of accuracy is achieved.

2. Can fsolve be used for systems with more than two equations?

Yes, fsolve can be used to solve systems of any number of non-linear equations. It takes in all the equations as inputs and returns a vector of solutions.

3. What is the syntax for using fsolve in Matlab?

The syntax for using fsolve is [x,fval,exitflag] = fsolve(fun,x0,options). Here, fun is the function that defines the system of equations, x0 is the initial guess for the solution, and options is an optional argument for setting specific options for the solver.

4. How do I check if fsolve has successfully solved the system?

Fsolve returns an exit flag as the third output argument. A value of 1 indicates that the solver has found a solution within the given tolerance, while a value of 0 indicates that it has reached the maximum number of iterations without finding a solution. You can also check the value of fval, which should be close to zero if a solution has been found.

5. Are there any limitations to using fsolve?

Fsolve may not be able to find a solution if the system of equations is ill-conditioned or if the initial guess is far from the true solution. It is also important to provide appropriate options and constraints for the solver to work efficiently. Additionally, fsolve may not be the most efficient method for solving large systems of equations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
0
Views
1K
Back
Top