42. Using Newton's iterative method to solve nonlinear high-dimensional equations (matlab program)

1. Brief description

      

If the vector symbol is X, the system of equations can be written in the form of F(X)=0.

We know that for the Newton iterative method of a function of one variable to find the root formula

Similarly, for multivariate function root finding formula

Where X is a vector and is the Jacobian matrix corresponding to the nonlinear equation system.

When solving the specific problem, we can first draw the graph through the drawing command to see the intersection point. Then bring the value near the intersection point into the iteration

matrix. Finally, find the closing solution that is less than the error.

2. Code

Main program:

function newton% Newton iterative method to solve nonlinear equations
    syms ax;
    beta=0.15;
    F=[1-beta+sqrt((1-beta)^2+4*a)-2*x;
       beta+a+2* x-(beta+x)^3];
    eps=10e-6;% accuracy
    num=1;% number of steps
    tol=1;% initial value of given error
    x0=[1;1];% parameter assignment initial value
    v=[a,x];
        while tol>eps
            Fx=subs(F,v,transpose(x0));
            dF=jacobian(F,v);
            c=subs(dF,v,transpose(x0))
            ; =x0-inv(c)*Fx;
            tol=norm(x-x0);
            x0=x;
            num=num+1;
                if (num>10^8)
                    disp('The number of iterations is greater than the maximum value, it may not converge' )
                    return
                end
        end
%Output
fprintf('Solve a = %g.\n',x0(1))
fprintf('Solve x = %g.\n',x0(2))
fprintf('Number of iterations n = %g times.\n',num)

Subroutine:

function [y,n]=newton_fun(F,x0)
        if nargin==2
            eps=1.0e-6;
        end
        num = 0;
        tol = 1;
        v=findsym(F);
        while tol>eps
            Fx = subs(F ,v,transpose(x0));
            dF=jacobian(F,v);
            c=subs(dF,v,transpose(x0));
            x=x0-inv(c)*Fx;
            tol=norm(x-x0 );
            x0=x;
            num=num+1;
                if (num>10^8)
                    disp('The number of iterations is greater than the maximum value, it may not converge')
                    return
                end
        end
        y = x0;
        n = num;
end
 

Subroutine:

function solve
  syms x
    gamma=0.5;
    m=5;
    theta=12;
    a=1-gamma;
    F=sin((m+1)*x)-a*sin(m*x);
   
    for i=0:m+1
        w0=i*pi/(m+1);
        [x,n]=newton_fun(F,w0);
        w(i+1)=x;
%       beta(i+1)=theta*gama^2/(sqrt(1+a^2-2*a*cos(x))-gama*(1-theta));
        b(i+1)=cos((m+1)*x)-a*cos(m*x);%先算出b
        beta(i+1)=theta*gamma^2/(b(i+1)-gamma*(1-theta));%再算出beta
        num(i+1)=n;
    end
    w
    beta
    num

Guess you like

Origin blog.csdn.net/m0_57943157/article/details/132156792