2
$\begingroup$

This works:

f[a_] := x /. FindRoot[x^2 - a == 0, {x, 1}]
f[3]
(* 1.73205 *)

I need this to work, but it doesn't:

y = x^2 - a == 0
g[a_] := x /. FindRoot[y == 0, {x, 1}]
g[3]
(* -a + x^2 == 0 *)

Instead, it produces the error messages

FindRoot::nlnum: The function value {1. -1. a==0.} is not a list of numbers with dimensions {1} at {x} = {1.}. >>
ReplaceAll::reps: {FindRoot[y==0,{x,1}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
(* x /. FindRoot[y == 0, {x, 1}] *)

Why doesn't the second method work? How can I get it to work for a large, far more complex problem with a long list of parameters on which the solution depends?

$\endgroup$
1
  • $\begingroup$ There is a Mathematica stack exchange. You posted this on math. $\endgroup$ – dustin Jan 21 '15 at 23:37
2
$\begingroup$

As written in the Question, Mathematica does not equate the a in y with the argument of g. This is a common issue. Use this instead, so that a appears explicitly.

y[a_] = x^2 - a; g[a_] := x /. FindRoot[y[a] == 0, {x, 1}]
g[3]
(* 1.73205 *)
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.