Re: Extracting functions from Solve
- To: mathgroup at smc.vnet.net
- Subject: [mg7057] Re: Extracting functions from Solve
- From: Dick Zacher <dick at loc3.tandem.com>
- Date: Sat, 3 May 1997 22:04:49 -0400 (EDT)
- Organization: Tandem Computers
- Sender: owner-wri-mathgroup at wolfram.com
mark christopher haase wrote: > > How may I extract and define as functions the solutions derived from > Solve? > > E.g., Solve[x^2+y^2==1,y], I would like to define two functions f1 and > f2 that are the solutions to this. > You could do it this way: (* Solve the equations *) In[1]:= solution = Solve[x^2 + y^2 == 1, y] Out[1]= {{y -> -Sqrt[1 - x^2]}, {y -> Sqrt[1 - x^2]}} (* Define functions based on the two solutions *) In[2]:= f1 = Function[ x, Evaluate[ y /. solution[[1]] ] ] Out[2]= Function[x, -Sqrt[1 - x^2]] In[3]:= f2 = Function[ x, Evaluate[ y /. solution[[2]] ] ] Out[3]= Function[x, Sqrt[1 - x^2]] (* Now test the functions *) In[4]:= f1[z] Out[4]= -Sqrt[1 - z^2] In[5]:= f2[z] Out[5]= Sqrt[1 - z^2] Or, if you were feeling playful, or were expecting to have to do this repeatedly, you could define a function to make the functions for you. An example: In[6]:= makeAFunction[soln_,yy_,xx_,fun_]:= (fun=Map[Function[xx, Evaluate[yy /.#]]&,soln]) In[7]:= SetAttributes[makeAFunction,HoldRest] In[8]:= makeAFunction[solution,y,x,g] Out[8]= {Function[x, -Sqrt[1 - x^2]], Function[x, Sqrt[1 - x^2]]} In[9]:= g[[1]][z] Out[9]= -Sqrt[1 - z^2] In[10]:= g[[2]][z] Out[10]= Sqrt[1 - z^2] -- ----------------------------- Dick Zacher Performance Engineering Dept., Tandem Computers zacher_dick at tandem.com phone: 408-285-5746 fax: 408-285-7079