Message Boards Message Boards

[?] Separate out the real and imaginary part and export data?

I have an Mathematica code which 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.

Thanks in advance

POSTED BY: Sreeraj T
6 Replies

Hey Sreeraj,

which version of Mathematica are you using? ReIm is a function the was introduced in 10.1 and I always forget to mention that my code runs on the latest version 11.1. In this case, the fix is easy, define the ReIm function yourself

ReIm[x_?NumericQ] := {Re[x], Im[x]};
ReIm[x_] := x;

As for your other question, this can easily be done, but you have to remember, that your root might be a complex number. Let's say you want to plot your k against the real part of the first root, you can use:

data = Table[Flatten@{k, roots}, {k, 0.01, 1, .01}];
ListPlot[Re@data[[All, {1, 2}]]]

Mathematica graphics

POSTED BY: Patrick Scheibe
POSTED BY: Sreeraj T

It means: take All values from the 1st and 2nd column of data and calculate their Real part. Since your k are real anyway it doesn't matter and from the first roots (in the 2nd column) all real parts are taken. This is then plotted, which gives you a graph of k against the real-part of the first root.

POSTED BY: Patrick Scheibe

Thanks a lot Patrick Scheibe. And sorry for delay in replying...

POSTED BY: Sreeraj T

From an analytical point of view, you should really consider solving your equation analytically. This can be done by

    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 = Rationalize[600/(w - 0.01)^2 + (400000 (w - 0.01)^2)/(k^2*0.125 ((w - 0.01)^2 - 0.25))];
    roots = w /. Solve[first - second - third == 0, w];

If you look at roots you see that it contains Root objects with unspecified k. This is perfectly fine, since we can evaluate them by plugging in values for k inside your Table. Since you are interested in a separation of real and imaginary part, we can do this directly in the table by using ReIm. Flattening out the result, gives you exactly what you want to export as one line: the k in the front and then all roots.

data = Table[Flatten@{k, ReIm /@ roots}, {k, 0.01, 1, .01}];
Export["tmp/roots.dat", data, "Table"]

That should be it.

POSTED BY: Patrick Scheibe

Thanks for putting efforts to solve my problem.

I would like to point out that in the exported file there are terms like "ReIm[-1.1653855769993797]". How to get rid of "ReIm[]" and print only "-1.1653855769993797".

On a different topic, I would like to ask one more question . If you replace the code "data = Table[Flatten@{k, ReIm /@ roots}, {k, 0.01, 1, .01}];" with "data = Table[Flatten@{k, roots}, {k, 0.01, 1, .01}]" you can see the whole data listed out. How can i plot a graph with k along x axis and only one root, say first one, in y axis?

Thanks in advance...

POSTED BY: Sreeraj T
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract