1
$\begingroup$

In Maple, I can get data from a plot3d like this,

restart;
Eq1:=diff(u(eta,t),t)=diff(u(eta,t),eta$2);
BCs := {u(0,t)=sin(t), u(10,t)=0};
ICs := {u(eta,0)=0};
sys:={Eq1};
pds:= pdsolve(sys,ICs union BCs,numeric,time=t,range=0..10);
p1:= pds:-plot3d(u(eta,t),t=0..35,eta=0..10,grid=[360,360]):

Here is the command for exporting data from plot p1

A:= op([1,3],p1):
  a,b:= op(op([1,1],p1)):
  c,d:= op(op([1,2],p1)):
  m:= rhs(rtable_dims(A)[1]):
  n:= rhs(rtable_dims(A)[2]):
  for i from 1 to m do
   for j from 1 to n do
     X:= ((m-i)*a + (i-1)*b)/(m-1);
     Y:= ((n-j)*c + (j-1)*d)/(n-1);
     fprintf("C:/tcdata/SL1.txt",
         "%f  %f  %f\n",Y,X,A[i,j]);
   end do
  end do:
  close("C:/tcdata/SL1.txt"):

This gives me data in the desired format, which I can later use in another software. The format is

0.000000  0.000000  0.000000
0.097493  0.000000  0.102694
0.194986  0.000000  0.200925
0.292479  0.000000  0.294693
0.389972  0.000000  0.383998
0.487465  0.000000  0.468841
0.584958  0.000000  0.549221
0.682451  0.000000  0.625138
0.779944  0.000000  0.696593
0.877437  0.000000  0.763584
..............

How can I get data from Plot3D in the same format (see above) in Mathematica?

ClearAll["Global`*"];

pdeset = {Derivative[1, 0][U][t, x] == Derivative[0, 2][U][t, x]}

ics = {U[0, x] == 0};

bcs = {U[t, 0] == Sin[t], U[t, 10] == 0};    

bcAll = Flatten[{ics, bcs}, 1];    

sol = NDSolve[{pdeset, bcAll}, {U}, {t, 0, 35}, {x, 0, 10}];

Plot3D[(U /. First@sol)[t, x],  {t, 0, 35}, {x, 0, 10},
 PlotRange -> All]
$\endgroup$
4
  • $\begingroup$ It isn't clear to me whether you want to export a table of values from Mathematica (as you did in your Maple code) or a 3D plot as an image. Please clarify. $\endgroup$
    – m_goldberg
    Commented Feb 11, 2018 at 7:04
  • $\begingroup$ @m_goldberg I want to export data values from Plot3D in Mathematica as I did in Maple. $\endgroup$
    – zhk
    Commented Feb 11, 2018 at 7:28
  • $\begingroup$ Export["data.txt", output, "Table"]; $\endgroup$
    – mrz
    Commented Feb 11, 2018 at 7:37
  • $\begingroup$ @mrz What exactly we are exporting here? $\endgroup$
    – zhk
    Commented Feb 11, 2018 at 7:38

2 Answers 2

6
$\begingroup$

No need to extract the points from the plot as you have the generating function already, unless this is exactly what you want

Export["pts-plot.dat", Plot3D[(U /. First@sol)[t, x], {t, 0, 35}, {x, 0, 10}, 
PlotRange -> All][[1, 1]], "Table"]

Otherwise, use Table or its parallel version to obtain as many points as you want:

pts = Flatten[ParallelTable[{t, x, (U /. First@sol)[t, x]}, {t, 0, 35, 0.5}, {x,0, 10, 0.5}], 1]:
Export["pts.dat", pts, "Table"]
$\endgroup$
3
  • $\begingroup$ Thanks, I learned "indexing" the Plot-object to get the points. tricky! $\endgroup$ Commented Feb 11, 2018 at 14:50
  • $\begingroup$ @UlrichNeumann Yes, very useful to access different features of Plot-object $\endgroup$ Commented Feb 11, 2018 at 15:35
  • $\begingroup$ Both approaches worked just fine for me. The first one is what I was looking for. Thx $\endgroup$
    – zhk
    Commented Feb 12, 2018 at 4:55
3
$\begingroup$

If you name your Plot

pic=Plot3D[(U /. First@sol)[t, x],  {t, 0, 35}, {x, 0, 10},PlotRange -> All]

you can pick the plotted points with

p=Flatten[Cases[bild, _GraphicsComplex , Infinity][[;; , 1]], 1]
Show[pic, Graphics3D[Point[p]]]

enter image description here

Export["data.txt", p, "Table"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.