-
Notifications
You must be signed in to change notification settings - Fork 0
/
lorenz.scala
52 lines (35 loc) · 1.06 KB
/
lorenz.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//> using scala 3.3.0
//> using dep org.scalanlp::breeze:2.1.0
//> using dep org.scalanlp::breeze-viz:2.1.0
/*
lorenz.scala
Use Breeze to integrate the Lorenz system and display the results in
a window on the console
https://en.wikipedia.org/wiki/Lorenz_system
scala-cli lorenz.scala
*/
import breeze.linalg.*
import breeze.numerics.*
import breeze.integrate.*
import breeze.plot.*
type DVD = DenseVector[Double]
object Lorenz:
@main
def run() =
println("Lorenz")
val rhs = lorenzRhs(28.0, 10.0, 8.0/3.0)
val ode = new HighamHall54Integrator(0.0001, 0.1)
val res = ode.integrate((x, t) => rhs(x),
DenseVector(1.0, 0.5, 0.1),
linspace(0, 100, 100000).toArray)
val fig = Figure("Lorenz attractor")
fig.width = 800
fig.height = 600
val p = fig.subplot(1,1,0)
p += plot(res map (_(0)), res map (_(1)))
fig.refresh()
fig.saveas("lorenz.pdf")
def lorenzRhs(rho: Double, sigma: Double, beta: Double)(v: DVD): DVD =
DenseVector( sigma*(v(1) - v(0)), v(0)*(rho - v(2)) - v(1),
v(0)*v(1) - beta*v(2) )
// eof