Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 KB | None | 0 0
  1. d = [(-70,2000,330),(-70,3000,303),(-70,4000,280),(-70,4500,265),(-70,5000,245),(-70,5500,220),(-70,6000,195),(-70,6500,160),(-70,7000,120),(-70,7500,80),(-60,1500,345),(-60,2000,344),
  2. (-60,2500,343),(-60,3000,341),(-60,3500,341),(-60,4000,340),(-60,4500,335),(-60,5000,330),(-60,5500,322),(-60,6000,312),(-60,6500,302),(-60,7000,290),(-60,7500,274),(-60,8000,250),
  3. (-60,8500,225),(-60,9000,198),(-60,9500,158),(-60,10000,120),(-50,1500,375),(-50,2000,376),(-50,2500,377),(-50,3000,378),(-50,3500,378),(-50,4000,379),(-50,4500,378.5),(-50,5000,378),
  4. (-50,5500,377),(-50,6000,375),(-50,6500,370),(-50,7000,365),(-50,7500,360),(-50,8000,352),(-50,8500,345),(-50,9000,340),(-50,9500,330),(-50,10000,320),(-50,11000,280),(-50,11500,255),
  5. (-50,12000,220),(-50,13000,145),(-50,13500,100),(-40,1500,380),(-40,2000,380),(-40,2500,381),(-40,3000,383),(-40,3500,385),(-40,4000,390),(-40,4500,394),(-40,5000,398),(-40,5500,400),
  6. (-40,6000,407),(-40,6500,410),(-40,7000,415),(-40,7500,417),(-40,8000,419),(-40,8500,418),(-40,9000,417),(-40,9500,415),(-40,10000,410),(-40,10500,405),(-40,11000,396),(-40,12000,379),
  7. (-40,12500,370),(-40,13000,355),(-40,13500,338),(-40,14000,320),(-40,14500,300),(-40,15000,275),(-40,16000,215),(-30,1500,400),(-30,2000,403),(-30,2500,405),(-30,3000,410),(-30,3500,415),
  8. (-30,4000,418),(-30,4500,420),(-30,5000,425),(-30,5500,430),(-30,6000,435),(-30,6500,438),(-30,7000,440),(-30,7500,442),(-30,8000,445),(-30,8500,447),(-30,9000,450),(-30,9500,452),
  9. (-30,10000,453),(-30,10500,455),(-30,11000,457),(-30,11500,455),(-30,12000,450),(-30,12500,447),(-30,13000,445),(-30,13500,442),(-30,14000,440),(-30,14500,435),(-30,15000,427),
  10. (-30,15500,419),(-30,16000,400),(-30,16500,380),(-30,17000,360),(-30,17500,330)]
  11.  
  12.  
  13. import numpy
  14. import itertools
  15. import matplotlib.pyplot as plt
  16. from matplotlib import cm
  17. import mpl_toolkits.mplot3d.axes3d as axes3d
  18.  
  19. #least square fitting function taken from https://stackoverflow.com/questions/7997152/python-3d-polynomial-surface-fit-order-dependent
  20. def polyfit2d(x, y, z, order=3):
  21.     ncols = (order + 1)**2
  22.     G = numpy.zeros((x.size, ncols))
  23.     ij = itertools.product(range(order+1), range(order+1))
  24.     for k, (i,j) in enumerate(ij):
  25.         G[:,k] = x**i * y**j
  26.     m, _, _, _ = numpy.linalg.lstsq(G, z)
  27.     return m
  28.  
  29. def polyval2d(x, y, m):
  30.     order = int(numpy.sqrt(len(m))) - 1
  31.     ij = itertools.product(range(order+1), range(order+1))
  32.     z = numpy.zeros_like(x)
  33.     for a, (i,j) in zip(m, ij):
  34.         z += a * x**i * y**j
  35.     return z
  36.  
  37. def equation(m):
  38.     order = int(numpy.sqrt(len(m))) - 1
  39.     ij = itertools.product(range(order+1), range(order+1))
  40.     print " z = ",
  41.     for a, (i,j) in zip(m, ij):
  42.         print "%1.8f*(x^%i*y^%i) +"%(a,i,j),
  43.  
  44. #SCALING
  45. d = numpy.array(d)
  46. dOffset = d.min(axis=0)
  47. d = d - dOffset #offset the d closer to zero
  48. dScale = d.max(axis=0)
  49. d = d/dScale #scale the d so it ranges from 0-1 on every axis
  50.  
  51. #FITTING
  52. m = polyfit2d(d[:,0],d[:,1],d[:,2],order = 3) # fit d, polynms returnded
  53.  
  54. #CALCULATION OF FIT DATA
  55. nx, ny = 50, 50 # gridsize
  56. xx, yy = numpy.meshgrid(numpy.linspace(d[:,0].min(), d[:,0].max(), nx), numpy.linspace(d[:,1].min(), d[:,1].max(), ny))
  57. zz = polyval2d(xx, yy, m) # calculate gridpoints
  58. fx = polyval2d(d[:,0], d[:,1], m) # calculate values at the original locations
  59. error = fx-d[:,2] #so we can get the error between actual and fit d
  60.  
  61. #SCALING
  62. xx = xx*dScale[0]+dOffset[0]  #scale the results
  63. yy = yy*dScale[1]+dOffset[1]
  64. zz = zz*dScale[2]+dOffset[2]
  65. d = d*dScale+dOffset  # scale the source d to original
  66. error = error*dOffset[2] #scale the errors too
  67.  
  68. #PRINT FIT FUNC
  69. equation(m)
  70.  
  71. #PLOTTING
  72. fig = plt.figure()
  73. ax = fig.add_subplot(111, projection='3d')
  74. p = ax.plot_wireframe(xx,yy,zz, rstride=4, cstride=4, alpha=0.4) # plot a mesh
  75. p = ax.scatter3D(d[:,0],d[:,1],d[:,2], c=error, cmap='hot', s=100) # plot the original d with error coloring
  76. fig.colorbar(p, ax=ax)
  77. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement