Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Denton-L
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Denton-L/e2b5c2ca0c77f29f0556 to your computer and use it in GitHub Desktop.
Save Denton-L/e2b5c2ca0c77f29f0556 to your computer and use it in GitHub Desktop.
Euler's Method for Estimating Differential Equations
public class EulersMethod {
private double x1;
private double y1;
private double dx;
private double x2;
private Differential dydx;
public interface Differential {
double equation(double x, double y);
}
public EulersMethod(double x1, double y1, double dx, double x2, Differential dydx) {
this.x1 = x1;
this.y1 = y1;
this.dx = dx;
this.x2 = x2;
this.dydx = dydx;
}
public double calculate() {
double y = y1;
for (double x = x1; x < x2; x += dx) {
y += dydx.equation(x,y) * dx;
}
return y;
}
public static void main(String[] args) {
System.out.println(new EulesMethod(0.0, 1.0, 0.1, 0.5, (x, y) -> y + x * y).calculate());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment