Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 ?

share|improve this question

closed as off-topic by Yves Klett, Oleksandr R., Sjoerd C. de Vries, bobthechemist, RunnyKine May 25 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.

1  
it doesn't what? Try: root[x_, y_] := Block[{z}, z /. Solve[x^2 + y^2 + z^2 == 0, z]] –  Kuba May 25 at 8:59
    
    

2 Answers 2

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} *)
share|improve this answer
    
Why don't you care about z, only s and f? ;) –  Kuba May 25 at 9:16
    
@Kuba: "... before zee Germans get here..." –  rasher May 25 at 9:18
    
Remove ClearAll[s, f] and I can agree ;P –  Kuba May 25 at 9:20
    
@rasher vut? vut are yoo talking aboot? –  Yves Klett May 25 at 9:21
    
@YvesKlett: Just to be sure no one gets offended, it's a reference from the movie Snatch –  rasher May 25 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]
share|improve this answer
1  
If this is going to be stable function I think you should take care of scoping of z. –  Kuba May 25 at 9:18

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