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); } } } }