-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewton_method.cc
123 lines (108 loc) · 4.12 KB
/
newton_method.cc
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
#include "runge-kutta_method.hh"
#include "newton_method.hh"
#include "defs.hh"
double calc_residual (boundary_conditions const & calculated, boundary_conditions const & wanted, double const * jacobi_matrix) {
double const k1 = jacobi_matrix[0] * jacobi_matrix[0] + jacobi_matrix[1] * jacobi_matrix[1];
double const k2 = jacobi_matrix[2] * jacobi_matrix[2] + jacobi_matrix[3] * jacobi_matrix[3];
return sqrt (
(calculated.x1 - wanted.x1) * (calculated.x1 - wanted.x1) / k1 +
(calculated.x2 - wanted.x2) * (calculated.x2 - wanted.x2) / k2
);
}
boundary_conditions get_bc_at_right_end (
double const step_length,
size_t const total_steps,
boundary_conditions const & bc_0,
double const parameter)
{
boundary_conditions start = bc_0;
return integrator (step_length, total_steps, start, parameter);
}
void get_jacobi_matrix (
double const step_length,
size_t const total_steps,
boundary_conditions const & bc_0,
boundary_conditions const & bc_at_right_end,
double const parameter,
double * jacobi_matrix
) {
boundary_conditions start = bc_0;
start.p1 = bc_0.p1 + DELTA;
boundary_conditions bc_at_right_end_moved = integrator (step_length, total_steps, start, parameter);
jacobi_matrix[0] = (bc_at_right_end_moved.x1 - bc_at_right_end.x1) / DELTA;
jacobi_matrix[2] = (bc_at_right_end_moved.x2 - bc_at_right_end.x2) / DELTA;
start.p1 = bc_0.p1;
start.p2 = bc_0.p2 + DELTA;
bc_at_right_end_moved = integrator (step_length, total_steps, start, parameter);
jacobi_matrix[1] = (bc_at_right_end_moved.x1 - bc_at_right_end.x1) / DELTA;
jacobi_matrix[3] = (bc_at_right_end_moved.x2 - bc_at_right_end.x2) / DELTA;
}
boundary_conditions get_next (
boundary_conditions const & bc_0,
boundary_conditions const & bc_1,
boundary_conditions const & bc_at_right_end,
boundary_conditions const & start,
double const gamma,
double const * jacobi_matrix
) {
double jacobi_det = jacobi_matrix[0] * jacobi_matrix[3] - jacobi_matrix[1] * jacobi_matrix[2];
boundary_conditions next = start;
next.p1 = bc_0.p1 - gamma * (
(bc_at_right_end.x1 - bc_1.x1) * jacobi_matrix[3] -
(bc_at_right_end.x2 - bc_1.x2) * jacobi_matrix[1]
)
/ jacobi_det;
next.p2 = bc_0.p2 - gamma * (
(bc_at_right_end.x2 - bc_1.x2) * jacobi_matrix[0] -
(bc_at_right_end.x1 - bc_1.x1) * jacobi_matrix[2]
)
/ jacobi_det;
return next;
}
int newton_method (
double const step_length, // step length
size_t const total_steps, // step number
boundary_conditions & bc_0, // conditions in 0
boundary_conditions & bc_1, // conditions in 1
double const parameter // parameter
)
{
boundary_conditions start, next, bc_at_right_end, bc_at_right_end_moved;
size_t iterations = 0;
double gamma, residual, next_residual;
double jacobi_matrix[4] = {0};
start = bc_0;
while (iterations < MAX_ITERATIONS) {
gamma = 1.0;
bc_at_right_end = get_bc_at_right_end(step_length, total_steps, bc_0, parameter);
get_jacobi_matrix (step_length, total_steps, bc_0, bc_at_right_end, parameter, jacobi_matrix);
residual = calc_residual (bc_at_right_end, bc_1, jacobi_matrix);
if (residual < EPS) {
bc_1.p1 = bc_at_right_end.p1;
bc_1.p2 = bc_at_right_end.p2;
return 0;
}
next_residual = residual + 1;
while (next_residual > residual) {
next = get_next (bc_0, bc_1, bc_at_right_end, start, gamma, jacobi_matrix);
bc_at_right_end = get_bc_at_right_end (step_length, total_steps, next, parameter);
get_jacobi_matrix (step_length, total_steps, next, bc_at_right_end, parameter, jacobi_matrix);
next_residual = calc_residual (bc_at_right_end, bc_1, jacobi_matrix);
if (next_residual < EPS) {
bc_0.p1 = next.p1;
bc_0.p2 = next.p2;
bc_1.p1 = bc_at_right_end.p1;
bc_1.p2 = bc_at_right_end.p2;
return 0;
}
gamma *= .5;
}
bc_0.p1 = next.p1;
bc_0.p2 = next.p2;
bc_1.p1 = bc_at_right_end.p1;
bc_1.p2 = bc_at_right_end.p2;
residual = next_residual;
++iterations;
}
return 1;
}