How to create a function directly from the output of Solve

让人想犯罪 __ 提交于 2019-12-18 13:15:59

问题


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 want to convert each of those x->somethings into a function. Typically, my requirements are low, and my function f[x] is at the most a cubic, with straightforward solutions for x. So I've always just defined g1[y_]:=something, g2[y_]:=... etc, manually.

However, for a function that I have now, Solve outputs a complicated polynomial running 4 pages long, and there are 4 such solutions. I've tried reducing to simpler forms using Simplify, Collect, Factor etc, but it just seems irreducible.

Is there a way I can automatically assign them to functions? (It's extremely hard to scroll through pages and copy each one... and I have to look for where the next one begins!)

Something like: {g1[y_], g2[y_], g3[y_]} = output of Solve?


回答1:


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


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.




回答2:


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



回答3:


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



回答4:


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]



回答5:


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



来源:https://stackoverflow.com/questions/5319771/how-to-create-a-function-directly-from-the-output-of-solve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!