-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME
More file actions
179 lines (111 loc) · 5.32 KB
/
Copy pathREADME
File metadata and controls
179 lines (111 loc) · 5.32 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
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
======================================
Contributor: Yinzhen Jin
Course: APC 524 Project 3
Program: Newton Solver
This is README instruction
======================================
This README file details how to run a implementation of a simple root
finder (Newton’s method). It is called Newton solver
The instructions assume that the underlying system is using python; It
may not be able to run by other languages.
The Newton solver contains two files, one is newton.py, the other is function.py
newton.py is the real solver, function.py is used to solve Jacobian matrix that
is used for Newton solver
To use Newton solver for the first time,
one should
1. Make a directory for solver:
copy function.py and newton.py
2. In order to use the solver it is better to create a solver.py that can use
the Newton Solver.
3. In solver.py, you have to first write:
*******************************
#!/usr/bin/env python
import newton
********************************
to use newton class
4. In Newton Solver you have 5 input parameters:
f: the function that you want to solve. It needs to be defined in the solver.py
tol: tolerance for iteration (iterate until |f(x)| < tol), default number is 1.e-6
maxiter: maximum number of iterations to perform, default number is 20
dx: step size for computing approximate Jacobian, default number is 1.e-6
Df: matrix that is used for analytical solution, default is None, using numerical solution
5. The basic format:
*********************************
solver = newton.Newton(f, tol = 1.e-8, maxiter = 20, Df = None)
x = solver.solve(x0)
*********************************
x0 is the initial guess for iterations
6. This Newton solver can solve any converge function numerically and can solve some functions
with more accurately using analytical Jacobian matrix. To decide whether use analytical Jacobian
or non-analytical using Df as input to decide
7. To use numerical Jacobian matrix:
The only thing need to be done is define the function you need to solve such as:
*********************************
f = lambda x : 3.0 * x + 6.0
*********************************
for simple linear
or
*********************************
def f(x = N.matrix(N.zeros((2,1)))):
ans = N.matrix(N.zeros((2,1)))
ans[0,0] = 10.0*x[0,0]**2 - x[1,0]**2 - x[0,0] + 1.0
ans[1,0] = 2.0*x[0,0] - x[1,0] - 1.0
return ans
*********************************
for more than one dimension for
#################################
10*(x^2)-(y^2)-x + 1 = 0
2*x - y = 1
#################################
8. This Newton solver can solve two kinds of equations:
Polynomial:
#################################
10*(x^2)-(y^2)-x + 1 = 0 (1)
2*x - y = 1 (2)
#################################
Like the equations above, you have to change to Df to list[]
Df = ["Polynomial", df[0], df[1]]:
"Polynomial" is to tell the solver the analytical solution for polynomial solution
df[0] is to store information in equation (1)
df[1] is to store all information in equation (2)
if you have more than 2 euquations, you will have to define df[2], df[3],...
* df[i] is a list contains two matrix:
* The first matrix is to store all coeffecients.
* The second matrix is to store all orders for very variables
for example, for equation (1):
There are three terms except constant with 2 variable x, y. They are:
10*(x^2), -y^2, -x
The coefficients are 10 -1 -1, thus the first matrix should be N.matrix("10.0, -1.0, -1.0")
For x, the power in three terms are 2 0 1
For y, the power in three terms are 0 2 0
Thus the second matrix should be N.matrix("2.0, 0, 1.0; 0.0, 2.0, 0.0")]
To sum up Df can be constructed by following:
*********************************
df = []
df.append([N.matrix("10.0, -1.0, -1.0"), N.matrix("2.0, 0, 1.0; 0.0, 2.0, 0.0")])
df.append([N.matrix("2.0, -1.0"), N.matrix("1.0, 0.0; 0.0, 1.0")])
solver = newton.Newton(f, tol = 1.e-9, maxiter = 40, dx = 1.e-6, Df = ["Polynomial", df[0], df[1]])
*********************************
Simple Complex:
This solver can also function as following:
#################################
sin(x) + 2.0*cos(y) - x^2 = 0 (1)
3*tan(x) - tan(y) = 0 (2)
#################################
The way to construct Df is a little different:
still Df has the format:
Df = ["SimpleComplex", df[0], df[1]]
but here df store all information for different equations.
for above system functions there are two equations, if there are more we will have df[2], df[3], ...
for example for df[0], we have two equations thus there are two list in df[0], every list contains dictionaries for each variables:
equation (1) for x, we have sin(x) and -x^2, the list for that equation is {"sin": 1, 2: -1},
"sin" and "2" are the operators, 1, -1 are the corresponding coeffecients. The left term is for y which is {"cos": 2}, thus df[0] = [{"sin": 1, 2: -1}, {"cos": 2}] if we have another varibale we will have
df[0] = [{}, {}, {}]
*********************************
df = []
df.append([{"sin": 1, 2: -1}, {"cos": 2}])
df.append([{"tan": 3}, {"tan": -1}])
Df = ["SimpleComplex", df[0], df[1]]
*********************************
9. There is another input in Newton solver which is r, by default 20000
It is used to test condition that the approximated root must lie within a radius r of theinitial guess x0, or the iteration loop raises an exception. You can add some value and see whether your initial guess will lie within the radius