01 /*
02 * $Id: VdpolRk4StepLoop.java,v 1.4 2008/02/02 03:06:25 koga Exp $
03 *
04 * Copyright (C) 2004 Koga Laboratory. All rights reserved.
05 *
06 */
07 package matxbook.chap21;
08
09 import org.mklab.nfc.matrix.DoubleMatrix;
10 import org.mklab.nfc.matrix.Matrix;
11 import org.mklab.nfc.ode.DifferentialEquationSolver;
12 import org.mklab.nfc.ode.DifferentialEquation;
13 import org.mklab.nfc.ode.SolverStopException;
14 import org.mklab.nfc.ode.RungeKutta4;
15
16
17 /**
18 * ヴァンデルポール方程式を4次のルンゲ・クッタ法でステップ毎に解くクラスです。
19 * @author koga
20 * @version $Revision: 1.4 $, 2004/04/22
21 */
22 public class VdpolRk4StepLoop {
23
24 /**
25 * メインメソッド
26 *
27 * @param args コマンドライン引数
28 * @throws SolverStopException ソルバーが停止された場合
29 */
30 public static void main(String[] args) throws SolverStopException {
31 DifferentialEquation eq = new Vdpol();
32 Matrix x0 = new DoubleMatrix(new double[] {0, 0.25}).transpose();
33 double t0 = 0.0;
34 double t1 = 20.0;
35 double timeStep = 0.1;
36 int n = (int)((t1 - t0) / timeStep);
37 DoubleMatrix tt = new DoubleMatrix(1, n + 1);
38 DoubleMatrix xx = new DoubleMatrix(2, n + 1);
39 DifferentialEquationSolver solver = new RungeKutta4();
40
41 Matrix x = x0;
42 double t = t0;
43 xx.setColumnVector(1, x);
44 tt.setElement(1, t);
45
46 for (int i = 1; i <= n; i++) {
47 x = solver.step(eq, t, x, timeStep);
48 t = t + timeStep;
49 xx.setColumnVector(i + 1, x);
50 tt.setElement(i + 1, t);
51 }
52 }
53 }
|