The study of iterative method

Source: Internet
Author: User

The study of iterative method

Iterative method is a common algorithm design method for solving the approximate root of equations or equations. Set the equation to f (x) = 0, use a mathematical method to derive the equivalent form x=g (x), and then follow these steps:

(1) Select an approximate root of the equation and assign it to the variable x0;

(2) The value of x0 is stored in the variable x1, then g (x1) is calculated, and the result is stored in the variable x0;

(3) When the absolute value of the difference between the x0 and the X1 is less than the specified precision, repeat step (2) calculation.

If the equation has roots, and the approximate root sequence converges by the above method, then the x0 obtained by the above method is considered to be the root of the equation. The above algorithm is expressed in the form of a program:

The "algorithm" iterative method to find the root of the equation
{x0= initial approximate root;
do {
x1=x0;
X0=g (x1);/* Calculates a new approximate root by a specific equation */
} while (Math.Abs (x0-x1) > Precision);
System.out.printf ("The approximate root of the equation is%f\n", x0);
}

public class test1{public   static double F (double x) {   double y;   y=x*x*x-x*x-x-1;   Calculates   return (y);}  public static void Main (String args[]) {   double x0=1.1,x1=1.1;   do{    x1=x0;    x0=1+1/x1+1/(X1*X1);   } while (Math.Abs (x0-x1) >1e-5);   System.out.println (f (x0));   System.out.printf ("Approximate root of the equation is%f\n", x0);}    }

C:\java>java Test1
-2.0056621405029063E-5
The approximate root of the equation is 1.839283

Example 2: Solution equation: x*x*x-x-1=0

public class test2{public   static double F (double x) {   double y;   y=x*x*x-x-1;   Calculates   return (y);}  public static void Main (String args[]) {   double x0=1.5,x1=1.5;   do{    x1=x0;    X0=math.pow ((1+x1), 1/3.0);   } while (Math.Abs (x0-x1) >1e-7);   System.out.println (f (x0));   System.out.printf ("Approximate root of the equation is%f\n", x0);}    }

C:\java>java Test2
4.4345108918264486E-8
The approximate root of the equation is 1.324718

Example 3:
The square root of a number A is obtained by iterative method. The iteration formula known as square root is: x [n+1] = (X[n] + a/x[n])/2
The absolute value of the difference obtained before and after two times is less than 10-5.

The algorithm is as follows:

① set an x's initial value x0; (In the following procedure take X0=A/2, through the iterative formula to find out the X1, can be sure compared with the real square root, the error is very large.) )
② uses the above formula to find the next value of x x1;
③ so continue until the x values (x n+1 and xn) that were calculated two times before and after it satisfy the following relationship: |x n+1-xn|<10-5.

public class test{
Public staic void Main (String args[]) {

Double A; /* Radicals */
Double x0, X1; /* Represents the previous and later items, respectively */
A=double.parsedouble ();
x0 = A/2; /* Take the initial value */
X1 = (x0 + a/x0)/2;
while (Math.Abs (x1-x0) >= 1e-5)
{
x0 = x1;
X1 = (x0 + a/x0)/2;
}
System.out.printf ("The square root of%5.2f is%8.5f\n", A, X1);
}
}

Operation Result:

C:\java>java Test 3
The square root of 3.00 is 1.73205

C:\java>java Test 100
The square root of 100.00 is 10.00000

C:\java>java Test 2
The square root of 2.00 is 1.41421

The study of iterative method

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.