How to resolve the algorithm Roots of a function step by step in the Java programming language
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:
-
Interface
Function
: The code defines an interfaceFunction
with a single methodf
, which takes a double as input and returns a double as output. This interface represents a mathematical function whose roots we want to find. -
sign
Method: Thesign
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. -
printRoots
Method: TheprintRoots
method takes four parameters:f
: An instance of theFunction
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
andy
to thelowerBound
and the value off(lowerBound)
, respectively. It also initializes two more variablesox
andoy
to store the previous values ofx
andy
.The method then enters a
for
loop that iterates through the range fromlowerBound
toupperBound
in steps ofstep
. Inside the loop, it calculates the current value ofy
by callingf(x)
and updatess
to be the sign ofy
.If
y
is zero, it means thatx
is a root of the function, and the method printsx
usingSystem.out.println
.If
s
is not equal toos
(the sign of the previous value ofy
), it means that the function changes sign atx
, and there is likely a root in that vicinity. The method calculates an approximate root using linear interpolation and prints it as~"cx
usingSystem.out.println
.After each iteration, it updates
ox
,oy
, andos
to store the previous values ofx
,y
, ands
. -
main
Method: In themain
method, an instance of theFunction
interface is created using an anonymous inner class to represent the polynomial functionx^3 - 3x^2 + 2x
. TheprintRoots
method is called with this function instance, along with the range[-1.0, 4]
and the step size0.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