Write a program to demonstrate Regula-Falsi Method in C language.

Tested on: Turbo C++ Version 4.0Time Complexity: O (n)Space Complexity: O (1)Open Source: MIT
1
2
3
    /*Program to demonstrate Regula-Falsi Method */
4
    #include <stdio.h>
5
    #include <math.h>
6
    double f(float x)
7
    {
8
        double val = 0.0;
9
        val = x * x * x + x - 3;
10
        return val;
11
    }
12
    void main()
13
    {
14
        int max, count = 0;
15
        float x0, x1, x2, aerr;
16
        double err, y0, y1, y2;
17
        printf("Enter initial guesses,no of iterations and maximum iteration error: ");
18
        scanf("%f%f%d%f", &x0, &x1, &max, &aerr);
19
        while (count < max)
20
        {
21
            y0 = f(x0);
22
            y1 = f(x1);
23
            if (y0 == 0)
24
            {
25
                printf("Root is %f", x0);
26
                return;
27
            }
28
            else if (y1 == 0)
29
            {
30
                printf("Root is %f", x1);
31
                return;
32
            }
33
            else if (y0 * y1 > 0)
34
            {
35
                printf("Unsuitable starting values");
36
                return;
37
            }
38
            else
39
            {
40
                x2 = x0 - ((x1 - x0) / (y1 - y0)) * y0;
41
                y2 = f(x2);
42
                if (y0 * y2 < 0)
43
                    x1 = x2;
44
                else
45
                    x0 = x2;
46
                err = fabs(x1 - x2);
47
                if (err < aerr)
48
                {
49
                    count++;
50
                    printf("\nAfter %d iteration root= \n%f\n", count, x2);
51
                    return;
52
                }
53
                count++;
54
                printf("Iteration no. %d: x=%f\n", count, x2);
55
            }
56
        }
57
        printf("Solution does't converge ,iterations not sufficient");
58
        return;
59
    }
60
       
61

Output

1
2
    
3
    Enter initial guesses, number of iterations and maximum iterations error: 
4
    1 2 14 0.0001
5
    Iteration number 1: x = 1.125000
6
    Iteration number 2: x = 1.177982
7
    Iteration number 3: x = 1.199415
8
    Iteration number 4: x = 1.207914
9
    Iteration number 5: x = 1.211257
10
    Iteration number 6: x = 1.212568
11
    Iteration number 7: x = 1.213082
12
    Iteration number 8: x = 1.213282
13
    Iteration number 9: x = 1.213361
14
    Iteration number 10: x = 1.213392
15
    Iteration number 11: x = 1.213404
16
    Iteration number 12: x = 1.213409
17
    Iteration number 13: x = 1.213410
18
            
19
    After 14 iterations root = 1.213411"
20
21
            

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

How to run Regula-Falsi Method program in C-Language?

To run Regula-Falsi Method program in C-Language, follow the below mentioned steps:

  1. Copy the Code: Press the copy button in the code block to copy the complete code.
  2. Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
  3. Paste the code: Paste the copied code into Turbo C++’s editor.
  4. Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
  5. Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
  6. Enter the Input: Provide the required inputs or your desired inputs, if applicable.
  7. Receive the Result: Get the result of the program on the screen based on the provided inputs.

More About the Regula Falsi Method

Discovered by: Babylonian, Egyptian, and Chinese mathematicians,

Discovered Year: In the 13th Century.

Regula Falsi is a numerical method also known as the False Position method, which is used to find the root of non-linear equations like f(x)=0.

It should have an interval at the starting point, like f(a) x f(b) <0.

How does it work?

Regula Falsi method connects two points with a straight line (secant line) and detects the exact intersection point with the x-axis. And this intersection point could be considered a new root: c = (a · f (b) – b · f (a))/f(b)-f(a).

Why Regula Falsi?

Because Regula Falsi method accepts the exact function values to find out the root instead of blinding mid-point detection like bisection method.

Real-life applications:

  1. Chemical & Process Engineering: To solve the non-linear thermodynamic equations and reaction rates.
  2. Engineering & Design: To get the exact value of any structure or mechanism for the design parameters, as well as to find the generated convergence.
  3. To get the soil settlement in big structures like a dam, bridges, or high-rise buildings

Limitations

One-sided convergence: The biggest problem of the Regula Falsi method is that if one-sided curve is larger, the method gets stuck at that side, and on the opposite side it works extremely slowly; as a result, it is very slow in that case.

Alternate Methods of Regula Falsi Methods

  1. The modified regula falsi method solves this stuck issue by reducing the weight of the stagnant side.
  2. The secant method solves this problem by removing the bracketing condition; as a result, it works faster.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.