Write two functions, in_mandelbrot recursive and in_mandelbrot, that check wheth
ID: 3871976 • Letter: W
Question
Write two functions, in_mandelbrot recursive and in_mandelbrot, that check whether a complex number c is part of the Mandelbrot set. Implement in_mandelbrot_recursive using recursion and in_mandelbrot using while loops. The absolute value of a complex number can be checked with the built-in python function abs or numpy.abs. To check for divergence, it is sufficient to check if > 2, rather than if] oo While the set is defined for n - oo, to get pretty pictures going to some high, but finite, n, is enough, e.g., 1000.Explanation / Answer
import numpy as np
r = input('Real part of c: ')
i = input('Imaginary part of c: ')
c = complex(float(-1),float(0));
def in_mandelbort_recursive( c ):
n=0;
test=False;
zn=znold=0;
if n>1000:
if(test):
return 'Mandelbort';
else:
return 'Not Mandelbort';
else:
zn=np.abs(znold)+c;
znold=zn;
n=n+1;
if (np.abs(zn)<2):
test=True;
Using While loop:
def in_mandelbort:in
import numpy as np
n=0;
test=False;
zn=znold=0;
r = input('Real part of c: ')
i = input('Imaginary part of c: ')
c = complex(float(-1),float(0));
while (n < 1000):
zn=np.abs(znold)+c;
znold=zn;
n=n+1;
if (np.abs(zn)<2):
test=True;
if(test):
print('Mandelbort');
else:
print('Not Mandelbort');
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.