How to get data points from plot?

  • Context: Mathematica 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Data Plot Points
Click For Summary

Discussion Overview

The discussion revolves around extracting data points from a plot generated by NDSolve in Mathematica for use in LaTeX, specifically with TikZ. Participants explore methods for obtaining these data points and share their experiences with the process.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks assistance in plotting a solution from NDSolve in LaTeX using TikZ.
  • Another suggests taking a screen grab of the plot as a simpler alternative to embedding data points.
  • A participant explains that the output of NDSolve is an InterpolatingFunction object and provides a method to extract interpolation points using "f[[2]]".
  • Further, a participant shares code for evaluating the interpolated function at specific points, suggesting two different approaches using Table functions.
  • Some participants express challenges with formatting Mathematica's output for LaTeX, noting that it can be cumbersome.
  • One participant acknowledges that their initial approach was more complex than they remembered, preferring the method suggested by another participant.

Areas of Agreement / Disagreement

Participants share various methods for extracting data points, but there is no consensus on a single best approach. Some prefer direct extraction methods while others suggest alternative solutions like screen grabs.

Contextual Notes

Participants mention encountering error messages when attempting to execute certain code, indicating potential issues with their implementation or understanding of the output structure from NDSolve.

member 428835
Hi PF!

I used NDSolve to find the solution to a differential equation. I then plotted the solution in mathematica. However, I would like to be able to plot this in LaTex, specifically in TikZ. Can anyone help me here?

Thanks so much!
 
Physics news on Phys.org
It is usually better to get a screen grab of your plot then embed in your LaTeX document as a graphic.
 
joshmccraney said:
Hi PF!

I used NDSolve to find the solution to a differential equation. I then plotted the solution in mathematica. However, I would like to be able to plot this in LaTex, specifically in TikZ. Can anyone help me here?

Thanks so much!
The output of NDSolve is an InterpolatingFunction object. If the InterpolatingFunction object is named "f" then "f[[2]]" will produce a table containing the interpolation points.
 
Dale said:
The output of NDSolve is an InterpolatingFunction object. If the InterpolatingFunction object is named "f" then "f[[2]]" will produce a table containing the interpolation points.
Hi Dale!

I have the following code
Code:
a=0;
s = NDSolve[{y[x] y''[x] + 2 (y'[x])^2 + x y'[x] - 2 a/(1 + a) y[x] ==
      0, y[1] == .0000000000001, y'[1] == -1/2}, y, {x, 0, 3/2}];
s[[2]]
but then I receive the response "Part 2 of..." followed by a long error message. Am I doing something wrong?

Thanks so much for your help!
 
You could simply evaluate the interpolated function at the desired points:

Code:
Table[{x, Evaluate[y[x] /. s][[1]]}, {x, 0, 3/2, stepsize}]
or
Code:
Table[{x, s[[1, 1, 2]][x]}, {x, 0, 3/2, stepsize}]

The interpolating function itself is nested within [[1,1,2]] of the NDSolve output.
 
  • Like
Likes   Reactions: Dale and member 428835
Very nice Fightfish! Inputting these into latex will be the worst, but this is a good way to get it done! Thanks!
 
Actually, it took no time at all! Once copy-pasting mathematica's output, I used the "replace and find" search key and replaced "}" with ")" and the commas after the parenthesis with "to" etc. Thanks a ton!
 
joshmccraney said:
then I receive the response "Part 2 of..." followed by a long error message. Am I doing something wrong?

Thanks so much for your help!
I like Fightfish's approach better. It turns out that my approach is not as simple as I has remembered.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K