# Creating a range of solutions using NSolve [closed]

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.

• The roots are (x /. NSolve[f[#, 3, 2, 2] == 0 && Abs[x] < 3, x]) & /@ Range – 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[[k]] // Evaluate,
{b, -1, 1},      (* increase range to ±10 if desired *)
PlotStyle -> {ColorData[k], Directive[ColorData[k], Dashed]}],
{k, 4}];
GraphicsGrid@Partition[plots, 2] Show[plots, PlotRange -> All] • 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} does not exist – Aschoolar Jun 16 '17 at 0:28
• @Aschoolar Try ColorData instead of ColorData. 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.) – Michael E2 Jun 16 '17 at 1:05
• If that doesn't fix point 2), perhaps you redefined roots. – Michael E2 Jun 16 '17 at 1:11
• (I just noticed I remembered ReIm was new, but for about ColorData. ) – Michael E2 Jun 16 '17 at 1:13
• ColorData solved the problem and I will keep in mind about version used every time I post a question. – Aschoolar Jun 16 '17 at 22:30

Here is my solution:

f[a_, b_] := x^a + x^2 - b;
xx=Range[0,4,1];
yy=Re[Table[x/.NSolve[f[4,i]==0 && Abs[x]<10,x],{i,0,4,1}]]
data=TemporalData[yy,{xx}];
ListLinePlot[data,BaseStyle->PointSize[0.02],PlotRange->{-2,2}]


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