Skip to content

Instantly share code, notes, and snippets.

@TPAKC
Created April 19, 2015 08:38
Show Gist options
  • Save TPAKC/c78cd6a9673af1d3e1ad to your computer and use it in GitHub Desktop.
Save TPAKC/c78cd6a9673af1d3e1ad to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace Lab1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
chart1.Series[0].Color = Color.Red;
}
private void выходToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void Input(object sender, EventArgs e)
{
double a = Convert.ToDouble(textBox1.Text);
double b = Convert.ToDouble(textBox2.Text);
if ((sender as ToolStripMenuItem).Text.Equals("Построить график"))
{
MakeChart(a, b);
return;
}
double eps = Convert.ToDouble(textBox3.Text);
if (!Check(a, b))
{
MessageBox.Show("На задаенном интервале нет корней", "Ошибка");
return;
}
if ((sender as ToolStripMenuItem).Text.Equals("Метод А")) A(a, b, eps);
if ((sender as ToolStripMenuItem).Text.Equals("Метод Б2")) B2(a, b, eps);
}
private void MakeChart(double a, double b)
{
double dx = 0.01;
chart1.Series[0].Points.Clear();
for (double x = a; x <= b; x += dx)
chart1.Series[0].Points.AddXY(x, F(x));
}
private double F(double x)
{
return 3 * x - Math.Cos(x) - 1;
}
private void A(double a, double b, double eps)
{
double c;
int i = 0;
do
{
c = (a + b) / 2;
if (F(a) * F(c) < 0) b = c;
else a = c;
i++;
} while (Math.Abs(F(c)) > eps);
toolStripStatusLabel1.Text = "Метод : A";
toolStripStatusLabel2.Text = "Кол-во итераций : " + i;
toolStripStatusLabel3.Text = "Корень : " + c;
}
private bool Check(double a, double b)
{
return F(a) * F(b) < 0;
}
private double D1(double x, double eps)
{
return (F(x + eps) - F(x)) / eps;
}
private double D2(double x, double eps)
{
return (D1(x + eps, eps) - D1(x, eps)) / eps;
}
private void B2(double a, double b, double eps)
{
double x = (a + b) / 2;
double x1, x2, t;
x1 = x2 = 0;
int i = 0;
do
{
x2 = x1;
x1 = x;
if (F(a) * D2(a, eps) < 0) x = (b * F(x) - x * F(b)) / (F(x) - F(b));
if (F(b) * D2(b, eps) < 0) x = (a * F(x) - x * F(a)) / (F(x) - F(a));
t = (Math.Pow(x - x1, 2)) / (Math.Abs(2 * x1 - x - x2));
i++;
} while (t > eps);
toolStripStatusLabel1.Text = "Метод : Б2";
toolStripStatusLabel2.Text = "Кол-во итераций : " + i;
toolStripStatusLabel3.Text = "Корень : " + x;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment