include iostream include cmath include GL glut include GL GL using nam

  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
#include <iostream>
#include <cmath>
#include <GL\glut.h>
#include <GL\GL.h>
using namespace std;
double f(double x, double y)
{
double f;
f = y*y - 2 * x*y - 3 * x*x;
return f;
}
double g(double x, double y)
{
double g;
g = y*y - x*y - 2 * x*x - 4;
return g;
}
double f_diff_x(double x, double y)
{
double f_diff_x;
f_diff_x = -2 * y - 6 * x;
return f_diff_x;
}
double f_diff_y(double x, double y)
{
double f_diff_y;
f_diff_y = 2 * y - 2 * x;
return f_diff_y;
}
double g_diff_x(double x, double y)
{
double g_diff_x;
g_diff_x = -y - 4 * x;
return g_diff_x;
}
double g_diff_y(double x, double y)
{
double g_diff_y;
g_diff_y = 2 * y - x;
return g_diff_y;
}
void display()
{
//координатная плоскость
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_LINES);
glVertex2f(-550, 0);
glVertex2f(550, 0);
glEnd();
glBegin(GL_LINES);
glVertex2f(0, 150);
glVertex2f(0, -150);
glEnd();
glPointSize(2.0);
for (int i = -550; i < 550; i = i + 10)
{
if (i == 0)
{
glPointSize(4.0);
glBegin(GL_POINTS);
glColor3f(1.0, 1.0, 1.0);
glVertex2d(i, 0);
glEnd();
}
else
{
glPointSize(2.0);
glBegin(GL_POINTS);
glColor3f(1.0, 1.0, 1.0);
glVertex2d(i, 0);
glEnd();
}
}
for (int i = -150; i < 150; i = i + 10)
{
if (i == 0)
{
glPointSize(4.0);
glBegin(GL_POINTS);
glColor3f(1.0, 1.0, 1.0);
glVertex2d(0, i);
glEnd();
}
else
{
glPointSize(2.0);
glBegin(GL_POINTS);
glColor3f(1.0, 1.0, 1.0);
glVertex2d(0, i);
glEnd();
}
}
double x0[2] = { 15, 10 }; //начальное приближение
double xk = x0[0];
double yk = x0[1];
double _xk; //промежуточное приближение
double qk, pk, x_next, y_next, max;
double eps = 0.0001;
int t = 10;
do
{
_xk = xk - f(xk, yk) / f_diff_x(xk, yk);
qk = (g(_xk, yk)*f_diff_x(xk, yk)) / (f_diff_x(xk, yk)*g_diff_y(_xk, yk) - f_diff_y(xk, yk)*g_diff_x(_xk, yk));
pk = (f(xk, yk) - qk*f_diff_y(xk, yk)) / f_diff_x(xk, yk);
x_next = xk - pk;
y_next = yk - qk;
glPointSize(3.0);
glBegin(GL_POINTS);
glColor3f(0.0, 0.0, 1.0);
glVertex2d(xk * t, yk * t);
glVertex2d(x_next * t, y_next * t);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3f(1.0, 0.0, 0.0);
glVertex2d(xk * t, yk * t);
glVertex2d(x_next * t, y_next * t);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3f(1.0, 0.0, 1.0);
glVertex2d(xk * t, yk * t);
glVertex2d(xk * t, 0);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3f(1.0, 0.0, 1.0);
glVertex2d(xk * t, yk * t);
glVertex2d(0, yk * t);
glEnd();
printf("curr: (%.5f ; %.5f)\n", xk, yk);
printf("next: (%.5f ; %.5f)\n", x_next, y_next);
xk = x_next;
yk = y_next;
max = (abs(pk) > abs(qk)) ? abs(pk) : abs(qk);
cout << endl;
} while (max > eps);
glFlush();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600*2, 165*2);
glutInitWindowPosition(0, 0);
glutCreateWindow("Sin");
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-600.0, 600.0, -200.0, 200.0, -1.0, 1.0);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}