2023
Numerical Calculus
This is a small C practice project that demonstrates how numerical derivatives and integrals can be calculated from a sampled dataset. The project is intended as an educational example and a testing ground for the more advanced PID controller project, where derivative and integral calculations are important building blocks. It focuses on presenting the core ideas of numerical calculus in a very easy-to-read and understandable manner, making it especially useful for students who are learning numerical methods and how those methods can be implemented in software.
The code works by first defining a mathematical function, f(x), which is used to generate a dataset. In the example, the function is y = x³. A fixed number of samples and a resolution factor are configured in the program. The resolution factor determines the time interval between samples, meaning a higher resolution creates smaller time steps and usually produces more accurate numerical results. The generated function values are stored in a ring buffer, which is also used in the related PID controller project.
The derivative is calculated using a simple finite difference method. For each sample point, the program compares the current value with the previous value and divides the difference by the time interval. This gives an approximation of the rate of change at that point. The integral is calculated using trapezoidal-style methods, where the area under the curve is estimated by summing small sections of the sampled function. The code includes both a typical trapezoidal implementation and a modified integral function, making it easy to compare and understand different numerical approaches.
The implementation is intentionally simple. The main logic is contained in readable C functions such as calculate_derivative, trapezoidal, and calculate_integral. The project is built using CMake and links against my ring buffer module, keeping the structure clear while still showing how code can be split into reusable components.
Although this is not intended to be a full numerical analysis library, it is useful as a learning resource. Students, hobbyists, and beginner C programmers can use it to understand how mathematical formulas for derivatives and integrals translate into working code. It can also serve as a starting point for more advanced simulations, control systems, signal processing experiments, or embedded software projects that require simple numerical calculations.
