The project is a mathematical program that allows to numerically compute integrals (2 methods: trapezy and Sympson's methods), find the polynom on a grid of values (Lagrange method) and display the plots associated with this polynoms or other functions.
You need to install glut and another libraries. You can use Cabal (you should install it), and then simply cabal install opengl
, cabal install glut
(for the last one needed C glut libraries, you need to install it, in Ubuntu - sudo apt-get install freeglut-dev
).
To compile the program run ghc -o main ./Main.hs
.
Main.hs is the entry point to the program. Start it to get the functionality.
List of supported masthematical functions:
- sin(x)
- cos(x)
- tg(x)
- exp(x)
- lg(x) - log with base 10
Every function f(x)
can be written as f x
too.
List of binary operators:
- x + y
- x - y
- x * y
- x / y
- x ^ y - computes the y-th power of x
x
andy
are real values (Double type)
An example of use for Windows PowerShell:
.\main.exe
Input:
1. Integrate by trapezy method (steps count)
2. Integrate by trapezy method (step)
3. Integrate by Simpson's method
4. Interpolate by Lagrange method
4
Enter the function:
x ^ 2
Enter a, b range and the step by whitespace:
1 5 4
The result of this example is shown below:
The main modules of the program are Algorithm
, ExpressionParser
, and Plot
. Each of them responces to for own stage of function analysis.
Contains algorithms to transform list of characters (string representation of the function) to the data structure that is useful to compute the expression. The data structure namely Function
is a recursive tree, each node of it is an operator, a value or a variable. Main functions of this module are
create_func :: String -> Function
and
evaluate_func :: Function -> Double -> Double
Another approach to solve the problem of parsing the string representation of functions is to use built-in Haskell interpretator (Language.Haskell.Interpreter
module), but it seems too hard to understand how MonadInterpreter
works, but attemptions was made :) (First parser)
This module separates in two submodules: Integral
and Interpolation
.
Integral submodule allows to compute an integral of functions in range [a, b] with a fixed step by 2 methods: trapezy and Simpson's. The signatures of this functions are
trap_integral_step :: Function -> Double -> Double -> Step -> Area
and
simpson_integral :: Function -> Double -> Double -> Step -> Area
Interpolation submodule contains
interpolate_lagrange :: [Point] -> Function
method that implements Lagrange polynom interpolation by set of points (x_i, y_i = f(x_i))
.
The module intends for plotting functions by own algorithm, based on OpenGL
and GLUT
library.
The drawing function is represented by DescriptorFunc
structure:
data DescriptorFunc = DescriptorFunc {
f :: Function,
a :: Double,
b :: Double,
step :: Double,
color :: Color4 GLfloat
}
To draw some function you should put certain DescriptorFunc
instance to the queue via
add_func :: QueueOfFunc -> DescriptorFunc -> QueueOfFunc
and then send the queue to
draw_window :: QueueOfFunc -> IO ()