How to resolve the algorithm Roots of a function step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Roots of a function step by step in the Java programming language

Table of Contents

Problem Statement

Create a program that finds and outputs the roots of a given function, range and (if applicable) step width.
The program should identify whether the root is exact or approximate.

For this task, use:     ƒ(x)   =   x3 - 3x2 + 2x

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Roots of a function step by step in the Java programming language

The Java code you provided defines a class called Roots that provides a static method printRoots for finding and printing the roots of a function within a given range. Here's a breakdown of how the code works:

  1. Interface Function: The code defines an interface Function with a single method f, which takes a double as input and returns a double as output. This interface represents a mathematical function whose roots we want to find.

  2. sign Method: The sign method takes a double as input and returns -1 if the number is negative, 1 if it's positive, and 0 if it's zero. This method is used to determine the sign of a number.

  3. printRoots Method: The printRoots method takes four parameters:

    • f: An instance of the Function interface representing the function whose roots we want to find.
    • lowerBound: The lower bound of the range to search for roots.
    • upperBound: The upper bound of the range to search for roots.
    • step: The step size for iterating through the range.

    The method starts by initializing two variables x and y to the lowerBound and the value of f(lowerBound), respectively. It also initializes two more variables ox and oy to store the previous values of x and y.

    The method then enters a for loop that iterates through the range from lowerBound to upperBound in steps of step. Inside the loop, it calculates the current value of y by calling f(x) and updates s to be the sign of y.

    If y is zero, it means that x is a root of the function, and the method prints x using System.out.println.

    If s is not equal to os (the sign of the previous value of y), it means that the function changes sign at x, and there is likely a root in that vicinity. The method calculates an approximate root using linear interpolation and prints it as ~"cx using System.out.println.

    After each iteration, it updates ox, oy, and os to store the previous values of x, y, and s.

  4. main Method: In the main method, an instance of the Function interface is created using an anonymous inner class to represent the polynomial function x^3 - 3x^2 + 2x. The printRoots method is called with this function instance, along with the range [-1.0, 4] and the step size 0.002.

When you run this program, it will print the approximate roots of the polynomial function x^3 - 3x^2 + 2x within the range [-1.0, 4] using a step size of 0.002. In this case, it will print three roots:

~0.0
1.0
3.0

Source code in the java programming language

public class Roots {
    public interface Function {
	public double f(double x);
    }

    private static int sign(double x) {
	return (x < 0.0) ? -1 : (x > 0.0) ? 1 : 0;
    }

    public static void printRoots(Function f, double lowerBound,
				  double upperBound, double step) {
	double x = lowerBound, ox = x;
	double y = f.f(x), oy = y;
	int s = sign(y), os = s;

	for (; x <= upperBound ; x += step) {
	    s = sign(y = f.f(x));
	    if (s == 0) {
		System.out.println(x);
	    } else if (s != os) {
		double dx = x - ox;
		double dy = y - oy;
		double cx = x - dx * (y / dy);
		System.out.println("~" + cx);
	    }
	    ox = x; oy = y; os = s;
	}
    }

    public static void main(String[] args) {
	Function poly = new Function () {
	    public double f(double x) {
		return x*x*x - 3*x*x + 2*x;
	    }
	};
	printRoots(poly, -1.0, 4, 0.002);
    }
}


  

You may also check:How to resolve the algorithm Long multiplication step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Inverted index step by step in the Rust programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the Java programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Best shuffle step by step in the Prolog programming language