3+5*6
import math
math.sqrt(3**2+4**2)
1/7
a=3+4j
print(a)
a.real
math.exp(1)**math.pi-math.pi**math.exp(1)
abs(a)
print([a.real, a.imag, abs(a)])
abs(math.cos(math.pi/6)+math.sin(math.pi/6)*1j)
import numpy as np
a=np.array([1,2,3])
print(a)
np.log(a)
np.power(a,2)
a=np.linspace(1,5,3)
print(a)
a=np.linspace(0,10.0,10)
print(a)
a=np.logspace(-1,3,5)
print(a)
a=np.array([1,2,3]); b=np.array([2,4,5])
print(a,b)
a+b
a+1
a*b
a=np.array([[1],[2],[3]])
print(a)
print(a.T)
a=np.ones(20)
print(a)
b=np.zeros(5)
print(b)
c=np.random.rand(5)
print(c)
print(c[0:3])
print(c[2:4])
c[4]
sum(c)
a=np.array([1,2,3,4,5])
print(a)
np.cumsum(a)
np.diff(a)
import matplotlib.pyplot as plt
x=np.array([1,2,3,5,7])
y=np.array([2,1,4,6,5])
plt.figure(figsize=(15,5))
plt.subplot(1,3,1)
plt.plot(x,y)
plt.subplot(1,3,2)
plt.plot(x,y,'-o')
plt.subplot(1,3,3)
plt.plot(x,y,'.')
plt.show()
x=np.linspace(1,6,1000)
y=math.exp(1)*np.log(x)/x
plt.plot(x,y)
plt.grid()
plt.show()
def Logi_map(n,a):
x=np.zeros(n)
x[0]=0.12345
for k in range(n-1):
x[k+1]=a*x[k]*(1-x[k])
#print(k)
plt.plot(x,'-o')
plt.show()
Logi_map(101,3.5)
Logi_map(100,1)
Logi_map(100,2)
Logi_map(100,3)
Logi_map(200,4)
def Henon_map(n,a,b):
x=np.zeros(n)
y=np.zeros(n)
x[0]=0
y[0]=0.2
for k in range(n-1):
x[k+1]=1-a*x[k]**2+y[k]
y[k+1]=b*x[k]
#print(k)
plt.plot(x,y,'.')
plt.show()
Henon_map(10000,1.4,0.3)
Henon_map(20000,0.2,0.99)
Henon_map(10000,0.2,-0.9991)
$u(x)=\sin(\exp(3\sin(x)))$.
The period is 2$\pi$.
import numpy as np
import matplotlib.pyplot as plt
def discritization(n):
xmax=2*np.pi; dx=xmax/n
jj=np.arange(n); xx=jj*dx
h=np.exp(3*np.sin(xx)); u=np.sin(h)
v=np.cos(h)*3*h*np.cos(xx)
w=3*np.cos(h)*(3*np.cos(xx)**2-np.sin(xx))*h-np.sin(h)*9*h**2*np.cos(xx)**2
plt.figure(figsize=(15,10))
plt.subplot(3,2,1)
plt.plot(xx,u,'-k')
jp1=jj+1; jp1[n-1]=1
jp2=jj+2; jp2[n-2]=1; jp2[n-1]=2
jm1=jj-1; jm1[0]=n-1;
jm2=jj-2; jm2[0]=n-2; jm2[1]=n-1
up1=u[jp1]; up2=u[jp2]; um1=u[jm1]; um2=u[jm2]
#centered
vc1=(up1-um1)/(2*dx)
wc1=(up1-2*u+um1)/dx**2
#centered (higher order)
vc2=(-up2+8*up1-8*um1+um2)/(12*dx)
wc2=(-up2+16*up1-30*u+16*um1-um2)/(12*dx**2)
#fft
fu=np.fft.fft(u)
kk=2*np.pi/xmax*np.r_[np.arange(n/2),np.arange(-n/2,0,1)]
vf=np.real(np.fft.ifft(1j*kk*fu))
wf=np.real(np.fft.ifft(-kk**2*fu))
#print(len(kk))
#print(kk)
plt.subplot(3,2,3)
plt.plot(xx,vc1,xx,vc2)
plt.subplot(3,2,5)
plt.plot(xx,wc1)
plt.subplot(3,2,4)
plt.semilogy(xx,np.abs(vc1-v),xx,np.abs(vc2-v),xx,np.abs(vf-v))
plt.subplot(3,2,6)
plt.semilogy(xx,np.abs(wc1-w),xx,np.abs(wc2-w),xx,np.abs(wf-w))
plt.show()
discritization(256)
Consider a partial dirrential equation (PDE) for $u(x,t)$ $$\frac{\partial u}{\partial t}+c\frac{\partial u}{\partial x}=0$$ where $c$ is a constant. A simple way to discretize the equation above is to use "forward time" and "centered space" shemes, i.e., $$\frac{\partial u(x,t)}{\partial t}\rightarrow\frac{u_j^{k+1}-u_j^k}{\Delta t} \qquad\qquad\frac{\partial u(x,t)}{\partial x}\rightarrow\frac{u_{j+1}^k-u_{j-1}^k}{2\Delta x}$$ which lead to an explicit expression for $u_j^{k+1}$ $$u_j^{k+1}=u_j^k-\frac{c\Delta t}{2\Delta x}(u_{j+1}^k-u_{j-1}^k)$$.
#%matplotlib nbagg
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
#plt.ion()
#from IPython.display import display, clear_output
import matplotlib.animation as animation
def FTCS(dx,nx,dt,nt):
u=np.r_[np.zeros(int(nx/4)), np.ones(int(nx/2)), np.zeros(int(nx/4))]
jj=np.arange(nx); jp=np.r_[jj[0:nx-1]+1,0]; jm=np.r_[nx-1,jj[1:nx]-1] #jm=jj-1; jm[0]=nx-1
#jj=np.arange(nx); jp=jj+1; jp[nx-1]=1; jm=jj-1; jm[0]=nx-1
#print(jj, jp, jm)
xx=dx*jj; xmax=dx*nx; c=1
plt.plot(xx,u,'-o')
plt.show()
cf=c*dt/2/dx
fig = plt.figure()
ims = []
#axe = fig.add_subplot(111)
for it in range(nt):
#clear_output(wait=True)
u=u-cf*(u[jp]-u[jm])
plt.ylim(-1,2)
im=plt.plot(xx,u,'k-o')
ims.append(im)
ani = animation.ArtistAnimation(fig, ims)
#plt.show()
#ani.save('hoge.gif')
return(ani)
FTCS(1,100,0.1,200)
Let us try the different scheme, Lax-Friedrichs: $$\frac{\partial u(x,t)}{\partial t}\rightarrow\frac{u_j^{k+1}-(u_{j+1}^k+u_{j-1}^k)/2}{\Delta t}\qquad\qquad\frac{\partial u(x,t)}{\partial x}\rightarrow\frac{u_{j+1}^k-u_{j-1}^k}{2\Delta x}$$ Then, one again has an iteration relation $$u_j^{k+1}=\frac{u_{j+1}^k+u_{j-1}^k}{2}-\frac{c\Delta t}{2\Delta x}(u_{j+1}^k-u_{j-1}^k)$$
#%matplotlib nbagg
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
from IPython.display import display, clear_output
#import matplotlib.animation as animation
def LaxFried(dx,nx,dt,nt):
u=np.r_[np.zeros(int(nx/4)), np.ones(int(nx/2)), np.zeros(int(nx/4))]
jj=np.arange(nx); jp=np.r_[jj[0:nx-1]+1,0]; jm=np.r_[nx-1,jj[1:nx]-1] #jm=jj-1; jm[0]=nx-1
#print(jj, jp, jm)
xx=dx*jj; xmax=dx*nx; c=1.
plt.plot(xx,u,'-o')
# plt.show()
cf=c*dt/2/dx
print(cf)
fig = plt.figure()
axe = fig.add_subplot(111)
for it in range(nt):
clear_output(wait=True)
u=(u[jp]+u[jm])/2-cf*(u[jp]-u[jm])
plt.ylim(-1,2)
plt.xlim(0,xmax)
axe.plot(xx,u,'-o')
#fig.set_size_inches(18.5, 10.5)
display(fig)
axe.cla()
LaxFried(1,256,.1,100)
import numpy as np
a=np.array([[2,1,3,5,4,5],[1,3,5,5,2,2],[3,3,5,3,2,2],[2,3,1,3,2,3]])
a[2,:]
a[:,2]
xx,yy=np.meshgrid(np.linspace(1,3,3),np.linspace(2,5,4))
xx
yy
def plots():
x,y=np.meshgrid(np.linspace(-10,10,50),np.linspace(-10,10,50))
r=np.sqrt(x**2+y**2)
f=np.sin(r)/r
#print(x)
plt.subplot(2,2,1); plt.contour(x,y,f)
plt.subplot(2,2,2); plt.pcolor(x,y,f)
plt.show()
plots()
def vecplot():
x,y=np.meshgrid(np.linspace(-4,4,18),np.linspace(-4,4,18))
px=x; py=-y
plt.quiver(x,y,px,py)
plt.show()
vecplot()
#%matplotlib nbagg
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
def LaxWend(dx,nx,dt,nt):
u=np.r_[np.zeros(int(nx/4)), np.ones(int(nx/2)), np.zeros(int(nx/4))]
jj=np.arange(nx); jp=np.r_[jj[0:nx-1]+1,0]; jm=np.r_[nx-1,jj[1:nx]-1] #jm=jj-1; jm[0]=nx-1
#print(jj, jp, jm)
xx=dx*jj; xmax=dx*nx; c=1; cc=c*dt/dx; cch=0.5*cc
tt=dt*np.arange(nt)
#plt.plot(xx,u,'-o')
udata=np.zeros((nt,nx))
udata[0,:]=u
cf=c*dt/2/dx
#print(cf)
#fig = plt.figure()
#axe = fig.add_subplot(111)
for it in range(nt):
#clear_output(wait=True)
u=u-cch*(u[jp]-u[jm])+cc*cch*(u[jp]-2*u+u[jm])
#u=u-cf*(u[jp]-u[jm])
udata[it,:]=u
#u=(u[jp]+u[jm])/2-cf*(u[jp]-u[jm])
plt.pcolor(xx,tt,udata); plt.show()
plt.plot(xx,u); plt.show()
%time LaxWend(1,256,0.1,1000)
#%matplotlib nbagg
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
def rk4fft(dx,nx,dt,nt):
u=np.r_[np.zeros(int(nx/4)), np.ones(int(nx/2)), np.zeros(int(nx/4))]
#u=np.sin(2*np.pi/nx*np.arange(nx)*2)
jj=np.arange(nx)#; jp=np.r_[jj[0:nx-1]+1,0]; jm=np.r_[nx-1,jj[1:nx]-1] #jm=jj-1; jm[0]=nx-1
#print(jj, jp, jm)
xx=dx*jj; xmax=dx*nx; c=1
tt=dt*np.arange(nt)
kk=2*np.pi/xmax*np.r_[np.arange(nx/2),0,np.arange(-nx/2+1,0,1)]
plt.plot(xx,u,'-o')
udata=np.zeros((nt,nx))
udata[0,:]=u
#cf=c*dt/2/dx
#print(cf)
fig = plt.figure()
axe = fig.add_subplot(111)
for it in range(nt):
#clear_output(wait=True)
kk1=-c*dt*np.real(np.fft.ifft(1j*kk*np.fft.fft(u)))
kk2=-c*dt*np.real(np.fft.ifft(1j*kk*np.fft.fft(u+0.5*kk1)))
kk3=-c*dt*np.real(np.fft.ifft(1j*kk*np.fft.fft(u+0.5*kk2)))
kk4=-c*dt*np.real(np.fft.ifft(1j*kk*np.fft.fft(u+ kk3)))
u =u+(kk1+2*kk2+2*kk3+kk4)/6
#u=u-cch*(u[jp]-u[jm])+cc*cch*(u[jp]-2*u+u[jm])
udata[it,:]=u
#u=(u[jp]+u[jm])/2-cf*(u[jp]-u[jm])
plt.pcolor(xx,tt,udata); plt.show()
plt.plot(xx,u,'-o'); plt.show()
%time rk4fft(1,256,0.1,1024)
def julia(c):
size=400
x=np.linspace(-1.5,1.5,size)
y=np.linspace(-1.0,1.0,size)
z=np.zeros((size,size),dtype=complex)
julia=np.zeros((size,size))
X,Y=np.meshgrid(x,y)
z=X+Y*1j
it=0
while z.any!=False:
z[z!=False]=z[z!=False]**2+c
ind=np.where(np.abs(z)>2.0)
julia[ind]=1/(10+it)
z[ind]=False
it+=1
if it>80: break
plt.figure(figsize=(20,10))
plt.imshow(julia,origin='lower',cmap='terrain')
plt.colorbar()
plt.show()
c=-0.70176-0.3842j
%time julia(c)
c=0.285+0.01j
%time julia(c)
c=0.0000-0.8000j
%time julia(c)
c=-0.4000+0.6000j
%time julia(c)
c=-0.8+0.156j
%time julia(c)