-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmidpoint.py
175 lines (128 loc) · 5.02 KB
/
midpoint.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import numpy as np
from math import exp
from mPlot import MPlot
import argparse
import time
import csv
import os.path
# INPUT ########################################################################
def validate_int_greater_than(lower_bound, arg_name, arg_val):
if arg_val <= lower_bound:
print(f'{arg_name} is expected to be an int greater than {lower_bound}')
print(f'{arg_name} value: {arg_val}')
exit()
def validate_order(arg1_name, arg1_val, arg2_name, arg2_val):
if arg1_val >= arg2_val:
print(f'{arg1_name} is expected to be smaller than {arg2_name}')
print(f'{arg1_name} value: {arg1_val}, {arg2_name} value: {arg2_val}')
exit()
def get_args():
arg_parser = argparse.ArgumentParser(
description='''Midpoint method for equations of the form x'(t) = A*x + B*exp(-t)''',
formatter_class=argparse.RawTextHelpFormatter)
arg_parser.add_argument(
'-A', type=float, default=-3, help='A coefficient')
arg_parser.add_argument(
'-B', type=float, default=2, help='B coefficient')
arg_parser.add_argument(
'-a', type=float, default=0, help='Start of the range to be inspected')
arg_parser.add_argument(
'-b', type=float, default=10, help='End of the range to be inspected')
arg_parser.add_argument(
'-n', type=int, default=16, help='Amount of steps')
arg_parser.add_argument(
'-xa', type=float, default=2, help='f(a), initial value')
arg_parser.add_argument(
'-E', type=float, default=0.001, help='Precision of solution')
args = arg_parser.parse_args()
validate_int_greater_than(1, 'n', args.n)
validate_order('a', args.a, 'b', args.b)
return args
# INPUT ########################################################################
# OUTPUT #######################################################################
def get_output_data(n, xs_act, xs, prec, id):
N = len(xs_act)
data = [None] * N
for i in range(N):
data[i] = {'id': id,
'steps': n,
'precision': prec,
'actual value': xs_act[i],
'calculated value': xs[i]}
return data
def print_output_data(data):
for r in data:
print(str(r))
print()
def append_to_file(path, data):
file_exists = os.path.isfile(path)
with open(path, 'a+') as file:
writer = csv.DictWriter(file, fieldnames=data[0].keys(), delimiter='|')
if not file_exists:
writer.writeheader()
writer.writerows(data)
def output(output_file_path, fig, n, ts, xs_act, xs, prec, steps, id):
output_data = get_output_data(n, xs_act, xs, prec, id)
print_output_data(output_data)
append_to_file(output_file_path, output_data)
fig.update_displayer(ts, [xs_act, xs], ['actual', 'calculated'], steps)
# OUTPUT #######################################################################
# LOGIC ########################################################################
def f_deriv(A, B, t, x):
return A * x + B * exp(-t)
def calculate_C(A, B, a, xa):
return (xa * (A + 1) + B * exp(-a)) / (exp(A * a) * (A + 1))
def f(A, B, C, t):
return C * exp(A * t) - ((B * exp(-t)) / (A + 1))
def calculate_xs_act(A, B, C, ts):
N = len(ts)
xs_act = np.zeros(N)
for i in range(N):
xs_act[i] = f(A, B, C, ts[i])
return xs_act
def midpoint(A, B, xa, ts):
N = len(ts)
xs = np.zeros(N)
xs[0] = xa
for i in range(N - 1):
h = ts[i + 1] - ts[i]
xk1 = xs[i] + h / 2 * f_deriv(A, B, ts[i], xs[i])
xk2 = xs[i] + h * f_deriv(A, B, ts[i] + h / 2, xk1)
xs[i + 1] = xk2
return xs
def calculate_precision(xs, xs_act):
return np.sum(np.abs(np.subtract(xs, xs_act, dtype=np.float))) / xs.shape[0]
def midpoint_iteration(A, B, C, a, b, n, xa):
ts = np.linspace(a, b, n)
xs_act = calculate_xs_act(A, B, C, ts)
xs = midpoint(A, B, xa, ts)
prec = calculate_precision(xs, xs_act)
return ts, xs_act, xs, prec
def midpoint_with_precision(A, B, a, b, n, xa, E):
C = calculate_C(A, B, a, xa)
ts = np.linspace(a, b, n)
xs_act = calculate_xs_act(A, B, C, ts)
output_file_path = f'midpoint_report_{time.strftime("%Y-%m-%d_%H-%M-%S")}.csv'
fig = MPlot(output_file_path, a, b)
fig.update_displayer(ts, [xs_act], ['actual'], 0, True)
id = 0
while True:
ts, xs_act, xs, prec = midpoint_iteration(A, B, C, a, b, n, xa)
output(output_file_path, fig, n, ts, xs_act, xs, prec, n, id)
if prec <= E:
fig.end_interation_and_add_buttons()
return xs, xs_act
n *= 2
id += 1
time.sleep(1)
# LOGIC ########################################################################
if __name__ == '__main__':
args = get_args()
try:
midpoint_with_precision(args.A, args.B, args.a, args.b, args.n, args.xa, args.E)
except ZeroDivisionError:
print('Zero division error')
except OverflowError:
print('Too large values')
except:
print('Error occurred')