-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathode_bvp.py
53 lines (42 loc) · 1.11 KB
/
ode_bvp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python3
import numpy as np
from math import *
# ODE parameters
alpha=0.5
beta=-2.0
gamma=0.1
# Length of interval and boundary conditions
c_1=0.0
c_2=0.0
a=0.0
b=1.0
# Set grid resolution
n=100
h=(b-a)/(n-1)
x=np.linspace(a,b,n)
# Generate the centered difference differentiation matrix for u'
f=1.0/(2*h)
D1=np.diag(-np.ones(n-1)*f,-1)+np.diag(np.ones(n-1)*f,1)
# Generate the centered difference differentiation matrix for u''
f=1/(h*h)
D2=np.diag(-np.ones(n)*f*2)+np.diag(np.ones(n-1)*f,1)+np.diag(np.ones(n-1)*f,-1)
# Build the system matrix, A
A=-alpha*D2+beta*D1+gamma*np.identity(n)
# Define the right-hand side vector
F=np.array([-alpha*exp(z)*(4*pi*cos(2*pi*z)+(1-4*pi*pi)*sin(2*pi*z))+ \
beta*exp(z)*(sin(2*pi*z)+2*pi*cos(2*pi*z))+ \
gamma*exp(z)*sin(2*pi*z) for z in x])
# Set first and last rows of A to enforce Dirichlet conditions
A[0,0]=1
A[0,1]=0
A[n-1,n-2]=0
A[n-1,n-1]=1
# Set first and last rows of F
F[0]=c_1
F[n-1]=c_2
# Solve the linear system
U=np.linalg.solve(A,F)
# Print the exact and approximate solutions
for i in range(n):
uex=exp(x[i])*sin(2*pi*x[i])
print(x[i],U[i],uex,U[i]-uex)