Newton-Raphson fractal script problem
Show older comments
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);
Answers (1)
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])
2 Comments
hamza kharbouch
on 2 Jan 2022
Matt J
on 2 Jan 2022
You're welcome, but if the answer works for you, please Accept-click it.
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!