//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** @author John Miller * @version 1.6 * @date Wed Dec 23 14:12:56 EST 2020 * @see LICENSE (MIT style license file). * * @title Simulate a System of Dynamic Equations over Time */ package scalation.dynamics import scala.math.exp import scalation.linalgebra.{MatriD, MatrixD, VectoD, VectorD} import scalation.math.double_exp import scalation.plot.Plot //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** The `DynamicEq` class may be used for determining trajectories 'x_t' from a * system of dynamic equations. *

* x_t = f(t) *

* 'f(t)' is the vector function of time. * @param f the vector-valued function of time * @param d the number of dimensions */ class DynamicEq (f: Double => VectoD, d: Int = 2) extends Error { def eval (t: Double): VectoD = f(t) def trajectory (t0: Double, t1: Double, n: Int): MatriD = { val traj = new MatrixD (n, d) val dt = (t1 - t0) / n.toDouble var t = t0 for (i <- 0 until n) { traj(i) = f(t) t += dt } // for traj } // trajectory } // DynamicEq class //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** The `DynamicEqTest` object to test the `DynamicEq` class using example at * in Introduction to Computational Data Science using ScalaTion, section 13.1. * > runMain scalation.dynamics.DynamicEqTest */ object DynamicEqTest extends App { val g = 9.807 // graivational constant val mps = 60.0 // meters per second (mps) def f (t: Double): VectorD = VectorD (-0.5 * g * t~^2 + mps * t, -g * t + mps) val dyn = new DynamicEq (f) val n = 100 val te = 12.0 val t = VectorD.range (0, n) * (te / n.toDouble) val trj = dyn.trajectory (0.0, te, n) new Plot (t, trj.col(0), trj.col(1), "Plot (x_t0, x_t1) vs. t") } // DynamicEqTest object