0
$\begingroup$

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[y214862+2y21486-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.

$\endgroup$
  • $\begingroup$ Hello, welcome to Mathematica.SE. Then, where's the FindRoot? $\endgroup$ – xzczd Apr 11 at 9:22
  • $\begingroup$ you can not do find root on expression with symbolic symbol in it like this. Try FindRoot[y^2 + 2 y - x == 0, {y, 0}] and you'll see. name is like x here. ps. you changed the question, so will delete my answer below. $\endgroup$ – Nasser Apr 11 at 10:08
  • $\begingroup$ Apologies my mistake - I've updated it with the original code which includes FindRoot now. $\endgroup$ – Archie Watts-Farmer Apr 11 at 10:10
  • $\begingroup$ Crossposted here. $\endgroup$ – Rohit Namjoshi Apr 11 at 14:14
  • $\begingroup$ Restrict the argument: del[x_?NumericQ] := Module[{y, dP, res, OP}, res := y /. FindRoot[y^2 + 2 y - x, {y, 0}]; OP := res; dP := OP + 1; Nest[(# + dP/#)/2 &, 1.5, 2]] $\endgroup$ – Bob Hanlon Apr 11 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.