Implementation of numerical approximations of partial derivatives of PDEs
This is a very straight forward implementation of the
Forward Time- Central Space -
method
for partial derivatives of PDEs in Java.
So far, the derivates in one, two and three dimensions are implemented.
For each case a data set has to be given as a parameter
( double
[ ][ ][ ] data for the 3D case)
as well as the space grid points (x,y,z for the 3D case) and the
space step size h.
/**
* An implementation of numerical approximations
* of partial derivatives of Partiel Differential Equations
* - 1 space dimension
* - 2 space dimensions
* - 3 space dimensions
*
* @see java.Object
* @author Heiko Enderling
*/
public class PDE extends Object
{
//******************************************//
//************ 1 dimension *********//
//******************************************//
/** returns the partial derivative of _n(x,t)
with respect to x
approximated at the space grid point _x
with a space step size of _h **/
public static double p_x (double[] _n,
int _x,
double _h )
{
double _n_x = 0.0;
try { _n_x = (_n[_x+1]-_n[_x-1])*0.5/_h; }
catch (ArrayIndexOutOfBoundsException _e) {
_e.printStrackTrace(); }
return _n_x;
}
/** returns the second partial derivative of _n(x,t)
with respect to x
approximated at the space grid point _x
with a space step size of _h **/
public static double p_xx (double[] _n,
int _x,
double _h )
{
double _n_xx = 0.0;
try { _n_xx = (_n[_x+1]-(2*_n[_x])+_n[_x-1])/(_h*_h); }
catch (ArrayIndexOutOfBoundsException _e) {
_e.printStrackTrace();}
return _n_xx;
}
/** returns the result of the laplace operator of _n(x,t)
approximated at the space grid point _x
with a space step size of _h **/
public static double p_laplace(double[] _n,
int _x,
double _h )
{
return p_xx(_n,_x,_h);
}
//******************************************//
//************ 2 dimensions *********//
//******************************************//
/** returns the partial derivative of _n(x,y,t)
with respect to x
approximated at the space grid point [_x][_y]
with a space step size of _h **/
public static double pp_x (double[][] _n,
int _x, int _y,
double _h )
{
double _nn_x = 0.0;
try { _nn_x = (_n[_x+1][_y]-_n[_x-1][_y])*0.5/_h; }
catch (ArrayIndexOutOfBoundsException _e) {
_e.printStrackTrace(); }
return _nn_x;
}
/** returns the partial derivative of _n(x,y,t)
with respect to y
approximated at the space grid point [_x][_y]
with a space step size of _h **/
public static double pp_y (double[][] _n,
int _x, int _y,
double _h )
{
double _nn_y = 0.0;
try { _nn_y = (_n[_x][_y+1]-_n[_x][_y-1])*0.5/_h; }
catch (ArrayIndexOutOfBoundsException _e) {
_e.printStrackTrace(); }
return _nn_y;
}
/** returns the second partial derivative of _n(x,y,t)
with respect to x
approximated at the space grid point [_x][_y]
with a space step size of _h **/
public static double pp_xx (double[][] _n,
int _x, int _y,
double _h )
{
double _nn_xx = 0.0;
try { _nn_xx = (_n[_x+1][_y]-(2*_n[_x][_y])+_n[_x-1][_y])/(_h*_h); }
catch (ArrayIndexOutOfBoundsException _e) {
_e.printStrackTrace();}
return _nn_xx;
}
/** returns the second partial derivative of _n(x,y,t)
with respect to y
approximated at the space grid point [_x][_y]
with a space step size of _h **/
public static double pp_yy (double[][] _n,
int _x, int _y,
double _h )
{
double _nn_yy = 0.0;
try { _nn_yy = (_n[_x][_y+1]-(2*_n[_x][_y])+_n[_x][_y-1])/(_h*_h); }
catch (ArrayIndexOutOfBoundsException _e) {
_e.printStrackTrace();}
return _nn_yy;
}
/** returns the result of the laplace operator of _n(x,y,t)
approximated at the space grid point _x, _y
with a space step size of _h **/
public static double pp_laplace(double[][] _n,
int _x, int _y,
double _h )
{
return pp_xx(_n,_x,_y,_h)+pp_yy(_n,_x,_y,_h);
}
//******************************************//
//************ 3 dimensions *********//
//******************************************//
/** returns the partial derivative of _n(x,y,z,t)
with respect to x
approximated at the space grid point [_x][_y][_z]
with a space step size of _h **/
public static double ppp_x (double[][][] _n,
int _x, int _y, int _z,
double _h )
{
double _nnn_x = 0.0;
try { _nnn_x = (_n[_x+1][_y][_z]-_n[_x-1][_y][_z])*0.5/_h; }
catch (ArrayIndexOutOfBoundsException _e) {
_e.printStrackTrace(); }
return _nnn_x;
}
/** returns the partial derivative of _n(x,y,z,t)
with respect to y
approximated at the space grid point [_x][_y][_z]
with a space step size of _h **/
public static double ppp_y (double[][][] _n,
int _x, int _y, int _z,
double _h )
{
double _nnn_y = 0.0;
try { _nnn_y = (_n[_x][_y+1][_z]-_n[_x][_y-1][_z])*0.5/_h; }
catch (ArrayIndexOutOfBoundsException _e) {
_e.printStrackTrace(); }
return _nnn_y;
}
/** returns the partial derivative of _n(x,y,z,t)
with respect to z
approximated at the space grid point [_x][_y][_z]
with a space step size of _h **/
public static double ppp_z (double[][][] _n,
int _x, int _y, int _z,
double _h )
{
double _nnn_z = 0.0;
try { _nnn_z = (_n[_x][_y][_z+1]-_n[_x][_y][_z-1])*0.5/_h; }
catch (ArrayIndexOutOfBoundsException _e) {
_e.printStrackTrace(); }
return _nnn_z;
}
/** returns the second partial derivative of _n(x,y,z,t)
with respect to x
approximated at the space grid point [_x][_y][_z]
with a space step size of _h **/
public static double ppp_xx (double[][][] _n,
int _x, int _y, int _z,
double _h )
{
double _nnn_xx = 0.0;
try { _nnn_xx = (_n[_x+1][_y][_z]-(2*_n[_x][_y][_z])+_n[_x-1][_y][_z])/(_h*_h); }
catch (ArrayIndexOutOfBoundsException _e) {
_e.printStrackTrace();}
return _nnn_xx;
}
/** returns the second partial derivative of _n(x,y,z,t)
with respect to y
approximated at the space grid point [_x][_y][_z]
with a space step size of _h **/
public static double ppp_yy (double[][][] _n,
int _x, int _y, int _z,
double _h )
{
double _nnn_yy = 0.0;
try { _nnn_yy = (_n[_x][_y+1][_z]-(2*_n[_x][_y][_z])+_n[_x][_y-1][_z])/(_h*_h); }
catch (ArrayIndexOutOfBoundsException _e) {
_e.printStrackTrace();}
return _nnn_yy;
}
/** returns the second partial derivative of _n(x,y,z,t)
with respect to z
approximated at the space grid point [_x][_y][_z]
with a space step size of _h **/
public static double ppp_zz (double[][][] _n,
int _x, int _y, int _z,
double _h )
{
double _nnn_zz = 0.0;
try { _nnn_zz = (_n[_x][_y][_z+1]-(2*_n[_x][_y][_z])+_n[_x][_y][_z-1])/(_h*_h); }
catch (ArrayIndexOutOfBoundsException _e) {
_e.printStrackTrace();}
return _nnn_zz;
}
/** returns the result of the laplace operator of _n(x,y,z,t)
approximated at the space grid point _x, _y, _z
with a space step size of _h **/
public static double ppp_laplace(double[][][] _n,
int _x, int _y, int _z,
double _h )
{
return ppp_xx(_n,_x,_y,_z,_h)+ppp_yy(_n,_x,_y,_z,_h)+ppp_zz(_n,_x,_y,_z,_h);
}
}