# Separate out the real and imaginary part and exporting data

I have an Mathematica code whoch goes like this:

ClearAll["Global*"]
first = 1 + 17000000/k^2 + 5000000/k^2 + 868670/k^2;
second = 14000/w^2 + (5000000 w^2)/(k^2*(w^2 - 1));
third = 600/(w - 0.01)^2 + (400000 (w - 0.01)^2)/(k^2*0.125 ((w - 0.01)^2 - 0.25));
(*First way*)
hello = Table[{k, NSolve[first - second - third == 0, w]}, {k, 0.01,1, 0.01}]
(*Second way*)
eqn = NSolve[first - second - third == 0, w];
hello = Table[{k, eqn}, {k, 0.01, 1, 0.01}]
(*Third way*)
rt := (r = Solve[ first - second - third == 0, w];
s = Evaluate[w /. r];
Return[s])
Table[{k, rt}, {k, 0.01, 1, 0.01}]


I have some question in this regard:

1. From an analytical point of view you can see that for a single value of k, 8 different omegas are posibble. For k=0.01, first way, second way and third way are giving 8 omegas. But at some places, for e.g 0.7, first way gives you 4 roots, on the other hand second way and third way gives 8 roots. I have to tell you that 4 roots which are given by first way are still included in second and third way. Why/ What is this happening?
2. I want to export this to a dat file, with first colum with k, next column with real value of first w, next with imaginary of first w, next with real of second omega, etc... How can I do that? Simple export is not helping me.

By the by, I am using Mathematica 9.

I have to tell you that 4 roots which are given by first way are still included in second and third way. Why/ What is this happening?

You can fix it by increasing WorkingPrecision.

Block[{k = 0.7}, w /. NSolve[first - second - third == 0, w,
WorkingPrecision -> 10]] // Length


4

Block[{k = 0.7}, w /. NSolve[first - second - third == 0, w,
WorkingPrecision -> 20]] // Length


8

As @Jenny_mathy pointed out, to get the values of the solution you need to use

w /. Solve[first - second - third == 0, w]


I want to export this to a dat file,... second omega, etc... How can I do that?

ClearAll["Global*"]
first = 1 + 17000000/k^2 + 5000000/k^2 + 868670/k^2;
second = 14000/w^2 + (5000000 w^2)/(k^2*(w^2 - 1));
third = 600/(w - 0.01)^2 + (400000 (w - 0.01)^2)/(k^2*0.125 ((w - 0.01)^2 - 0.25));

data = Table[Flatten[{k, {Re[#], Im[#]} & /@ (w /. Solve[first - second - third == 0, w])}]
, {k, 0.1, 0.2, 0.1}]

Export["data.dat",data]

• Thanks @Sumit it worked – sreeraj t Sep 25 '17 at 3:40

ClearAll["Global*"]
first = 1 + 17000000/k^2 + 5000000/k^2 + 868670/k^2;
second = 14000/w^2 + (5000000 w^2)/(k^2*(w^2 - 1));
third = 600/(w -
0.01)^2 + (400000 (w - 0.01)^2)/(k^2*0.125 ((w - 0.01)^2 -
0.25));
(*First way*)
hello = Table[{k, w /. NSolve[first - second - third == 0, w]}, {k,
0.01, 1, 0.01}];
S = Last /@ hello;
R = Column[
Flatten /@Table[{First@hello[[i]], Riffle[Re@S[[i]], Im@S[[i]]]},  {i,Length@hello}]]
Export["file.dat", R]
`

I only added the last lines note that you must type "w/." before NSolve

the result is this (the first few rows)

{0.01, 1.1657148595160352, 0, -1.16538557699938, 0, 0.5454625611896099, 0, -0.5257918443805711, 0, 0.010051236799639747, 0, 0.009948762867236649, 0, 0.000247421814019949, 0, -0.000247420806590309, 0}
{0.02, 1.1657148945451121, 0, -1.1653856118992674, 0, 0.5454625839970997, 0, -0.5257918693401715, 0, 0.010102565649496185, 0, 0.00989742913842765, 0, 0.0004948661947012897, 0, -0.0004948582853979998, 0}
{0.03, 0.0007423593902444178, 0, -0.0007423268959720817, 0}
{0.04, 0.0009899333367360303, 0, -0.0009898384178381049, 0}
{0.05, 0.0012376268986364573, 0, -0.0012374023948149274, 0}
{0.060000000000000005, 0.0014854874198802967, 0, -0.0014850265020769791, 0}
{0.06999999999999999, 0.0017335726782780096, 0, -0.001732716850543624, 0}
{0.08, 0.001981953369266321, 0, -0.001980478240165601, 0}
{0.09, 0.0022307162928971442, 0, -0.002228314371346828, 0}

and it is exported to file.dat

• Thanks @Jenny_mathy for your answer. the exported dat file contains the braces like these{. How to get rid of this? – sreeraj t May 11 '17 at 11:50