using System using System Collections Generic using System Drawing usi

 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LR2
{
class SeidelMethod
{
PictureBox pictureBox;
RichTextBox richTextBox;
Graphics g;
float scale = 35;
public SeidelMethod(PictureBox pictureBox, RichTextBox richTextBox, float scale = 35)
{
this.scale = scale;
this.pictureBox = pictureBox;
this.richTextBox = richTextBox;
if (pictureBox != null)
{
g = pictureBox.CreateGraphics();
g.TranslateTransform(pictureBox.Width / 2, pictureBox.Height / 2);
g.Clear(Color.White);
g.DrawLine(Pens.Black, 0, -200, 0, 200);
g.DrawLine(Pens.Black, -200, 0, 200, 0);
drawGraphic();
}
}
public void solve(double eps, PointF startPoint)
{
richTextBox.Text = string.Empty;
double x1_start = startPoint.X;
double x2_start = startPoint.Y;
double curX1 = 1e9;
double curX2 = 1e9;
double nextX1 = x1_start;
double nextX2 = x2_start;
richTextBox.Text += "x(k1) = " + nextX1.ToString() + ";\nx(k2) = " + nextX2 + ";\n\n";
int nIter = 0;
while (Math.Max(Math.Abs(nextX1 - curX1), Math.Abs(nextX2 - curX2)) > eps)
{
curX1 = nextX1;
curX2 = nextX2;
nextX1 = (1 - curX2 * curX2) / 2;
nextX2 = -1 * (nextX1 * nextX1) - 1; //Если просто МПИ, то curX1 (только предыдущая итерация)
if (nIter > 50 || Math.Abs(nextX1) > 1e6 || Math.Abs(nextX2) > 1e6)
{
MessageBox.Show("Метод Зейделя не сходится");
return;
}
g.DrawLine(Pens.Black, (float)(scale * curX1), (float)(-scale * curX2), (float)(nextX1 * scale), (float)(-nextX2 * scale));
g.FillEllipse(Brushes.Red, (float)(nextX1 * scale - 2), (float)(-nextX2 * scale - 2), 4, 4);
richTextBox.Text += "x(k1) = " + nextX1.ToString() + ";\nx(k2) = " + nextX2 + ";\n\n";
++nIter;
}
g.FillEllipse(Brushes.Blue, (float)(x1_start * scale - 3), (float)(-x2_start * scale - 3), 6, 6);
g.FillEllipse(Brushes.Green, (float)(nextX1 * scale - 3), (float)(-nextX2 * scale - 3), 6, 6);
}
private bool isEqualNull(double x)
{
return Math.Abs(x) <= 1e-8;
}
private void drawGraphic()
{
float x, y1, y2, _y2;
for (x = -4; x < 4; x += (float)0.01)
{
y1 = -1 - x * x;
y2 = (float)Math.Sqrt((1.0 - 2 * (double)x));
_y2 = -(float)Math.Sqrt((1.0 - 2 * (double)x));
g.FillEllipse(Brushes.Blue, (float)(x * scale - 1), (float)(-y1 * scale - 1), 2, 2);
g.FillEllipse(Brushes.Cyan, (float)(x * scale - 1), (float)(-y2 * scale - 1), 2, 2);
g.FillEllipse(Brushes.Cyan, (float)(x * scale - 1), (float)(-_y2 * scale - 1), 2, 2);
}
}
}
}