-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.py
More file actions
104 lines (84 loc) · 3.24 KB
/
methods.py
File metadata and controls
104 lines (84 loc) · 3.24 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import json
from sympy import *
import csv
class Newtons:
def __init__(self):
with open('config.json') as f:
config = json.load(f)
self.func_str = config['f(x)']
if config['Iteration'] == 0:
self.xi = config['xi']
else:
self.xi = config['xi+1']
self.eps = config['Eps']
self.Rest = config['Rest']
self.Method = config['Method']
self.criteria = config['Stop_Criteria']
self.number_iteration = config['Number of Iteration']
self.iteration = config['Iteration']+1
x = symbols('x')
self.func_expr = sympify(self.func_str)
self.fxi = self.func_expr.subs('x', self.xi).evalf()
self.f_prime = self.find_derivative(self.func_expr)
self.f_sec_prime = self.find_derivative(self.f_prime)
self.tangent = self.find_tanget()
def find_derivative(self, func_expr):
x = symbols('x')
func_prime = diff(func_expr, x)
return func_prime
def find_tanget(self):
x = symbols('x')
tanget = self.f_prime.subs('x', self.xi).evalf()*(x-self.xi)+ self.func_expr.subs('x', self.xi).evalf()
return tanget
def Iteration(self):
if not self.criteria:
x = self.xi
f_prime_val = self.f_prime.subs('x', self.xi).evalf()
if f_prime_val !=0:
self.xi_1 = x - self.fxi / f_prime_val
else:
self.xi_1 = x
self.fxi_1 = self.func_expr.subs('x', self.xi_1).evalf()
if abs(self.fxi-self.fxi_1)<self.eps or self.iteration >= self.number_iteration:
self.criteria = True
elif not self.fxi_1.is_real:
self.criteria = True
self.fxi_1 = 0
def update_config(self):
with open('config.json', "r") as f:
config = json.load(f)
if self.iteration == 1:
config['dx'] = str(self.f_prime)
config['d2x'] = str(self.f_sec_prime)
config['f(xi)'] = float(self.fxi)
config['f(x0)'] = float(self.fxi)
else:
config['xi-1'] = float(self.xi)
config['f(xi-1)'] = config['f(xi)']
config['xi'] = config['xi+1']
config['f(xi)'] = config['f(xi+1)']
config['xi+1'] = float(self.xi_1)
config['tangent'] = str(self.tangent)
config['f(xi+1)'] = float(self.fxi_1)
config['Rest'] = self.Rest
config['Iteration'] = self.iteration
config['Stop_Criteria'] = self.criteria
with open('config.json', "w") as f:
json.dump(config, f, indent=4)
with open('result.csv', 'a', newline='') as csvfile:
# Create a CSV writer object
writer = csv.writer(csvfile)
# Write the data row
if self.iteration == 1:
writer.writerow([config['xi'], config['f(xi)']])
writer.writerow([config['xi+1'], config['f(xi+1)']])
def root_search():
methods = {
"Newtons": Newtons,
}
with open('config.json', "r") as f:
config = json.load(f)
Num_method = methods[config['Method']]()
Num_method.Iteration()
Num_method.update_config()
root_search()