给定浮点数x以及两个数字’a’和’b’的函数f(x),以使f(a)* f(b)<0且f(x)在[a,b]中是连续的。在此,f(x)表示代数或先验方程。在区间[a,b]中找到函数的根(或找到x的值,使得f(x)为0)。
例子:
Input: A function of x, for example x3 - x2 + 2.
And two values: a = -200 and b = 300 such that
f(a)*f(b) < 0, i.e., f(a) and f(b) have
opposite signs.
Output: The value of root is : -1.0025
OR any other value with allowed
deviation from root.
什么是代数和先验函数?
代数函数可以用多项式形式表示,如f(x)= a 1 x 3 + a 2 x 2 +….. + e其中aa 1 ,a 2 ,…是常数,x是变量。
超越函数是非代数函数,例如f(x)= sin(x)* x – 3或f(x)= e x + x 2或f(x)= ln(x)+ x…。
什么是对分法?
该方法也称为间隔减半法,二分查找法或二分法。此方法用于查找给定间隔中方程的根,该间隔为f(x)= 0的’x’值。
该方法基于中间值定理,该中间值定理指出如果f(x)是连续函数,并且存在两个实数a和b,使得f(a)* f(b)0和f(b)<0) ,那么可以确保它们之间至少有一个根。
假设:
- f(x)是区间[a,b]中的连续函数
- f(a)* f(b)<0
脚步:
- 求出中间点c =(a + b)/ 2。
- 如果f(c)== 0,则c是解的根。
- 否则f(c)!= 0
- 如果值f(a)* f(c)<0,则根位于a和c之间。因此,我们再次求出a和c
- 否则,如果f(b)* f(c)<0,则根位于b和c之间。因此,我们重复b和c。
- 其他给定函数不遵循假设之一。
由于root可能是浮点数,因此我们在a和b之间的差小于值?时重复上述步骤。 (非常小的值)。
以下是上述步骤的实现。
C++
// C++ program for implementation of Bisection Method for
// solving equations
#include
using namespace std;
#define EPSILON 0.01
// An example function whose solution is determined using
// Bisection Method. The function is x^3 - x^2 + 2
double func(double x)
{
return x*x*x - x*x + 2;
}
// Prints root of func(x) with error of EPSILON
void bisection(double a, double b)
{
if (func(a) * func(b) >= 0)
{
cout << "You have not assumed right a and b\n";
return;
}
double c = a;
while ((b-a) >= EPSILON)
{
// Find middle point
c = (a+b)/2;
// Check if middle point is root
if (func(c) == 0.0)
break;
// Decide the side to repeat the steps
else if (func(c)*func(a) < 0)
b = c;
else
a = c;
}
cout << "The value of root is : " << c;
}
// Driver program to test above function
int main()
{
// Initial values assumed
double a =-200, b = 300;
bisection(a, b);
return 0;
}
Java
// Java program for implementation of Bisection Method
// for solving equations
class GFG{
static final float EPSILON = (float)0.01;
// An example function whose solution is determined using
// Bisection Method. The function is x^3 - x^2 + 2
static double func(double x)
{
return x*x*x - x*x + 2;
}
// Prints root of func(x) with error of EPSILON
static void bisection(double a, double b)
{
if (func(a) * func(b) >= 0)
{
System.out.println("You have not assumed"
+ " right a and b");
return;
}
double c = a;
while ((b-a) >= EPSILON)
{
// Find middle point
c = (a+b)/2;
// Check if middle point is root
if (func(c) == 0.0)
break;
// Decide the side to repeat the steps
else if (func(c)*func(a) < 0)
b = c;
else
a = c;
}
//prints value of c upto 4 decimal places
System.out.printf("The value of root is : %.4f"
,c);
}
// Driver program to test above function
public static void main(String[] args)
{
// Initial values assumed
double a =-200, b = 300;
bisection(a, b);
}
// This code is contributed by Nirmal Patel
}
Python3
# Python program for implementation
# of Bisection Method for
# solving equations
# An example function whose
# solution is determined using
# Bisection Method.
# The function is x^3 - x^2 + 2
def func(x):
return x*x*x - x*x + 2
# Prints root of func(x)
# with error of EPSILON
def bisection(a,b):
if (func(a) * func(b) >= 0):
print("You have not assumed right a and b\n")
return
c = a
while ((b-a) >= 0.01):
# Find middle point
c = (a+b)/2
# Check if middle point is root
if (func(c) == 0.0):
break
# Decide the side to repeat the steps
if (func(c)*func(a) < 0):
b = c
else:
a = c
print("The value of root is : ","%.4f"%c)
# Driver code
# Initial values assumed
a =-200
b = 300
bisection(a, b)
# This code is contributed
# by Anant Agarwal.
C#
// C# program for implementation
// of Bisection Method for
// solving equations
using System;
class GFG
{
static float EPSILON = (float)0.01;
// An example function whose
// solution is determined using
// Bisection Method. The function
// is x^3 - x^2 + 2
static double func(double x)
{
return x * x * x -
x * x + 2;
}
// Prints root of func(x)
// with error of EPSILON
static void bisection(double a,
double b)
{
if (func(a) * func(b) >= 0)
{
Console.WriteLine("You have not assumed" +
" right a and b");
return;
}
double c = a;
while ((b - a) >= EPSILON)
{
// Find middle point
c = (a + b) / 2;
// Check if middle
// point is root
if (func(c) == 0.0)
break;
// Decide the side
// to repeat the steps
else if (func(c) * func(a) < 0)
b = c;
else
a = c;
}
// prints value of c
// upto 4 decimal places
Console.WriteLine("The value of " +
"root is : "+ c);
}
// Driver Code
static public void Main ()
{
// Initial values assumed
double a = -200, b = 300;
bisection(a, b);
}
}
// This code is contributed by ajit
PHP
= 0)
{
echo "You have not assumed " .
"right a and b","\n";
return;
}
$c = $a;
while (($b - $a) >= $EPSILON)
{
// Find middle point
$c = ($a + $b) / 2;
// Check if middle
// point is root
if (func($c) == 0.0)
break;
// Decide the side to
// repeat the steps
else if (func($c) * func($a) < 0)
$b = $c;
else
$a = $c;
}
echo "The value of root is : " , $c;
}
// Driver Code
// Initial values assumed
$a =-200;
$b = 300;
bisection($a, $b);
// This code is contributed by ajit
?>
Javascript
输出:
The value of root is : -1.0025