Group Abstract Group Abstract

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
POSTED BY: Patrick Scheibe

Patrick Scheibe, Thanks a lot. This was indeed what I wanted and I am using Mathematica 9. By the by, i have a doubt in {1,2} in ListPlot[Re@data[[All, {1, 2}]]]. What does this actually represent?

Thanks in advance

POSTED BY: Sreeraj T
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