Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float[] a = new float[4];
- double fx(float x) {
- return a[0] * Math.Pow(x, 3) + a[1] * Math.Pow(x, 2) + a[2] * x + a[3];
- }
- double fxS(float x) {
- return 3 * a[0] * Math.Pow(x, 2) + 2 * a[1] * x + a[2];
- }
- private void button1_Click(object sender, EventArgs e) {
- textBox3.Clear();
- textBox4.Clear();
- float x0, xn, h;
- int n = 0;
- float[] x;
- string[] t = textBox5.Text.Split(" ,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
- for (int i = 0; i < a.Length; i++)
- a[i] = Convert.ToSingle(t[i]);
- float eps = Convert.ToSingle(textBox7.Text);
- x0 = Convert.ToSingle(textBox1.Text);
- xn = Convert.ToSingle(textBox2.Text);
- h = Convert.ToSingle(textBox6.Text);
- n = Convert.ToInt32(xn / h);
- x = new float[n * n * n];
- double first = 0, last = 0;
- int k = 0;
- for (double i = x0; i < xn; i += h) {
- k++;
- double val = fx((float) i);
- if (k == 1) first = val;
- if (k == n) last = val;
- if (Convert.ToString(i).Length == 1) {
- textBox3.Text += i + ": \t" + val + Environment.NewLine;
- continue;
- }
- if (Convert.ToString(i).Length > 3)
- if (Convert.ToString(val).Length > 6)
- textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + Convert.ToString(val).Substring(0, 6) + Environment.NewLine;
- else
- textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + val + Environment.NewLine;
- else
- textBox3.Text += i + ": " + val + Environment.NewLine;
- }
- x[0] = Convert.ToSingle((last + first) / 2.0);
- textBox4.Text += "Находим x0: " + x[0] + Environment.NewLine;
- int j = 0;
- while (Math.Abs(fx(x[j])) > eps) {
- x[j + 1] = x[j] - (float)(fx(x[j]) / fxS(x[j]));
- textBox4.Text += "Находим x" + (j + 1) + ": " + x[j + 1] + Environment.NewLine;
- j++;
- }
- }
- private void button2_Click(object sender, EventArgs e) {
- textBox3.Clear();
- textBox4.Clear();
- float x0, xn, h;
- int n = 0;
- float[] x;
- string[] t = textBox5.Text.Split(" ,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
- for (int i = 0; i < a.Length; i++)
- a[i] = Convert.ToSingle(t[i]);
- float eps = Convert.ToSingle(textBox7.Text);
- x0 = Convert.ToSingle(textBox1.Text);
- xn = Convert.ToSingle(textBox2.Text);
- h = Convert.ToSingle(textBox6.Text);
- n = Convert.ToInt32(xn / h);
- x = new float[n * n * n];
- double first = 0, last = 0;
- int k = 0;
- for (double i = x0; i < xn; i += h) {
- k++;
- double val = fx((float) i);
- if (k == 1) first = val;
- if (k == n) last = val;
- if (Convert.ToString(i).Length == 1) {
- textBox3.Text += i + ": \t" + val + Environment.NewLine;
- continue;
- }
- if (Convert.ToString(i).Length > 3)
- if (Convert.ToString(val).Length > 6)
- textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + Convert.ToString(val).Substring(0, 6) + Environment.NewLine;
- else
- textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + val + Environment.NewLine;
- else
- textBox3.Text += i + ": " + val + Environment.NewLine;
- }
- x[0] = (float) last;
- x[1] = (float) first;
- textBox4.Text += "Находим x0: " + x[0] + Environment.NewLine;
- textBox4.Text += "Находим x1: " + x[1] + Environment.NewLine;
- int j = 1;
- while (Math.Abs(fx(x[j])) > eps) {
- x[j + 1] = x[j] - (float)(fx(x[j]) * (x[j] - x[j - 1])) / (float)(fx(x[j]) - fx(x[j - 1]));
- textBox4.Text += "Находим x" + (j + 1) + ": " + x[j + 1] + Environment.NewLine;
- j++;
- }
- }
- private void button3_Click(object sender, EventArgs e) {
- textBox3.Clear();
- textBox4.Clear();
- float x0, xn, h;
- int n = 0;
- float[] x;
- string[] t = textBox5.Text.Split(" ,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
- for (int i = 0; i < a.Length; i++)
- a[i] = Convert.ToSingle(t[i]);
- float eps = Convert.ToSingle(textBox7.Text);
- x0 = Convert.ToSingle(textBox1.Text);
- xn = Convert.ToSingle(textBox2.Text);
- h = Convert.ToSingle(textBox6.Text);
- n = Convert.ToInt32(xn / h);
- x = new float[n * n * n];
- //табулирование
- double first = 0, last = 0;
- int k = 0;
- for (double i = x0; i < xn; i += h) {
- k++;
- double val = fx((float) i);
- if (k == 1) first = val;
- if (k == n) last = val;
- if (Convert.ToString(i).Length == 1) {
- textBox3.Text += i + ": \t" + val + Environment.NewLine;
- continue;
- }
- if (Convert.ToString(i).Length > 3)
- if (Convert.ToString(val).Length > 6)
- textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + Convert.ToString(val).Substring(0, 6) + Environment.NewLine;
- else
- textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + val + Environment.NewLine;
- else
- textBox3.Text += i + ": " + val + Environment.NewLine;
- }
- x[0] = (float)(first + last) / 2.0f;
- x[1] = -x[0];
- textBox4.Text += "Находим x0: " + x[0] + Environment.NewLine;
- int j = 0;
- float x1 = 0, x2 = 0, x22 = 0;
- x0 = x[0];
- x1 = x[1];
- while (Math.Abs(x2 - x22) > eps || j < 2) {
- x22 = x2;
- x2 = (x1 + x0) / 2;
- if (fx(x2) > 0) {
- x0 = (x1 + x0) / 2;;
- } else {
- x1 = (x1 + x0) / 2;;
- }
- if (Convert.ToString(x2).Length >= 9)
- textBox4.Text += "Находим x" + (j + 1) + ": " + Convert.ToString(x2).Substring(0, 9) + Environment.NewLine;
- else
- textBox4.Text += "Находим x" + (j + 1) + ": " + x2 + Environment.NewLine;
- j++;
- if (j > 150)
- break;
- }
- }
- private void button4_Click(object sender, EventArgs e) {
- textBox3.Clear();
- textBox4.Clear();
- float x0, xn, h;
- int n = 0;
- float[] x;
- string[] t = textBox5.Text.Split(" ,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
- for (int i = 0; i < a.Length; i++)
- a[i] = Convert.ToSingle(t[i]);
- float eps = Convert.ToSingle(textBox7.Text);
- x0 = Convert.ToSingle(textBox1.Text);
- xn = Convert.ToSingle(textBox2.Text);
- h = Convert.ToSingle(textBox6.Text);
- n = Convert.ToInt32(xn / h);
- x = new float[n * n * n * n];
- double first = 0, last = 0;
- int k = 0;
- for (double i = x0; i < xn; i += h) {
- k++;
- double val = fx((float) i);
- if (k == 1) first = val;
- if (k == n) last = val;
- if (Convert.ToString(i).Length == 1) {
- textBox3.Text += i + ": \t" + val + Environment.NewLine;
- continue;
- }
- if (Convert.ToString(i).Length > 3)
- if (Convert.ToString(val).Length > 6)
- textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + Convert.ToString(val).Substring(0, 6) + Environment.NewLine;
- else
- textBox3.Text += Convert.ToString(i).Substring(0, 3) + ":\t" + val + Environment.NewLine;
- else
- textBox3.Text += i + ": " + val + Environment.NewLine;
- }
- x[0] = /* 0.5f;*/ Convert.ToSingle((last + first) / 2.0);
- textBox4.Text += "Находим x0: " + x[0] + Environment.NewLine;
- double proizv = fxS(x[0]);
- float x1 = 0;
- int i1 = 0;
- while (Math.Abs(x1 - x[i1]) > eps) {
- x1 = x[i1];
- x[i1 + 1] = x[i1] - (float)(fx(x[i1]) / proizv);
- textBox4.Text += "Находим x" + (i1 + 1) + ": " + x[i1 + 1] + Environment.NewLine;
- i1++;
- if (i1 > n * n * n)
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment