I am solving following differential equation using Mathematica,

s = NDSolve[{u''[x] == (42/10)*Sin[x]*Sin[x]*u[x], u[-3] == 0, 
   u'[3] == 1}, u, {x, -3, 3}, WorkingPrecision -> 22, 
  InterpolationOrder -> All]
Plot[Evaluate[{u[x] /. s}, {x, -3, 3}], AxesLabel -> {"x", "v(x)"}, 
 PlotStyle -> {Thickness[0.005]}, PlotRange -> All]

I would like to plot this output with gnuplot (as all of the other figure in my report are generated using gnuplot) to be consistent. How can I generate file with $x$ and $u(x)$ values written in 2 columns?

I tried to write output in a file,

  sW = OpenWrite["out.dat", FormatType -> TraditionalForm];
    $Output = {sW};
    Write[sW, x, Evaluate[u[x] /. s], {x, -3, 3, 1}]

But this does not print values of $u(x)$

  • $\begingroup$ You can grab the actual points used by Plot with Cases[plot, _Line, Infinity][[1, 1]]. BTW, not part of your question, but you should use Export instead of raw Open/Write operations. $\endgroup$
    – george2079
    Sep 11, 2014 at 17:46
  • $\begingroup$ Have you seen this question? $\endgroup$ Sep 11, 2014 at 21:35

1 Answer 1


After solving your DE, do Table[{x, u[x]} /. %, {x, -3, 3, 0.01}] and save the output to a file "u.dat", which should make it a double column table.


Your Answer

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

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