-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewtonRaphson.java
60 lines (51 loc) · 1.58 KB
/
newtonRaphson.java
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
/**
* A program using the Newton-Raphson method to approximate a solution.
*
* @author William Carr
* @version 11.21.2022
*/
public class newtonRaphson {
/**
* The main() function to run the other functions.
*
* @param args
*/
public static void main(String[] args) {
double startPt = 1; //x sub 1 for our purposes
int approximations = 10; //how many approximations do we attempt?
System.out.println("x sub 1:\t" + startPt);
thirdNewtonRaphsonMethod(startPt, approximations);
}
/**
* Uses the Newton-Raphson method to find an estimated value.
*
* @param xN The x sub 1 to be used for this equation.
* @param approximation The highest level of approximation to attempt. Must be >= 1.
* @return The base value after doing the method a certain number(<iterations>) of times
*/
public static double thirdNewtonRaphsonMethod(double xN, int approximation) {
for(int i = 1; i < approximation; i++) { // xN is x sub 1, so we skip the first index
xN = xN - function(xN)/derivative(xN);
System.out.println("x sub " + (i+1) + ":\t" + xN);
}
return xN;
}
/**
* Formula for original function. Must exist at x.
*
* @param x Any x in the domain of f(x).
* @return f(x)
*/
public static double function(double x) {
return Math.cos(x) - x;
}
/**
* Formula for the derivative of the function. Must be differentiable at x.
*
* @param x Any x in the domain of f(x) and f'(x).
* @return f'(x)
*/
public static double derivative(double x) {
return -1 * Math.sin(x) - 1;
}
}