/
error_plot.py
125 lines (99 loc) · 2.65 KB
/
error_plot.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 8 15:42:05 2019
@author: maged
part of <poisson2D.py>
"""
import numpy as np
import matplotlib.pyplot as plt
from solver import *
from l2error import *
from h1error import *
start = 2
stop = 7
step = 1
v = 0
for k in range(start,stop,step):
v = v + 1
h1_error = np.zeros(v)
e2 = np.zeros(v)
h = np.zeros(v)
node_num = np.zeros(v)
elements = np.zeros(v)
print ( '' )
print ( ' Total Nodes Total Elements h L2_error H1_error' )
print ( '' )
v = 0
for k in range(start,stop,step):
i = 2**k
print ('i=',i)
elements[v]= i * i
u,h[v],node_num[v] = solver(element_linear_num = i)
e2[v] = L2_error(element_linear_num = i,u = u)
h1_error[v] = H1_error(element_linear_num = i,u = u)
print ( ' %4d %4d %8f %14g %14g'
% ( node_num[v],elements[v], h[v], e2[v], h1_error[v] ) )
v = v + 1
print ( '' )
#
# plotting L2 error
#
plt.plot(node_num,e2,'b')
plt.xlabel('Nodes')
plt.ylabel('$L_2 \,\,\, Error$')
plt.grid()
plt.title('Solution error in the $L_2$ norm\n with the total number of nodes')
plt.show()
plt.plot(elements,e2,'b')
plt.xlabel('Elements')
plt.ylabel('$L_2 \,\,\, Error$')
plt.grid()
plt.title('Solution error in the $L_2$ norm\n with the total number of elements')
plt.show()
plt.plot(h,e2,'r')
plt.xlabel('h')
plt.ylabel('$L_2 \,\,\, Error$')
plt.grid()
plt.title('Solution error in the $L_2$ norm\n with the grid spacing $\Delta x = \Delta y = h $')
plt.show()
plt.plot(np.log2(h),np.log2(e2),'r')
plt.xlabel('log(h)')
plt.ylabel('$log(L_2 \,\,\, Error)$')
plt.grid()
plt.show()
#
# plotting H1 error
#
plt.plot(node_num,h1_error,'b')
plt.xlabel('Nodes')
plt.ylabel('$H_1 \,\,\, Error$')
plt.grid()
plt.title('Solution error in the $H_1$ semi-norm\n with the total number of nodes')
plt.show()
plt.plot(elements,h1_error,'b')
plt.xlabel('Elements')
plt.ylabel('$H_1 \,\,\, Error$')
plt.grid()
plt.title('Solution error in the $H_1$ semi-norm\n with the total number of elements')
plt.show()
plt.plot(h,h1_error,'r')
plt.xlabel('h')
plt.ylabel('$H_1 \,\,\, Error$')
plt.grid()
plt.title('Solution error in the $H_1$ semi-norm\n with the grid spacing $\Delta x = \Delta y = h $')
plt.show()
plt.plot(np.log2(h),np.log2(h1_error),'r')
plt.xlabel('log(h)')
plt.ylabel('$log(H_1 \,\,\, Error)$')
plt.grid()
plt.show()
#
# To compare the errors
#
plt.plot(np.log2(h),np.log2(e2),'b', label = '$log(L_2 \,\,\, Error)$')
plt.plot(np.log2(h),np.log2(h1_error),'r',label = '$log(H_1 \,\,\, Error)$')
plt.xlabel('log(h)')
plt.legend()
plt.grid()
plt.show()