0% found this document useful (0 votes)
33 views10 pages

Lab 05

The document provides notes from Lab 05 which includes solutions to Assignment 3 questions, hints for Assignment 4, and offers to answer any other questions. For Assignment 3, question 1b was calculated incorrectly by directly using the cycloid function instead of taking the distance between points, and question 2a was missed by some students for not using the correct formula. The document also includes code examples and solutions for Assignment 3 questions regarding numerical integration and 3D surface plots.

Uploaded by

harrisonyky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views10 pages

Lab 05

The document provides notes from Lab 05 which includes solutions to Assignment 3 questions, hints for Assignment 4, and offers to answer any other questions. For Assignment 3, question 1b was calculated incorrectly by directly using the cycloid function instead of taking the distance between points, and question 2a was missed by some students for not using the correct formula. The document also includes code examples and solutions for Assignment 3 questions regarding numerical integration and 3D surface plots.

Uploaded by

harrisonyky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Lab 05

Xingkai [email protected]
Xizhi [email protected]
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

Q2b almost right


Students made mistakes because they didn’t use the correct formula.
def g(x,y):
r=(x**2+y**2)**.5
R=1/3**.5*(1/2)**1.5*r*np.exp(-r/2)
Y=3**.5/2/np.pi**.2*x/r
return R*Y

Assignment 3 def f1(x,y):


z=np.zeros((len(y),len(x)))
for ix in range(len(x)):
for jy in range(len(y)):
z[jy,ix]=f(x[ix],y[jy])
return z

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

theta = np.linspace(0, 2.*np.pi, n, endpoint=True)


phi = np.linspace(0, 2.*np.pi, n, endpoint=True)
theta, phi = np.meshgrid(theta, phi)
c, a = 4, 1
x = (c + a*np.cos(theta)) * np.cos(phi)
y = (c + a*np.cos(theta)) * np.sin(phi)
z = a * np.sin(theta)

import matplotlib.tri as mtri


trii=mtri.Triangulation(np.ravel(phi),np.ravel(theta))

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

from mpl_toolkits.mplot3d import Axes3D


from matplotlib import cm
from matplotlib.ticker import LinearLocator
import matplotlib.pyplot as plt
import numpy as np

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

You might also like