Created
August 8, 2014 09:14
-
-
Save thomasdarimont/9fb679ad7884800475df to your computer and use it in GitHub Desktop.
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
package de.tutorials.training.math; | |
import de.tutorials.training.JavaFxUtil; | |
import javafx.application.Application; | |
import javafx.collections.ObservableList; | |
import javafx.scene.Node; | |
import javafx.scene.Scene; | |
import javafx.scene.chart.LineChart; | |
import javafx.scene.chart.NumberAxis; | |
import javafx.scene.chart.XYChart; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.StackPane; | |
import javafx.stage.Stage; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class DglSolverExample { | |
public static void main(String[] args) throws Exception { | |
Dgl dgl = // | |
// new DglExample1(); | |
new DglExample2(); | |
DglSolver solver = new EulerDglSolver(0.01); | |
SolverParams params = new SolverParams(0.0, 1, 10); | |
List<Point> points = solver.solve(dgl, params); | |
System.out.println(points); | |
Plotter plotter = new Plotter(points, params); | |
JavaFxUtil.runApp(plotter, args); | |
} | |
static class Plotter extends Application { | |
private final Point maxX; | |
private final Point minX; | |
private List<Point> points; | |
private SolverParams params; | |
public Plotter(List<Point> points, SolverParams params) { | |
this.points = points; | |
this.params = params; | |
maxX = points.stream().max((p1, p2) -> Double.compare(p1.x, p2.x)).get(); | |
minX = points.stream().min((p1, p2) -> Double.compare(p1.x, p2.x)).get(); | |
} | |
@Override | |
public void start(Stage stage) throws Exception { | |
/* | |
StackPane pane = new StackPane(); | |
Node label = new Label("Hello World!"); | |
pane.getChildren().add(label); | |
stage.setScene(new Scene(pane,250,75)); | |
*/ | |
//defining the axes | |
NumberAxis tAxis = new NumberAxis(); | |
tAxis.setLabel("t"); | |
NumberAxis xAxis = new NumberAxis(); | |
xAxis.setLabel("x"); | |
//creating the chart | |
LineChart<Number, Number> lineChart = | |
new LineChart<Number, Number>(tAxis, xAxis); | |
lineChart.setTitle("DGL"); | |
//defining a series | |
XYChart.Series series = new XYChart.Series(); | |
series.setName("DGL Eulers Method"); | |
//populating the series with data | |
ObservableList data = series.getData(); | |
for (Point point : points) { | |
data.add(new XYChart.Data(point.t, point.x)); | |
} | |
System.out.println("done"); | |
Scene scene = new Scene(lineChart, 800, 600); | |
lineChart.getData().add(series); | |
stage.setScene(scene); | |
stage.setTitle("Plotter"); | |
stage.setResizable(true); | |
stage.centerOnScreen(); | |
stage.show(); | |
} | |
} | |
static interface Dgl { | |
double eval(double t, double x); | |
} | |
static class DglExample1 implements Dgl { | |
public double eval(double t, double x) { | |
//x(t) => sin(t) + x(t)^2 | |
return Math.sin(t) + x * x; | |
} | |
} | |
static class DglExample2 implements Dgl { | |
public double eval(double t, double x) { | |
//x(t) => x * cos(t) | |
return x * Math.cos(t); | |
} | |
} | |
static class SolverParams { | |
private final double t0; | |
private final double x0; | |
private final double t1; | |
SolverParams(double t0, double x0, double t1) { | |
this.t0 = t0; | |
this.x0 = x0; | |
this.t1 = t1; | |
} | |
} | |
static interface DglSolver { | |
List<Point> solve(Dgl dgl, SolverParams solverParams); | |
} | |
static class EulerDglSolver implements DglSolver { | |
private final double stepSize; | |
EulerDglSolver(double stepSize) { | |
this.stepSize = stepSize; | |
} | |
@Override | |
public List<Point> solve(Dgl dgl, SolverParams solverParams) { | |
List<Point> points = new ArrayList<>(); | |
double x = solverParams.x0; | |
double t = solverParams.t0; | |
points.add(new Point(x, t)); | |
while (t < solverParams.t1) { | |
x += dgl.eval(t, x) * stepSize; | |
t += stepSize; | |
points.add(new Point(x, t)); | |
} | |
return points; | |
} | |
} | |
static class Point { | |
private final double x; | |
private final double t; | |
Point(double x, double t) { | |
this.x = x; | |
this.t = t; | |
} | |
@Override | |
public String toString() { | |
return "{" + x + "," + t + '}'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment