aboutsummaryrefslogtreecommitdiff
path: root/hw3/Lorenz.jl
blob: c4c501d613897ce8c6d816d3e73b4dd9624dea9a (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using DifferentialEquations
using Plots

r_max = 160
r_steps = 320

sim_time = 1000.0

# STEP 1, go over each value of r and store the results
# Simulate Lorenz 63 system and investigate sensitivity to initial conditions
function tendency!(du, u, p, t)
    x,y,z = u
    σ,ρ,β = p
    
    du[1] = dx = σ*(y-x)
    du[2] = dy = x*(ρ-z) - y
    du[3] = dz = x*y - β*z
end


# make a linspace for these values of r
r_values = range(0, r_max, length=r_steps)
sols = []
for i in 1:r_steps
    r = r_values[i]
    p = [10.0, r, 8/3] # parameters of the Lorentz 63 system
    tspan = (0.0, sim_time)

    u0 = [1.0, 0.0, 0.0]
    prob = ODEProblem(tendency!, u0, tspan, p)
    sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8) # control simulation

    push!(sols, sol)

    println("r=$r")
end

# STEP 2, map data to arrays where plane crosses the x-axis
r_maxes = []
z_maxes = []
r_mins = []
z_mins = []
for i in 1:r_steps
    println("i: ", length(sols[i].t))
    z_values = sols[i][3, :]
    # iterate over to find the local maxima and minima
    # take off the first 300 values to avoid transient
    for j in 301:length(z_values)-1
        if z_values[j] > z_values[j-1] && z_values[j] > z_values[j+1]
            push!(r_maxes, r_values[i])
            push!(z_maxes, z_values[j])
        end
        if z_values[j] < z_values[j-1] && z_values[j] < z_values[j+1]
            push!(r_mins, r_values[i])
            push!(z_mins, z_values[j])
        end
    end
end

# println("r_maxes: ", r_maxes)
# println("z_maxes: ", z_maxes)

# STEP 3, plot the bifurcation diagram
plot(r_maxes, z_maxes, seriestype = :scatter, mc=:blue, ms=.25, ma=0.25, label="Maxima")
plot!(r_mins, z_mins, seriestype = :scatter, mc=:green, ms=.25, ma=0.25, label="Minima")

savefig("hw3/test3.png")