-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.cpp
162 lines (152 loc) · 4.55 KB
/
app.cpp
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
#include "stdafx.h"
#include "math.h"
#include "conio.h"
#include "iostream"
#include "fstream"
const double eps = 1e-5;
typedef double vec[2];
using namespace std;
ofstream fout1("Result I.txt");
ofstream fout2("Result II.txt");
double fnw1(double x, double y) {
return((2 * y*(sin(x + y) + *x - ) - (x*x + y*y - 1)*cos(x+y)) / (2 *
y*(cos(x + y) + ) - 2 * x*cos(x + y)));
}
double fnw2(double x, double y) {
return((-2*x*(sin(x + y) + *x - ) + (cos(x + y) + )*(x*x + y*y -
1)) / (2 * y*(cos(x + y) + ) - 2 * x*cos(x + y)));
}
double f11(double x, double y) {
return (sin(x+y)+ *x- );
}
double f22(double x, double y) {
return (x*x+y*y-1);
}
double f1(double x, double y) {
return (cos(x+ )- *y- );
}
double f2(double x, double y) {
return (x+sin(y- )- );
}
double x1(double x, double y) {
return ( + sin( - y));
}
double y1(double x, double y) {
return (- / + cos(x + ) / );
}
double norm(vec ar) {
return (sqrt(ar[0] * ar[0] + ar[1] * ar[1]));
}
void iter(double xf, double yf, double fx(double, double), double fy(double, double),
double f1(double, double), double f2(double, double)) {
vec xkp, xk, dif, fn;
int it = 0;
xk[0] = xf;
xk[1] = yf;
fn[0] = f1(xk[0], xk[1]);
fn[1] = f2(xk[0], xk[1]);
fout1 << "(x" << it << ",y" << it << ") = (" << xk[0] << "," << xk[1] << ")" <<
endl;
fout1 << "(f1(x" << it << ",y" << it << "),f2(x" << it << ",y" << it << ")) = ("
<< fn[0] << "," << fn[1] << ")" << endl;
fout1 << "||F|| = " << norm(fn) << endl << endl;
xkp[0] = fx(xk[0],xk[1]);
xkp[1] = fy(xk[0],xk[1]);
dif[0] = xkp[0] - xk[0];
dif[1] = xkp[1] - xk[1];
fn[0] = f1(xkp[0], xkp[1]);
fn[1] = f2(xkp[0], xkp[1]);
++it;
fout1 << "(x" << it << ",y" << it << ") = (" << xkp[0] << "," << xkp[1] << ")" <<
endl;
fout1 << "(f1(x" << it << ",y" << it << "),f2(x" << it << ",y" << it << ")) = ("
<< fn[0] << "," << fn[1] << ")" << endl;
fout1 << "||F|| = " << norm(fn) << endl << endl;
while ((norm(dif) > eps)||(norm(fn) > eps )) {
++it;
xk[0] = xkp[0];
xk[1] = xkp[1];
xkp[0] = fx(xk[0], xk[1]);
xkp[1] = fy(xk[0], xk[1]);
dif[0] = xkp[0] - xk[0];
dif[1] = xkp[1] - xk[1];
fn[0] = f1(xkp[0], xkp[1]);
fn[1] = f2(xkp[0], xkp[1]);
fout1 << "(x" << it << ",y" << it << ") = (" << xkp[0] << "," << xkp[1] <<
")" << endl;
fout1 << "(f1(x" << it << ",y" << it << "),f2(x" << it << ",y" << it << "))
= (" << fn[0] << "," << fn[1] << ")" << endl;
fout1 << "||F|| = " << norm(fn) << endl << endl;
}
cout << "Answer : (" << xkp[0] << "," << xkp[1] << ")" << endl << endl;
fout1 << "Answer : (" << xkp[0] << "," << xkp[1] << ")" << endl;
}
void newton(double xf, double yf, double fnw1(double, double), double fnw2(double,
double), double f11(double, double), double f22(double, double)) {
vec xkp, xk, dif, fn;
int it = 0;
xk[0] = xf;
xk[1] = yf;
fn[0] = f11(xk[0], xk[1]);
fn[1] = f22(xk[0], xk[1]);
fout2 << "(x" << it << ",y" << it << ") = (" << xk[0] << "," << xk[1] << ")" <<
endl;
fout2 << "(f1(x" << it << ",y" << it << "),f2(x" << it << ",y" << it << ")) = ("
<< fn[0] << "," << fn[1] << ")" << endl;
fout2 << "||F|| = " << norm(fn) << endl << endl;
xkp[0] = xk[0] - fnw1(xk[0], xk[1]);
xkp[1] = xk[1] - fnw2(xk[0], xk[1]);
dif[0] = xkp[0] - xk[0];
dif[1] = xkp[1] - xk[1];
fn[0] = f1(xkp[0], xkp[1]);
fn[1] = f2(xkp[0], xkp[1]);
++it;
fout2 << "(x" << it << ",y" << it << ") = (" << xkp[0] << "," << xkp[1] << ")" <<
endl;
fout2 << "(f1(x" << it << ",y" << it << "),f2(x" << it << ",y" << it << ")) = ("
<< fn[0] << "," << fn[1] << ")" << endl;
fout2 << "||F|| = " << norm(fn) << endl << endl;
fout2 << "fnw1" << fnw1(xk[0], xk[1]) << "fnw2" << fnw2(xk[0], xk[1]) << endl;
while ((norm(dif) > eps) || (norm(fn) > eps)) {
++it;
xk[0] = xkp[0];
xk[1] = xkp[1];
xkp[0] = xk[0] - fnw1(xk[0], xk[1]);
xkp[1] = xk[1] - fnw2(xk[0], xk[1]);
dif[0] = xkp[0] - xk[0];
dif[1] = xkp[1] - xk[1];
fn[0] = f11(xkp[0], xkp[1]);
fn[1] = f22(xkp[0], xkp[1]);
fout2 << "(x" << it << ",y" << it << ") = (" << xkp[0] << "," << xkp[1] <<
")" << endl;
fout2 << "(f1(x" << it << ",y" << it << "),f2(x" << it << ",y" << it << "))
= (" << fn[0] << "," << fn[1] << ")" << endl;
fout2 << "||F|| = " << norm(fn) << endl << endl;
}
cout << "Answer : (" << xkp[0] << "," << xkp[1] << ")" << endl << endl;
fout2 << "Answer : (" << xkp[0] << "," << xkp[1] << ")" << endl;
}
int main()
{
double xc1, yc1, xc2, yc2;
cout << "I. Input the closest dot " << endl;
cout << "x = ";
cin >> xc1;
cout << endl;
cout << "y = ";
cin >> yc1;
cout << endl;
iter(xc1,yc1,x1,y1,f1,f2);
cout << "II. Input the closest dot " << endl;
cout << "x = ";
cin >> xc2;
cout << endl;
cout << "y = ";
cin >> yc2;
cout << endl;
newton(xc2, yc2, fnw1, fnw2, f11, f22);
fout1.close();
fout2.close();
_getch();
return 0;
}