The following finite difference method is particularly suitable for linear equations, although, it can be used
for nonlinear equations with a considerable increase in computational effort.
Consider the general second-order linear equation
P(x)y'' + Q(x)y' + R(x)y + S(x) = 0
with boundary conditions
y(a) = Þ, or y'(a) = Þ
y(b) = ß, or y'(b) = ß
In the finite differential method, the range of intgegration (a, b) is divided into n
equal sub-intervals of length h each. The values of the numerical solution at the mesh points
are denoted by yi, i = 0,1,2..., n.
This implementation shows a finite difference method for solving the boundary value of a second-order
linear differential equation. This method is general enough to handle boundary value problems with
specified y values or derivatives at the boundary.
Use the following equation,
xy'' - 2y' + 2 = 0, y(0) = y(1) = 0
P(x) = x, Q(x) = -2, R(x) = 0, S(x) = 2
and this equation has an analytical solution:
This way we can examine the accuracy of the numerical results. Running this equation produces the results shown above. One can
tell that the numerical results are fairly good compared to the analytical solution.
The accuracy is always improved by increasing the number of mesh points.
Testing the FiniteDifferenceLinear Method
In order to test the FiniteDifferenceLinear method as defined above, a new TestFiniteDifferenceLinear()
static method has been added and executed. Supporting code and methods are not shown.
static void TestFiniteDifferenceLinear();
BoundaryValue bv = new BoundaryValue();
strScore = txtInput.Text;
intScore = Int32.Parse(strScore);
bv.xa = 0.0;
bv.xb = 1.0;
bv.ya = 0.0;
bv.yb = 0.0;
bv.n = intScore;
VectorR y = bv.FiniteDifferenceLinear2(f2, out x);
for (int i = 0; i < x.Length; i++)
double exact = x[i] - x[i] * x[i] * x[i];
t1 = double.Parse(t1.ToString("#0.######"));
ListBox1.Items.Add(" " + t1);
double t2 = y[i];
t2 = double.Parse(t2.ToString("#0.######"));
ListBox2.Items.Add(" " + t2);
double t3 = exact;
ListBox3.Items.Add(" " + t3);