How to Plot a 3D Surface from Numerical Data in Mathematica?

AI Thread Summary
To plot a 3D surface from numerical data in Mathematica, users can define a function implicitly by setting f(x,y,z)=c, where c is a constant. The discussion highlights the importance of interpolating the data points to create a smooth surface, as raw plotting of discrete points may yield unsatisfactory results. Users are encouraged to collect data in pairs and perform interpolation across multiple planes to improve visualization. A sample code snippet demonstrates how to read and plot the data, while addressing potential issues with data formatting in Mathematica. Overall, the conversation emphasizes the need for careful data handling and interpolation to achieve a professional-looking 3D plot.
Telemachus
Messages
820
Reaction score
30
Hi there. I wanted to plot a surface, given implicitly by a function ##f(x,y,z)=c##, with ##c## a constant. My ##f(x,y,z)## is obtained numerically, so I don't know the expression explicitly, I have the values of ##f(x_i,y_j,z_k)## for given integer numbers ##(i,j,k)##, ##i=1,2,3...## same for ##j## and ##k##. And the discrete variables are ##x_i=(i-1)\Delta x## and similarly for ##y_j,z_k##.

So, in principle I can obtain, numerically a set of data for which ##f(x_i,y_j,z_k)=c##, where the ##x_i,y_j,z_k## are stored, and I know c (I just choose the value I want). And now I know that for those values, the magnitude of ##f(x,y,z)=c##, so this defines a 3D surface. Now, how can I plot this? it should be clear at this point that f might not define explicitly a function, it might not be bijective, it is likely to be a closed surface, like a spherical surface in 3D.
 
Technology news on Phys.org
can you use Matlab?
 
  • Like
Likes Telemachus
Yes, I can. But after thinking of it a bit, I think I should just plot my set of points, right? the obtained ##(x_i,y_j,z_k)## are the ones that defines my surface. Right?
 
Last edited:
Actually, you can do that, but I don't think it's a "professional" way. If you do it, the figure you obtained will be a lot of dots :biggrin:
How about interpolating and approximating that function, then use MATLAB to plot it? :wink:
 
  • Like
Likes Telemachus
Yes, that's a good idea. Is there a way to do it in MATLAB automatically? a way in which it plots the points, and makes the surface smooth by interpolation?
 
As I know, MATLAB can't do it automatically, we must code an interpolation program to interpolate and approximate that function :frown:
 
  • Like
Likes Telemachus
Ok. So here I have a set of data which I want to plot. I have also attached a plot of the data:

?temp_hash=fe4d2b10be01ef0dbe351705d9a7e895.png


So, as there are not many points, I really have to use some interpolation to get a descent picture. However, I don't think that even after the interpolation I will be able to plot it as a surface, I haven't found how to do it, but maybe if I have more points after interpolation, I can make it look more descent.

I was thinking to take pairs of points: (x,y), (x,z), (y,z) and make an interpolation for each pair. After that I can collect the whole bunch of data intro a single file back again, and plot it just like that. The idea is to have interpolations over the three planes.
 

Attachments

  • sur1001.txt
    sur1001.txt
    5.4 KB · Views: 490
  • surface.png
    surface.png
    5.1 KB · Views: 417
  • ?temp_hash=fe4d2b10be01ef0dbe351705d9a7e895.png
    ?temp_hash=fe4d2b10be01ef0dbe351705d9a7e895.png
    5.1 KB · Views: 435
Very easy in Mathematica. Here's the code to collect points {x,y,sin(x y)} in range (-2,2) and then I superimpose the actual plot (red) against the interpolation (blue):
Code:
p1 = Plot3D[Sin[x y], {x, -2, 2}, {y, -2, 2}, PlotStyle -> Red];
mySinTable =
  Flatten[Table[{x, y, Sin[x y]}, {x, -2, 2, 0.1}, {y, -1, 1, 0.1}],
   1];
mySinInterpolation = Interpolation[mySinTable];
p2 = Plot3D[mySinInterpolation[x , y], {x, -2, 2}, {y, -2, 2},
   PlotStyle -> Blue];
Show[{p1, p2}]
interpolation of sin.png
 

Attachments

  • interpolation of sin.png
    interpolation of sin.png
    23.7 KB · Views: 546
  • Like
Likes Telemachus
Can you do that with the data I have provided? I am wondering if the fact that this data isn't a function ##z=f(x,y)## (not bijective) generates any issue in mathematica. If possible the interpolation has to be done in many directions. If you, for example, only interpolate along the fixed z curves, it will look poorly along the z axis, and the same applies for the other planes.

Thanks!
 
  • #10
Telemachus said:
Can you do that with the data I have provided? I am wondering if the fact that this data isn't a function ##z=f(x,y)## (not bijective) generates any issue in mathematica. If possible the interpolation has to be done in many directions. If you, for example, only interpolate along the fixed z curves, it will look poorly along the z axis, and the same applies for the other planes.

Thanks!

Post the data as a table {{x1,y1,z1},...,{xn,yn,zn}} and I'll try to run it through the code above and see what happens.
 
  • Like
Likes Telemachus
  • #11
Is it okey like this?

0.44736842105263153 0.73684210526315785 0.28947368421052633
0.47368421052631576 0.73684210526315785 0.28947368421052633
0.50000000000000000 0.73684210526315785 0.28947368421052633
0.52631578947368418 0.73684210526315785 0.28947368421052633
0.55263157894736836 0.73684210526315785 0.28947368421052633
0.47368421052631576 0.76315789473684204 0.28947368421052633
0.50000000000000000 0.76315789473684204 0.28947368421052633
0.52631578947368418 0.76315789473684204 0.28947368421052633
0.44736842105263153 0.68421052631578938 0.31578947368421051
0.55263157894736836 0.68421052631578938 0.31578947368421051
0.39473684210526316 0.78947368421052633 0.31578947368421051
0.60526315789473684 0.78947368421052633 0.31578947368421051
0.44736842105263153 0.65789473684210520 0.34210526315789469
0.55263157894736836 0.65789473684210520 0.34210526315789469
0.34210526315789469 0.71052631578947367 0.34210526315789469
0.65789473684210520 0.71052631578947367 0.34210526315789469
0.34210526315789469 0.78947368421052633 0.34210526315789469
0.65789473684210520 0.78947368421052633 0.34210526315789469
0.39473684210526316 0.81578947368421051 0.34210526315789469
0.42105263157894735 0.81578947368421051 0.34210526315789469
0.44736842105263153 0.81578947368421051 0.34210526315789469
0.55263157894736836 0.81578947368421051 0.34210526315789469
0.57894736842105265 0.81578947368421051 0.34210526315789469
0.60526315789473684 0.81578947368421051 0.34210526315789469
0.47368421052631576 0.63157894736842102 0.36842105263157893
0.50000000000000000 0.63157894736842102 0.36842105263157893
0.52631578947368418 0.63157894736842102 0.36842105263157893
0.34210526315789469 0.68421052631578938 0.36842105263157893
0.65789473684210520 0.68421052631578938 0.36842105263157893
0.31578947368421051 0.71052631578947367 0.36842105263157893
0.68421052631578938 0.71052631578947367 0.36842105263157893
0.31578947368421051 0.78947368421052633 0.36842105263157893
0.68421052631578938 0.78947368421052633 0.36842105263157893
0.42105263157894735 0.63157894736842102 0.39473684210526316
0.57894736842105265 0.63157894736842102 0.39473684210526316
0.34210526315789469 0.81578947368421051 0.39473684210526316
0.65789473684210520 0.81578947368421051 0.39473684210526316
0.34210526315789469 0.65789473684210520 0.42105263157894735
0.65789473684210520 0.65789473684210520 0.42105263157894735
0.31578947368421051 0.68421052631578938 0.42105263157894735
0.68421052631578938 0.68421052631578938 0.42105263157894735
0.44736842105263153 0.84210526315789469 0.42105263157894735
0.47368421052631576 0.84210526315789469 0.42105263157894735
0.50000000000000000 0.84210526315789469 0.42105263157894735
0.52631578947368418 0.84210526315789469 0.42105263157894735
0.55263157894736836 0.84210526315789469 0.42105263157894735
0.36842105263157893 0.63157894736842102 0.44736842105263153
0.63157894736842102 0.63157894736842102 0.44736842105263153
0.28947368421052633 0.71052631578947367 0.44736842105263153
0.71052631578947367 0.71052631578947367 0.44736842105263153
0.42105263157894735 0.84210526315789469 0.44736842105263153
0.44736842105263153 0.84210526315789469 0.44736842105263153
0.47368421052631576 0.84210526315789469 0.44736842105263153
0.50000000000000000 0.84210526315789469 0.44736842105263153
0.52631578947368418 0.84210526315789469 0.44736842105263153
0.55263157894736836 0.84210526315789469 0.44736842105263153
0.57894736842105265 0.84210526315789469 0.44736842105263153
0.47368421052631576 0.60526315789473684 0.47368421052631576
0.50000000000000000 0.60526315789473684 0.47368421052631576
0.52631578947368418 0.60526315789473684 0.47368421052631576
0.36842105263157893 0.63157894736842102 0.47368421052631576
0.63157894736842102 0.63157894736842102 0.47368421052631576
0.28947368421052633 0.71052631578947367 0.47368421052631576
0.71052631578947367 0.71052631578947367 0.47368421052631576
0.42105263157894735 0.84210526315789469 0.47368421052631576
0.44736842105263153 0.84210526315789469 0.47368421052631576
0.55263157894736836 0.84210526315789469 0.47368421052631576
0.57894736842105265 0.84210526315789469 0.47368421052631576
0.47368421052631576 0.60526315789473684 0.50000000000000000
0.50000000000000000 0.60526315789473684 0.50000000000000000
0.52631578947368418 0.60526315789473684 0.50000000000000000
0.39473684210526316 0.84210526315789469 0.50000000000000000
0.42105263157894735 0.84210526315789469 0.50000000000000000
0.57894736842105265 0.84210526315789469 0.50000000000000000
0.60526315789473684 0.84210526315789469 0.50000000000000000
0.47368421052631576 0.60526315789473684 0.52631578947368418
0.50000000000000000 0.60526315789473684 0.52631578947368418
0.52631578947368418 0.60526315789473684 0.52631578947368418
0.36842105263157893 0.63157894736842102 0.52631578947368418
0.63157894736842102 0.63157894736842102 0.52631578947368418
0.28947368421052633 0.71052631578947367 0.52631578947368418
0.71052631578947367 0.71052631578947367 0.52631578947368418
0.42105263157894735 0.84210526315789469 0.52631578947368418
0.44736842105263153 0.84210526315789469 0.52631578947368418
0.55263157894736836 0.84210526315789469 0.52631578947368418
0.57894736842105265 0.84210526315789469 0.52631578947368418
0.36842105263157893 0.63157894736842102 0.55263157894736836
0.63157894736842102 0.63157894736842102 0.55263157894736836
0.28947368421052633 0.71052631578947367 0.55263157894736836
0.71052631578947367 0.71052631578947367 0.55263157894736836
0.42105263157894735 0.84210526315789469 0.55263157894736836
0.44736842105263153 0.84210526315789469 0.55263157894736836
0.47368421052631576 0.84210526315789469 0.55263157894736836
0.50000000000000000 0.84210526315789469 0.55263157894736836
0.52631578947368418 0.84210526315789469 0.55263157894736836
0.55263157894736836 0.84210526315789469 0.55263157894736836
0.57894736842105265 0.84210526315789469 0.55263157894736836
0.34210526315789469 0.65789473684210520 0.57894736842105265
0.65789473684210520 0.65789473684210520 0.57894736842105265
0.31578947368421051 0.68421052631578938 0.57894736842105265
0.68421052631578938 0.68421052631578938 0.57894736842105265
0.44736842105263153 0.84210526315789469 0.57894736842105265
0.47368421052631576 0.84210526315789469 0.57894736842105265
0.50000000000000000 0.84210526315789469 0.57894736842105265
0.52631578947368418 0.84210526315789469 0.57894736842105265
0.55263157894736836 0.84210526315789469 0.57894736842105265
0.42105263157894735 0.63157894736842102 0.60526315789473684
0.57894736842105265 0.63157894736842102 0.60526315789473684
0.34210526315789469 0.81578947368421051 0.60526315789473684
0.65789473684210520 0.81578947368421051 0.60526315789473684
0.47368421052631576 0.63157894736842102 0.63157894736842102
0.50000000000000000 0.63157894736842102 0.63157894736842102
0.52631578947368418 0.63157894736842102 0.63157894736842102
0.34210526315789469 0.68421052631578938 0.63157894736842102
0.65789473684210520 0.68421052631578938 0.63157894736842102
0.31578947368421051 0.71052631578947367 0.63157894736842102
0.68421052631578938 0.71052631578947367 0.63157894736842102
0.31578947368421051 0.78947368421052633 0.63157894736842102
0.68421052631578938 0.78947368421052633 0.63157894736842102
0.44736842105263153 0.65789473684210520 0.65789473684210520
0.55263157894736836 0.65789473684210520 0.65789473684210520
0.34210526315789469 0.71052631578947367 0.65789473684210520
0.65789473684210520 0.71052631578947367 0.65789473684210520
0.34210526315789469 0.78947368421052633 0.65789473684210520
0.65789473684210520 0.78947368421052633 0.65789473684210520
0.39473684210526316 0.81578947368421051 0.65789473684210520
0.42105263157894735 0.81578947368421051 0.65789473684210520
0.44736842105263153 0.81578947368421051 0.65789473684210520
0.55263157894736836 0.81578947368421051 0.65789473684210520
0.57894736842105265 0.81578947368421051 0.65789473684210520
0.60526315789473684 0.81578947368421051 0.65789473684210520
0.44736842105263153 0.68421052631578938 0.68421052631578938
0.55263157894736836 0.68421052631578938 0.68421052631578938
0.39473684210526316 0.78947368421052633 0.68421052631578938
0.60526315789473684 0.78947368421052633 0.68421052631578938
0.44736842105263153 0.73684210526315785 0.71052631578947367
0.47368421052631576 0.73684210526315785 0.71052631578947367
0.50000000000000000 0.73684210526315785 0.71052631578947367
0.52631578947368418 0.73684210526315785 0.71052631578947367
0.55263157894736836 0.73684210526315785 0.71052631578947367
0.47368421052631576 0.76315789473684204 0.71052631578947367
0.50000000000000000 0.76315789473684204 0.71052631578947367
0.52631578947368418 0.76315789473684204 0.71052631578947367
 
  • #12
