Advertisement
6E697465737472796B72

frac.py

Feb 23rd, 2025
31
0
15 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. def newton_fractal(xmin, xmax, ymin, ymax, width, height, max_iter, tol, power):
  5.     x = np.linspace(xmin, xmax, width)
  6.     y = np.linspace(ymin, ymax, height)
  7.     X, Y = np.meshgrid(x, y)
  8.     Z = X + 1j * Y
  9.     roots = np.exp(2j * np.pi * np.arange(power) / power)
  10.     F = np.zeros(Z.shape, dtype=int)
  11.     for i in range(max_iter):
  12.         Z = Z - (Z**power - 1) / (power * Z**(power - 1))
  13.         for k, r in enumerate(roots):
  14.             m = (np.abs(Z - r) < tol) & (F == 0)
  15.             F[m] = k + 1
  16.     return F
  17.  
  18. xmin, xmax, ymin, ymax = -2, 2, -2, 2
  19. width, height, max_iter, tol, power = 800, 800, 50, 1e-6, 5
  20. F = newton_fractal(xmin, xmax, ymin, ymax, width, height, max_iter, tol, power)
  21. plt.imshow(F, extent=[xmin, xmax, ymin, ymax], cmap='hsv')
  22. plt.axis('off')
  23. plt.show()
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement