def f(x): ''' Calculates the height of a projectile at a given horizontal distance. theta, v0, and y0 are global variables ''' from scitools.std import tan, cos g = 9.81 # m/s**2 y = x*tan(theta) - 1./(2*v0**2)*(g*x**2)/(cos(theta)**2) + y0 return y
def f(x): ''' Calculates the height of a projectile at a given horizontal distance. theta, v0, and y0 are global variables ''' from scitools.std import tan, cos g = 9.81 # m/s**2 y = x * tan(theta) - 1. / (2 * v0**2) * (g * x**2) / (cos(theta)**2) + y0 return y
def T(z, t): # T0, A, k, and omega are global variables return T0 + A1 * exp(-a1 * z) * cos(omega1 * t - a1 * z) + \ A2 * exp(-a2 * z) * cos(omega2 * t - a2 * z)
for name in glob.glob('pix/planet*.png'): os.remove(name) n = 100 #we want 100 frames a = 1 #set parameters for the orbit b = 1 w = 1 delta_t = (2 * pi) / (w * n) counter = 0 X = [] Y = [] for k in range(n + 1): tk = k * delta_t #each time through we need to add a new k value x = a * cos(w * tk) y = b * sin(w * tk) X.append(x) Y.append(y) XP = array(X) YP = array(Y) XF = array([x]) YF = array([y]) velo = w * sqrt(a**2 * sin(w * tk)**2 + b**2 * cos(w * tk)**2) plot(XP, YP, '-r', XF, YF, 'bo', axis=[-1.2, 1.2, -1.2, 1.2],
def _dg(x): return -2*0.1*x*exp(-0.1*x**2)*sin(pi/2*x) + \ pi/2*exp(-0.1*x**2)*cos(pi/2*x)
def points(N): x = [0.5 * cos(2 * pi * i / N) for i in range(N + 1)] y = [0.5 * sin(2 * pi * i / N) for i in range(N + 1)] return x, y
""" Exercise 5.6: Simulate by hand a vectorized expression Author: Weiyun Lu """ from scitools.std import array, sin, cos, exp, zeros x = array([0, 2]) t = array([1, 1.5]) y = zeros((2, 2)) f = lambda x, t: cos(sin(x)) + exp(1.0 / t) for i in range(2): for j in range(2): y[i][j] = f(x[i], t[j]) print y
def test_undampedWaves(): #define constants given in exercise A = 1 mx=7. my=2. #define function that give q=lambda x,y: 1 #define some variables Lx = 3 Ly = 1.3 T = 1 C = 0.5 dt= 0.1 #define omega so equation holds w=pi*sqrt((mx/Lx)**2 +(my/Ly)**2) #help varabeles kx = pi*mx/Lx ky = pi*my/Ly #Exact solution ue = lambda x,y,t: A*cos(x*kx)*cos(y*ky)*cos(t*w) #initial condition so we get result we want. I = lambda x,y: A*cos(x*kx)*cos(y*ky) #factor dt decreeses per step step=0.5 #number of steps I want to do val=5 #array to store errors E=zeros(val) for i in range(val): v='vector' #solve eqation u,x,y,t,e=solver(I,None,None,q,0,Lx,Ly,dt*step**(i),T,C,1,mode=v,ue=ue) E[i]=e #find convergence rate between diffrent dt values r =zeros(val-1) r = log(E[1:]/E[:-1])/log(step) print "Test convergence for undamped waves:" for i in range(val): if i==0: print "dt: ",dt," Error: ",E[i] else: print "dt: ",dt*step**(i)," Error: ",E[i], "rate of con.: ", r[i-1] #requiere close to 2 in convergence rate for last r. assert abs(r[-1]-2)<0.01
def test_manufactured(): A=1 B=0 mx=1 my=1 b=1 c=1.1 #define some variables Lx = 2. Ly = 2. T = 1 C = 0.3 dt= 0.1 #help varabeles kx = pi*mx/Lx ky = pi*my/Ly w=1 #Exact solution ue = lambda x,y,t: A*cos(x*kx)*cos(y*ky)*cos(t*w)*exp(-c*t) I = lambda x,y: A*cos(x*kx)*cos(y*ky) V = lambda x,y: -c*A*cos(x*kx)*cos(y*ky) q = lambda x,y: x**2+y**2 f = lambda x,y,t:A*(-b*(c*cos(t*w) + w*sin(t*w))*cos(kx*x)*cos(ky*y) + c**2*cos(kx*x)*cos(ky*y)*cos(t*w) + 2*c*w*sin(t*w)*cos(kx*x)*cos(ky*y) + kx**2*(x**2 + y**2)*cos(kx*x)*cos(ky*y)*cos(t*w) + 2*kx*x*sin(kx*x)*cos(ky*y)*cos(t*w) + ky**2*(x**2 + y**2)*cos(kx*x)*cos(ky*y)*cos(t*w) + 2*ky*y*sin(ky*y)*cos(kx*x)*cos(t*w) - w**2*cos(kx*x)*cos(ky*y)*cos(t*w))*exp(-c*t) #factor dt decreeses per step step=0.5 #number of steps I want to do val=5 #array to store errors E=zeros(val) for i in range(val): v='vector' #solve eqation u,x,y,t,e=solver(I,V,f,q,b,Lx,Ly,dt*step**(i),T,C,8,mode=v,ue=ue) E[i]=e #find convergence rate between diffrent dt values r =zeros(val-1) r = log(E[1:]/E[:-1])/log(step) print print "Convergence rates for manufactured solution:" for i in range(val): if i==0: print "dt: ",dt," Error: ",E[i] else: print "dt: ",dt*step**(i)," Error: ",E[i], "rate of con.: ", r[i-1] #requiere "close" to 2 in convergence rate for last r. assert abs(r[-1]-2)<0.5
def test_manufactured(): A = 1 B = 0 mx = 1 my = 1 b = 1 c = 1.1 #define some variables Lx = 2. Ly = 2. T = 1 C = 0.3 dt = 0.1 #help varabeles kx = pi * mx / Lx ky = pi * my / Ly w = 1 #Exact solution ue = lambda x, y, t: A * cos(x * kx) * cos(y * ky) * cos(t * w) * exp(-c * t) I = lambda x, y: A * cos(x * kx) * cos(y * ky) V = lambda x, y: -c * A * cos(x * kx) * cos(y * ky) q = lambda x, y: x**2 + y**2 f = lambda x, y, t: A * ( -b * (c * cos(t * w) + w * sin(t * w)) * cos(kx * x) * cos(ky * y) + c **2 * cos(kx * x) * cos(ky * y) * cos(t * w) + 2 * c * w * sin( t * w) * cos(kx * x) * cos(ky * y) + kx**2 * (x**2 + y**2) * cos(kx * x) * cos(ky * y) * cos(t * w) + 2 * kx * x * sin(kx * x) * cos(ky * y) * cos(t * w) + ky**2 * (x**2 + y**2) * cos(kx * x) * cos(ky * y) * cos(t * w) + 2 * ky * y * sin(ky * y) * cos(kx * x) * cos(t * w) - w**2 * cos(kx * x) * cos( ky * y) * cos(t * w)) * exp(-c * t) #factor dt decreeses per step step = 0.5 #number of steps I want to do val = 5 #array to store errors E = zeros(val) for i in range(val): v = 'vector' #solve eqation u, x, y, t, e = solver(I, V, f, q, b, Lx, Ly, dt * step**(i), T, C, 8, mode=v, ue=ue) E[i] = e #find convergence rate between diffrent dt values r = zeros(val - 1) r = log(E[1:] / E[:-1]) / log(step) print print "Convergence rates for manufactured solution:" for i in range(val): if i == 0: print "dt: ", dt, " Error: ", E[i] else: print "dt: ", dt * step**( i), " Error: ", E[i], "rate of con.: ", r[i - 1] #requiere "close" to 2 in convergence rate for last r. assert abs(r[-1] - 2) < 0.5
def test_undampedWaves(): #define constants given in exercise A = 1 mx = 7. my = 2. #define function that give q = lambda x, y: 1 #define some variables Lx = 3 Ly = 1.3 T = 1 C = 0.5 dt = 0.1 #define omega so equation holds w = pi * sqrt((mx / Lx)**2 + (my / Ly)**2) #help varabeles kx = pi * mx / Lx ky = pi * my / Ly #Exact solution ue = lambda x, y, t: A * cos(x * kx) * cos(y * ky) * cos(t * w) #initial condition so we get result we want. I = lambda x, y: A * cos(x * kx) * cos(y * ky) #factor dt decreeses per step step = 0.5 #number of steps I want to do val = 5 #array to store errors E = zeros(val) for i in range(val): v = 'vector' #solve eqation u, x, y, t, e = solver(I, None, None, q, 0, Lx, Ly, dt * step**(i), T, C, 1, mode=v, ue=ue) E[i] = e #find convergence rate between diffrent dt values r = zeros(val - 1) r = log(E[1:] / E[:-1]) / log(step) print "Test convergence for undamped waves:" for i in range(val): if i == 0: print "dt: ", dt, " Error: ", E[i] else: print "dt: ", dt * step**( i), " Error: ", E[i], "rate of con.: ", r[i - 1] #requiere close to 2 in convergence rate for last r. assert abs(r[-1] - 2) < 0.01