天天看點

Fractal Picture With Newton-Raphson

  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.         if abs(r-1)<0.0001:
  22.             X1[len(X1):]=[z.real]
  23.             Y1[len(Y1):]=[z.imag]
  24.         elif abs(r+1/2-a/2*1j)<0.0001:
  25.              X2[len(X2):]=[z.real]
  26.              Y2[len(Y2):]=[z.imag]
  27.         elif abs(r+1/2+a/2*1j)<0.0001:
  28.              X3[len(X3):]=[z.real]
  29.              Y3[len(Y3):]=[z.imag]
  30. plt.figure('frctal')
  31. plt.plot(X1,Y1,'r.',X2,Y2,'b.',X3,Y3,'g.')
  32. plt.show()
  33. stop=time.time()
  34. print("time=",stop-start)