Using matlab to realize the fractal pattern display of Newton iteration method in complex number domain space (newton method)

Using matlab to realize the fractal pattern display of Newton iteration method in complex number domain space (newton method)


This article was first published on the WeChat public account of matlab enthusiasts, welcome to pay attention.

Conventional statement: I have no relevant engineering application experience, and I only write this blog purely because I am interested in related algorithms. So if there are mistakes, welcome to correct them in the comment area, thank you very much. This article mainly focuses on the implementation of the algorithm. I have no experience in practical applications and other issues, so I will not involve it anymore.

This article is a small example found when searching for information when learning nonlinear dynamics. It feels very interesting, try it yourself. There are many related implementation cases, so this article will be simple.

1 Newton iteration method for one-dimensional function

Newton's iterative method is a classic method for finding the root of a function, and it is an iterative method. The idea is to continuously move the calculation point along the local tangent direction to make it approach the real root value continuously.

This method is relatively stable when the derivative is relatively simple, or when the initial point is close enough to the root.

Suppose the initial value is xk, the function is f(x)
xk + 1 = xk − f ( xk ) f ′ ( xk ) x_{k+1}=x_k-\frac{f(x_k)}{f'(x_k) }xk+1=xkf(xk)f(xk)
The root of its next prediction is xk + 1 x_{k+1}xk+1. Then iterates successively, and the x value will get closer and closer to the real root value.

Let's take y=x^2-2 as an example. It can be seen that each iteration, the process of each point gradually approaching the root. Due to the characteristics of this function, all points run to the outside of the root after the first iteration, and then gradually approach the root.
Please add a picture description

clear
clc
close all

x=-2.5:0.01:2.5;
y=Fx(x);%绘制函数用

xp=[-2.5:0.1:2.5];%绘制点用

figure(1);
clf
hold on
plot(x,y,'color','k','linewidth',1);
plot(x,0*x,'color','k','linewidth',0.5);
yp2=Fx(xp);
scatter(xp,yp2,30,1:length(xp),'filled');%绘制点
hold off
xlim([min(x),max(x)])
F=getframe(gcf);
I=frame2im(F);
[I,map]=rgb2ind(I,256);
%保存动图
imwrite(I,map,'test.gif','gif','Loopcount',inf,'DelayTime',0.8);

for k=2:6
    fxp=Fx(xp);
    dfxp=dFx(xp);%导数
    xp2=xp-fxp./dfxp;%经过牛顿迭代一次
    figure(1);
    clf
    hold on
    plot(x,y,'color','k','linewidth',1);
    plot(x,0*x,'color','k','linewidth',0.5);
    yp2=Fx(xp2);
    scatter(xp2,yp2,30,1:length(xp),'filled');%绘制点
    hold off
    pause(0.1)
    xp=xp2;%更新点的位置
    xlim([min(x),max(x)])
    %保存动图
    F=getframe(gcf);
    I=frame2im(F);
    [I,map]=rgb2ind(I,256);
    imwrite(I,map,'test.gif','gif','WriteMode','append','DelayTime',0.8);
end
function y=Fx(x)
y=x.^2-2;
end
function y=dFx(x)
%导数
y=2*x;
end

2. Newton's iterative method for the complex plane

The Newton iteration method of the complex plane is the same as the ordinary one-dimensional iteration method, and the formula is the same, that is, the original real number x is replaced by a complex number x. The rest remain unchanged.
xk + 1 = xk − f ( xk ) f ′ ( xk ) x_{k+1}=x_k-\frac{f(x_k)}{f'(x_k)}xk+1=xkf(xk)f(xk)

2.1 Simple equation results

Let's take as an example a simple quadratic equation in the complex plane:
f ( x ) = x 2 − 1 f(x)=x^2-1f(x)=x21
Its Newton iterative root-finding iterative equation is:
xk + 1 = xk − xk 2 − 1 2 xk x_{k+1}=x_{k}-\frac{x_{k}^2-1}{2x_k }xk+1=xk2x _kxk21
Of course, the above numbers are all complex numbers.

We draw the trajectory of the Newton iterative process at each point where the real and imaginary parts are both in the [-2,2] interval, as shown in the figure below: You
Please add a picture description
can see that the roots of the equation are only x=1+0i and x=- 1+0i these two. And in a relatively short number of iterations, the points of the entire plane can quickly converge to these two roots.

If you observe carefully, you can also see that the points on the left half (real part less than 0) converge to the root of x=-1, and the points on the right half converge to the root of x=1.

For f = x 2 − 1 f=x^2-1f=x21 , there is no nonlinear characteristic yet. Now keepf = xn − 1 f=x^n-1f=xn1. The form remains unchanged. When n>3, the Newton iteration will produce a non-linear convergence result.

Below f = x 3 − 1 f=x^3-1f=x31 as an example,
its Newton iterative root-finding iterative equation is:
xk + 1 = xk − xk 3 − 1 3 xk x_{k+1}=x_{k}-\frac{x_{k}^3-1 }{3x_k}xk+1=xk3x _kxk31
The equation has 3 roots, which are x=1, x=-0.5+0.866i, x=-0.5-0.866i. Both the real part and the imaginary part are in the [-2,2] interval, and each point Newton The trajectory of the iterative process is as Please add a picture description
shown in the figure below: Compared with the result of k=2, the convergence speed of these points is relatively slow, and they do not directly approach these three roots. · Some points are attracted back and forth by three roots, and converge after a long time.

%绘制牛顿迭代法的详解
clear;
clc
close all
res=100;
x=linspace(-2,2,res);
y=linspace(-2,2,res);
[xx,yy]=meshgrid(x,y);
zz=xx+yy*1i;
p=3;%3次多项式
N=50;
R=zeros(res,res,N);
zk=zz;
for k=1:N
    R(:,:,k)=zk;
    dz=(zk.^p-1)./(p*zk.^(p-1));%f(x)/f'(x)的形式
    zk = zk-dz;
end
%指定每个点的颜色
Color_R=interp2([-2,2;-2,2],[-2,-2;2,2],[0,0;1,1],xx,yy);
Color_G=interp2([-2,2;-2,2],[-2,-2;2,2],[1,0.2;0.3,0],xx,yy);
Color_B=interp2([-2,2;-2,2],[-2,-2;2,2],[0,1;0,1],xx,yy);
%动画中,每个帧之间插值补充m个帧
figure(1)
m=9;
count=0;
for k=1:9
    R_k_real_1=real(R(:,:,k));
    R_k_imag_1=imag(R(:,:,k));
    R_k_real_2=real(R(:,:,k+1));
    R_k_imag_2=imag(R(:,:,k+1));
    dt=1/(m+1);
    for j=0:1:m
        t=dt*j;
        R_k_real=R_k_real_1*(1-t)+R_k_real_2*t;
        R_k_imag=R_k_imag_1*(1-t)+R_k_imag_2*t;
        clf
        scatter( R_k_real(:) , R_k_imag(:) ,6,[Color_R(:),Color_G(:),Color_B(:)],'filled')
        xlim([-2,2])
        ylim([-2,2])
        box on
        pause(0.1)
        count=count+1;
        F=getframe(gca);
        I=frame2im(F);
        [I,map]=rgb2ind(I,192);
        if count == 1
            imwrite(I,map,'test.gif','gif','Loopcount',inf,'DelayTime',0.9);
        else
            imwrite(I,map,'test.gif','gif','WriteMode','append','DelayTime',0.1);
        end
    end
end

It is still the Newton iteration of this n=3 function.

If we mark all points that eventually converge to the root of x=1 in blue, all points that eventually converge to x=-0.5+0.866i are marked in yellow, and all points that converge to x=-0.5-0.866i are marked is green, then the distribution in space is the following pattern:
Please add a picture description
Similarly, changing the order of n, when n=6, will form the following more complex pattern.
Please add a picture description
As the saying goes, two begets three, and three begets all things. The phenomenon of chaos has an indissoluble bond with the number 3.

Another note: when n=4, since the root is on the diagonal of the pixel, the pixels on the diagonal may even not converge. It is recommended to add a little perturbation to the equation at this time, and change it to f = x 4 − 1 − 1 0 − 3 ∗ if=x^4-1-10^{-3}*if=x41103i . Others involving pixels just at the root connection (such as horizontal lines, vertical lines or diagonal lines) are handled in the same way.
See the next section for the code.
Please add a picture description

2.2 Results of other nonlinear equations

Since the equation I selected above is in the form of x^n-1, the distribution of roots all presents a centrally symmetrical pattern.

If the equation is changed, many new patterns will be produced. Here are a few simple examples:

We take f = 0.5 ∗ x 3 − 1.2 ∗ x 2 + 0.5 ∗ x + 0.9 f=0.5*x^3-1.2*x^2+0.5*x+0.9f=0.5x31.2x2+0.5x+0 . 9 as an example, its Newton iteration formula is as follows:
xk + 1 = xk − 0.5 xk 3 − 1.2 xk 2 + 0.5 xk + 0.9 1.5 xk 2 − 2.4 xk + xk x_{k+1}=x_{k}- \frac{0.5x_{k}^3-1.2x_{k}^2+0.5x_{k}+0.9}{1.5x_{k}^2-2.4x_{k}+x_{k}}xk+1=xk1 . 5 xk22 . 4x _k+xk0 . 5 xk31 . 2x _k2+0 . 5 xk+0.9
The generated picture is as follows:
Please add a picture description
It can be seen that its basic framework is similar to the standard third-order, because it is dominated by three roots. But the details of the generated patterns turned out to be quite different. For higher order patterns, if the parameters are changed, richer results can be produced.

Of course, it is not limited to polynomials, and other functions are also possible, as long as they can be expressed by complex numbers. For example:
f = x 2 sin ⁡ xf=x^2\sin{x}f=x2sin
The Newton iteration formula of x
is as follows: xk + 1 = xk − x 2 sin ⁡ xx 2 cos ⁡ x + 2 x sin ⁡ x x_{k+1}=x_{k}-\frac{x^2\sin{x }}{x^2\cos{x}+2x\sin{x}}xk+1=xkx2cosx+2x _sinxx2sinx
Since there are many plural roots, only a part of the roots are taken for dyeing, the effect is as follows:
insert image description here

code show as below:

%Mandelbrot
%?res是目标分辨率,(xc,yc)是图像中心,xoom是放大倍数
clear;
close all
%设置分辨率和画面中心位置
res=1024;xc=0;yc=0;
%设置放大系数
xoom=1;
%求解画框内所有像素点的坐标
x0=xc-2/xoom;x1=xc+2/xoom;
y0=yc-2/xoom;y1=yc+2/xoom;
x=linspace(x0,x1,res);
y=linspace(y0,y1,res);
[xx,yy]=meshgrid(x,y);
z=xx+yy*1i;

p=3;%3次多项式
%牛顿迭代
R = ones(res,res);
for k=1:numel(R)
    zk=z(k);
    dz=1;
    while abs(dz)>1e-3
        %在这里更改牛顿迭代的f(x)/f'(x)形式
        %dz=(zk^p-1)/(p*zk^(p-1));%f(x)/f'(x)的形式
        %dz=(zk^p-1-1e-3*1i)/(p*zk^(p-1));%f(x)/f'(x)的形式,这里是n=4以及其它情况,加一个1e-3的小扰动
        dz=(0.5*zk^3-1.2*zk^2+0.5*zk+0.9)/(1.5*zk^2-2.4*zk+0.5);%x^3+x^2+x+1的方程,对应p=3
        %dz=(zk^2*sin(zk)-1)/(zk^2*cos(zk)+2*zk*sin(zk));%x*x*sinx的方程.改p=24
        zk = zk-dz;
    end
    R(k)=zk;
end
%对所有得到的根进行聚类分析,来判断究竟收敛到了哪个根。
%因为迭代得到的根往往是一个有误差的大概值,不是精确值。
Z_real=real(R);
Z_imag=imag(R);
idx=kmeans([Z_real(:),Z_imag(:)],p);
%然后按照顺序,将像素与所在根进行一一对应
RootNum=zeros(res,res);
RootNum(:)=idx(:);

%绘图
figure()
pcolor(x,y,RootNum);shading flat
%调colormap用,这里只要好看就行,没有特别要求
% colormap(gallery('uniformdata',[p,3],13))%改p=24%x*x*sinx的方程演示用
colormap(gallery('uniformdata',[p,3],7))

Guess you like

Origin blog.csdn.net/weixin_42943114/article/details/121905957