# FindRoot not recognising replaced symbol in nested function

**URL:** <https://community.wolfram.com/t/findroot-not-recognising-replaced-symbol-in-nested-function/15727>\
**Category:** Questions\
**Tags:** equation-solving\
**Created:** [April 11, 2020, 9:01am UTC](https://community.wolfram.com/t/findroot-not-recognising-replaced-symbol-in-nested-function/15727 "2020-04-11T09:01:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![archie1](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/archie1/32/2468_2.png) [@archie1](https://community.wolfram.com/u/archie1)\
**Post date:** [April 11, 2020, 9:01am UTC](https://community.wolfram.com/t/findroot-not-recognising-replaced-symbol-in-nested-function/15727/1 "2020-04-11T09:01:14Z")

</div>

Hi All,

I have the following example FindRoot function as part of a Nest function.

```
Clear[del]
del[x_] := Module[{y, dP, res, OP},
  res := {y} /. FindRoot[y^2 + 2 y - x, {y, 0}];
  OP := res[[1]];
  dP := OP + 1;
  Nest[(# + dP/#)/2 &, 1.5, 2]
  ]
del[name] /. name -> 3

```

Mathematica doesn’t recognise the “name” symbol and returns the following error:

```
"FindRoot::nlnum: The function value {0. -1. name} is not a list of numbers with dimensions {1} at {y$21486} = {0.}.

ReplaceAll::reps: {FindRoot[y$21486^2+2 y$21486-name,{y$21486,0}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing."

```

If you set Nest to apply zero times or replace it with a function that doesn’t repeat the code works fine. It also works if you replace FindRoot with a simple assignment for symbol “OP”. However, it doesn’t work if you use Solve instead of FindRoot. Can anyone shine any light on this?

Many thanks for any help.

[Replace.nb](https://community.wolfram.com/uploads/short-url/qssgntbtbm1UuBV5DOHc7DVu5oF.nb) (26.5 KB)

---

<div class="post-metadata">

**Author:** ![mthiel](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mthiel/32/42877_2.png) [@mthiel](https://community.wolfram.com/u/mthiel)\
**Post date:** [April 11, 2020, 10:23am UTC](https://community.wolfram.com/t/findroot-not-recognising-replaced-symbol-in-nested-function/15727/2 "2020-04-11T10:23:00Z")

</div>

Hi,

this works:

```
Clear[del]
del[x_] := 
 Module[{y, dP, res, OP}, 
  res := {y} /. FindRoot[y^2 + 2 y - x, {y, 0}];
  OP := res[[1]];
  dP := OP + 1;
  Nest[(# + dP/#)/2 &, 1.5, 2]]
del[name /. name -> 3]

```

Of course,

```
del[3]

```

is more concise.

Cheers,

Marco

---

<div class="post-metadata">

**Author:** ![archie1](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/archie1/32/2468_2.png) [@archie1](https://community.wolfram.com/u/archie1)\
**Post date:** [April 11, 2020, 11:03am UTC](https://community.wolfram.com/t/findroot-not-recognising-replaced-symbol-in-nested-function/15727/3 "2020-04-11T11:03:46Z")

</div>

Many thanks Marco, much appreciated. Interestingly, if you use solve with the expression equated to zero it also works (in my original post I’d tried Solve without equating the expression to zero).

```
Clear[del]
del[x_] := Module[{y, dP, res, OP},
  res := {y} /. Solve[y^2 + 2 y - x == 0, y];
  OP := res[[1]];
  dP := OP + 1;
  Nest[(# + dP/#)/2 &, 1.5, 2]
  ]
del[name] /. name -> 3

```

Can’t figure out why this works and FindRoot doesn’t.

---

<div class="post-metadata">

**Author:** ![mthiel](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mthiel/32/42877_2.png) [@mthiel](https://community.wolfram.com/u/mthiel)\
**Post date:** [April 11, 2020, 11:47am UTC](https://community.wolfram.com/t/findroot-not-recognising-replaced-symbol-in-nested-function/15727/4 "2020-04-11T11:47:56Z")

</div>

Hi,

there is a clue in the error message.

```
{FindRoot[y214862+2y21486-name,{y$21486,0}]}

```

FindRoot attempts to find a root numerically. As you see the error message says that it fails to do so, because there is the variable y (with the $21486, because of the localisation of Module), and the variable: name. That cannot work. Solve uses other methods and in particular symbolic solutions which makes it work in this case (it doesn’t care that name is substituted later.

This is basically a combination of (i) when are the variables substituted by numbers and (ii) do you use symbolics or numerics, I would think.

Best wishes,  
Marco

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [April 11, 2020, 2:15pm UTC](https://community.wolfram.com/t/findroot-not-recognising-replaced-symbol-in-nested-function/15727/5 "2020-04-11T14:15:19Z")

</div>

Crossposted [here](https://mathematica.stackexchange.com/q/219303/58370).

---

<div class="post-metadata">

**Author:** ![archie1](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/archie1/32/2468_2.png) [@archie1](https://community.wolfram.com/u/archie1)\
**Post date:** [April 14, 2020, 9:04am UTC](https://community.wolfram.com/t/findroot-not-recognising-replaced-symbol-in-nested-function/15727/6 "2020-04-14T09:04:16Z")

</div>

Many thanks for the comprehensive explanation Marco.
