# how to define roots of an equation as a function? [closed]

I need to solve an equation depending on 3variables in only one variables and define the roots as a function of the remaining variables? I used some thing like that:

root[x_,y_]:=Solve[x^2+y^2+z^2==0,z][1];


but it doesn't ?

-

## closed as off-topic by Yves Klett, Oleksandr R., Sjoerd C. de Vries, bobthechemist, RunnyKineMay 25 '14 at 17:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Yves Klett, Oleksandr R., bobthechemist, RunnyKine
If this question can be reworded to fit the rules in the help center, please edit the question.

it doesn't what? Try: root[x_, y_] := Block[{z}, z /. Solve[x^2 + y^2 + z^2 == 0, z]] –  Kuba May 25 '14 at 8:59
possible duplicate of What are the most common pitfalls awaiting new users? –  Sjoerd C. de Vries May 25 '14 at 13:07

ClearAll[s, f];

s = Solve[x^2 + y^2 + z^2 == 0, z];

f[x_, y_] = z /. s;

(* find z for some x,y *)

f[1, 2]

(* {-I Sqrt[5],I Sqrt[5]} *)

(* check those are solutions *)

(x^2 + y^2 + z^2 == 0) /. {x -> 1, y -> 2, z -> #} & /@ f[1, 2]

(* {True,True} *)

-
Why don't you care about z, only s and f? ;) –  Kuba May 25 '14 at 9:16
@Kuba: "... before zee Germans get here..." –  ciao May 25 '14 at 9:18
Remove ClearAll[s, f] and I can agree ;P –  Kuba May 25 '14 at 9:20
@rasher vut? vut are yoo talking aboot? –  Yves Klett May 25 '14 at 9:21
@YvesKlett: Just to be sure no one gets offended, it's a reference from the movie Snatch –  ciao May 25 '14 at 9:36

Why []? You need [[]] to access element:

In[1]  root[x_, y_] := Solve[x^2 + y^2 + z^2 == 0, z][[1]];
In[2]  root[1, 1]
Out[2] {z -> -I Sqrt[2]}


Or, if you want a pure value:

In[3]  root[x_, y_] := z /. Solve[x^2 + y^2 + z^2 == 0, z][[1]];
In[4]  root[1, 1]
Out[2] -I Sqrt[2]

-
If this is going to be stable function I think you should take care of scoping of z. –  Kuba May 25 '14 at 9:18