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

**URL:** <https://community.wolfram.com/t/how-to-use-nsolve-or-findroot-for-a-function-over-a-range/3167>\
**Category:** Questions\
**Tags:** mathematica\
**Created:** [September 3, 2014, 8:36am UTC](https://community.wolfram.com/t/how-to-use-nsolve-or-findroot-for-a-function-over-a-range/3167 "2014-09-03T08:36:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leilazargarzadeh](https://avatars.discourse-cdn.com/v4/letter/l/46a35a/32.png) [@leilazargarzadeh](https://community.wolfram.com/u/leilazargarzadeh)\
**Post date:** [September 3, 2014, 8:36am UTC](https://community.wolfram.com/t/how-to-use-nsolve-or-findroot-for-a-function-over-a-range/3167/1 "2014-09-03T08:36:38Z")

</div>

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]]

```

---

<div class="post-metadata">

**Author:** ![sukrause](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sukrause/32/41348_2.png) [@sukrause](https://community.wolfram.com/u/sukrause)\
**Post date:** [September 6, 2014, 7:46pm UTC](https://community.wolfram.com/t/how-to-use-nsolve-or-findroot-for-a-function-over-a-range/3167/2 "2014-09-06T19:46:07Z")

</div>

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]]

```

---

<div class="post-metadata">

**Author:** ![leilazargarzadeh](https://avatars.discourse-cdn.com/v4/letter/l/46a35a/32.png) [@leilazargarzadeh](https://community.wolfram.com/u/leilazargarzadeh)\
**Post date:** [September 11, 2014, 12:18am UTC](https://community.wolfram.com/t/how-to-use-nsolve-or-findroot-for-a-function-over-a-range/3167/3 "2014-09-11T00:18:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sukrause](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sukrause/32/41348_2.png) [@sukrause](https://community.wolfram.com/u/sukrause)\
**Post date:** [September 13, 2014, 2:17pm UTC](https://community.wolfram.com/t/how-to-use-nsolve-or-findroot-for-a-function-over-a-range/3167/4 "2014-09-13T14:17:31Z")

</div>

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](https://wolfram.discourse.group/t/448/1) and here [Where can I find examples of good Mathematica programming practice?](http://mathematica.stackexchange.com/questions/18/where-can-i-find-examples-of-good-mathematica-programming-practice) and here [Are there good online video tutorials for learning Mathematica?](http://stackoverflow.com/questions/8672115/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](http://www.springer.com/physics/theoretical,+mathematical+&+computational+physics/book/978-3-642-64429-0) … a substantial body of work represent Trott’s [Mathematica Guide Books](http://www.mathematicaguidebooks.org/)
