# FindRoot for a simple nonlinear system

Posted 2 months ago
697 Views
|
7 Replies
|
4 Total Likes
|
 Hi,I am new to Mathematica so probably I still do not know where to look for information properly. I wanted to solve a simple system of nonlinear equations with Findroot f1[x_, y_, z_] = 2 x + y - 5 + 2*z^2; f2[y_, z_] = y^3 + 4*z - 4; f3[x_, y_, z_] = x*y + z - Exp[z]; I have noticed that when I try FindRoot[{2 x + y - 5 + 2*z^2, y^3 + 4*z - 4, x*y + z - Exp[z]}, {{x, 1.422}, {y, 0.975}, {z, 0.768}}] I do obtain a result {x -> 1.42247, y -> 0.975389, z -> 0.768008} but when I use: FindRoot[{f1==0,f2==0,f3==0}, {{x,1.422},{y,0.975},{z,0.768}}] or FindRoot[{f1,f2,f3}, {{x,1.422},{y,0.975},{z,0.768}}] I get the following error: FindRoot::nlnum: The function value {f1,f2,f3} is not a list of numbers with dimensions {3} at {x,y,z} = {1.422,0.975,0.768}. Why I cannot use the second syntax? What does the error mean? What do you recommend me to learn by doing with Mathematica?Sorry for this questions, but I just started and I am still a bit disorientated. Thanks.Best regards. Dani
7 Replies
Sort By:
Posted 2 months ago
 Try f1[x_,y_,z_]:=2 x+y-5+2*z^2; f2[y_,z_]:=y^3+4*z-4; f3[x_,y_,z_]:=x*y+z-Exp[z]; FindRoot[{f1[x,y,z]==0,f2[y,z]==0,f3[x,y,z]==0}, {{x,1.422},{y,0.975},{z,0.768}}] Alternatives that are probably not a good idea because this can sometimes cause problems: f1= 2 x+y-5+2*z^2;f2= y^3+4*z-4;f3= x*y+z-Exp[z]; FindRoot[{f1==0,f2==0,f3==0}, {{x,1.422},{y,0.975},{z,0.768}}] FindRoot[{f1,f2,f3}, {{x,1.422},{y,0.975},{z,0.768}}] 
Posted 2 months ago
 Thanks, it works. Related to this question, I have increase the number of variables and functions. But I have failed to run the FindRoot function. I think that probably is a typo, but not sure what kind (I have not found it). I get the message a similar message as before.The notebook is attached if somebody wants to look at it.Thanks Attachments:
Posted 2 months ago
 If you look very closely you may see that you defined a name epsi followed by the digit zero and then you used a name epsi followed by the letter O.
Posted 2 months ago
 Thanks for your time, although changing the typo still get a similar output message. I guess, I still should have define something wrong.
Posted 2 months ago
 Without my being able to see your screen it is impossible for me to point out any flaw.Since your notebook is only a couple of dozen lines you can just scrape the contents off your screen and tap the little "<>" icon above the reply text box and paste your code in your reply. That will keep the posting process from making most changes to your code and then anyone can look at the code to see if they notice any small problem.You can tell if this worked if after posting you are able to scrape the code off the screen and paste it back into a new fresh notebook and run it.
 If I post the script it looks like this right now.  Kw = 10^-14; pHfix = 3; F = 96485.3328959; R = 8.314472; T = 298.15; KSOH2 = 10^4; KSO = 10^-8.8; KSOH2Cl = 10^5.82; KSONa = 10^-7; TCl = 10^-3; TNa = 10^-3; TSOH = 9.9635 10^-6; s = 1; a = 1; C1 = 1.05; C2 = 3.36; C3 = 0.27; epsi0 = 8.85418787 10^-12; epsi = 78.45203; zOH = -1; zH = 1; zCl = -1; zNa = 1; f1[H_] := H - 10^-pHfix; f2[H_, OH_] := Log10[OH] + Log10[H] - Log10[Kw]; f3[H_, SOH_, SOH2_, psiO_] := Log10[SOH] + Log10[H] + Log10[Exp[(-F psiO)/(R T)]] + Log10[KSOH2] - Log10[SOH2]; f4[H_, SO_, SOH_, psiO_] := Log10[SOH] - Log10[H] - Log10[Exp[(-F psiO)/(R T)]] + Log10[KSO] - Log10[SO]; f5[H_, Cl_, SOH_, SOH2Cl_, psiO_, psiA_] := Log10[SOH] + Log10[H] + Log10[Exp[(-F psiO)/(R T)]] + Log10[Cl] - Log10[Exp[(-F psiA)/(R T)]] + Log10[KSOH2Cl] - Log10[SOH2Cl]; f6[H_, Na_, SOH_, SONa_, psiO_, psiC_] := Log10[SOH] - Log10[H] - Log10[Exp[(-F psiO)/(R T)]] + Log10[Na] + Log10[Exp[(-F psiC)/(R T)]] + Log10[KSONa] - Log10[SONa]; f7[Cl_, SOH2Cl_] := Cl + SOH2Cl - TCl; f8[Na_, SONa_] := Na + SONa - TNa; f9[SOH_, SOH2_, SOH2Cl_, SONa_] := SOH + SOH2 + SOH2Cl + SONa - TSOH; f10[SO_, SOH2_, SOH2Cl_, SONa_, psiO_, psiC_] := SOH2 - SO + SOH2Cl - SONa - ((s*a)/F)*C1*(psiO - psiC); f11[SONa_, psiO_, psiC_, psiA_] := SONa - ((s*a)/F)*(C1*(psiC - psiO) + C2*(psiC - psiA)); f12[SOH2Cl_, psiC_, psiA_, psiD_] := -SOH2Cl - ((s*a)/F)*(C2*(psiA - psiC) + C3*(psiA - psiD)); f13[H_, OH_, Cl_, Na_, psiA_, psiD_] := -Sqrt[ 8*epsi*epsi0*R* T*1000*0.5*(H*zH^2 + OH*zOH^2 + Na*zNa^2 + Cl*zCl^2)]* Sinh[(F*psiD)/(2*R*T)] - ((s*a)/F)*C3*(psiD - psiA); FindRoot[{f1[H] == 0, f2[H, OH] == 0, f3[H, SOH, SOH2, psiO] == 0, f4[H, SO, SOH, psiO] == 0, f5[H, Cl, SOH, SOH2Cl, psiO, psiA] == 0, f6[H, Na, SOH, SONa, psiO, psiC] == 0, f7[Cl, SOH2Cl] == 0, f8[Na, SONa] == 0, f9[SOH, SOH2, SOH2Cl, SONa] == 0, f10[SO, SOH2, SOH2Cl, SONa, psiO, psiC] == 0, f11[SONa, psiO, psiC, psiA] == 0, f12[SOH2Cl, psiC, psiA, psiD] == 0, f13[H, OH, Cl, Na, psiA, psiD] == 0 }, {{H, 10^-3}, {OH, 10^-7}, {SOH2, 3.189 10^-8}, {SO, 4.844 10^-8}, {SOH2Cl, 7.2569 10^-10}, {SONa, 9.9035 10^-9}, {Cl, 9.9999 10^-4}, {Na, 9.9999 10^-4}, {SOH, 9.9635 10^-6}, {psiA, -0.258}, {psiC, -0.258}, {psiO, -0.258}, \ {psiD, 0}}] FindRoot::nlnum: The function value {f1[0.001],f2[0.001,1.*10^-7],f3[0.001,9.9635*10^-6,3.189*10^-8,-0.258],f4[0.001,4.844*10^-8,9.9635*10^-6,-0.258],f5[0.001,0.00099999,9.9635*10^-6,7.2569*10^-10,-0.258,-0.258],f6[0.001,0.00099999,9.9635*10^-6,9.9035*10^-9,-0.258,-0.258],0.000999991 -1. TCl,0.001 -1. TNa,0.000010006 -1. TSOH,-2.57278*10^-8,9.9035*10^-9,-7.2569*10^-10-(1. a (0. -0.258 C3) s)/F,0. -(0.258 a C3 s)/F} is not a list of numbers with dimensions {13} at {H,OH,SOH2,SO,SOH2Cl,SONa,Cl,Na,SOH,psiA,psiC,psiO,psiD} = {0.001,1.*10^-7,3.189*10^-8,4.844*10^-8,7.2569*10^-10,9.9035*10^-9,0.00099999,0.00099999,9.9635*10^-6,-0.258,-0.258,-0.258,0.}. FindRoot[{f1[H] == 0, f2[H, OH] == 0, f3[H, SOH, SOH2, psiO] == 0, f4[H, SO, SOH, psiO] == 0, f5[H, Cl, SOH, SOH2Cl, psiO, psiA] == 0, f6[H, Na, SOH, SONa, psiO, psiC] == 0, f7[Cl, SOH2Cl] == 0, f8[Na, SONa] == 0, f9[SOH, SOH2, SOH2Cl, SONa] == 0, f10[SO, SOH2, SOH2Cl, SONa, psiO, psiC] == 0, f11[SONa, psiO, psiC, psiA] == 0, f12[SOH2Cl, psiC, psiA, psiD] == 0, f13[H, OH, Cl, Na, psiA, psiD] == 0}, {{H, 1/10^3}, {OH, 1/ 10^7}, {SOH2, 3.189/10^8}, {SO, 4.844/10^8}, {SOH2Cl, 7.2569/ 10^10}, {SONa, 9.9035/10^9}, {Cl, 9.9999/10^4}, {Na, 9.9999/ 10^4}, {SOH, 9.9635/ 10^6}, {psiA, -0.258}, {psiC, -0.258}, {psiO, -0.258}, {psiD, 0}}] 
 Thank youIf I scrape your latest code and paste it into a fresh new notebook and evaluate it then I get {H->0.001 -3.10595*^-28 i,OH->1.*^-11+7.12467*^-35 i,SOH2->2.82852*^-11+1.96265*^-11 i, SO->3.73704*^-7-2.63567*^-7 i,SOH2Cl->2.54809*^-8+3.79633*^-9 i, SONa->1.16786*^-12-1.40912*^-13 i,Cl->0.000999975 -3.79633*^-9 i, Na->0.001 +1.40912*^-13 i,SOH->9.96666*^-7-3.81581*^-9 i,psiA->0.239837 -0.0117853 i, psiC->0.25969 -0.0126969 i,psiO->0.323108 -0.0156836 i,psiD->0.0000108769 -5.34467*^-7 i} where i is the square root of -1.And that doesn't give the error message that you saw. So probably what happened was that you were experimenting with your code and you assigned some value to one of your variables and later you did your FindRoot.Mathematica quietly keeps cached history of assignments made. Sometimes you might not realize that a previous assignment has been made and think that a variable has no value and can be solved for, but the previous assignment now means that your input doesn't look like what you expect.You can sometimes avoid problems like this by getting rid of cached assignments by having your first line in your code be Clear["Global*"] which deletes all the cached previous assignments. Or saving your notebook and then exiting and restarting Mathematica is equivalent if you are getting an odd error message that you think you shouldn't get.The next issue are those i, which represent complex numbers, that are appearing in your result. Since you are dealing with chemical concentrations you probably don't expect to get complex numbers.Mathematica's model of numbers, especially when you use decimal points which Mathematica understands to be approximate values, can sometimes give very tiny complex values. One way of eliminating most small spurious values is with Chop. If I evaluate Chop[%] immediately after I see the result from Findroot then the % means to use the immediate prior value just displayed and the Chop will replace all sufficiently small constants with zero. That should give you {H->0.001,OH->0,SOH2->0,SO->3.73704*^-7-2.63567*^-7 i,SOH2Cl->2.54809*^-8+3.79633*^-9 i, SONa->0,Cl->0.000999975 -3.79633*^-9 i,Na->0.001,SOH->9.96666*^-7-3.81581*^-9 i, psiA->0.239837 -0.0117853 i,psiC->0.25969 -0.0126969 i,psiO->0.323108 -0.0156836 i, psiD->0.0000108769 -5.34467*^-7 i} `which has eliminated some, but not all of the complex values, because some were not quite small enough for Chop to discard.You can carefully consider the results, with and without Chop, and see whether these might represent an error in the construction of your system or whether these are just numerical noise.