Message Boards Message Boards

0
|
10014 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to use Nsolve or findroot for a function / over a range

Posted 10 years ago

Hello

Here is the case I don't know how to solve: Over a range of "R", I want to solve etha=Exp[function of etha and R]. That should give an "etha" for each "R" Then calculate some other parameters that are function of both "etha" and "R" I really appreciate any help The code is

Clear["Global`*"]
k = 1.3806488*10^-23;      
Av = 6.02*10^23;                   
T = 25 + 273.15;                       
Psat1 = 3.168*10^3;          
PL = 101.325*10^3;             
vLinf = (1/997.0479)*18.015/1000;   
gama = 0.07199;                      
theta = Degree 10 ;                
qc = 1.22*10^8;
W = 50*10^-6;                        
alfa = Degree 45;
L = 0.45;                                    
Vcontainer = L^3 ;             
N1 = 3.049*10^27;             
N2 = 3.67*10^22;               
KH = 9.077*10^4;        
bound1 = W/Cos [theta - alfa];
Print["bound1", "=", N[bound1]]
(***********************************************************)
Vv[R_] := (Pi*R^3/3)*(2 - 
    3*Sin[theta - alfa] + (Sin[
       theta - alfa])^3 + (Cos[theta - alfa])^3/Tan[alfa])
Asv[R_] := Pi*R^2*(Cos[theta - alfa])^2/Sin[alfa];
Alv[R_] := 2*Pi*R^2*(1 - Sin[theta - alfa]);
H[R_] := R*(1 - Sin[theta - alfa] + Cos[theta - alfa]/Tan[alfa]);

etha[R_] := 
  Exp[(vLinf/(6.02*10^23))*(PL - Psat1)/(k*T) - 
    N2*k*T/(N1*k*T - qc*Vv[R]*(etha[R]*Psat1 - KH))];

N1L[R_] := N1 - qc*(etha[R]*Psat1*Vv[R]/(k*T));     
N2sL[R_] := (N1L[R]*PL/KH);    
N2L[R_] := N2/(1 + qc*PL*Vv[R]/(N2sL[R]*k*T));    

Rc[R_] := 2*gama/(etha*Psat1 + PL*(N2L[R]/N2sL[R]) - PL);    
B[R_] := qc*(-2*gama * Vv[R]/Rc[R] + 
      gama*(Asv[R]* Cos [theta] + Alv[R])) + 
   N1*k*T*(N2/N1 - N2L[R]/N1L[R]) + 
   N2*k*T*Log[N1*k*T/(N1*k*T - qc*etha*Psat1*Vv[R] + qc*Vv[R]*KH)];


(* I want to plot B vs. H same as Fig 2*)
t2 = Table[{ H[R], B[R]/qc}, {R, 10^-6, bound1, 10^-7}];
ListPlot[t2, Joined -> True, PlotStyle -> {Thick}, 
 AxesLabel -> {Style["H", "Graphics", FontSize -> 13], 
   Style["B-B0 /qc (J)", "Graphics", FontSize -> 13]}, 
 AxesOrigin -> {0, 0}, TicksStyle -> Directive[12]]
3 Replies

First one has to solve the non-linear functional equation in eta. To avoid the Overflow error message, you define a function

In[77]:= Clear[leila]
leila[[Eta]_, R_] := Block[{x = (vLinf/Av)*(PL - Psat1)/(k*T) - N2*k*T/(N1*k*T - qc*Vv[R]*([Eta] *Psat1 - KH))}, Exp[x]]

this function is rather dull, because you can do

In[87]:= Table[{R, FindRoot[[Eta] == leila[[Eta], R], {[Eta], 0}]}, {R, 0, 10, 1}]
Out[87]= {{0, {[Eta] -> 1.0007}}, {1, {[Eta] -> 1.00072}}, {2, {[Eta] -> 1.00072}}, {3, {[Eta] -> 1.00072}}, 
              {4, {[Eta] -> 1.00072}}, {5, {[Eta] -> 1.00072}}, {6, {[Eta] -> 1.00072}}, {7, {[Eta] -> 1.00072}}, 
              {8, {[Eta] -> 1.00072}}, {9, {[Eta] -> 1.00072}}, {10, {[Eta] -> 1.00072}}}

with

In[95]:= leila[1.00072, 8]
Out[95]= 1.00072

as well as

In[96]:= Table[{R, FindRoot[[Eta] == leila[[Eta], R], {[Eta], 0}]}, {R, 0, 10^8, 10^7}] 
Out[96]= {{0, {[Eta] -> 1.0007}}, {10000000, {[Eta] -> 1.00072}}, {20000000, {[Eta] -> 1.00072}}, 
              {30000000, {[Eta] -> 1.00072}}, {40000000, {[Eta] -> 1.00072}}, {50000000, {[Eta] -> 1.00072}}, 
              {60000000, {[Eta] -> 1.00072}}, {70000000, {[Eta] -> 1.00072}}, {80000000, {[Eta] -> 1.00072}}, 
              {90000000, {[Eta] -> 1.00072}}, {100000000, {[Eta] -> 1.00072}}}

which means Eta is nearly a constant over R with the constants given. Anyway, in case you had a non-constant Eta you could fit its value table {R, Eta}

In[97]:= Table[{R, FindRoot[[Eta] == leila[[Eta], R], {[Eta], 0}][[-1, -1]]}, {R, 0,10^8, 10^7}]
Out[97]= {{0, 1.0007}, {10000000, 1.00072}, {20000000, 1.00072}, {30000000, 1.00072}, 
    {40000000, 1.00072}, {50000000, 1.00072}, {60000000, 1.00072}, {70000000, 1.00072},
     {80000000, 1.00072}, {90000000, 1.00072}, {100000000, 1.00072}}

e.g. with Interpolation and use it afterwards because you know it's a function of R now. A symbolic solution for Eta is unavailable as the simplest examples show

In[100]:= Reduce[[Eta] == Exp[b/(a - [Eta])], [Eta]]
During evaluation of In[100]:= Reduce::nsmet: This system cannot be solved with the methods available to Reduce. >>
Out[100]= Reduce[[Eta] == E^(b/(a - [Eta])), [Eta]]
POSTED BY: Udo Krause

Thank for the reply I tried symbolic solve and no answer as you mentioned. I don't use mathematica quite often, and this reply is a great help as I could not figure it out from help

Any suggestion for a book or online resource from which I can learn more basics of Mathematica?

Thanks again Leila

OMG, finally I got this old post to appear formatted not only in the post-preview, but also in the community itself.

Secondly, resources to learn Mathematica have been mentioned here How to learn Mathematica and here Where can I find examples of good Mathematica programming practice? and here Are there good online video tutorials for learning Mathematica? ... and then it depends on the knowledge you do have already to find an insightful learning path ... for example, if you are a theoretical physicist and you read German by chance, why not look into Mathematica und C in der modernen Theoretischen Physik ... a substantial body of work represent Trott's Mathematica Guide Books

POSTED BY: Udo Krause
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract