Implementing Newton Iteration Method for Solving Nonlinear Equations Using MATLAB

Source: Internet
Author: User

Http://hi.baidu.com/aillieo/blog/item/0800e2a10ac9a59647106493.html

The known nonlinear equations are as follows:
3 * x1-cos (X2 * X3)-1/2 = 0

X1 ^ 2-81*(X2 + 0.1) ^ 2 + sin (X3) + 1.06 = 0

Exp (-X1 * x2) + 20 * X3 + (10 * pi-3)/3 = 0

The accuracy of the solution must reach 0.00001.

--------------------------------

First, create the function fun

The storage equations program saves fun. m to the working path as follows:

Function f = fun (X );

% Defines the nonlinear equations as follows:

% Variable X1 X2 X3

% Function F1 F2 F3

Syms X1 X2 X3

F1 = 3 * x1-cos (X2 * X3)-1/2;

F2 = x1 ^ 2-81*(X2 + 0.1) ^ 2 + sin (X3) + 1.06;

F3 = exp (-X1 * x2) + 20 * X3 + (10 * pi-3)/3;

F = [F1 F2 F3];

--------------------------------

Create Function dfun

Save dfun. m to the working path of the yake matrix used to find the equations:

Function df = dfun (X );

% The yakebi matrix used to solve the equations is stored in dfun.

F = fun (X );

DF = [diff (F, 'x1'); diff (F, 'x2 '); diff (F, 'x3')];

DF = conj (DF ');

--------------------------------

The programming Newton method solves the nonlinear equations and saves Newton. m to the working path:

Function x = Newton (x0, EPS, N );

Con = 0;

% X0 indicates the initial iteration value. EPS indicates the accuracy. N indicates the maximum number of iterations. Con indicates whether the result is converged.

For I = 1: N;

F = Subs (fun (x0), {'x1' X2 'x3'}, {x0 (1) x0 (2) x0 (3 )});

DF = Subs (dfun (x0), {'x1 ''X2 ''x3'}, {x0 (1) x0 (2) x0 (3 )});

* = X0-f/DF;

For j = 1: length (x0 );

Il (I, j) = x (j );

End

If norm (x-x0) <EPS

Con = 1;

Break;

End

X0 = X;

End

 

In the following example, the TXT file name is iteration.txt.

Fidgefopen('iteration.txt ', 'w ');

Fprintf (FID, 'iteration ');

For j = 1: length (x0)

Fprintf (FID, 'x % d', J );

End

For j = 1: I

Fprintf (FID, '\ n % 6D', J );

For k = 1: length (x0)

Fprintf (FID, '% 10.6f', il (j, k ));

End

End

If con = 1

Fprintf (FID, '\ n calculation result convergence! ');

End

If con = 0

Fprintf (FID, '\ n too many iterations may not converge! ');

End

Fclose (FID );

--------------------------------

Run the program

Enter the following content in MATLAB:

Newton ([0.1 0.1-0.1], 0.00001, 20)

--------------------------------

Output result

Ans =

 

0.5000 0.0000-0.5236

 

--------------------------------------------------

View iteration process in Iteration

 

Iteration X1 X2 X3

1 0.490718 0.031238-0.519661

2 0.509011 0.003498-0.521634

3 0.500928 0.000756-0.523391

4 0.500227 0.000076-0.523550

5 0.500019 0.000018-0.523594

6 0.500005 0.000002-0.523598

7 0.500000 0.000000-0.523599

Computation result convergence!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.