WOLFRAM

How do I use the output of functions like Solve?

Solve and other functions such as FindInstance, NSolve, and NDSolve return a list of rules. In the Wolfram Language, rules associate symbols with values. For example, consider the output of:

roots = Solve[x^2 + 4 x - 1 == 0, x]
{{x -> -2 - Sqrt[5]}, {x -> -2 + Sqrt[5]}}

The output is a list of lists of rules. Each sublist represents a possible value of x. To look at the first solution as a rule, we can run:

roots[[1]]
{x -> -2 - Sqrt[5]}

The symbol is x, and its value is - 2 - Sqrt[5]. The ReplaceAll command ( /. ) replaces every instance of the symbol with a value according to a rule. For example, if we want to replace all instances of x in the polynomial x^2 + 4 x - 1 with the first root of the polynomial above, we could write:

x^2 + 4 x - 1 /. roots[[1]]

This results in an expression that is equal to zero.

If we want to get the second root of the polynomial and assign it to a new variable called secondRoot, we could evaluate:

secondRoot = x /. roots[[2]]

Please see the tutorial on using rules for more examples and information on how to use them.

For some functions, such as NDSolve, an equivalent “Value” function exists (e.g., NDSolveValue ) that returns a value or function instead of rules.

Is this article helpful?
Yes
No

Any comments?

Thank you for your feedback.

Submit

Contact Support

Whether you have a question about billing, activation or something more technical, we are ready to help you.

1-800-WOLFRAM (+1-217-398-0700 for international callers)

Customer Support

Monday–Friday
8am–5pm US Central Time

  • Product registration or activation
  • Pre-sales information and ordering
  • Help with installation and first launch

Advanced Technical Support (for eligible customers)

Monday–Thursday
8am–5pm US Central Time

Friday
8:30–10am & 11am–5pm US Central Time

  • Priority technical support
  • Product assistance from Wolfram experts
  • Help with Wolfram Language programming
  • Advanced installation support