Numerical Solutions of Partial Differential Equations
First we solve an ordinary differential equaiton d2udx2−12xu=−1
To solve this problem numerically, we divide the interval [0,1] into n equal subintervals with length Δx=xn−x0n. For our case Δx=1−0n=1n. The set of partitioning points or mesh points are given as x0=0
xxxxxxxxxx
#This program solves $$ \frac{d^2 u}{dx^2}-12 xu =-1 $$
# with \( 0 < x < 1 \) and the boundary conditions \( u(0)=1\) and \( u(1)=-1. \)
#Codes written by Ramjee Sharma
#All rights reserved
#10/20/2024
#Import Python libraries
import numpy as np
f=[-26, -1, -1,24]
# Taking a 3 * 3 matrix
A = np.array([[-52.4,25,0,0],
[25,-54.8,25,0],
[0,25,-57.2,25],
[0,0,25,-59.6]])
Â
A_inv=np.linalg.inv(A)
u=np.dot(A_inv, f)
print(np.round(u,4))
Heat Equaiton
Consider the one dimensional heat equaiton ut=uxx,0<x<1,0<t,
We use the mesh points x0,x1,x2,⋯,xi,⋯,xn
For a special case, we take Δx=1/4 and r=1/2. This will give Δt=1/32. The equations at i=1,2,3 become u1(m+1)=12(u0(m)+u2(m)),
The following Python program can be used to compute the numerical solutions of the heat equation.
xxxxxxxxxx
#Import Python libraries
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
Â
#Define parameters
Delta_x=1.0/4.0
r=0.500
x0=0
xN=1
N=5
total_time_steps=18
u=np.zeros((total_time_steps,N))
Â
Â
#space discritization
x=np.linspace(x0,xN,N)
t=np.linspace(0,total_time_steps,total_time_steps)
x1,x2=np.meshgrid(x,t)
Â
Â
Â
#Initial value function
def f(x):
return x
Â
#Initial and boundary values of u
u[0]=f(x)
u[0][0]=0.00
u[0][N-1]=0.00
Â
Â
#Time loop
for m in range (1,total_time_steps):
for i in range (1,N-1):
u[m][i]=r*u[m-1][i-1]+(1-2*r)*u[m-1][i]+r*u[m-1][i+1]
Â
print("u = ")
print(np.round(u,6))
Â
# Plot the surface
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface(x1, x2, u, vmin=u.min() * 2, cmap=cm.Blues)
Â
ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])
Â
plt.show()
Â
#Printing data as a table
import pandas as pd
data=pd.DataFrame(u)
data.index.name='m'
data.columns=['x1','x2','x3','x4','x5']
print(data)
Wave Equaiton
Consider the one dimensional wave equaiton utt=uxx,0<x<1,0<t,
We use the mesh points x0,x1,x2,⋯,xi,⋯,xn
xxxxxxxxxx
#Import Python libraries
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
Â
#Define parameters
Delta_x=1.0/4.0
rho=1
Delta_t=rho*Delta_x
rhosq=rho*rho
x0=0
xN=1
N=5
total_time_steps=18
u=np.zeros((total_time_steps,N))
Â
Â
#space discritization
x=np.linspace(x0,xN,N)
t=np.linspace(0,total_time_steps,total_time_steps)
x1,x2=np.meshgrid(x,t)
Â
Â
Â
#Initial value function
def f(x):
if x < 0.5:
return 2*x
else:
return 2*(1-x)
Â
#Initial value function
def g(x):
return 0
Â
#Initial and boundary values of u
Â
for k in range(1, N-1):
u[0][k]=f(x[k])
Â
u[0][0]=0.00
u[0][N-1]=0.00
Â
#Values of u at m=1
for j in range (1, N-1):
u[1][j]=0.5*rhosq*f(x[j-1])+(1-rhosq)*f(x[j])+0.5*rhosq*f(x[j+1])+Delta_t*g(x[j])
Â
Â
#Time loop
for m in range (2,total_time_steps):
for i in range (1,N-1):
u[m][i]=rhosq*u[m-1][i-1]+2*(1-rhosq)*u[m-1][i]+rhosq*u[m-1][i+1]-u[m-2][i]
Â
print("u = ")
print(np.round(u,6))
Â
# Plot the surface
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface(x1, x2, u, vmin=u.min() * 2, cmap=cm.Blues)
Â
ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])
Â
plt.show()
Â
#Printing data as a table
import pandas as pd
data=pd.DataFrame(u)
data.index.name='m'
data.columns=['x1','x2','x3','x4','x5']
print(data)
Two Dimensional Laplace Equaiton
Consider the two dimensional Laplace Equation with the boundary conditions ∂2u∂x2+∂2u∂x2=0
xxxxxxxxxx
#This program solves the two dimensional laplace equaiton $$\nabla^2 u=0$$
# with \( 0 < x < 1 , 0 < y < 1 \)
# and the boundary conditions u(0,y) = 0, \quad u(x,0)=0, \quad u(x,1)=50, \quad u(1,y)=50
#Import Python libraries
import numpy as np
f=[0, 0, 0, 0, 0, -25, -50, -50, -75]
# Taking a 9 * 9 matrix
A = np.array([[-4, 1, 0, 1, 0, 0, 0, 0, 0],
[1, -4, 1, 0, 1, 0, 0, 0, 0],
[0, 1, -4, 0, 0, 1, 0, 0, 0],
[0, 0, 0, -4, 1, 0, 1, 0, 0],
[0, 1, 0, 1, -4, 1, 0, 1, 0],
[0, 0, 1, 0, 1, -4, 0, 0, 1],
[0, 0, 0, 1, 0, 0, -4, 1, 0],
[0, 0, 0, 0, 1, 0, 1, -4, 1],
[1, 0, 0, 0, 0, 1, 0, 1, -4]])
Â
A_inv=np.linalg.inv(A)
u=np.dot(A_inv, f)
print(np.round(u,4))