39
39
40
40
function generate_traj (integrator, x_init, tspan, dt)
41
41
# 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 ))
43
43
for j in 1 : length (x_init)
44
44
reinit! (integrator, x_init[j])
45
45
n = 1 ;
46
46
for i in tspan[1 ]: dt: tspan[2 ]
47
+ xdat[j, n,:] = integrator. u;
47
48
step! (integrator, dt, true );
48
- xdat[j, :, n] = integrator. u;
49
-
50
49
n+= 1 ;
51
50
end
52
51
end
@@ -70,54 +69,46 @@ if numTraj >= 2
70
69
end
71
70
end
72
71
73
- # tspan = (0.0,100.0*dt);
74
- # this is generating wrong trajectories
75
72
xdat = generate_traj (integrator, x_init, tspan, dt);
76
73
77
- plot (xdat[3 ,1 , 1 : 10000 ])
74
+ plot (xdat[3 ,1 : 10000 , 1 ])
78
75
79
76
# Aggregate stroboscopic section data
80
77
# Counting parameter
81
78
# Initialize
82
- Psec = Float64[]
79
+ Psec = Float64[0 ]
83
80
PsecNext = Float64[]
84
81
85
82
# Create Poincaré section data
86
83
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
88
85
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
93
90
end
94
91
end
92
+ Psec = Psec[1 : length (Psec)- 1 ];
95
93
end
96
- Psec = Psec[1 : length (Psec)- 2 ];
97
94
# Create the recurrence data
98
95
xn = Psec;
99
96
xnp1 = PsecNext;
100
97
101
- size (xn), size (xnp1)
102
-
103
98
# Create Θ matrix from monomial upto degree 5
104
99
polyorder = 5 ; # change maximal polynomial order of monomials in library
105
100
Θ = ones ((polyorder+ 1 ,length (xn))); # constant term
106
101
107
- # (print options below will not work if polyorder is not 5)
108
102
for p = 1 : polyorder
109
103
Θ[p+ 1 ,:] = xn.^ p;
110
104
end
111
105
112
- size (pinv (Θ))
113
106
xnp1 = reshape (xnp1, (1 ,length (xnp1)));
114
107
115
108
total_dim = 1 ;
116
- Xi_new = sindy (xnp1, Θ,total_dim);
109
+ Xi_new = sindy (xnp1, Θ,total_dim, 1e-5 );
117
110
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)" ])
121
112
122
113
# # Hopf Normal Form ODE
123
114
function Hopf! (du,u,p,t)
@@ -144,33 +135,27 @@ if numTraj >= 3
144
135
end
145
136
end
146
137
147
- size (x_init)
148
- x_init[1 ]
149
-
150
138
prob = ODEProblem (Hopf!, x_init[1 ], tspan, om)
151
139
integrator = init (prob, reltol = 1e-12 , abstol = 1e-12 )
152
140
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 ,:];
156
142
157
143
# Aggregate Poincare section
158
- # Counting parameter
159
- count = 1 ;
160
144
# Initialize
161
145
Psec = Float64[];
162
146
PsecNext = Float64[];
163
147
push! (Psec, xdat[1 ,1 ,1 ]);
164
148
# Create Poincare section data
165
149
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
170
154
end
171
155
end
172
156
end
173
157
# Create the recurrence data
158
+
174
159
Psec = Psec[1 : length (Psec)- 1 ];
175
160
xn = Psec;
176
161
xnp1 = PsecNext;
@@ -179,15 +164,13 @@ xnp1 = PsecNext;
179
164
polyorder = 2 ; # change maximal polynomial order of monomials in library
180
165
Θ = ones ((polyorder+ 1 ,length (xn))); # constant term
181
166
182
- # (print options below will not work if polyorder is not 5)
183
167
for p = 1 : polyorder
184
168
Θ[p+ 1 ,:] = xn.^ p;
185
169
end
186
170
187
- size (pinv (Θ))
188
171
xnp1 = reshape (xnp1, (1 ,length (xnp1)));
189
172
190
173
total_dim = 1 ;
191
- Xi_new = sindy (xnp1, Θ,total_dim);
174
+ Xi_new = sindy (xnp1, Θ,total_dim, 1e-5 );
192
175
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