I am using NSolve to find roots of complex equation. Typical code is in the form of

f[a_, b_] := x^a + x^2 - b ;
NSolve[f[4, 3] == 0 && Abs[x] < 10, x]

The above code finds say four roots of the equation. How do I create set of roots for range of b = 1 to 4 and plot line x1 vs i, x2 vs i, x3 vs i etc.

I used

f[a_, b_] := x^a + x^2 - b;
DiscretePlot[x/.NSolve[f[4, i] == 0 && Abs[x] < 10, x], {i, 0, 4, 1}]

It does the plot I want to see, but I have a problem assigning colour for each x root or (x1, x2, x3, x4). Note that I am assuming my complex function always gives four roots.


closed as unclear what you're asking by m_goldberg, MarcoB, garej, LCarvalho, yohbs Jun 17 '17 at 4:46

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ The roots are (x /. NSolve[f[#, 3, 2, 2] == 0 && Abs[x] < 3, x]) & /@ Range[4] $\endgroup$ – Bob Hanlon Jun 11 '17 at 2:25

If the "typical" f is a polynomial, then you can use Root instead of NSolve.

ClearAll[f, roots, a, b, x];
f[x_, a_, b_] := x^a + x^2 - b;   (* need the polynomial to be a _function_ of x *)
roots[a_Integer?Positive] := Table[Root[f[#, a, b] &, k], {k, a}];

I'll plot the imaginary parts in a Dashed style.

(* SetAttributes[ReIm, Listable];   (* uncomment for V9 & earlier *)
   ReIm[z_] := {Re[z], Im[z]};  *)
plots = Table[
   Plot[ReIm@roots[4][[k]] // Evaluate,
    {b, -1, 1},      (* increase range to ±10 if desired *)
    PlotStyle -> {ColorData[97][k], Directive[ColorData[97][k], Dashed]}],
   {k, 4}];
GraphicsGrid@Partition[plots, 2]

Mathematica graphics

Show[plots, PlotRange -> All]

Mathematica graphics

  • $\begingroup$ I am getting 2 error messages in Mathematica v9. 1) ColorData::notent: 97 is no known entity, class or tag for ColorData. User ColorData[] for a list of entities. 2) Part::partw Part 2 of {-1,0,0,1}[4] does not exist $\endgroup$ – Aschoolar Jun 16 '17 at 0:28
  • 1
    $\begingroup$ @Aschoolar Try ColorData[1] instead of ColorData[97]. With V10, WRI changed the color scheme for plots. (There were many changes with V10. You might want to mention you're using V9 in your questions, and folks will probably try to keep to V9-compatible solutions, as best as they can remember.) $\endgroup$ – Michael E2 Jun 16 '17 at 1:05
  • 1
    $\begingroup$ If that doesn't fix point 2), perhaps you redefined roots. $\endgroup$ – Michael E2 Jun 16 '17 at 1:11
  • 1
    $\begingroup$ (I just noticed I remembered ReIm was new, but for about ColorData[97]. ) $\endgroup$ – Michael E2 Jun 16 '17 at 1:13
  • $\begingroup$ ColorData[1] solved the problem and I will keep in mind about version used every time I post a question. $\endgroup$ – Aschoolar Jun 16 '17 at 22:30

Here is my solution:

f[a_, b_] := x^a + x^2 - b;
yy=Re[Table[x/.NSolve[f[4,i]==0 && Abs[x]<10,x],{i,0,4,1}]]

It plots only real part. For imaginary switch from Re to Im.


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