📜  虚位法程序

📅  最后修改于: 2022-05-13 01:58:23.199000             🧑  作者: Mango

虚位法程序

给定浮点数 x 上的函数f(x) 和两个数 'a' 和 'b' 使得 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.00
        OR any other value close to root.

我们强烈建议将以下帖子作为本文的先决条件。
代数和超越方程的解|第 1 组(二分法)
在这篇文章中讨论了错误位置的方法。这种方法也被称为 Regula Falsi 或和弦方法。
与二分法的相似之处:

  1. 相同假设:此方法还假设函数在 [a, b] 中是连续的,并且给定两个数字“a”和“b”使得 f(a) * f(b) < 0。
  2. 总是收敛:像二分法一样,它总是收敛,通常比二分法快得多——但有时比二分法慢得多。

与二分法的区别:
不同之处在于我们制作了一个连接两个点 [a, f(a)] 和 [b, f(b)] 的和弦。我们考虑和弦接触x轴的点并将其命名为c。
脚步:

  1. 写出连接两点的直线方程。
y – f(a) =  ( (f(b)-f(a))/(b-a) )*(x-a)

Now we have to find the point which touches x axis. 
For that we put y = 0.

so x = a - (f(a)/(f(b)-f(a))) * (b-a)
   x = (a*f(b) - b*f(a)) / (f(b)-f(a)) 

This will be our c that is c = x. 
  1. 如果f(c) == 0,则 c 是解的根。
  2. 否则f(c) != 0
    1. 如果值 f(a)*f(c) < 0,则根位于 a 和 c 之间。所以我们重复 a 和 c
    2. 否则,如果f(b)*f(c) < 0,则根位于 b 和 c 之间。所以我们重复b和c。
    3. 其他给定的函数不遵循假设之一。

由于根可能是一个浮点数,并且在最坏的情况下收敛速度可能非常慢,因此我们迭代了很多次,使得答案变得更接近根。

正则假

以下是实现。

C++
// C++ program for implementation of Bisection Method for
// solving equations
#include
using namespace std;
#define MAX_ITER 1000000
 
// 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) in interval [a, b]
void regulaFalsi(double a, double b)
{
    if (func(a) * func(b) >= 0)
    {
        cout << "You have not assumed right a and b\n";
        return;
    }
 
    double c = a;  // Initialize result
 
    for (int i=0; i < MAX_ITER; i++)
    {
        // Find the point that touches x axis
        c = (a*func(b) - b*func(a))/ (func(b) - func(a));
 
        // Check if the above found point is root
        if (func(c)==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;
    regulaFalsi(a, b);
    return 0;
}


Java
// java program for implementation
// of Bisection Method for
// solving equations
import java.io.*;
 
class GFG {
 
    static int MAX_ITER = 1000000;
 
    // 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)
    // in interval [a, b]
    static void regulaFalsi(double a, double b)
    {
        if (func(a) * func(b) >= 0)
        {
            System.out.println("You have not assumed right a and b");
        }
        // Initialize result
        double c = a;
 
        for (int i = 0; i < MAX_ITER; i++)
        {
            // Find the point that touches x axis
            c = (a * func(b) - b * func(a))
                 / (func(b) - func(a));
 
            // Check if the above found point is root
            if (func(c) == 0)
                break;
 
            // Decide the side to repeat the steps
            else if (func(c) * func(a) < 0)
                b = c;
            else
                a = c;
        }
        System.out.println("The value of root is : " + (int)c);
    }
 
    // Driver program
    public static void main(String[] args)
    {
        // Initial values assumed
        double a = -200, b = 300;
        regulaFalsi(a, b);
    }
}
 
// This article is contributed by vt_m


Python3
# Python3 implementation of Bisection
# Method for solving equations
 
MAX_ITER = 1000000
 
# 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) in interval [a, b]
def regulaFalsi( a , b):
    if func(a) * func(b) >= 0:
        print("You have not assumed right a and b")
        return -1
     
    c = a # Initialize result
     
    for i in range(MAX_ITER):
         
        # Find the point that touches x axis
        c = (a * func(b) - b * func(a))/ (func(b) - func(a))
         
        # Check if the above found point is root
        if func(c) == 0:
            break
         
        # Decide the side to repeat the steps
        elif func(c) * func(a) < 0:
            b = c
        else:
            a = c
    print("The value of root is : " , '%.4f' %c)
 
# Driver code to test above function
# Initial values assumed
a =-200
b = 300
regulaFalsi(a, b)
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# program for implementation
// of Bisection Method for
// solving equations
using System;
 
class GFG {
 
    static int MAX_ITER = 1000000;
 
    // 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)
    // in interval [a, b]
    static void regulaFalsi(double a, double b)
    {
        if (func(a) * func(b) >= 0)
        {
            Console.WriteLine("You have not assumed right a and b");
        }
        // Initialize result
        double c = a;
 
        for (int i = 0; i < MAX_ITER; i++)
        {
            // Find the point that touches x axis
            c = (a * func(b) - b * func(a))
                / (func(b) - func(a));
 
            // Check if the above found point is root
            if (func(c) == 0)
                break;
 
            // Decide the side to repeat the steps
            else if (func(c) * func(a) < 0)
                b = c;
            else
                a = c;
        }
 
        Console.WriteLine("The value of root is : " + (int)c);
    }
 
    // Driver program
    public static void Main(String []args)
    {
        // Initial values assumed
        double a = -200, b = 300;
        regulaFalsi(a, b);
    }
}
 
// This code is contributed by Sam007.


Javascript


输出:

The value of root is : -1