Fractal Picture With Newton-Raphson

来源:互联网 发布:看京剧的软件 编辑:程序博客网 时间:2024/05/22 11:35
  1. # -*- coding: utf-8 -*-
  2. '''
  3. calculate the root of f=z**3-1 and plot fractal picture
  4. with  Newton-Raphson method
  5. '''
  6. import matplotlib.pyplot as plt
  7. import time
  8. start=time.time()
  9. X1,X2,X3,Y1,Y2,Y3=[],[],[],[],[],[]
  10. a=1.732  #np.sqrt(3)
  11. z=-1-1j
  12. #Newton-Raphson
  13. for i in range(0,2000):
  14.     z=z+0.001-z.imag*1j-1j
  15.     for j in range(0,2000):
  16.         z=z+0.001j
  17.         r=z
  18.         for k in range(0,15):
  19.             r2=r-(r**3-1)/(3*r*r)
  20.             r=r2
  21.             
  22.         if abs(r-1)<0.0001:
  23.             X1[len(X1):]=[z.real]
  24.             Y1[len(Y1):]=[z.imag]
  25.         elif abs(r+1/2-a/2*1j)<0.0001:
  26.              X2[len(X2):]=[z.real]
  27.              Y2[len(Y2):]=[z.imag]
  28.         elif abs(r+1/2+a/2*1j)<0.0001:
  29.              X3[len(X3):]=[z.real]
  30.              Y3[len(Y3):]=[z.imag]
  31.              
  32. plt.figure('frctal')
  33. plt.plot(X1,Y1,'r.',X2,Y2,'b.',X3,Y3,'g.')
  34. plt.show()
  35. stop=time.time()
  36. print("time=",stop-start)