Код:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace sinker_on_spring
{
public partial class FormMain : Form
{
System.Drawing.Graphics gr;
System.Drawing.SolidBrush myBrush;
System.Drawing.Pen myPen;
System.Drawing.Font drawFont;
int midX;
int midY;
int yy, kk;
double koef;
DataTable dt;
Thread thr;
bool Start;
double m, k, betta;
double t, h;
int n, i, j;
double[] x = new double[2];
double[] y = new double[2];
double[] k1 = new double[2];
double[] k2 = new double[2];
double[] k3 = new double[2];
double[] k4 = new double[2];
double[] fx = new double[2];
double[] tt = new double[10000];
double[] xx = new double[10000];
double[] vv = new double[10000];
public FormMain()
{
InitializeComponent();
gr = pBox.CreateGraphics();
midX = pBox.Width / 2 - 50;
midY = pBox.Height / 2 + 30;
myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Chocolate);
myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
drawFont = new System.Drawing.Font("Arial", 10);
dt = new DataTable();
dt.Columns.Add("t");
dt.Columns.Add("x(t)");
dt.Columns.Add("v(t)");
chart1.ChartAreas[0].AxisY.Interval = 0.1;
chart1.ChartAreas[0].AxisX.Minimum = 0;
kk = 0;
Start = false;
yy = midY - 100;
}
private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
{
if (thr != null)
if (thr.IsAlive)
thr.Abort();
}
private void button3_Click(object sender, EventArgs e)
{
m = Convert.ToDouble(tbM.Text);
k = Convert.ToDouble(tbK.Text);
betta = Convert.ToDouble(tbBetta.Text);
x[0] = Convert.ToDouble(tbX0.Text);
x[1] = 0;
RungeKutt();
dt.Rows.Clear();
for (int i = 0; i < j; i++)
dt.Rows.Add(String.Format("{0:0.00}",tt[i]), String.Format("{0:0.0000000}",xx[i]), String.Format("{0:0.0000000}",vv[i]));
формы DataGridView
dataGridView1.DataSource = dt;
DataGridViewColumn column = dataGridView1.Columns[0];
column.Width = 40;
column = dataGridView1.Columns[1];
column.Width = 120;
column = dataGridView1.Columns[2];
column.Width = 120;
// Рисуем график
chart1.Series[0].Points.Clear();
chart1.Series[1].Points.Clear();
for (int i = 0; i < j; i++)
{
chart1.Series[0].Points.AddXY(tt[i], xx[i]);
chart1.Series[1].Points.AddXY(tt[i], vv[i]);
}
button1.Enabled = true;
koef = (pBox.Height - 110) / (Math.Abs(xx[0])+Math.Abs(xx[1]));
kk = 0;
Start = false;
button1.Enabled = true;
}
private void Funk()
{
y[0]=fx[1];
y[1] = -betta / m * fx[1] - k / m * fx[0];
}
private void Shag()
{
for(i=0; i<n; i++) fx[i]=x[i];
Funk();
for(i=0; i<n; i++) k1[i]=y[i];
for(i=0; i<n; i++) fx[i]=x[i]+h*k1[i]/2;
Funk();
for(i=0; i<n; i++) k2[i]=y[i];
for(i=0; i<n; i++) fx[i]=x[i]+h*k2[i]/2;
Funk();
for(i=0; i<n; i++) k3[i]=y[i];
for(i=0; i<n; i++) fx[i]=x[i]+h*k3[i];
Funk();
for(i=0; i<n; i++) k4[i]=y[i];
for(i=0; i<n; i++) x[i]=x[i]+(h/6)*(k1[i]+2*k2[i]+2*k3[i]+k4[i]);
}
private void RungeKutt()
{
n=2;
t=0;
h=0.01;
j=0;
while ( ((Math.Abs(x[0])>0.001)||(Math.Abs(x[1])>0.001))&&(j<10000))
{
tt[j] = t;
xx[j] = x[0];
vv[j] = x[1];
Shag();
t = t + h;
j = j + 1;
}
}
}
}