-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewtonRaphson.java
More file actions
65 lines (46 loc) · 2.26 KB
/
Copy pathNewtonRaphson.java
File metadata and controls
65 lines (46 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package MohamedMohamedIssa2023123341_Say04;
import java.util.function.Function;
import java.util.Scanner;
public class NewtonRaphson {
public static double newtonRaphson(Function<Double, Double> f, Function<Double, Double> df, double initialGuess, double tolerance, int maxIterations) {
double x = initialGuess;
int iteration = 0;
double previousX;
double trueValue = initialGuess;
System.out.println("--------------------------------------------------");
System.out.println("| i | x_i | ε_a (%) | ε_t (%) |");
System.out.println("--------------------------------------------------");
while (iteration < maxIterations) {
previousX = x;
double newX = x - f.apply(x) / df.apply(x);
double approxError = (iteration == 0) ? 100.0 : Math.abs((newX - previousX) / newX) * 100;
double trueError = Math.abs((trueValue - newX) / trueValue) * 100;
System.out.printf("| %2d | %10.6f | %8.2f | %8.2f |%n", iteration, newX, approxError, trueError);
if (Math.abs(f.apply(newX)) < tolerance) {
System.out.println("--------------------------------------------------");
return newX;
}
x = newX;
iteration++;
}
System.out.println("--------------------------------------------------");
throw new RuntimeException("Newton-Raphson method did not converge within the given iterations.");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Function<Double, Double> f = x -> x * x - 4;
Function<Double, Double> df = x -> 2 * x;
System.out.print("Enter initial guess: ");
double initialGuess = scanner.nextDouble();
System.out.print("Enter tolerance: ");
double tolerance = scanner.nextDouble();
System.out.print("Enter maximum iterations: ");
int maxIterations = scanner.nextInt();
try {
double root = newtonRaphson(f, df, initialGuess, tolerance, maxIterations);
System.out.println("Approximate root: " + root);
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
}
}