Skip to content

Commit 8528fc8

Browse files
Update Sindy_map.jl
1 parent ce3c806 commit 8528fc8

File tree

1 file changed

+20
-37
lines changed

1 file changed

+20
-37
lines changed

Identifying Nonlinear Dynamics/Julia Scripts/Sindy_map.jl

+20-37
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,13 @@ end
3939

4040
function generate_traj(integrator, x_init, tspan, dt)
4141
# returns back trajectories for different x0
42-
xdat = zeros((length(x_init), 2, Int((tspan[2]-tspan[1])/dt + 1)))
42+
xdat = zeros((length(x_init), Int((tspan[2]-tspan[1])/dt + 1),2))
4343
for j in 1:length(x_init)
4444
reinit!(integrator, x_init[j])
4545
n = 1;
4646
for i in tspan[1]:dt:tspan[2]
47+
xdat[j, n,:] = integrator.u;
4748
step!(integrator, dt, true);
48-
xdat[j, :, n] = integrator.u;
49-
5049
n+=1;
5150
end
5251
end
@@ -70,54 +69,46 @@ if numTraj >= 2
7069
end
7170
end
7271

73-
# tspan = (0.0,100.0*dt);
74-
# this is generating wrong trajectories
7572
xdat = generate_traj(integrator, x_init, tspan, dt);
7673

77-
plot(xdat[3,1,1:10000])
74+
plot(xdat[3,1:10000,1])
7875

7976
# Aggregate stroboscopic section data
8077
#Counting parameter
8178
#Initialize
82-
Psec = Float64[]
79+
Psec = Float64[0]
8380
PsecNext = Float64[]
8481

8582
#Create Poincaré section data
8683
for i in 1:numTraj
87-
for j in 1:(size(xdat, 3) - 1) # Loop over all time steps except the last
84+
for j in 1:(size(xdat, 2) - 1) # Loop over all time steps except the last
8885
if (j == 1) && (i > 1) # Trajectories start in the section
89-
push!(Psec, xdat[i, 1, j]) # Append to Psec
90-
elseif (mod(xdat[i, 2, j], 2π / ω) >= 2π / ω - dt && mod(xdat[i, 2, j+1], 2π / ω) <= dt)
91-
push!(Psec, xdat[i, 1, j + 1]) # nth iterate
92-
push!(PsecNext, xdat[i, 1, j + 1]) # (n+1)st iterate
86+
push!(Psec, xdat[i, j, 1]) # Append to Psec
87+
elseif (mod(xdat[i, j, 2,], 2π / ω) >= 2π / ω - dt && mod(xdat[i, j+1, 2], 2π / ω) <= dt)
88+
push!(Psec, xdat[i, j+1, 1,]) # nth iterate
89+
push!(PsecNext, xdat[i, j+1, 1,]) # (n+1)st iterate
9390
end
9491
end
92+
Psec = Psec[1:length(Psec)-1];
9593
end
96-
Psec = Psec[1:length(Psec)-2];
9794
# Create the recurrence data
9895
xn = Psec;
9996
xnp1 = PsecNext;
10097

101-
size(xn), size(xnp1)
102-
10398
# Create Θ matrix from monomial upto degree 5
10499
polyorder = 5; # change maximal polynomial order of monomials in library
105100
Θ = ones((polyorder+1,length(xn))); # constant term
106101

107-
# (print options below will not work if polyorder is not 5)
108102
for p = 1:polyorder
109103
Θ[p+1,:] = xn.^p;
110104
end
111105

112-
size(pinv(Θ))
113106
xnp1 = reshape(xnp1, (1,length(xnp1)));
114107

115108
total_dim = 1;
116-
Xi_new = sindy(xnp1, Θ,total_dim);
109+
Xi_new = sindy(xnp1, Θ,total_dim, 1e-5);
117110
mons = ["" "x" "x^2" "x^3" "x^4" "x^5"];
118-
print_model(Xi_new,mons, total_dim)
119-
120-
# Check why we are getting wrong answers here, probably the answer may lies in timespan, or in numerical accuracy or something else
111+
print_model(Xi_new,mons, total_dim, ["f(x)"])
121112

122113
## Hopf Normal Form ODE
123114
function Hopf!(du,u,p,t)
@@ -144,33 +135,27 @@ if numTraj >= 3
144135
end
145136
end
146137

147-
size(x_init)
148-
x_init[1]
149-
150138
prob = ODEProblem(Hopf!, x_init[1], tspan, om)
151139
integrator = init(prob, reltol = 1e-12, abstol = 1e-12)
152140
xdat = generate_traj(integrator, x_init, tspan, dt);
153-
154-
init_pt = 1;
155-
plot(xdat[init_pt,1,:]) # change init to see trajectories from different initial pt
141+
xdat = xdat[:,1:end-1,:];
156142

157143
# Aggregate Poincare section
158-
# Counting parameter
159-
count = 1;
160144
# Initialize
161145
Psec = Float64[];
162146
PsecNext = Float64[];
163147
push!(Psec, xdat[1,1,1]);
164148
# Create Poincare section data
165149
for i = 1:numTraj
166-
for j = 1:length(xdat[1,1,:])-1
167-
if (xdat[i,2,j] < 0) && (xdat[i,2,j+1] >= 0)
168-
push!(Psec,xdat[i,1,j+1]); #nth iterate
169-
push!(PsecNext, xdat[i,1,j+1]); #(n+1)st iterate
150+
for j = 1:length(xdat[1,:,1])-1
151+
if (xdat[i,j,2] < 0) && (xdat[i,j+1,2] >= 0)
152+
push!(Psec,xdat[i,j+1,1]); #nth iterate
153+
push!(PsecNext, xdat[i,j+1,1]); #(n+1)st iterate
170154
end
171155
end
172156
end
173157
# Create the recurrence data
158+
174159
Psec = Psec[1:length(Psec)-1];
175160
xn = Psec;
176161
xnp1 = PsecNext;
@@ -179,15 +164,13 @@ xnp1 = PsecNext;
179164
polyorder = 2; # change maximal polynomial order of monomials in library
180165
Θ = ones((polyorder+1,length(xn))); # constant term
181166

182-
# (print options below will not work if polyorder is not 5)
183167
for p = 1:polyorder
184168
Θ[p+1,:] = xn.^p;
185169
end
186170

187-
size(pinv(Θ))
188171
xnp1 = reshape(xnp1, (1,length(xnp1)));
189172

190173
total_dim = 1;
191-
Xi_new = sindy(xnp1, Θ,total_dim);
174+
Xi_new = sindy(xnp1, Θ,total_dim, 1e-5);
192175
mons = ["" "x" "x^2" "x^3" "x^4" "x^5"];
193-
print_model(Xi_new,mons, total_dim)
176+
print_model(Xi_new,mons, total_dim, ["f(x)"])

0 commit comments

Comments
 (0)