在编码牛顿分形时获取颜色矩阵

时间:2019-04-27 02:07:52

标签: python-3.x

在此处输入代码`我正在尝试生成代码以对函数z ** n = 1进行牛顿分形,其中z是复数。我不确定颜色矩阵应该包含什么,在绘制图形时我只会得到两种没有图案的颜色。

import numpy as np
import random
import matplotlib.pyplot as plt
n=input("Newton fractal z**n=1, Enter n (default 3):")
val=input("Enter xmin,xmax,ymin,ymax (default -1.35,1.35,-1.35,1.35):")
if n == "":
    n=3
else:
    n=int(n)
if val == "":
    xmin,xmax,ymin,ymax= -1.35,1.35,-1.35,1.35
def derivative(n):
    return (n)*z**(n-1)
sol=[]
for i in range(n):
    sol1=np.exp(1j*((2*np.pi*i)/n))
    sol.append(sol1) ###solutions of the function
    print(sol[i])
x=np.linspace(xmin+0.00011,xmax,num=1000)
y=np.linspace(ymin+0.00011,ymax,num=1000)
C=np.zeros((1000,1000),dtype=complex)
xx1=np.array([])
for i in range (1000):
    for j in range(1000):
       xx=x[i]+1j*y[999-i]
       C[i, j] = xx  # matrix to store all the complex values


Z = C
for k in range(20):

     Z = Z - (Z ** n - 1) / (n * Z **(n - 1))  ### creating the new iterations

s = [0] * n
color = np.zeros(C.shape,dtype=np.uint8)
row, column = Z.shape
for i in range(row):
    for j in range(column):
        for k in range(n):
             s[k]  = abs(C[i, j]-sol[k])/abs(sol[k])  #the relative error
             color[i, j] = s.index(min(s)) * (255 / (n - 1))

plt.imshow(color, cmap ="rainbow" , origin = "lower", extent = (xmin, 
xmax,ymin,ymax),interpolation="bilinear" )
plt.show()


I am supposed to get a pattern in violent , red and green colors but all I'm getting is a half the plot filled with red and the other half filled with violet color.

0 个答案:

没有答案