That will do. Just paste into myNumList as a string and (basically spherical):

Code:
myList = ReadList[StringToStream[myNumList], Record];
myDataPoints =
  ReadList[StringToStream[#], Record, RecordSeparators -> " "] & /@
   myList;
data2 = ToExpression[#] & /@ myDataPoints;
ListPointPlot3D[data2, BoxRatios -> {1, 1, 1}]
ListSurfacePlot3D[data2, BoxRatios -> {1, 1, 1}]

gives:
plotforpf.png
 

Attachments

  • plotforpf.png
    plotforpf.png
    40.2 KB · Views: 902
  • Like
Likes Telemachus
  • #13
Thanks a lot! I think I should make some interpolation anyway to get it nicer, or maybe run it with a finer grid size (which is certainly too computationally expensive). But that's exactly what I wanted. Thank you!
 
  • #14
Is that really a 3D plot?
I mean you give the x,y,z values but at each point you have a value for f... That's a 4D plot (that can be visualized with a gradient colored 3D plot)
 
  • Like
Likes Telemachus
  • #15
Yes. The plot of f(x,y,z) is four dimensional, because if you want to plot it, you would have to plot f(x,y,z) for each value of (x,y,z) But now I am fixing the value of f(x,y,z), f(x,y,z)=constant. So I have one dimension less, and what I am plotting is the (x,y,z) for which f(x,y,z)=some constant.
 
  • #16
aheight said:
That will do. Just paste into myNumList as a string and (basically spherical):

Code:
myList = ReadList[StringToStream[myNumList], Record];
myDataPoints =
  ReadList[StringToStream[#], Record, RecordSeparators -> " "] & /@
   myList;
data2 = ToExpression[#] & /@ myDataPoints;
ListPointPlot3D[data2, BoxRatios -> {1, 1, 1}]
ListSurfacePlot3D[data2, BoxRatios -> {1, 1, 1}]

gives:
View attachment 224872

Hi. I was finally trying to use this. However, I did not know how to introduce the list of numbers.

Could you exemplify with a list of a few numbers?

I get an error when I try to load the number list. Does mathematica has an issue with spaces between numbers? and is there a way to make mathematica interpret, for example, somethingE-002 as something##\times 10^{-2}##? I think I have an issue with that too.

My list of numbers looks like this now:

" 0.47368421052631576 , 0.10526315789473684 , 7.8947368421052627E-002 "
" 0.50000000000000000 , 0.10526315789473684 , 7.8947368421052627E-002 "
" 0.52631578947368418 , 0.10526315789473684 , 7.894736842052627E-002 "

And so on. There are blank spaces between the commas, but the forum ignores them.

I've defined at the beginning: myNumList= (And the list of numbers here).Thanks!
 
Back
Top