# Defining solutions to FindRoot as a function of parameters in Mathematica

This works:

f[a_] := x /. FindRoot[x^2 - a == 0, {x, 1}]
f
(* 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
(* -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?

• There is a Mathematica stack exchange. You posted this on math. Jan 21 '15 at 23:37

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}]