Professional Documents
Culture Documents
Lab 05
Lab 05
Lab 05
Xingkai xchengas@connect.ust.hk
Xizhi xfuam@connect.ust.hk
Outlines
• Solutions for assignment 3
• Hints for assignment 4
• Any other questions
Assignment 3
Q1a almost right
Q1 b
Someone calculated the arc length of the cycloid with function directly
Since the requirement is adding up the distance between each two consecutive points,
for dx,dy, it's better to use np.diff() not derivation,
the result is infinitely close to 8 but below 8
def frame():
arc_length=0
N_frames=101
Assignment 3 t_list=np.linspace(0,2*np.pi,N_frames)
x3_list=[] # cycloid, dense scatter
y3_list=[]
x3,y3=0,0
import numpy as np def roll_wo_slipping(i,t_list): for i in range(len(t_list)):
import matplotlib.pyplot as plt t=t_list[i] x2,y2=x3,y3
x1,y1=translate(t) x3,y3=roll_wo_slipping(i,t_list)
def translate(t): x2,y2=rotate(t) x3_list.append(x3)
x1,y1=t,1 x3=x1+x2 y3_list.append(y3)
return x1,y1 y3=y1+y2 if i>0:
return x3,y3 arc_length+=((x3-x2)**2+(y3-y2)**2)**.5
def rotate(t): fig,ax=plt.subplots(figsize=[8,8])
x2=-np.sin(t) ax.set_aspect("equal")
y2=-np.cos(t) plt.scatter(x3_list,y3_list,s=5,c="C0")
return x2,y2 plt.ylim(0,2.1)
plt.xlim(-0.1,6.4)
plt.xlabel(r"$x\ (r)$")
plt.ylabel(r"$y\ (r)$")
Define two velocity firstly Record all locations
plt.savefig("cycloid.png")
print("arc length",arc_length)
return 0
Assignment 3
Q2a several people missed part of questions
def plot_color(x,y,z,name):
fig,ax=plt.subplots(figsize=[5,5])
ax.set_aspect("equal") # overrides the ratio in figsize
surface=plt.pcolormesh(x,y,z,shading="nearest",cmap="bwr")
#plt.colorbar()
#plt.contourf(x,y,z1,cmap="bwr")
plt.xlabel(r"$x\ (a_0)$")
plt.ylabel(r"$y\ (a_0)$")
#plt.title(r"$3d_{xy}$")
plt.title(name)
return 0
x=np.linspace(-20,20,100)
y=np.linspace(-20,20,100)
start_time=time.time()
z1=f1(x,y)
runtime=time.time()-start_time
print("runtime for z1: ",runtime,"s")
x2,y2=np.meshgrid(x,y)
start_time=time.time()
z2=f(x2,y2)
runtime=time.time()-start_time
print("runtime for z2: ",runtime,"s")
x3,y3=np.meshgrid(x,y)
start_time=time.time()
z3=g(x3,y3)
runtime=time.time()-start_time
print("runtime for z3: ",runtime,"s")
plot_color(x,y,z1,r"$3d_{xy}$")
plot_color(x2,y2,z2,r"$3d_{xy}$")
plot_color(x3,y3,z3,r"$2p_x$")
# -*- coding: utf-8 -*-
Assignment 3 """
Created on Tue Feb 28 12:46:29 2023
@author: xchengas
"""
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=plt.figaspect(0.5))
n = 30
ax = fig.add_subplot(1,2,1, projection='3d')
ax.set_zlim3d(-4, 4)
ax.plot_trisurf(x.flatten(), y.flatten(), z.flatten(), cmap='viridis',triangles=trii.triangles)
ax = fig.add_subplot(1,2,2, projection='3d')
ax.set_zlim3d(-4, 4)
ax.plot_surface(x,y,z)
Assignment 3
fig=plt.figure()
ax=fig.gca(projection='3d')
#ax = axes3d.Axes3D(fig)
[x,t]=np.meshgrid(np.array(range(25))/24.0,np.arange(0,575.5,0.5)/575*17*np.pi-
2*np.pi)
p=(np.pi/2)*np.exp(-t/(8*np.pi))
u=1-(1-np.mod(3.6*t,2*np.pi)/np.pi)**4/2
y=2*(x**2-x)**2*np.sin(p)
r=u*(x*np.sin(p)+y*np.cos(p))
surf=ax.plot_surface(r*np.cos(t),r*np.sin(t),u*(x*np.cos(p)-
y*np.sin(p)),rstride=1,cstride=1,cmap=cm.gist_rainbow_r,
linewidth=0,antialiased=True)
plt.show()
import scipy.integrate
import numpy as np
Q1 def f(x):
return np.exp(-x**2)
a,b=0,1
scipy.integrate.romberg(f,a,b,show=True)
Q2
• Just follow three methods in lecture slides
• Notice the log-log plot
• Use gaussxw.py we provided