Programming Environment for Numerical Computing

Ordinary Differential Equations

With the Comet ODE solver can integrate differential system given:
the system functions, the initial values and the independent variable value (x, t, ...).
It is not necessary to provide the Jacobian which is approximated by the solver.

The ODE solver:

y = ode.solve(func, y0, t0, t1, tol) Integrates ODE system, where:
* func: name of the ODE system function.
func defined as func(t, y, ydot) where:

ydot vector updated with respect to y and t:
ODE System

* y0 table containing the initial values
* t0 value for y0
* t1 value to integrate for
* tol the solver tolerance (optional)

Example:

Copy the following script in the editor and click Run (or press F12 on Windows and Linux)

-- Damped Oscillator: y'' + c y' + k y = 0

local c = 0.25
local k = 1

-- Oscillator function
function func(t, y, ydot)
   ydot[1] = y[2]
   ydot[2] = (-c * y[2]) + (-k * y[1])
end

-- Solve with 0.1 seconds as interval
y0 = {2, 0}
t0 = 0
dt = 0.1
t1 = t0 + dt
tol = 1e-3
tm = {}
y = {}
dy = {}
for i = 1,100,1 do
   yy = ode.solve("func", y0, t0, t1, tol)
   tm[i] = t1
   y[i] = yy[1]
   dy[i] = yy[2]
   t0 = t1
   t1 = t1 + dt
   y0[1] = yy[1]
   y0[2] = yy[2]
end

-- plot solution y and y'
p = plot.new(800, 600)
plot.set(p, "title", "Damped Oscillator")
plot.add(p, tm, y)
plot.add(p, tm, dy)
plot.set(p, "xlabel", "time (s)")
plot.set(p, "ylabel", "amplitude")
plot.set(p, 1, "legend", "y(t)")
plot.set(p, 1, "style", "-")
plot.set(p, 1, "color", "red")
plot.set(p, 2, "legend", "y'(t)")
plot.set(p, 2, "style", "-")
plot.set(p, 2, "color", "blue")
plot.update(p)

 

 Copyright(C) 2010-2017 Dr. Sidi HAMADY