Java Program to Implement Regular 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.

/**
 * Java Program to Implement Regular Falsi Algorithm
 **/
 
public class RegularFalsi
{
    /** function to find root for **/
    public double f(double x)
    {
        /** make your own function here but accordingly change (s, t) **/
 
         return Math.cos(x) - x * x * x;
 
        // return x * x * x - 3 * x + 4;
        // return Math.cos(x) - 3 * x + 1;
        // return 2 * x - Math.log(x)/Math.log(10) - 7;
        // return x * x - Math.log(x) - 12;
    }
    /** function to find root **/
    public double findRoot(double s, double t, double e, int m)
    {
        double r = 0.0,fr;
        int n, side = 0;
 
        /** starting values at endpoints of interval **/
        double fs = f(s);
        double ft = f(t);
 
        for (n = 0; n < m; n++)
        {
 
            r = (fs * t - ft * s) / (fs - ft);
            if (Math.abs(t - s) < e * Math.abs(t + s)) 
                break;
            fr = f(r);
 
            if (fr * ft > 0)
            {
                /** fr and ft have same sign, copy r to t **/
                t = r; 
                ft = fr;
                if (side == -1) 
                    fs /= 2;
                side = -1;
            }
            else if (fs * fr > 0)
            {
                /** fr and fs have same sign, copy r to s **/
                s = r;  
                fs = fr;
                if (side == +1) 
                    ft /= 2;
                side = +1;
            }
            else
            {
                /** fr * f_ very small (looks like zero) **/
                break;
            } 
        }
        return r;
    }
    /** Main function **/
    public static void main(String[] args)
    {
        System.out.println("Regular Falsi Test ");
 
        RegularFalsi rf = new RegularFalsi();
        /** lower limit **/
        double s = 0;
        /** upper limit **/
        double t = 1;
        /** half of upper bound for relative error **/
        double e = 5E-15;
        /** number of iterations **/
        int iterations = 100;
 
        System.out.println("\nRoot : "+ rf.findRoot(s, t, e, iterations));
    }
}

Output:

Regular Falsi Test
 
Root : 0.8654740331016145

Related posts:

How to Remove the Last Character of a String?
Introduction to Eclipse Collections
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Java Program to Implement Find all Forward Edges in a Graph
Custom Error Pages with Spring MVC
Quick Guide to @RestClientTest in Spring Boot
Guide to PriorityBlockingQueue in Java
Spring Boot - Hystrix
Spring Security – security none, filters none, access permitAll
Intro to the Jackson ObjectMapper
Java Program to Implement Counting Sort
Spring Boot: Customize the Jackson ObjectMapper
How to Define a Spring Boot Filter?
How to Kill a Java Thread
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Hướng dẫn Java Design Pattern – Transfer Object
Exploring the Spring Boot TestRestTemplate
Spring RestTemplate Error Handling
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Spring 5 Testing with @EnabledIf Annotation
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java Program to Represent Linear Equations in Matrix Form
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Java Program to Implement ConcurrentLinkedQueue API
Java – Create a File
Extra Login Fields with Spring Security
Java 8 and Infinite Streams
Check if there is mail waiting
Rate Limiting in Spring Cloud Netflix Zuul
Apache Camel with Spring Boot
Java Program to Implement Doubly Linked List