# Java Program to Implement Regula Falsi Algorithm

«
»
This is a Java Program to Implement Regular Falsi Algorithm. Regular Falsi method is used for finding roots of functions.

Here is the source code of the Java Program to Implement Regular Falsi Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

1. `/**`
2. ` * Java Program to Implement Regular Falsi Algorithm`
3. ` **/`
4. ` `
5. `public class RegularFalsi`
6. `{`
7. `    /** function to find root for **/`
8. `    public double f(double x)`
9. `    {`
10. `        /** make your own function here but accordingly change (s, t) **/`
11. ` `
12. `         return Math.cos(x) - x * x * x;`
13. ` `
14. `        // return x * x * x - 3 * x + 4;`
15. `        // return Math.cos(x) - 3 * x + 1;`
16. `        // return 2 * x - Math.log(x)/Math.log(10) - 7;`
17. `        // return x * x - Math.log(x) - 12;`
18. `    }`
19. `    /** function to find root **/`
20. `    public double findRoot(double s, double t, double e, int m)`
21. `    {`
22. `        double r = 0.0,fr;`
23. `        int n, side = 0;`
24. ` `
25. `        /** starting values at endpoints of interval **/`
26. `        double fs = f(s);`
27. `        double ft = f(t);`
28. ` `
29. `        for (n = 0; n < m; n++)`
30. `        {`
31. ` `
32. `            r = (fs * t - ft * s) / (fs - ft);`
33. `            if (Math.abs(t - s) < e * Math.abs(t + s)) `
34. `                break;`
35. `            fr = f(r);`
36. ` `
37. `            if (fr * ft > 0)`
38. `            {`
39. `                /** fr and ft have same sign, copy r to t **/`
40. `                t = r; `
41. `                ft = fr;`
42. `                if (side == -1) `
43. `                    fs /= 2;`
44. `                side = -1;`
45. `            }`
46. `            else if (fs * fr > 0)`
47. `            {`
48. `                /** fr and fs have same sign, copy r to s **/`
49. `                s = r;  `
50. `                fs = fr;`
51. `                if (side == +1) `
52. `                    ft /= 2;`
53. `                side = +1;`
54. `            }`
55. `            else`
56. `            {`
57. `                /** fr * f_ very small (looks like zero) **/`
58. `                break;`
59. `            } `
60. `        }`
61. `        return r;`
62. `    }`
63. `    /** Main function **/`
64. `    public static void main(String[] args)`
65. `    {`
66. `        System.out.println("Regular Falsi Test ");`
67. ` `
68. `        RegularFalsi rf = new RegularFalsi();`
69. `        /** lower limit **/`
70. `        double s = 0;`
71. `        /** upper limit **/`
72. `        double t = 1;`
73. `        /** half of upper bound for relative error **/`
74. `        double e = 5E-15;`
75. `        /** number of iterations **/`
76. `        int iterations = 100;`
77. ` `
78. `        System.out.println("\nRoot : "+ rf.findRoot(s, t, e, iterations));`
79. `    }`
80. `}`

```Regular Falsi Test

Root : 0.8654740331016145```

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now! 