0

I need to extract the data from plots created inside the module environment to re draw my plots in a better plotter that Mathematica.

I have a code that includes several matrix multiplications, and works with data that i introduce every time i need to run the function created in the module environment. So, i already have this graphs thanks to the Print option, but i can't extract the coordinates from this plots inside that environment with the answers given in the forums.

Code:

grafi[inf_,sup_,tem_] := Module[{x, m1, m2, matri, elem22, co},
  m1[x_] = ( {{x, 100},{10, tem}} );
  m2[x_] = ( {{200, 1},{tem, x}} );
  matri[x_] = m2[x].m1[x].m2[x];

  elem22[x_] := matri[x][[2, 2]] ;
  co[x_] := 1/Abs[elem22[x]]^2;

  Print[Plot[co[x], {x, inf, sup}]]

  ]

If later I call the function grafi like this:

grafi[10,10000,50]

I obtain the plot i want. But i can't extract the data from here. Help! Thanks.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Ah77
  • 1
  • 3
  • 5
    Please provide a minimal example to work with and describe what exactly do you need at the end. What about 94481? – Kuba Sep 08 '17 at 09:14
  • 1
    How about not just printing them, but placing them explicitly in the output? e.g. Module[{vars}, calculations; Print[plots]; {originaloutput, plot1, plot2, plot3}]? Then manipulate the output as you please. – LLlAMnYP Sep 08 '17 at 11:37
  • Hey @LLlAMnYP, i think i had that idea in the past but i couldn't get the data for some reason. Would you be more specific so i can try it again in this way? Thanks. – Ah77 Sep 09 '17 at 19:41
  • Use . instead of * to multiply matrices; see this. Also, remove the Print[] as it does not make the plot easily usable by other functions. Thus: grafi[inf_, sup_, opts___] := Plot[1/Abs[({{200, 1}, {x, x}}.{{x, 100}, {10, 50}}.{{200, 1}, {x, x}})[[2, 2]]]^2 // Evaluate, {x, inf, sup}, opts]; grafi[10, 1*^4] – J. M.'s missing motivation Sep 09 '17 at 22:27
  • "I can't extract the coordinates from this plot" - again, the Print[] is the problem there; remove it so that you can do something like Cases[plot, Line[l_] :> l, ∞]. – J. M.'s missing motivation Sep 11 '17 at 08:16

1 Answers1

1

You can simplify your function a bit since Print is not needed:

grafi[inf_, sup_, tem_] := 
 Module[{x, m1, m2, matri, elem22, co}, 
  m1[x_] = ({{x, 100}, {10, tem}});
  m2[x_] = ({{200, 1}, {tem, x}});
  matri[x_] = m2[x].m1[x].m2[x];
  elem22[x_] := matri[x][[2, 2]];
  co[x_] := 1/Abs[elem22[x]]^2;
  Plot[co[x], {x, inf, sup}]]

and then this

pl = grafi[10, 10000, 50]

gives you a plot

enter image description here

Now the list of points to be plotted have the tree coordinates {1, 1, 1, 3, 1, 2, 1}. Thus, this

lst = pl[[1, 1, 1, 3, 1, 2, 1]]

gives you the desired list of points. Let us check it:

PlotList[lst]

enter image description here

Done, have fun!

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96