Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. from mpl_toolkits.mplot3d import axes3d
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4.  
  5. def plot_implicit(fn, bbox=(-2.5,2.5)):
  6. ''' create a plot of an implicit function
  7. fn ...implicit function (plot where fn==0)
  8. bbox ..the x,y,and z limits of plotted interval'''
  9. xmin, xmax, ymin, ymax, zmin, zmax = bbox*3
  10. fig = plt.figure()
  11. ax = fig.add_subplot(111, projection='3d')
  12. A = np.linspace(xmin, xmax, 100) # resolution of the contour
  13. B = np.linspace(xmin, xmax, 15) # number of slices
  14. A1,A2 = np.meshgrid(A,A) # grid on which the contour is plotted
  15.  
  16. for z in B: # plot contours in the XY plane
  17. X,Y = A1,A2
  18. Z = fn(X,Y,z)
  19. cset = ax.contour(X, Y, Z+z, [z], zdir='z')
  20. # [z] defines the only level to plot for this contour for this value of z
  21.  
  22. for y in B: # plot contours in the XZ plane
  23. X,Z = A1,A2
  24. Y = fn(X,y,Z)
  25. cset = ax.contour(X, Y+y, Z, [y], zdir='y')
  26.  
  27. for x in B: # plot contours in the YZ plane
  28. Y,Z = A1,A2
  29. X = fn(x,Y,Z)
  30. cset = ax.contour(X+x, Y, Z, [x], zdir='x')
  31.  
  32. # must set plot limits because the contour will likely extend
  33. # way beyond the displayed level. Otherwise matplotlib extends the plot limits
  34. # to encompass all values in the contour.
  35. ax.set_zlim3d(zmin,zmax)
  36. ax.set_xlim3d(xmin,xmax)
  37. ax.set_ylim3d(ymin,ymax)
  38.  
  39. plt.show()
  40.  
  41. def goursat_tangle(x,y,z):
  42. a,b,c = 0.0,-5.0,11.8
  43. return x**4+y**4+z**4+a*(x**2+y**2+z**2)**2+b*(x**2+y**2+z**2)+c
  44.  
  45. plot_implicit(goursat_tangle)
  46.  
  47. def hyp_part1(x,y,z):
  48. return -(x**2) - (y**2) + (z**2) - 1
  49.  
  50. plot_implicit(hyp_part1, bbox=(-100.,100.))
  51.  
  52. def sphere(x,y,z):
  53. return x**2 + y**2 + z**2 - 2.0**2
  54.  
  55. def translate(fn,x,y,z):
  56. return lambda a,b,c: fn(x-a,y-b,z-c)
  57.  
  58. def union(*fns):
  59. return lambda x,y,z: np.min(
  60. [fn(x,y,z) for fn in fns], 0)
  61.  
  62. def intersect(*fns):
  63. return lambda x,y,z: np.max(
  64. [fn(x,y,z) for fn in fns], 0)
  65.  
  66. def subtract(fn1, fn2):
  67. return intersect(fn1, lambda *args:-fn2(*args))
  68.  
  69. plot_implicit(union(sphere,translate(sphere, 1.,1.,1.)), (-2.,3.))
  70.  
  71. from scipy import *
  72. from scipy import optimize
  73.  
  74. xrange = (0,1)
  75. yrange = (0,1)
  76. density = 100
  77. startz = 1
  78.  
  79. def F(x,y,z):
  80. return x**2+y**2+z**2-10
  81.  
  82. x = linspace(xrange[0],xrange[1],density)
  83. y = linspace(yrange[0],yrange[1],density)
  84.  
  85. points = []
  86. for xi in x:
  87. for yi in y:
  88. g = lambda z:F(xi,yi,z)
  89. res = optimize.fsolve(g, startz, full_output=1)
  90. if res[2] == 1:
  91. zi = res[0]
  92. points.append([xi,yi,zi])
  93.  
  94. points = array(points)
  95.  
  96. import matplotlib.pyplot as plt
  97. import numpy as np
  98. from mpl_toolkits.mplot3d import Axes3D
  99.  
  100. def hyp_part1(x,y,z):
  101. return -(x**2) - (y**2) + (z**2) - 1
  102.  
  103. fig = plt.figure()
  104. ax = fig.add_subplot(111, projection='3d')
  105.  
  106. x_range = np.arange(-100,100,10)
  107. y_range = np.arange(-100,100,10)
  108. X,Y = np.meshgrid(x_range,y_range)
  109. A = np.linspace(-100, 100, 15)
  110.  
  111. A1,A2 = np.meshgrid(A,A)
  112.  
  113. for z in A:
  114. X,Y = A1, A2
  115. Z = hyp_part1(X,Y,z)
  116. ax.contour(X, Y, Z+z, [z], zdir='z')
  117.  
  118. for y in A:
  119. X,Z= A1, A2
  120. Y = hyp_part1(X,y,Z)
  121. ax.contour(X, Y+y, Z, [y], zdir='y')
  122.  
  123. for x in A:
  124. Y,Z = A1, A2
  125. X = hyp_part1(x,Y,Z)
  126. ax.contour(X+x, Y, Z, [x], zdir='x')
  127.  
  128. ax.set_zlim3d(-100,100)
  129. ax.set_xlim3d(-100,100)
  130. ax.set_ylim3d(-100,100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement