# Why does this function inside FindRoot fail to evaluate?

Can someone help me understand why function mu found below does not execute when everything before it seems ok.

Cc = 1
Cw = 1
δ = 1.3

(the following function finds a root of an expression)

MM[μ_, λ1_, T1_, n1_] :=  FindRoot[2 Sqrt[Cc Cw E^((m1 δ)/n1) n1] +
Cc E^((m1 δ)/n1) n1 T1 λ1 -
m1 ((Cc Cw E^((m1 δ)/n1) δ)/Sqrt[Cc Cw E^((m1 δ)/n1) n1] +
Cc E^((m1 δ)/n1) T1 δ λ1) + μ == 0, {m1, 10}][[1, 2]]

MM[6, 2, 3, 100]
77.9641628564483

(MM seems to work)

(*The following function solves differential equation using NDSolve. It seems to work ok.)

w[μ_?NumericQ, λT_?NumericQ, T_?NumericQ, n_?NumericQ] :=
{cc = μ T λT;
λ = λT/T; M = MM[μ, λ, T, n];
{x[0], x} /.NDSolve[{ 1/(2 Cw λT x[t]^3)
Sqrt[Cc Cw E^((δ Derivative[1][x][t])/x[t])
λT x[t]] (2 x[t]^2 (Cw λT +
T λSqrt[Cc Cw E^((δ Derivative[1][x][t])/x[t]) λT x[t]])
+ δ^2 (Cw λT +
2 T λ Sqrt[Cc Cw E^((δ Derivative[1][x][t])/x[t]) λT x[t]])
Derivative[1][x][t]^2 - δ x[t] (Cw λT +
2 T λSqrt[Cc Cw E^((δ Derivative[1][x][t])/x[t]) λT x[t]])
(Derivative[1][x][t] + δ (x^′′)[t])) == 0,
x[T] == n, x'[T] == M}, x, {t, 0, T}]}[[1, 1]];

w[100, 2, 3, 100]
(* {2.404002904182026, InterpolatingFunction[{{0., 3.}}, <>]}*)

w seems to work: Observation: w[mv, 2, 3, 100][[1]] - 1 has a zero at about mv=181.4.

Next, function mu tries to find the above root for w-1==0 using FindRoot

mu[λT2_?NumericQ, T2_?NumericQ, n2_?NumericQ] :=
FindRoot[w[mv, λT2, T2, n2][[1]] - 1 == 0, {mv, 180}]

mu[2, 3, 100]
{mv -> 1.}

BUT mu does not evaluate w and [[1]] just takes the first domain variable and uses it in the equation mv-1==0.

PS: I have spent (wasted) a huge number of days trying to figure this out. FullForm is uninformative to me at least. The expression for mu is very simple as I have decomposed the problem into parts. The w function works. If I just cut and paste it out of the expression for mu, it executes and gives me the correct answer: w[mv, λT2, T2, n2][[1]]=.944..... I can plot w as well with no problems. Why does w not evaluate when put in FindRoot?

• You might want to have a close look at that expression for mu. Maybe check the FullForm or something. Commented Dec 16, 2014 at 20:42
– gpap
Commented Dec 17, 2014 at 16:46

I wasted a bit of time on this myself. Here is what I get from cutting and pasting your code panes.

Cc = 1;
Cw = 1;
δ = 1.3;

MM[μ_, λ1_, T1_, n1_] :=
FindRoot[2 Sqrt[Cc Cw E^((m1 δ)/n1) n1] +
Cc E^((m1 δ)/n1) n1 T1 λ1 -
m1 ((Cc Cw E^((m1 δ)/n1) δ)/
Sqrt[Cc Cw E^((m1 δ)/n1) n1] +
Cc E^((m1 δ)/n1) T1 δ λ1) + μ ==
0, {m1, 10}][[1, 2]]

MM[6, 2, 3, 100]

(* Out[710]= 77.9641628564 *)

Good so far. Now for the next function.

w[μ_?NumericQ, λT_?NumericQ, T_?NumericQ,
n_?NumericQ] := {cc = μ T λT; λ = λT/T;
M = MM[μ, λ, T, n]; {x[0], x} /.
NDSolve[{1/(2 Cw λT x[t]^3) Sqrt[
Cc Cw E^((δ Derivative[1][x][t])/x[t]) λT x[
t]] (2 x[t]^2 (Cw λT +
T λSqrt[
Cc Cw E^((δ Derivative[1][x][t])/
x[t]) λT x[
t]]) + δ^2 (Cw λT +
2 T λ Sqrt[
Cc Cw E^((δ Derivative[1][x][t])/
x[t]) λT x[t]]) Derivative[1][x][
t]^2 - δ x[
t] (Cw λT +
2 T λSqrt[
Cc Cw E^((δ Derivative[1][x][t])/
x[t]) λT x[t]]) (Derivative[1][x][
t] + δ (x^′′)[t])) == 0,
x[T] == n, x'[T] == M}, x, {t, 0, T}]}[[1, 1]];

w[100, 2, 3, 100]

During evaluation of In[711]:= NDSolve::dvnoarg: The function x appears with no arguments. >>

During evaluation of In[711]:= ReplaceAll::reps: {NDSolve[{(Sqrt[Power[<<2>>] x[<<1>>]] (2 Power[<<2>>] Plus[<<2>>]+1.69 Plus[<<2>>] Power[<<2>>]-1.3 x[<<1>>] Plus[<<2>>] Plus[<<2>>]))/(2 Sqrt[2] x[t]^3)==0,x[3]==100,(x^′)[3]==90.5349669061},x,{t,0,3}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

Out[712]= {x[0], x}

Okay, that was a mess. So I have no idea what is actually supposed to be in that w function. Presumably something like λSqrt was intended to be λ*Sqrt (notice the times sign)? This is why I had suggested checking the FullForm. Also there is this fellow (x^′′ which is almost certainly intended to be something else, maybe a second derivative formed with two primes. Again, it is important that you carefully check the code that you posted, so that others do not get thwarted atttempting to figure out issues involving basic syntax.

I would also suggest not using a parameter called T, one because it's not so good an idea to use capitalized names and especially not of one character, and two because it is then easy to get confused by λT vs. λ T (note space, that is, implicit multiplication).

Anyway, if you sort out the syntax, maybe there will be some hope of someone figuring out the evaluation issues you are having.

• Daniel, can you tell me if anything is planned to address this?: (54721) Commented Dec 17, 2014 at 17:31
• @Mr.Wizard I have no idea (that font stuff is all Klingon to me..) Commented Dec 17, 2014 at 19:09

I can fix your syntax issues, but I am not sure I can pinpoint exactly which of the fixes I made was the decisive one. I made three changes to your w function:

1. Fixed up the syntax errors (the λSqrt thing and the double prime thing Daniel mentioned)
2. Modularised the local variables
3. Made sure it returned the value, not the InterpolatingFunction as well (using [[1,1,1]] instead of [[1,1]] at the end.

w[μ_?NumericQ, λT_?NumericQ, T_?NumericQ, n_?NumericQ] :=
Module[{cc = μ T λT, λ = λT/T, M},
M = MM[μ, λ, T, n]; {{x[0], x} /.
NDSolve[{1/(2 Cw λT x[t]^3) Sqrt[
Cc Cw E^((δ Derivative[1][x][t])/x[t]) λT x[
t]] (2 x[t]^2 (Cw λT +
T λ Sqrt[
Cc Cw E^((δ Derivative[1][x][t])/
x[t]) λT x[
t]]) + δ^2 (Cw λT +
2 T λ Sqrt[
Cc Cw E^((δ Derivative[1][x][t])/
x[t]) λT x[t]]) Derivative[1][x][
t]^2 - δ x[
t] (Cw λT +
2 T λ Sqrt[
Cc Cw E^((δ Derivative[1][x][t])/
x[t]) λT x[t]]) (Derivative[1][x][
t] + δ Derivative[2][x][t])) == 0, x[T] == n,
x'[T] == M}, x, {t, 0, T}]}][[1, 1, 1]]

If w returns a scalar not a list, then it is in the correct form for FindRoot to work out the function value, and you don't need a separate Part extraction in the FindRoot argument. Your test case then returns {mv -> 181.214} as desired.

I believe this was the crux of your problem, but the messiness of the code didn't help.