Created
September 2, 2022 02:04
-
-
Save jaredtbates/5a362554cd16e4563da531787d1e99ec to your computer and use it in GitHub Desktop.
A little differentiation class I wrote for my Numerical Methods homework, a bit overkill, but had fun doing it!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.Class; | |
import java.lang.Math; | |
import java.util.function.Function; | |
public class Differentiation { | |
private static final double TARGET_ERROR = 1 * Math.pow(10, -8); | |
private static final Function<Double, Double> FUNCTION = (Double x) -> ((7 * Math.pow(x, 3)) - (5 * x) + 1) / ((2 * Math.pow(x, 4)) + Math.pow(x, 2) + 1); | |
private static final double X = 3; | |
private static final double H = 1; | |
public static void main(String[] args) { | |
System.out.println(); | |
for (DifferentiatorType type : DifferentiatorType.values()) { | |
Differentiator differentiator = createDifferentiator(type); | |
double start = X; | |
double step = H; | |
Double error = null; | |
Double prev = null; | |
do { | |
double next = differentiator.differentiate(FUNCTION, start, step); | |
if (prev != null) { | |
error = (prev - next) / next; | |
} | |
prev = next; | |
step /= 2; | |
} while (error == null || Math.abs(error) > TARGET_ERROR); | |
System.out.println("Differentiator: " + type.getName()); | |
System.out.println(String.format("Answer: %.16f", prev)); | |
System.out.println(String.format("Error: %.16f", error)); | |
System.out.println(); | |
} | |
} | |
static Differentiator createDifferentiator(DifferentiatorType type) { | |
try { | |
return type.getDifferentiator().getDeclaredConstructor().newInstance(); | |
} catch (Exception e) { | |
System.err.println("Could not create differentiator of type: " + type); | |
System.err.println("Exiting..."); | |
System.exit(1); | |
return null; | |
} | |
} | |
} | |
enum DifferentiatorType { | |
FORWARD("Forward", ForwardDifferentiator.class), | |
BACKWARD("Backward", BackwardDifferentiator.class), | |
CENTER_DIVIDED("Center Divided", CenterDividedDifferentiator.class); | |
private String name; | |
private Class<? extends Differentiator> differentiator; | |
private DifferentiatorType(String name, Class<? extends Differentiator> differentiator) { | |
this.name = name; | |
this.differentiator = differentiator; | |
} | |
public String getName() { | |
return name; | |
} | |
public Class<? extends Differentiator> getDifferentiator() { | |
return differentiator; | |
} | |
} | |
interface Differentiator { | |
double differentiate(Function<Double, Double> f, double x, double h); | |
} | |
class ForwardDifferentiator implements Differentiator { | |
@Override | |
public double differentiate(Function<Double, Double> f, double x, double h) { | |
return (f.apply(x + h) - f.apply(x)) / h; | |
} | |
} | |
class BackwardDifferentiator implements Differentiator { | |
@Override | |
public double differentiate(Function<Double, Double> f, double x, double h) { | |
return (f.apply(x) - f.apply(x - h)) / h; | |
} | |
} | |
class CenterDividedDifferentiator implements Differentiator { | |
@Override | |
public double differentiate(Function<Double, Double> f, double x, double h) { | |
return (f.apply(x + h) - f.apply(x - h)) / (2 * h); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment