Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

I can plot a function but when I try FindRoot it complains

2 views
Skip to first unread message

Toan

unread,
Oct 10, 2008, 4:37:25 AM10/10/08
to

Hi all,

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?!

The mathematica cell is listed below.

Could someone help me with this error? I thank you in advance, a lot.

Toan

=================================
k5.62; Ro = 27.2; Ltotal = 13509.0; F0 = 1.2*10^4; c = 0.30;

R[L_, ds_] = Sqrt[Ro^2 - ((3 Sqrt[3])/(8 Pi) ds^2 L)^(2/3)];

f1[L_, ds_] := (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_, ds_] := (Sqrt[3] F0 Exp[-(ds/c)] (c^2 + c ds) + k/(
2 (R[L, ds]^2) ))/(Pi);

myDs[p_] := ( 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}]

Jean-Marc Gulliet

unread,
Oct 11, 2008, 6:42:22 AM10/11/08
to
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

0 new messages