Skip to content

Varga-Aron/finite-difference-calculator

Repository files navigation

Table of Contents
  1. About The Project
  2. Theory
  3. Usage
  4. Glossary
  5. Contact

About The Project

This project's aim is to implement a finite difference coefficients calculator in MATLAB that can be used in any number of dimensions.

(back to top)

Built With

(back to top)

Theory

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 $\alpha$ derivative approximation in any number of dimensions like so: $\partial^{\alpha}f(p) \approx \frac{\sum_{i=1}^mc_if(p + hv_i)}{h^{|\alpha|}}$

This tool is capable of calculating the finite difference coefficients ($c_i$), calculating the approximations themselves, and can also recommend a few simple set of sampling directions ($v_i$ sampling direction, $P + h \dot v_i$ sampling point, h is a sufficiently small unit of distance) and h value for a good approximation of a given derivative value.

Note: the coefficients ($c_i$) should be divided by $h^{|\alpha|}$, but the coefficient calculator skips this part of the calculations to make the human management of these coefficients easier. When the approximation is done by hand and not by this calculator, then the division by $h^{|\alpha|}$ must also be done by hand.

(back to top)

Usage

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 $\alpha$ derivative, call calculateCoefficients(V, alpha) from calculateCoefficients.m. This will return $C$, which contains the finite difference coefficients in order, so $c_1$ is the finite difference coefficient for $v_1$ and so on. Then take the V, alpha and C and call 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 $\partial^{\alpha}f(p)$.

You can also use the displayCoefficients(V, ds, C) method from displayCoefficients.m to display the $\partial^{\alpha}f(p) \approx \frac{\sum_{i=1}^mc_if(p + hv_i)}{h^{|\alpha|}}$ equation substituted with the given values.

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.

(back to top)

Glossary

  • 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.
  • 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}}$

(back to top)

Contact

Áron Varga - Eötvös Loránd University, Budapest, Hungary - xldtte@inf.elte.hu

(back to top)