с помощью метода Рунге-Кутты:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1: Form
{
double[] t, y, y1; int n=50;double dt=0.01,T0=0,Y0=0;
int i = 0; Double My = 0, MaxY = 0,Mt=0,MaxT=0,My1=0,MaxY1=0;
Bitmap myBmp,myBmp2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{ //Решение
dataGridView1.Rows.Clear();
t = new double[n+1];
y = new double[n + 1];
y1 = new double[n + 1];
T0 = Convert.ToDouble(textBox1.Text);
Y0 = Convert.ToDouble(textBox2.Text);
dt = Convert.ToDouble(textBox3.Text);
t[0]=T0;
y[0]=Y0;
y1[0]=dYdt(t[0],y[0]);
for (i = 1; i < n + 1; i++)
{
t[i] =T0+ dt * i;
y[i]=y[i-1]+Dy(t[i-1],y1[i-1]);
y1[i]=dYdt(t[i],y[i]);
}
for (i = 0; i < n+1; i++)
{
// Заполнение таблицы
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = i;
dataGridView1.Rows[i].Cells[1].Value = t[i];
dataGridView1.Rows[i].Cells[2].Value = y[i];
dataGridView1.Rows[i].Cells[3].Value = y1[i];
}
if (checkBox1.Checked)
{
postroenie();
}
}
double dYdt(double D,double S)
{
//Выражение для производной
return 9*Math.Cos(Math.PI*D) * Math.Exp(-D)-S;
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
double Dy(double D,double S)
{
//Коэффициенты метода Рунге-Кутты
double K1, K2, K3, K4;
K1=dt*dYdt(t[i-1],y[i-1]);
|
|
K2 = dt * dYdt(t[i - 1]+dt/2, y[i - 1]+K1/2);
K3 = dt * dYdt(t[i - 1] + dt / 2, y[i - 1] + K2 / 2);
K4 = dt * dYdt(t[i - 1]+dt, y[i - 1]+K3);
return 1.0/6*(K1+2*K2+2*K3+K4);
}
void postroenie()
{
// Построение графика
int[] yint, y1int, tint;
if (t== null || y==null ||y1 ==null) return;
MaxY = 0;
MaxY1 = 0;
MaxT = 0;
myBmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics gr1 = Graphics.FromImage(myBmp);
Pen P1 = new Pen(Color.Red, 2);
gr1.DrawRectangle(P1, 1, 1, pictureBox1.Width - 2, pictureBox1.Height - 2);
for (int i = 0; i < n+1; i++)
{
if (System.Math.Abs(y[i]) > MaxY)
{
MaxY = System.Math.Abs(y[i]);;
}
if (System.Math.Abs(t[i]) > MaxT)
{
MaxT = System.Math.Abs(t[i]);
}
if (System.Math.Abs(y1[i]) > MaxY1)
{
MaxY1 = System.Math.Abs(y1[i]);
}
}
My = (pictureBox1.Height) / 2.2 / MaxY;
My1 = (pictureBox1.Height) / 2.2 / MaxY1;
Mt = (pictureBox1.Width) / 2.2 / MaxT;
yint = new int[n + 1];
y1int = new int[n + 1];
tint = new int[n + 1];
for (int i = 0; i < n+1; i++)
{
yint[i] = (pictureBox1.Height) / 2 -Convert.ToInt32(y[i] * My);
y1int[i] = (pictureBox1.Height) / 2-Convert.ToInt32(y1[i] * My1);
tint[i] = pictureBox1.Width/2+Convert.ToInt32(t[i] * Mt);
}
Pen P2 = new Pen(Color.Green, 6);
for (int i = 0; i < n; i++)
{
gr1.DrawLine(P2, tint[i], yint[i],tint[i+1],yint[i+1]);
}
Pen P3 = new Pen(Color.Brown, 6);
for (int i = 0; i < n; i++)
{
gr1.DrawLine(P3, tint[i], y1int[i], tint[i + 1], y1int[i + 1]);
}
Pen P4 = new Pen(Color.Black, 3);
//Стиль линии с наконечниками
P4.SetLineCap(LineCap.Flat, LineCap.ArrowAnchor, DashCap.Flat);
//Кисть для шрифта на рисунке
Brush B2 = new SolidBrush(Color.Black);
System.Drawing.Font E = new Font("time new roman", 14);
// Вывод надписей на рисунке
gr1.DrawString("y dy/dt", E, B2, pictureBox1.Width / 2-20,0);
gr1.DrawString("t", E, B2, pictureBox1.Width-20,pictureBox1.Height / 2);
gr1.DrawLine(P4, 10, (pictureBox1.Height) / 2, pictureBox1.Width - 10, (pictureBox1.Height) / 2);
gr1.DrawLine(P4, pictureBox1.Width/2, (pictureBox1.Height) -10,pictureBox1.Width/2, 10);
pictureBox1.Image = myBmp;
legenda();
}
void legenda()
{// Вывод легенды
myBmp2 = new Bitmap(pictureBox2.Width, pictureBox2.Height);
Graphics gr1 = Graphics.FromImage(myBmp2);
Pen P1 = new Pen(Color.Green, 6);
gr1.DrawLine(P1, pictureBox2.Width / 2, 20, pictureBox2.Width -10,20);
Pen P2 = new Pen(Color.Brown, 6);
gr1.DrawLine(P2, pictureBox2.Width / 2, 50, pictureBox2.Width - 10, 50);
Brush B2 = new SolidBrush(Color.Black);
System.Drawing.Font E = new Font("time new roman", 14);
gr1.DrawString("y(t)", E, B2, 7, 7);
|
|
gr1.DrawString("dy/dt", E, B2, 7, 37);
pictureBox2.Image = myBmp2;
}
}
}
Рис. 6.1 Форма проекта.