0% found this document useful (0 votes)
148 views

Project in Math24-A1

This document describes a project to solve differential equations numerically using the Runge-Kutta method. The objectives are to create a program that can set points for a given differential equation, solve it using the Runge-Kutta method, and graph the solution. The document provides the source code for a C# program that implements the Runge-Kutta method to solve the test differential equation y' = 4xy with initial condition y(0) = 0.

Uploaded by

April Saccuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views

Project in Math24-A1

This document describes a project to solve differential equations numerically using the Runge-Kutta method. The objectives are to create a program that can set points for a given differential equation, solve it using the Runge-Kutta method, and graph the solution. The document provides the source code for a C# program that implements the Runge-Kutta method to solve the test differential equation y' = 4xy with initial condition y(0) = 0.

Uploaded by

April Saccuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Mapua Institute of Technology

Department of Mathematics
3rd Quarter S.Y. 2015-2016

PROJECT IN DIFFERENTIAL EQUATIONS


MATH24/A1

Gatchalian, Claire Anne F.


Rojales, Franchesca Nicole
Villegas, James Carl

Dr. Richard Earnhart

March 15, 2016 Tuesday

OBJECTIVE

To create a program that can set points with the


given differential equation
To solve a differential equation using Runge-kutta
method
To graph of solution for the differential equation
against
Y and X.
PROJECT DESCRIPTION
The Runge-Kutta method is a fourth-order method and
has truncation error of the order h5. It is implemented
by Eqs.:

The above integration methods are one-step in that the


integration is based on values of (t,y) in a single
interval. Multistep methods provide even greater
accuracy but require values for yk from more than one
interval. Therefore, one of the challenges in starting
them is to provide the necessary initial values, which is
typically done with a one-step method.
Methods also exist for integrating nonlinear differential
equations of order higher than one. For example, for
the second-order differential equation:
an algorithm may be based on Eqs.:

where the prime denotes differentiation with respect to


time and the value for ykcan be substituted from Eq.
(30). A more accurate method is provided by a
generalization of the Runge-Kutta method.
Solutions of equations for nonlinear systems based on
the above integration algorithms are ill-posed in that an
incorrect steady-state value may be reached.
Alternative schemes based on state variables avoid this
problem and are faster.
PROBLEM
The differential equation y=4xy has the following
parameters:
h=0.01
X=0.1, 0.2, 0.3
Y(0)=0
Solution:
X
0
0.1
0.2
0.3

Y
0

SOURCE CODE
using System;
using System.Collections.Generic;
using System.ComponentModel;

using
using
using
using
using
using
using
using

System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
info.lundin.math;
ZedGraph;

namespace DECalc
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender,
EventArgs e)
{
try
{
Variables.function = textBox1.Text.ToString();
Variables.r = 0; Variables.c = 0;
Variables.Results = "";
dataGridView1.DataSource = null;
dataGridView1.Rows.Clear();
dataGridView1.Columns.Clear();
double stepsize =
double.Parse(textBox5.Text.ToString());
double xInit =
double.Parse(textBox2.Text.ToString());

double yInit =
double.Parse(textBox3.Text.ToString());
double xEnd =
double.Parse(textBox4.Text.ToString());
Equation eq = new Equation(xInit, yInit,
stepsize, xEnd);
Variables.iterations = Convert.ToInt32(((xEnd
- xInit) / stepsize) + 1);
dataGridView1.Columns.Add("Column1", "x");
dataGridView1.Columns.Add("Column2", "y");
Variables.array = new
double[Variables.iterations, 2];
eq.Run();
for (int i = 0; i < Variables.iterations; i++)
{
dataGridView1.Rows.Add(new object[]
{ Variables.array[i, 0].ToString("0.000000"),
Variables.array[i, 1].ToString("0.0000000000") });
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString(),"Error",Messa
geBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void button3_Click(object sender,
EventArgs e)
{
try
{
CreateGraph(zedGraphControl1);

// Leave a small margin around the outside of


the control
zedGraphControl1.Size = new
Size(ClientRectangle.Width - 180,
ClientRectangle.Height - 350);
zedGraphControl1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(),
"Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
// Build the Chart
private void CreateGraph(ZedGraphControl zgc)
{
try
{
zgc.GraphPane.CurveList.Clear();
zgc.GraphPane.GraphObjList.Clear();
// get a reference to the GraphPane
GraphPane myPane = zgc.GraphPane;
// Set the Titles
myPane.Title.Text = "DECalc Graph";
myPane.XAxis.Title.Text = "X Axis";
myPane.YAxis.Title.Text = "Y Axis";
PointPairList list1 = new PointPairList();
for (int i = 0; i < Variables.iterations; i++)
{
list1.Add(Variables.array[i, 0],
Variables.array[i, 1]);
}

LineItem myCurve =
myPane.AddCurve("Data", list1, Color.Red,
SymbolType.Default);
// Tell ZedGraph to refigure the
// axes since the data have changed
zgc.AxisChange();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(),
"Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void button2_Click(object sender,
EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
dataGridView1.DataSource = null;
dataGridView1.Rows.Clear();
dataGridView1.Columns.Clear();
zedGraphControl1.GraphPane.CurveList.Clear();
zedGraphControl1.GraphPane.GraphObjList.Clear();
PointPairList list1 = new PointPairList();
zedGraphControl1.Refresh();

}
public class Variables
{
public static string Results = "";
public static string function;
public static ExpressionParser myParser;
public static int iterations;
public static double[,] array;// = new
double[iterations, iterations];
public static int r = 0, c = 0;
}
public static class RungeKutta
{
public delegate double SmallRkDelegate(double x,
double y);
static double sixth = 1.0 / 6.0;
public static double rk4(double x, double y, double
dx, SmallRkDelegate f)
{
double halfdx = 0.5 * dx;
double
double
double
double

k1
k2
k3
k4

=
=
=
=

dx
dx
dx
dx

*
*
*
*

f(x, y);
f(x + halfdx, y + k1 * halfdx);
f(x + halfdx, y + k2 * halfdx);
f(x + dx, y + (k3 * dx ));

return (y + sixth * (k1 + 2 * k2 + 2 * k3 + k4));


}

}
public class Equation
{
double x, y, dx, target;

public string Results = "";


public Equation(double x, double y, double dx,
double target)
{
this.x = x;
this.y = y;
this.dx = dx;
this.target = target;
}
public void Run()
{
try
{
while (x <= target)
{
Variables.Results += "x: " + x + " y: " + y
+ "\n";

Variables.array[Variables.r, Variables.c] =

x;

Variables.c++;
Variables.array[Variables.r, Variables.c] =

y;

Variables.r++;
Variables.c = 0;
y = RungeKutta.rk4(x, y, dx, dy_dt);
x += dx;
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(),
"Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}

private double dy_dt(double t, double y)


{
Variables.myParser = new ExpressionParser();
Variables.myParser.Values.Add("x", t);
Variables.myParser.Values.Add("y", y);
double result =
Variables.myParser.Parse(Variables.function);
return (result);
}
}
}

You might also like