Using Newton Iteration Method to Solve Nonlinear Equations

Source: Internet
Author: User

Recently, a buddy used the Newton Iteration Method to Solve the optimal solution problem of a four-variable equations. he searched for code on the Internet to improve the problem, but it was always a bit unsatisfactory. He had too many iterations, however, the accuracy is not improved!

After analysis, it is found that there are many local extreme points in this equations. It is the problem that the Newton iteration method cannot be used to enter the local extreme values. It is also related to the initial values of the program!

I found that I haven't used MATLAB for a long time. By the way, I checked the code on the Internet and modified it myself!

First popularize Newton Iteration Method: (from Baidu encyclopedia)

Newton Iteration Method (Newton's method) Is also calledNewton-Raphson method)It is an approximate method proposed by Newton in the 17th century to solve equations in real and complex fields. Most equations do not have root formulas, so it is very difficult or even impossible to find the exact root, so it is particularly important to find the approximate root of the equation. Methods The first few items of the Taylor series of function f (x) are used to find the root of the equation f (x) = 0. The Newton iteration method is one of the most important methods for finding the root of the equation. Its biggest advantage is that it has square convergence near a single root of the equation f (x) = 0, in addition, this method can be used to obtain the multiple root and Compound Root of the equation. At this time, the linear convergence can be achieved, but some methods can be used to convert it into hyper-linear convergence. In addition, this method is widely used in computer programming.

Set R to the root of f (x) = 0. Select x0 as the initial approximate value of R, and use the point (x0, F (x0) as the tangent of the curve to obtain the intersection of the tangent and the X axis, and obtain the abscissa of the vertex, x1 is an approximation of R. In this way, we can deduce the Newton iteration formula.

It has been proved that if it is continuous and the zero point to be obtained is isolated, there is a region around the zero point. As long as the initial value is located in the adjacent area, the Newton method will surely converge. In addition, if it is not 0, the Newton method will have the square convergence performance. Roughly speaking, this means that the number of valid results for each iteration will double.

I checked some code on the Internet to specify a number of functions for export. If I changed the number of functions, I had to make a big fuss about the original program. Really Worried.

Find the http://hi.baidu.com/aillieo/item/f4d2c4de85af6be954347f25 this program, it seems that the MATLAB can not run very well, for the return value of the data is empty without processing, and later found a Netease friend blog, it's okay to take his code and run it. but for different function equations, and the number of variables, it's really worrying. This is the issue of programming and coding!

I changed it myself and can support multiple equations and multiple variables! The following is an example of my code! Thank you for your guidance!

 

[Python]View plaincopyprint?
  1. Function [R, N] = mulnewton (x0, funcmat, VAR, EPS)
  2. % X0 is the starting value of two variables, funcmat is the two equations, VaR is the two variables of the two equations, and EPS control accuracy
  3. % Newton Iteration Method for Solving Binary Nonlinear Equations
  4. If nargin = 0
  5. X0 = [0.2, 0.6];
  6. Funcmat = [Sym ('(15 * X1 + 10 * x2)-(40-30 * x1-10 * x2) ^ 2*(15-15 * X1 )) * 5e-4 ')...
  7. Sym ('(15 * X1 + 10 * x2)-(40-30 * x1-10 * x2) * (10-10 * x2) * 4e-2')];
  8. Var = [Sym ('x1 ') Sym ('x2')];
  9. EPS = 1.0e-4;
  10. End
  11. N_var = size (VAR, 2); % number of variables
  12. N_func = size (funcmat, 2); % Number of functions
  13. N_x = size (x0, 2); % number of variables
  14. If N_x ~ = N_var & N_x ~ = N_func
  15. Fprintf ('Expression error! \ N ');
  16. Exit (0 );
  17. End
  18. R = x0-myf (x0, funcmat, VAR) * inv (dmyf (x0, funcmat, VAR ));
  19. N = 0;
  20. Tol = 1;
  21. While tol> = EPS
  22. X0 = R;
  23. R = x0-myf (x0, funcmat, VAR) * inv (dmyf (x0, funcmat, VAR ));
  24. Tol = norm (r-x0 );
  25. N = n + 1;
  26. If (n> 100000)
  27. Disp ('the number of iterations is too large, and the equation may not converge ');
  28. Return;
  29. End
  30. End
  31. End % end mulnewton


 

[Python]View plaincopyprint?
  1. Function f = Myf (x, funcmat, varmat)
  2. % The input parameter X is two numeric values, func is the 1*2 Symbol Variable matrix, and VaR is the variable in the 1*2 Symbol Variable matrix.
  3. % The returned value is a 1*2 matrix with a numerical value.
  4. N_x = size (x, 2); % number of variables
  5. F_val = zeros (1, N_x );
  6. For I = 1: N_x
  7. Tmp_var = cell (1, N_x );
  8. Tmp_x = cell (1, N_x );
  9. For j = 1: N_x
  10. Tmp_var {J} = varmat (1, J );
  11. Tmp_x {J} = x (1, J );
  12. End
  13. F_val (I) = Subs (funcmat (1, I), tmp_var, tmp_x );
  14. End
  15. F = f_val;
  16. End % end Myf

 

[Python]View plaincopyprint?
    1. Function df_val = dmyf (x, funcmat, varmat)
    2. % The returned value is a 2*2 matrix with a numerical value.
    3. % Df = [DF1/X1, DF1/X2;
    4. % Df2/x1. df2/x2];
    5. N_x = size (x, 2); % number of variables
    6. DF = cell (N_x, N_x );
    7. For I = 1: N_x
    8. For j = 1: N_x
    9. DF {I, j} = diff (funcmat (1, I), varmat (1, j ));
    10. End
    11. End
    12. Df_val = zeros (N_x, N_x );
    13. For I = 1: N_x
    14. For j = 1: N_x
    15. Tmp_var = cell (1, N_x );
    16. Tmp_x = cell (1, N_x );
    17. For k = 1: N_x
    18. Tmp_var {k} = varmat (1, k );
    19. Tmp_x {k} = x (1, k );
    20. End
    21. Df_val (I, j) = Subs (DF {I, j}, tmp_var, tmp_x );
    22. End
    23. End
    24. End % end dmyf

Http://blog.csdn.net/berguiliu/article/details/25339347

Using Newton Iteration Method to Solve Nonlinear Equations

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.