Last active
August 29, 2015 14:14
-
-
Save Denton-L/e2b5c2ca0c77f29f0556 to your computer and use it in GitHub Desktop.
Euler's Method for Estimating Differential Equations
This file contains hidden or 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
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