@rfhongyi
2016-11-14T03:24:43.000000Z
字数 9371
阅读 763

import pylab as plimport mathclass oscillatory:def __init__(self,g=9.8,l=9.8,q=0.5,F_D=1.2,O_D=2/3,time_step=3*math.pi/1000,\total_time=100,initial_theta=0.2,initial_omega=0):self.g=gself.l=lself.q=qself.F=F_Dself.O=O_Dself.t=[0]self.initial_theta=initial_thetaself.initial_omega=initial_omegaself.dt=time_stepself.time= total_timeself.omega= [initial_omega]self.theta= [initial_theta]self.nsteps=int(total_time//time_step+1)self.tmpo=[0]self.tmpt=[0]def run(self):for i in range(self.nsteps):tmpo=self.omega[i]+(-1*(self.g/self.l)*math.sin(self.theta[i])-\self.q*self.omega[i]+self.F*math.sin(self.O*self.t[i]))*self.dttmpt=self.theta[i]+tmpo*self.dtwhile(tmpt<(-1*math.pi) or tmpt>math.pi):if tmpt<(-1*math.pi):tmpt+=2*math.piif tmpt>math.pi:tmpt-=2*math.piself.omega.append(tmpo)self.theta.append(tmpt)self.t.append(self.t[i]+self.dt)for i in range(len(self.t)):if self.t[i] // (3 * math.pi) > 300:if ((2 / 3 * self.t[i]) % (2 * math.pi)) <= (2 / 3 * self.dt * 0.5) or (2 * math.pi - ((2 / 3 * self.t[i]) % (2 * math.pi))) <= (2 / 3 * self.dt * 0.5):self.tmpo.append(self.omega[i])self.tmpt.append(self.theta[i])def show_results(self):font = {'family': 'serif','color': 'darkred','weight': 'normal','size': 16,}pl.plot(self.t,self.theta)#pl.scatter(self.tmpt, self.tmpo)pl.title(r'$\theta$ versus time', fontdict = font)pl.xlabel(r'$\theta$(radians)')pl.ylabel(r'$\omega$(rad/s)')#pl.xlim(0,3)#pl.ylim(-3,0)pl.legend((['$F_D$=1.2']))pl.show()a = oscillatory()a.run()a.show_results()

It's easy to see that when , the system is in chaotic state. When , the system become well-aligned again, its period is the same as the drive period. When , its period is the twice as the drive period. When , its period is the fourth times as the drive period.
If we chose the time :
It's easy to see that when , there is just one point,when , there is two points, when , there is four points.It's easy to understand this.
import pylab as plimport mathclass oscillatory:def __init__(self,g=9.8,l=9.8,q=0.5,F_D=1.2,O_D=2/3,time_step=3*math.pi/1000,\total_time=100,initial_theta=0.2,initial_omega=0):self.g=gself.l=lself.q=qself.F=F_Dself.O=O_Dself.t=[0]self.initial_theta=initial_thetaself.initial_omega=initial_omegaself.dt=time_stepself.time= total_timeself.omega= [initial_omega]self.theta= [initial_theta]self.nsteps=int(total_time//time_step+1)self.tmpo=[0]self.tmpt=[0]def run(self):for i in range(self.nsteps):tmpo=self.omega[i]+(-1*(self.g/self.l)*math.sin(self.theta[i])-\self.q*self.omega[i]+self.F*math.sin(self.O*self.t[i]))*self.dttmpt=self.theta[i]+tmpo*self.dtwhile(tmpt<(-1*math.pi) or tmpt>math.pi):if tmpt<(-1*math.pi):tmpt+=2*math.piif tmpt>math.pi:tmpt-=2*math.piself.omega.append(tmpo)self.theta.append(tmpt)self.t.append(self.t[i]+self.dt)for i in range(len(self.t)):if self.t[i] // (5 * math.pi) > 300:if ((2 / 5 * self.t[i]) % (2 * math.pi)) <= (2 / 3 * self.dt * 0.5) or (2 * math.pi - ((2/ 5 * self.t[i]) % (2 * math.pi))) <= (2 / 5 * self.dt * 0.5):self.tmpo.append(self.omega[i])self.tmpt.append(self.theta[i])def show_results(self):font = {'family': 'serif','color': 'darkred','weight': 'normal','size': 16,}#pl.plot(self.t,self.theta)pl.scatter(self.tmpt, self.tmpo)pl.title(r'$\theta$ versus time', fontdict = font)pl.xlabel(r'$\theta$(radians)')pl.ylabel(r'$\omega$(rad/s)')#pl.xlim(0,3)#pl.ylim(-3,0)pl.legend((['$F_D$=1.2']))pl.show()a = oscillatory()a.run()a.show_results()
I chose the , but unfortunately, I didn't get the right answer. Because I did'n have enough time to solve the bug, so it's not completely.
from __future__ import divisionfrom visual import *from math import *q=0.5l=9.8g=9.8f=2/3dt=0.04theta0=0.2omega0=0ceil=box(pos=(0,5,0),size=(5,0.5,2),material=materials.bricks)ball=sphere(pos=(l*sin(theta0),5-l*cos(theta0),0),radius=1,material=materials.emissive,color=color.green)ball.omega=omega0ball.theta=theta0ball.t=0ball.F=0.5string=curve(pos=[ceil.pos,ball.pos],color=color.blue)po=arrow(pos=ball.pos,axis=(9.8*ball.omega*cos(ball.theta),9.8*ball.omega*sin(ball.theta),0),color=color.blue)while True:rate(100)ball.omega=ball.omega-((g/l)*sin(ball.theta)+q*ball.omega-ball.F*sin(f*ball.t))*dtangle_new=ball.theta+ball.omega*dtwhile angle_new>pi:angle_new-=2*piwhile angle_new<-pi:angle_new+=2*piball.theta=angle_newball.pos=(l*sin(ball.theta),5-l*cos(ball.theta),0)string.pos=[ceil.pos,ball.pos]po.pos=ball.pospo.axis=(9.8*ball.omega*cos(ball.theta),9.8*ball.omega*sin(ball.theta),0)ball.t=ball.t+dt




import mathimport matplotlib.pyplot as plpl.rc('text', usetex=True)pl.rc('font', family='serif')class bifurcation_diagram:'''docstring for assignment this week, using the Runga-Kutta method, with reseting process'''def __init__(self, total_time, time_interval,driving_force_interval):self.FD = [1.35]self.dFD = driving_force_intervalself.dt = time_intervalself.steps = int(total_time // time_interval) + 1self.t = [0]self.omega = [0]self.theta = [0.2]self.theta_in_phase = []self.theta_fixed = []while self.FD[-1] < 1.5:self.FD.append(self.FD[-1] + self.dFD)def calculate(self):for j in range(len(self.FD)):self.t = [0]self.omega = [0]self.theta = [0.2]self.omega_in_phase = []self.theta_in_phase = []for i in range(self.steps):midpoint_omega = self.omega[i] + 0.5 * (-math.sin(self.theta[i]) - 0.5 * self.omega[i] + self.FD[j] * math.sin(0.66666666667 * self.t[i])) * self.dtmidpoint_time = self.t[i] + 0.5 * self.dtmidpoint_theta = self.theta[i] + 0.5 * self.dttemporary_theta = self.theta[i] + midpoint_omega * self.dttemporary_omega = self.omega[i] + (-math.sin(midpoint_theta) - 0.5 * midpoint_omega + self.FD[j] * math.sin(0.66666666667 * midpoint_time)) * self.dtif math.fabs(temporary_theta) > math.pi :if temporary_theta > 0 :while temporary_theta > math.pi :temporary_theta = temporary_theta - 2 * math.pielse :while temporary_theta < -math.pi :temporary_theta = temporary_theta + 2 * math.piself.theta.append(temporary_theta)self.omega.append(temporary_omega)self.t.append(self.t[i] + self.dt)for i in range(len(self.t)):if self.t[i] // (3 * math.pi) > 300:if ((2 / 3 * self.t[i]) % (2 * math.pi)) <= (2 / 3 * self.dt * 0.5) or (2 * math.pi - ((2 / 3 * self.t[i]) % (2 * math.pi))) <= (2 / 3 * self.dt * 0.5):self.theta_in_phase.append(self.theta[i])pl.plot([self.FD[j]] * len(self.theta_in_phase), self.theta_in_phase,'b.', linewidth = 0, alpha = 0.1)pl.xlim(1.35,1.5)pl.ylim(1,3)pl.xlabel("$F_D$")pl.ylabel(r'$\theta$(radians)')pl.title(r'Bifurcation diagram $\theta$ versus $F_D$')pl.show()a1 = bifurcation_diagram(total_time = 4000, time_interval = 0.01, driving_force_interval = 0.001)a1.calculate()
