Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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.

share|improve this question

marked as duplicate by Ajasja, Artes, m_goldberg, Michael E2, Mr.Wizard May 29 '13 at 1:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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]. – PlatoManiac May 28 '13 at 8:34
@PlatoManiac. Thanks for suggestion. – Biswajit May 28 '13 at 10:46
@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. – Sjoerd C. de Vries May 28 '13 at 15:24
@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. – PlatoManiac May 28 '13 at 20:40
up vote 5 down vote accepted

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]
share|improve this answer
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? – Biswajit May 28 '13 at 9:56
I think I've got it. 'Plot[b/.{sol1,sol2,sol3,sol4},.....]' It's working for me. – Biswajit May 28 '13 at 10:45

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