How to create a function directly from the output of Solve

前端 未结 5 1842
天命终不由人
天命终不由人 2020-12-31 14:34

If I evaluate Solve[f[x,y]==0,x], I get a bunch of solutions like:

{{x -> something g[y]}, {x -> something else}}, etc.

Now I

相关标签:
5条回答
  • 2020-12-31 14:36

    Here's a simple solution that could be cleaned up

    In[1]:= solns = Solve[x^2+a x+b==0, x]
    Out[1]= {{x -> 1/2 (-a-Sqrt[a^2-4 b])}, {x -> 1/2 (-a+Sqrt[a^2-4 b])}}
    
    In[2]:= Table[Symbol["g"<>ToString[i]][a_,b_] := Evaluate[x/.solns[[i]]],
                  {i,Length[solns]}];
    
    In[3]:= DownValues/@{g1,g2}
    Out[3]= {{HoldPattern[g1[a_,b_]]:>1/2 (-a-Sqrt[a^2-4 b])},
             {HoldPattern[g2[a_,b_]]:>1/2 (-a+Sqrt[a^2-4 b])}}
    
    0 讨论(0)
  • 2020-12-31 14:39

    This is really cool. Thanks. By converting Solve results into functions I could use Manipulate in a Plot. Something like

    In[73]:= g = solutionFunctions[x^2 + a x + b == 0, x]
    Out[73] = {Function[{a, b}, 1/2 (-a - Sqrt[a^2 - 4 b])], 
      Function[{a, b}, 1/2 (-a + Sqrt[a^2 - 4 b])]}
    
    In[74]:= Manipulate[Plot[g[[1]][a, b], {a, 0, 4}], {{b, 1}, 0, 10}]
    

    And you get a plot where you can manipulate parameter b

    0 讨论(0)
  • 2020-12-31 14:42

    It appears Simon beat me to an answer (I am glad that StackOverflow gives me a pop-up to let me know!), therefore I will take a different approach. You should know how to use the output of Solve directly, as quite a few times it will be convenient to do that.

    Starting with

    ClearAll[a, x, sols]
    
    sols = Solve[x^2 + a x + 1 == 0, x]
    

    Here are some things you can do.


    Find the solutions to x for a == 7

    x /. sols /. a -> 7
    

    Plot the solutions

    Evaluate is used here not out of necessity for basic function, but to allow the Plot function to style each solution separately

    Plot[Evaluate[x /. sols], {a, 1, 4}]
    

    enter image description here


    Define a new function of a for the second solution

    Notice the use of = rather than := here

    g[a_] = x /. sols[[2]]
    

    Here is an alternative to Simon's method for defining functions for each solution

    MapIndexed[(gg[#2[[1]]][a_] := #) &, x /. sols]
    

    The function is then used with the syntax gg[1][17] to mean the first solution, and a == 17

    Plot[gg[1][a], {a, 1, 4}]
    
    gg[2] /@ {1, 2, 3}
    

    These uses do generally require that a (in this example) remain unassigned.

    0 讨论(0)
  • 2020-12-31 14:46

    The following function will automatically convert the output of Solve to a list of functions (assuming Solve finds solutions of course):

    solutionFunctions[expr_, var_] :=
      Check[Flatten @ Solve[expr, var], $Failed] /.
        (_ -> x_) :>
          Function[Evaluate[Union @ Cases[x, _Symbol?(!NumericQ[#]&), Infinity]], x]
    

    Here is an example:

    In[67]:= g = solutionFunctions[x^2+a x+1==0, x]
    Out[67]= {Function[{a},1/2(-a-Sqrt[-4+a^2])],Function[{a},1/2(-a+Sqrt[-4+a^2])]}
    

    The functions can be called individually:

    In[68]:= g[[1]][1]
    Out[68]= 1/2 (-1-I Sqrt[3])
    
    In[69]:= g[[2]][1]
    Out[69]= 1/2 (-1+I Sqrt[3])
    

    Or, all of the functions can be called at once to return all solutions:

    In[70]:= Through[g[1]]
    Out[70]= {1/2 (-1-I Sqrt[3]),1/2 (-1+I Sqrt[3])}
    

    The function will fail if Solve cannot find any solutions:

    In[71]:= solutionFunctions[Log[x]==Sin[x],x]
    During evaluation of In[71]:=
      Solve::nsmet: This system cannot be solved with the methods available to Solve.
    Out[71]= $Failed
    

    Variables are automatically identified:

    In[72]:= solutionFunctions[a x^2 + b x + c == 0, x]
    
    Out[72]= { Function[{a, b, c}, (-b - Sqrt[b^2 - 4 a c])/(2 a)],
               Function[{a, b, c}, (-b + Sqrt[b^2 - 4 a c])/(2 a)] }
    
    0 讨论(0)
  • 2020-12-31 14:54

    Here's the simplest way:

    In[1]:= f = Solve[x^2 + ax + 1 == 0, x]
    Out[1]= {{x -> -Sqrt[-1 - ax]}, {x -> Sqrt[-1 - ax]}}
    
    In[2]:= g1[y_] := x /. f[[1]] /. a -> y
            g2[y_] := x /. f[[2]] /. a -> y
    
    In[4]:= g1[a]
            g2[a]
    
    Out[4]= -Sqrt[-1 - ax]
    Out[5]= Sqrt[-1 - ax]
    
    0 讨论(0)
提交回复
热议问题