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
?
Here's the simplest way:
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
Here are some things you can do.
Find the solutions to
x
fora == 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 separatelyDefine a new function of
a
for the second solutionNotice the use of
=
rather than:=
hereHere is an alternative to Simon's method for defining functions for each solution
The function is then used with the syntax
gg[1][17]
to mean the first solution, anda == 17
These uses do generally require that
a
(in this example) remain unassigned.This is really cool. Thanks. By converting Solve results into functions I could use Manipulate in a Plot. Something like
And you get a plot where you can manipulate parameter b
Here's a simple solution that could be cleaned up
The following function will automatically convert the output of
Solve
to a list of functions (assumingSolve
finds solutions of course):Here is an example:
The functions can be called individually:
Or, all of the functions can be called at once to return all solutions:
The function will fail if
Solve
cannot find any solutions:Variables are automatically identified: