Table of Contents
This project's aim is to implement a finite difference coefficients calculator in MATLAB that can be used in any number of dimensions.
- Matlab
- Matlab Symbolic Math Toolbox
- Statistics and Machine Learning Toolbox
- Curve Fitting Toolbox
- Extract linearly independent subset of matrix columns
For a more in depth view of the theoretical background for this calculator, please read Chapter 2 of the TDK thesis in this repository.
In short, the finite difference coefficients enable us to approximate function derivative values when the form of the function is unknown, meaning that we cannot derive the function as usual.
For the long explanations, when the form of the function is known, calculating the derivative value in a given P position is trivial, since we can just determine the derivative function and evaluate that at P.
However, when the form of the function is not known, the previous way of determining the derivative value at P becomes impossible.
A simple solution to this problem is the central difference approximation, where we sample the original function from h distance away on both sides of the P location, and taking the average of these two values. The finite difference coefficients work on the same logic, but they can include as many sampling positions as we want, and can be used with functions that take in more than one variable as input (so in higher dimensions than 2D).
Further expanding the theory, we can write up this equation for calculating an
This tool is capable of calculating the finite difference coefficients (
Note: the coefficients (
First of, make sure that the tables and the tests folders are included in the path in MATLAB.
Then check out the tests\examples.m file for simple examples on how to use this library, such as this one:
syms x;
P = 3;
h = 0.1;
V = [
-1; 0; 1;
];
alpha = 1;
f = x^5;
f = matlabFunction(f, "vars", {x});
C = calculateCoefficients(V, alpha);
displayCoefficients(V, alpha, C);
In short, to calculate the finite difference coefficients for a given V sampling pattern
and calculateCoefficients(V, alpha) from calculateCoefficients.m.
This will return calculateApproximation(V, alpha, C, h, f, P) with them
from calculateApproximation.m. You will also need to specify a small enough
unit of distance (h), a MATLAB function (f), and the location, where to approximate the given
f function's alpha derivative value (P).
The result will be an approximation for the value of
You can also use the displayCoefficients(V, ds, C) method from displayCoefficients.m to display the
For a given alpha derivative, you can ask the library to provide recommendations for a
sampling pattern (V) and sufficiently small unit of distance (h). You can also see examples for this in the
tests\examples.m file.
Further examples can be seen in the tests\tests.m file as well, and also almost all the methods declared in
the various files in the +helpers folder is documented with code comments, so the usage of this code base is quiet easy
if the reader is willing to read for a few more minutes.
-
n
- number, indicates the number of variables we are currently working with, same as in n-variable.
-
m
- number, indicates how many sampling positions will be used in the approximation.
-
f
- n-variable function returning one number, the function for which to calculate the approximation.
-
alpha (
$\alpha$ )- n-variable multi-index, represents the derivative for which we want to search for an approximation of.
-
P
- vector of n numbers, indicates the position where the approximation of
$\partial^{\alpha}f$ needs to happen.
- vector of n numbers, indicates the position where the approximation of
-
h
- positive number, a sufficiently small unit of distance.
-
V
- mxn matrix of numbers, sampling pattern or can also be called a vector of sampling directions. Each sampling position can be calculated by taking (P + h * v), and v can be any sampling direction from matrix V.
-
Multi-indexes
$\alpha = (\alpha_1, \dots, \alpha_n) \in N^n$ $|\alpha| = \alpha_1 + \alpha_2 + \dots + \alpha_n$ -
$\alpha! = \alpha_1! \cdot \alpha_2! \cdot \ldots \cdot \alpha_n!$ (where 0! = 1) $P^{\alpha} = p_1^{\alpha_1} \cdot p_2^{\alpha_2} \cdot \ldots \cdot p_n^{\alpha_n}$ $\partial^{\alpha}f = \partial_1^{\alpha_1} \partial_2^{\alpha_2} \dots \partial_n^{\alpha_n}f = \frac{\partial^{|\alpha|}f}{\partial p_1^{\alpha_1} \partial p_2^{\alpha_2} \dots \partial p_n^{\alpha_n}}$
Áron Varga - Eötvös Loránd University, Budapest, Hungary - xldtte@inf.elte.hu


