Newton-Raphson fractal script problem

matlab doesn't give results to this script and keeps running it non stop (i waited for +10 min)
any thoughts ?
f = @(z) z.^3-1;
df = @(z) 3*z.^2;
e = 0.00001;
x=-3:0.001:3;
y=x*1i;
[r,k]=ndgrid(x,y);
z=r+k; % setting a complexe plane
for i=1:6000
for j=1:6000
while f(z(i,j))<e %newton methode
dorime(i,j)=z(i,j)-f(z(i,j))/df(z(i,j));
end
end
end
%coloring in white,black,grey
if abs(dorime(i,j)-(-1+sqrt(3))/2) <e
fractale (i,j)=1;
end
if dorime (i,j)-1<e
fractale (i,j)=1/2;
end
if abs(dorime(i,j)-(-1-sqrt(3))/2) <e
fractale (i,j)=0;
end
% showing the fractal
imshow(fractal);

Réponses (1)

Matt J
Matt J le 2 Jan 2022
Modifié(e) : Matt J le 2 Jan 2022
I would recommend doing the Newton's method iterations as below.
f = @(z) z.^3-1;
df = @(z) 3*z.^2;
e = 0.00001;
x=-3:0.001:3;
y=x*1i;
[r,k]=ndgrid(x,y);
z=r+k; % setting a complexe plane
maxiter=15;
iter=0;
while any( abs ( f(z) ) > e ,'all') && iter < maxiter %newton methode
z=z-f(z)./df(z);
iter=iter+1
end
dorime=z;
The rest of the code, I don't understand. You seem to think z.^3-1 will have the 3 real roots,
z = [ (-1-sqrt(3))/2) 1 (-1+-sqrt(3))/2)]
but it doesn't
roots([1 0 0 -1])
ans =
-0.5000 + 0.8660i -0.5000 - 0.8660i 1.0000 + 0.0000i

2 commentaires

hamza kharbouch
hamza kharbouch le 2 Jan 2022
<< You seem to think z.^3-1 will have the 3 real roots,
>> oh my bad . a typo
thank u very much
Matt J
Matt J le 2 Jan 2022
You're welcome, but if the answer works for you, please Accept-click it.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by