 
 
 
 
 
 
Re: I can plot a function but when I try FindRoot it complains
- To: mathgroup at smc.vnet.net
- Subject: [mg92703] Re: I can plot a function but when I try FindRoot it complains
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sat, 11 Oct 2008 06:42:17 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <gcn485$742$1@smc.vnet.net>
Toan wrote:
> I have a problem with Mathematica error that I don't know how to fix.
> I define a function
> f2 [L,ds(L)]  where ds(L) is found by solving f1(L,ds)==1.
> 
> When I plot f2 [L, ds(L)] as a function of L, everything is good.
> However, when I try to
> find the value of L where f2 ==1, Mathematica complains that f2 is not
> a number?!
*snip*
Add a test on the variables to ensure that the functions are called only 
when the arguments are numeric. Thus,
Clear[R, f1, f2, myDs]
k = 5.62; Ro = 27.2; Ltotal = 13509.0; F0 = 1.2*10^4; c = 0.30;
R[L_?NumericQ, ds_?NumericQ] =
   Sqrt[Ro^2 - ((3 Sqrt[3])/(8 Pi) ds^2 L)^(2/3)];
f1[L_?NumericQ,
    ds_?NumericQ] := (k/(R[L, ds]^2 ds^2) +
      3 k/ds^2 (1/((Ro^2) - (R[L, ds]^2)) +
         Ro/(Ro^2 - R[L, ds]^2)^(3/2) Log[(Ro -
              Sqrt[Ro^2 - R[L, ds]^2])/R[L, ds]]))/(Sqrt[
       3] F0 Exp[-ds/c]);
f2[L_?NumericQ,
    ds_?NumericQ] := (Sqrt[3] F0 Exp[-(ds/c)] (c^2 + c ds) +
      k/(2 (R[L, ds]^2)))/(Pi);
myDs[p_?NumericQ] := (result =
    FindRoot[f1[Ltotal*p, ds] - 1, {ds, 2.5}];
   result[[1, 2]])
Plot[f2[Ltotal*p, myDs[p]], {p, 0.01, 1.0}, PlotRange -> {0, 1},
  Frame -> True]
FindRoot[f2[Ltotal*p, myDs[p]] - 1, {p, 0.8}]
Out[11]= [...  graphic deleted  ...]
Out[12]= {p -> 0.708307}
For more info, see "How do I write a function which evaluates only when 
the argument is a number?" at
http://support.wolfram.com/mathematica/kernel/features/evalwhennumber.html
Regards,
-- Jean-Marc

