-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.py
More file actions
81 lines (61 loc) · 1.54 KB
/
errors.py
File metadata and controls
81 lines (61 loc) · 1.54 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import numpy as np
import matplotlib.pyplot as plt
import os,sys
from math import pi
def sumx(n):
a = 2*((np.sin(n*pi) - n*pi)/(n*pi)**2)*np.sin(n*pi*x)
return a
def sumt(n,t):
return np.exp(-t*(n*pi)**2)
error = os.system('make')
if error:
print 'make funket ikke'
sys.exit(1)
run = './main.x'
error = os.system(run)
if error:
print 'runtime error'
sys.exit(1)
x = np.linspace(0,1,11)[1:-1]
t = np.array((0.02,0.5))
v = np.zeros((9,2))
for m in range(1,300):
v[:,0] += sumx(m)*sumt(m,t[0])
v[:,1] += sumx(m)*sumt(m,t[1])
FE = np.loadtxt('ForwardEuler.dat').T
BE = np.loadtxt('BackwardEuler.dat').T
CN = np.loadtxt('CrankNicolson.dat').T
dFE = np.abs((FE)/v-1)
dBE = np.abs((BE)/v-1)
dCN = np.abs((CN)/v-1)
mdfe0 = max(dFE[0])
mdfe1 = max(dFE[1])
mdbe0 = max(dBE[0])
mdbe1 = max(dBE[1])
mdcn0 = max(dCN[0])
mdcn1 = max(dCN[1])
print 'maximal relative errors for FE,BE and CN at t = 0.02:'
print mdfe0, '\t', mdbe0, '\t',mdcn0
print 'maximal relative errors for FE,BE and CN at t = 0.5:'
print mdfe1, '\t', mdbe1, '\t',mdcn1
plt.figure()
plt.plot(x,v[:,0],'-x',label="sol")
plt.hold(1)
plt.plot(x,FE[:,0],'-x', label="FE")
plt.plot(x,BE[:,0],'-x',label="BE")
plt.plot(x,CN[:,0],'-x',label="CN")
plt.legend(loc=4)
plt.xlabel('x')
plt.ylabel('v')
plt.title('t = 0.02')
plt.figure()
plt.plot(x,v[:,1],'-x',label="sol")
plt.hold(1)
plt.plot(x,FE[:,1],'-x',label="FE")
plt.plot(x,BE[:,1],'-x',label="BE")
plt.plot(x,CN[:,1],'-x',label="CN")
plt.legend(loc=4)
plt.xlabel('x')
plt.ylabel('v')
plt.title('t = 0.02')
plt.show()