Guest User

Untitled

a guest
Nov 12th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.79 KB | None | 0 0
  1. float[] a = new float[4];
  2. double fx(float x) {
  3.     return a[0] * Math.Pow(x, 3) + a[1] * Math.Pow(x, 2) + a[2] * x + a[3];
  4. }
  5. double fxS(float x) {
  6.     return 3 * a[0] * Math.Pow(x, 2) + 2 * a[1] * x + a[2];
  7. }
  8. private void button1_Click(object sender, EventArgs e) {
  9.     textBox3.Clear();
  10.     textBox4.Clear();
  11.     float x0, xn, h;
  12.     int n = 0;
  13.     float[] x;
  14.     string[] t = textBox5.Text.Split(" ,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  15.     for (int i = 0; i < a.Length; i++)
  16.         a[i] = Convert.ToSingle(t[i]);
  17.     float eps = Convert.ToSingle(textBox7.Text);
  18.     x0 = Convert.ToSingle(textBox1.Text);
  19.     xn = Convert.ToSingle(textBox2.Text);
  20.     h = Convert.ToSingle(textBox6.Text);
  21.     n = Convert.ToInt32(xn / h);
  22.     x = new float[n * n * n];
  23.     double first = 0, last = 0;
  24.     int k = 0;
  25.     for (double i = x0; i < xn; i += h) {
  26.         k++;
  27.         double val = fx((float) i);
  28.         if (k == 1) first = val;
  29.         if (k == n) last = val;
  30.         if (Convert.ToString(i).Length == 1) {
  31.             textBox3.Text += i + ": \t" + val + Environment.NewLine;
  32.             continue;
  33.         }
  34.         if (Convert.ToString(i).Length > 3)
  35.             if (Convert.ToString(val).Length > 6)
  36.                 textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + Convert.ToString(val).Substring(0, 6) + Environment.NewLine;
  37.             else
  38.                 textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + val + Environment.NewLine;
  39.             else
  40.                 textBox3.Text += i + ": " + val + Environment.NewLine;
  41.     }
  42.     x[0] = Convert.ToSingle((last + first) / 2.0);
  43.     textBox4.Text += "Находим x0: " + x[0] + Environment.NewLine;
  44.     int j = 0;
  45.     while (Math.Abs(fx(x[j])) > eps) {
  46.         x[j + 1] = x[j] - (float)(fx(x[j]) / fxS(x[j]));
  47.         textBox4.Text += "Находим x" + (j + 1) + ": " + x[j + 1] + Environment.NewLine;
  48.         j++;
  49.     }
  50. }
  51. private void button2_Click(object sender, EventArgs e) {
  52.     textBox3.Clear();
  53.     textBox4.Clear();
  54.     float x0, xn, h;
  55.     int n = 0;
  56.     float[] x;
  57.     string[] t = textBox5.Text.Split(" ,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  58.     for (int i = 0; i < a.Length; i++)
  59.         a[i] = Convert.ToSingle(t[i]);
  60.     float eps = Convert.ToSingle(textBox7.Text);
  61.     x0 = Convert.ToSingle(textBox1.Text);
  62.     xn = Convert.ToSingle(textBox2.Text);
  63.     h = Convert.ToSingle(textBox6.Text);
  64.     n = Convert.ToInt32(xn / h);
  65.     x = new float[n * n * n];
  66.     double first = 0, last = 0;
  67.     int k = 0;
  68.     for (double i = x0; i < xn; i += h) {
  69.         k++;
  70.         double val = fx((float) i);
  71.         if (k == 1) first = val;
  72.         if (k == n) last = val;
  73.         if (Convert.ToString(i).Length == 1) {
  74.             textBox3.Text += i + ": \t" + val + Environment.NewLine;
  75.             continue;
  76.         }
  77.         if (Convert.ToString(i).Length > 3)
  78.             if (Convert.ToString(val).Length > 6)
  79.                 textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + Convert.ToString(val).Substring(0, 6) + Environment.NewLine;
  80.             else
  81.                 textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + val + Environment.NewLine;
  82.             else
  83.                 textBox3.Text += i + ": " + val + Environment.NewLine;
  84.     }
  85.     x[0] = (float) last;
  86.     x[1] = (float) first;
  87.     textBox4.Text += "Находим x0: " + x[0] + Environment.NewLine;
  88.     textBox4.Text += "Находим x1: " + x[1] + Environment.NewLine;
  89.     int j = 1;
  90.     while (Math.Abs(fx(x[j])) > eps) {
  91.         x[j + 1] = x[j] - (float)(fx(x[j]) * (x[j] - x[j - 1])) / (float)(fx(x[j]) - fx(x[j - 1]));
  92.         textBox4.Text += "Находим x" + (j + 1) + ": " + x[j + 1] + Environment.NewLine;
  93.         j++;
  94.     }
  95. }
  96. private void button3_Click(object sender, EventArgs e) {
  97.     textBox3.Clear();
  98.     textBox4.Clear();
  99.     float x0, xn, h;
  100.     int n = 0;
  101.     float[] x;
  102.     string[] t = textBox5.Text.Split(" ,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  103.     for (int i = 0; i < a.Length; i++)
  104.         a[i] = Convert.ToSingle(t[i]);
  105.     float eps = Convert.ToSingle(textBox7.Text);
  106.     x0 = Convert.ToSingle(textBox1.Text);
  107.     xn = Convert.ToSingle(textBox2.Text);
  108.     h = Convert.ToSingle(textBox6.Text);
  109.     n = Convert.ToInt32(xn / h);
  110.     x = new float[n * n * n];
  111.     //табулирование
  112.     double first = 0, last = 0;
  113.     int k = 0;
  114.     for (double i = x0; i < xn; i += h) {
  115.         k++;
  116.         double val = fx((float) i);
  117.         if (k == 1) first = val;
  118.         if (k == n) last = val;
  119.         if (Convert.ToString(i).Length == 1) {
  120.             textBox3.Text += i + ": \t" + val + Environment.NewLine;
  121.             continue;
  122.         }
  123.         if (Convert.ToString(i).Length > 3)
  124.             if (Convert.ToString(val).Length > 6)
  125.                 textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + Convert.ToString(val).Substring(0, 6) + Environment.NewLine;
  126.             else
  127.                 textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + val + Environment.NewLine;
  128.             else
  129.                 textBox3.Text += i + ": " + val + Environment.NewLine;
  130.     }
  131.     x[0] = (float)(first + last) / 2.0f;
  132.     x[1] = -x[0];
  133.     textBox4.Text += "Находим x0: " + x[0] + Environment.NewLine;
  134.     int j = 0;
  135.     float x1 = 0, x2 = 0, x22 = 0;
  136.     x0 = x[0];
  137.     x1 = x[1];
  138.     while (Math.Abs(x2 - x22) > eps || j < 2) {
  139.         x22 = x2;
  140.         x2 = (x1 + x0) / 2;
  141.         if (fx(x2) > 0) {
  142.             x0 = (x1 + x0) / 2;;
  143.         } else {
  144.             x1 = (x1 + x0) / 2;;
  145.         }
  146.         if (Convert.ToString(x2).Length >= 9)
  147.             textBox4.Text += "Находим x" + (j + 1) + ": " + Convert.ToString(x2).Substring(0, 9) + Environment.NewLine;
  148.         else
  149.             textBox4.Text += "Находим x" + (j + 1) + ": " + x2 + Environment.NewLine;
  150.         j++;
  151.         if (j > 150)
  152.             break;
  153.     }
  154. }
  155. private void button4_Click(object sender, EventArgs e) {
  156.     textBox3.Clear();
  157.     textBox4.Clear();
  158.     float x0, xn, h;
  159.     int n = 0;
  160.     float[] x;
  161.     string[] t = textBox5.Text.Split(" ,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  162.     for (int i = 0; i < a.Length; i++)
  163.         a[i] = Convert.ToSingle(t[i]);
  164.     float eps = Convert.ToSingle(textBox7.Text);
  165.     x0 = Convert.ToSingle(textBox1.Text);
  166.     xn = Convert.ToSingle(textBox2.Text);
  167.     h = Convert.ToSingle(textBox6.Text);
  168.     n = Convert.ToInt32(xn / h);
  169.     x = new float[n * n * n * n];
  170.     double first = 0, last = 0;
  171.     int k = 0;
  172.     for (double i = x0; i < xn; i += h) {
  173.         k++;
  174.         double val = fx((float) i);
  175.         if (k == 1) first = val;
  176.         if (k == n) last = val;
  177.         if (Convert.ToString(i).Length == 1) {
  178.             textBox3.Text += i + ": \t" + val + Environment.NewLine;
  179.             continue;
  180.         }
  181.         if (Convert.ToString(i).Length > 3)
  182.             if (Convert.ToString(val).Length > 6)
  183.                 textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + Convert.ToString(val).Substring(0, 6) + Environment.NewLine;
  184.             else
  185.                 textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + val + Environment.NewLine;
  186.             else
  187.                 textBox3.Text += i + ": " + val + Environment.NewLine;
  188.     }
  189.     x[0] = /* 0.5f;*/ Convert.ToSingle((last + first) / 2.0);
  190.     textBox4.Text += "Находим x0: " + x[0] + Environment.NewLine;
  191.     double proizv = fxS(x[0]);
  192.     float x1 = 0;
  193.     int i1 = 0;
  194.     while (Math.Abs(x1 - x[i1]) > eps) {
  195.         x1 = x[i1];
  196.         x[i1 + 1] = x[i1] - (float)(fx(x[i1]) / proizv);
  197.         textBox4.Text += "Находим x" + (i1 + 1) + ": " + x[i1 + 1] + Environment.NewLine;
  198.         i1++;
  199.         if (i1 > n * n * n)
  200.             break;
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment