Suppose, I want to solve a equation and want to obtain it's first root. In this case I've used

In[1]=:Solve[b^2 + b*z + 1 == 0, b][[1]]

This gives

Out[2]={b -> 1/2 (-z - Sqrt[-4 + z^2])}

Now I want use this result directly in Plot command. My code for this is

In[3]:Plot[Out[2], {z, -1000, 1000}, PlotRange -> All]

But I'm not getting the plot. If I use

In[4]:Plot[1/2 (-z - Sqrt[-4 + z^2]), {z, -1000, 1000}, PlotRange -> All]

I'm getting the plot correctly. I guess in In[3], Out[2] contains b->, that's why it's unable to plot. How to handle this situation?

p.s. This is a prototype of my actual problem. I want to solve this in this way because my output is too large to copy and paste in the Plot command.

  • 2
    $\begingroup$ Try Plot[%[[1, 2]], {z, -1000, 1000}, PlotRange -> All]! Taking second part of the Rule is the trick. You can also use Plot[Out[1][[1, 2]], {z, -1000, 1000}, PlotRange -> All]. $\endgroup$ – PlatoManiac May 28 '13 at 8:34
  • $\begingroup$ @PlatoManiac. Thanks for suggestion. $\endgroup$ – Biswajit May 28 '13 at 10:46
  • 1
    $\begingroup$ @PlatoManiac I would strongly advise against this suggestion (i.e., using % or Out). It makes repeating the results when you re-run your notebook dependent on the order of execution. When you are going to use the results of a calculation assign a variable to that result. $\endgroup$ – Sjoerd C. de Vries May 28 '13 at 15:24
  • $\begingroup$ @SjoerdC.deVries Thx for pointing out the pitfall with Out or %. I though assumed the OP will understand this ordering issue himself while playing with the notebook and if not through the answers below. $\endgroup$ – PlatoManiac May 28 '13 at 20:40

One way to do this is to define the solution as

sol = Solve[b^2 + b*z + 1 == 0, b][[1]];

and then plot using the strategy suggested in the Help file for Solve, which replaces the variable in the rule (in this case b) with the desired solution:

Plot[b /. sol, {z, -10, 10}, PlotRange -> All]
| improve this answer | |
  • $\begingroup$ Thanks a lot @bill s. Now if I've a quartic equation in 'b', and want to plot all four the roots together what will be the syntax in that case? $\endgroup$ – Biswajit May 28 '13 at 9:56
  • $\begingroup$ I think I've got it. 'Plot[b/.{sol1,sol2,sol3,sol4},.....]' It's working for me. $\endgroup$ – Biswajit May 28 '13 at 10:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.