Using C # To compile a bipartite solution to a non-linear equation

Source: Internet
Author: User

In the calculation method, there is a solution for finding a non-linear equation, called a bipartite method.

A brief introduction is as follows:

The f (x) function is continuous in the range [a, B]. Assume that there is a unique real root in the range, which is counted as x *

The idea of the Bipartite method is: first determine the root interval, divide the second-class interval, and gradually narrow down the root interval by judging the symbol of f (x) until the root interval is small enough, you can find the approximate root that meets the precision requirements.

Then we will give an example and a programming solution for C:

// Returns the root of the equation f (x) = x ^ 4-x-10.27 = 0 on (1, 2. Accurate to 10 ^ (-2)
Using System;

Class Dichotomy
{
Static   Void Main ()
{
Int K =   - 1 ; // Record loop index
Double A =   1 ; // Lower limit
Double B =   2 ; // Upper Limit
Double X =   0 ; // Root

Console. Write ( " K A \ t B \ t x \ t f (x) \ t a-B \ n " );

// First computing
X = ( + B) / 2 ;
K ++ ;

Console. Write ( " {0,-4} {1,-13} {2,-13} {3,-13} {4,-20} {5} \ n " , K, a, B, X, f (x), - B );

While ( True )
{

K ++ ;
Double F = F (x );

If (F <   0 )
{
A=X;
}
Else   If (F >   0 )
{
B=X;
}
X = ( + B) / 2 ;
If (B - A) <   0.01 )
{
Console. Write ("{0,-4} {1,-13} {2,-13} {3,-13} {4,-20} {5} \ n", K, a, B, X, F,-B );
Break;
}

Console. Write ( " {0,-4} {1,-13} {2,-13} {3,-13} {4,-20} {5} \ n " , K, a, B, X, F, - B );

}
Console. Readline ();
}

Private   Static   Double F ( Double X)
{
ReturnMath. Pow (X,4)-X- 10.27;
}  
}

Output result:

K A B x F (x) A-B
0 1 2 1.5-6.7075-1
1 1.5 2 1.75-6.7075-0.5
2 1.75 2 1.875-2.64109375-0.25
3 1.75 1.875 1.8125 0.214619140625-0.125
4 1.8125 1.875 1.84375-1.29024841308594-0.0625
5 1.84375 1.875 1.859375-0.557734031677246-0.03125
6 1.859375 1.875 1.8671875-0.176621873378753-0.015625
7 1.859375 1.8671875 1.86328125 0.0177218760550026-0.0078125

It can be seen that B7-A7 is about 0.0078 <10 ^ (-2), and thus | x *-X7 | <= 0.5 * (B7-A7) <= 0.5*10 ^ (-2), that is, to meet the accuracy requirements of the question, so the root of the requirements is: x * is about 1.863

 

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.