Boundary value problems are much more difficult to solve than initial value problems, so we will only briefly cover them in this appendix. Boundary value problems (often “two point boundary value problems”) are characterized by the fact that we do not know the values of all of the dependent variables (y, the functions and derivatives) at one point. Rather, we know some of the values at one point and some of the values at the other point. This is the situation, for example, when solving for an electric field in space, given fixed electric potentials at some electrodes and a charge distribution.
There are two basic methods for solving these: “shooting” methods and “relaxation” methods. In the shooting methods, one guesses values for the unknown dependent variables at the starting point, then integrates them as if they were an IVP, and then corrects the initial guesses until the boundary conditions at the other end are met. Some variants involve “shooting” from both sides until they match at some middle point. The tricky part of the algorithm is in calculating how to make the adjustments based on the errors achieved at the point that was aimed at.
Relaxation methods make initial guesses of the function at all points between the boundaries and then adjust those guesses until they are self-consistent.
Relaxation Method
The relaxation method simplifies the differential equations into many “finite difference equations”. If there are \(N\) points between the boundaries, spaced by a distance \(d\), then at some point \(k\) we approximately have:
So a differential equation
can be approximated, at each point, \(k\), by:
The \(y_k\) and \(f\) could also be vectors in the case of a higher order ODE.
We can also represent the second derivative with the finite difference as:
We will do a simple example here of solving Poisson’s equation in one dimension.
Example A-2
Poisson’s equation describes the electric potential, given a charge distribution (Laplace’s equation is a special case when the charge distribution is 0). Poisson’s equation is:
where \(V\) is the electric potential, \(\rho\) is the charge density, and \(\epsilon\) is the permittivity of the material.
Let’s say that we want to solve the equation in one dimension, in vacuum, between \(x=0\) and \(x=L=1\) m, where we have a charge density given by:
and we have the boundary conditions \(V(x=0\,\mathrm{m})=100\) V and \(V(x=1\,\mathrm{m})=200\) V.
We start by choosing a grid size, say \(h=0.01\) m. We then divide our interval up into \(L/h+1=101\) grid points, and attempt to solve for \(V_k\) \((k=0\dots 100)\), at each point on the grid. We will use the fact that (from our above approximation of the second derivative):
We will fix the values of \(V\) at \(k=0\) and \(k=100\) to be equal to 100 V and 200 V, respectively, and (arbitrarily) set all the other \(V_k = 0\).
We will iteratively calculate the \(V_k\) using the above equation until they stop changing. This is implemented in the following code, which is in C++ and needs to be compiled against CERN’s ROOT libraries.
#include <iostream>
#include <TGraph.h>
#include <TCanvas.h>
#include <math.h>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
//simple Poisson equation solver in 1D
double Lx=1.0;
double epsilon=8.85e-12;//permittivity of free space
double V0=100;//voltage at x=0
double VL=200;//voltage at x=L
double h=0.01;//grid size
int N = int(Lx/h)+1;//number of grid points, k=0..N-1 spanning [0,L]
vector<double>V(N,0.);//potential, 0 everywhere
//Boundary conditions:
V[0]=V0;
V[N-1]=VL;
double diff=10;
double maxdiff=1e-5;//stop when V changes by less than this
double newV=0;
double rhoeps=0;//rho/epsilon as a function of x
double lastdiff=0;
int MaxCount=50000;//to avoid infinite loop!
int count=0;
while(diff>maxdiff && count<MaxCount){
diff=0;
for(int i=1;i<N-1;i++){//don't include boundaries in the loop
rhoeps=(0.5e-8+0.1e-8*(h*i))/epsilon;// x = i*h, so this gives rho(x)/epsilon
newV=0.5*(h*h*rhoeps+V[i+1]+V[i-1]);
lastdiff=fabs(newV-V[i]);
if(lastdiff >diff) diff=lastdiff;
V[i]=newV;
}
count++;
}
if(count>=MaxCount){
cout<<"Warning, did not converge, had to bail after "<<count<<" iterations"<<endl;
}
cout<<"achieved precision of "<<diff<<endl;
//Make a plot of the potential:
TGraph g;
g.SetMarkerStyle(8);
for(int i=0;i<V.size();i++){
g.SetPoint(i,i*h,V[i]);
}
TCanvas c1;
g.Draw("ap");
c1.Print("potential.gif");
return 0;
}
In this simple program, we first start by creating a vector, \(V\), to hold our answer at each point in \(x\), separated by \(h\). We fix the boundary value at the edges of \(V\), and set all other values of \(V=0\). We then loop through the values of \(V\) and update them according to our equation. We keep track of the difference between the previous value and the updated value. Once that difference is small enough, we claim to have converged (as the difference, we take that of the \(V\) which changed the most, tracked by lastdiff and diff).