How to get data points from plot?

  • Mathematica
  • Thread starter joshmccraney
  • Start date
  • #1
joshmccraney
Gold Member
2,234
139
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!
 

Answers and Replies

  • #2
Simon Bridge
Science Advisor
Homework Helper
17,874
1,657
It is usually better to get a screen grab of your plot then embed in your LaTeX document as a graphic.
 
  • #3
Dale
Mentor
Insights Author
2021 Award
33,458
10,923
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.
 
  • #4
joshmccraney
Gold Member
2,234
139
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!
 
  • #5
Fightfish
954
117
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 Dale and joshmccraney
  • #6
joshmccraney
Gold Member
2,234
139
Very nice Fightfish! Inputting these into latex will be the worst, but this is a good way to get it done! Thanks!
 
  • #7
joshmccraney
Gold Member
2,234
139
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!
 
  • #8
Dale
Mentor
Insights Author
2021 Award
33,458
10,923
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.
 

Suggested for: How to get data points from plot?

  • Last Post
Replies
1
Views
304
  • Last Post
Replies
2
Views
513
Replies
1
Views
314
  • Last Post
Replies
8
Views
447
Replies
0
Views
265
Replies
6
Views
105
Replies
2
Views
1K
Replies
1
Views
1K
  • Last Post
Replies
2
Views
338
Replies
0
Views
588
Top