Group Abstract Group Abstract

Message Boards Message Boards

0
|
7.8K Views
|
5 Replies
|
1 Total Like
View groups...
Share
Share this post:

FindRoot not recognising replaced symbol in nested function

Posted 6 years ago
Attachments:
5 Replies

Many thanks for the comprehensive explanation Marco.

Posted 6 years ago

Crossposted here.

POSTED BY: Rohit Namjoshi

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

POSTED BY: Marco Thiel

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.

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

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard