-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDerivative.java
More file actions
25 lines (23 loc) · 883 Bytes
/
Copy pathDerivative.java
File metadata and controls
25 lines (23 loc) · 883 Bytes
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
public class Derivative{
public static void main(String[] args){
System.out.println(newt(4, 0.03125, 0.001));
}
public static double f(double x){//function method
return x*x*x*x-3*x*x*x+x-11;//returns function output(in this case, x^2-17)
}
//newton's method; approximate the closest zero of a function
//precondition: x is not a local maximum or minimum
//parameters: initial approximation x, double h, error epsilon
public static double newt(double x, double h, double epsilon){
double x1= x;
x = x + epsilon + epsilon;
while(Math.abs(x1-x)>epsilon){//while difference greater than error
x=x1;
x1=x - f(x)/deriv(x, h);//newton's method
}
return x1;//returns current approximation
}
public static double deriv(double x, double h){//derivative method
return (f(x+h)-f(x-h))/(2*h);//returns numerical derivative
}
}