Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed May 21 06:03:32 2018
@author: nolte
Introduction to Modern Dynamics, 2nd edition (Oxford University Press, 2019)
Duffing oscillator
"""
import numpy as np
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
from scipy import integrate
from matplotlib import pyplot as plt
from matplotlib import cm
import time
import os
plt.close('all')
# model_case 1 = Pendulum
# model_case 2 = Double Well
print(' ')
print('Duffing.py')
alpha = -1 # -1
beta = 1 # 1
delta = 0.3 # 0.3
gam = 0.15 # 0.15
w = 1
def flow_deriv(x_y_z,tspan):
x, y, z = x_y_z
a = y
b = delta*np.cos(w*tspan) - alpha*x - beta*x**3 - gam*y
c = w
return[a,b,c]
T = 2*np.pi/w
px1 = np.random.rand(1)
xp1 = np.random.rand(1)
w1 = 0
x_y_z = [xp1, px1, w1]
# Settle-down Solve for the trajectories
t = np.linspace(0, 2000, 40000)
x_t = integrate.odeint(flow_deriv, x_y_z, t)
x0 = x_t[39999,0:3]
tspan = np.linspace(1,20000,400000)
x_t = integrate.odeint(flow_deriv, x0, tspan)
siztmp = np.shape(x_t)
siz = siztmp[0]
y1 = x_t[:,0]
y2 = x_t[:,1]
y3 = x_t[:,2]
plt.figure(2)
lines = plt.plot(y1[1:2000],y2[1:2000],'ko',ms=1)
plt.setp(lines, linewidth=0.5)
plt.show()
for cloop in range(0,3):
#phase = np.random.rand(1)*np.pi;
phase = np.pi*cloop/3
repnum = 5000
px = np.zeros(shape=(2*repnum,))
xvar = np.zeros(shape=(2*repnum,))
cnt = -1
testwt = np.mod(tspan-phase,T)-0.5*T;
last = testwt[1]
for loop in range(2,siz):
if (last < 0)and(testwt[loop] > 0):
cnt = cnt+1
del1 = -testwt[loop-1]/(testwt[loop] - testwt[loop-1])
px[cnt] = (y2[loop]-y2[loop-1])*del1 + y2[loop-1]
xvar[cnt] = (y1[loop]-y1[loop-1])*del1 + y1[loop-1]
last = testwt[loop]
else:
last = testwt[loop]
plt.figure(3)
if cloop == 0:
lines = plt.plot(xvar,px,'bo',ms=1)
elif cloop == 1:
lines = plt.plot(xvar,px,'go',ms=1)
else:
lines = plt.plot(xvar,px,'ro',ms=1)
plt.show()
plt.savefig('Duffing')