Question

Mathematica has a bevy of useful functions (Solve, NDSolve, etc.). These functions output in a very strange manner, ie {{v -> 2.05334*10^-7}}. The major issue is that there does not appear to be any way to use the output of these functions in the program; that is to say all of these appear to be terminal functions where the output is for human viewing only.

I have tired multiple methods (Part, /., etc.) to try to get the output of functions into variables so the program can use them for further steps, but nothing works. The documentation says it can be done but nothing they list actually functions. For example, if I try to use /. to move variables, it continues to treat the variable I assigned to as empty and does symbolic math with it instead of seeing the value. If I try to access the variable ie [[1]], it says the variable is not that deep.

The only method I have found is to put the later steps in separate blocks and copy-paste the output to continue evaluation. Is there any way to get the output of these functions into variables programmatically?

Was it helpful?

Solution

Solve etc. produce a list of replacement rules. So you need to apply these rules to the pattern to be replaced. For instance

solutions = x /. Solve[x^2 == 3, x]

gives you all the solutions in a list.

Here is a quick way to get variable names for the solutions:

x1 = solutions[[1]]
x2 = solutions[[2]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top