Commit dcf253fe authored by amitjans's avatar amitjans
Browse files

Rename files

parent 73b1dc25
//Simpson's integration method
#include "simpson.h"
double simpsonIntegral(double *f; double a; double b; const int N) {
assert(b >= a);
assert(N > 0);
const double binSize = (b - a)/N;
const double C = binSize/6;
for (int i = 0; i < N; i++) {
integral += C * (f(a + i * binSize) + 4 * f(a + i * binSize + binSize/2) + f(a + (i + 1) * binSize));
}
return integral;
}
/*
* Simpson integration:
*
* Preconditions:
* - b >= a
* - N > 0
*
* Postconditions:
* ??? */
double simpsonIntegral(double *f, double a, double b, const int N);
# Define our tools and parameters, preferably using the implicit variable names.
CXX = g++
CXXFLAGS = -std=c++11
CXXFLAGS += -Wall -Wextra -Wpedantic
CXXFLAGS += -O3 -march=native
LDFLAGS = -Llib # Library flags
LDLIBS = -lintegrate
# Export all variables into the environment. This will come handy further below.
export
# The first target is called 'all' by convention and used to delegate.
.PHONY: all
all: data/convergence.png
# Compile our code and generate an executable binary together with the library.
print_integrals: main.cpp lib/libintegrate.a
$(CXX) $(CXXFLAGS) -o $@ $< $(LDFLAGS) $(LDLIBS)
# Libraries often know how to build themselves (provide their own build system).
# Exported variables will also be visible in a sub-make.
lib/libintegrate.a:
$(MAKE) -C lib # first move to folder lib, and then run make
# Rerun the file if either our program or the input parameter changes.
data/results.txt: print_integrals maxbins.conf
@mkdir -p data # create the directory if it doesn't exist
./print_integrals < maxbins.conf
# Regenerate the plot if the data or plotting configuration changes.
data/convergence.png: data/results.txt convergence.gnuplot
gnuplot convergence.gnuplot
# Always offer a way to clean up!
.PHONY: clean
clean:
rm -f print_integrals
rm -f *.o *.a
rm -rf data
$(MAKE) -C integrator clean
set title 'Simpson rule: convergence for I=int sin(x)dx over [0, PI]'
set xlabel 'Resolution N'
set ylabel "Integral\nvalue I" offset 5,0.5 rotate by 0
set lmargin 14
set yrange [1.999:]
#~ set log x
set grid
set key box
set terminal png
set output 'data/convergence.png'
plot 'data/results.txt' title 'libintegrate.a results' with linespoints,\
2 title 'analytic solution'
2 2.00455975498442
3 2.00086318967354
4 2.00026916994839
5 2.000109517315
6 2.00005262434119
7 2.00002834355147
8 2.00001659104794
9 2.00001034770577
10 2.0000067844418
# Create a library from the object code
libintegrate.a : simpson.o
ar rvs $@ $^
# Object code depending on header and implementation
simpson.o: simpson.cpp simpson.h
$(CXX) $(CXXFLAGS) -c -o $@ $<
# Always offer a way to clean up!
.PHONY: clean
clean:
rm -f *.o *.a
//Simpson's integration method
#include "simpson.h"
#include <cassert>
#include <cmath>
double simpsonIntegral(double (*f)(double),
const double a,
const double b,
const unsigned N) {
assert(b >= a);
assert(N > 0);
assert(f != NULL); // we make sure the pointer is not NULL
const double binSize = (b - a)/N;
const double C = binSize/6;
double integral = 0;
for (unsigned i = 0; i < N; i++) {
integral += (f(a + i * binSize) + 4 * f(a + i * binSize + binSize/2) + f(a + (i + 1) * binSize));
}
return integral * C;
}
/*
* Simpson library header
*/
#ifndef SIMPSON_HPP // IMPORTANT! To avoid duplications
#define SIMPSON_HPP
double simpsonIntegral(double (*f)(double),
const double a,
const double b,
const unsigned N);
/* PRECONDITIONS:
*
* the domain of the function f(x) covers the interval [min(a,b), max(a,b)]
*
* boundary values 'a' and 'b' are convertible to double
*
* 'N' convertible to unsigned
*
* 'N' > 0
*
* POSTCONDITIONS: the return value will approcimate the integarl of the
* function f(x) over the interval [min(a,b), max(a,b)] by
* the use of the Simpson rule with 'N' equally sized intervals
*
* COMPLEXITY: number of function calls = 2*'N' + 1
*
* DOCUMENTATION: the inerval given by the boundaries 'a' and 'b' will be
* divided to 'N' equally sized bins, the function 'f' will be
* approximated by a parabola using the function values at the
* bin-boundaries and in the bin-midpoint the integral will be
* approximated by the sum of the integrals over each bin of
* the corresponding interpolating parabol(x) covers the
* interval [min(a,b), max(a,b)]
*
* boundary values 'a' and 'b' are convertible to double
*
* 'N' convertible to unsigned
*
* 'N' > 0
*
* POSTCONDITIONS: the return value will approcimate the integarl of the
* function f(x) over the interval [min(a,b), max(a,b)] by
* the use of the Simpson rule with 'N' equally sized intervals
*
* COMPLEXITY: number of function calls = 2*'N' + 1
*
* DOCUMENTATION: the inerval given by the boundaries 'a' and 'b' will be
* divided to 'N' equally sized bins, the function 'f' will be
* approximated by a parabola using the function values at the
* bin-boundaries and in the bin-midpoint the integral will be
* approximated by the sum of the integrals over each bin of
* the corresponding interpolating parabola
* */
#endif
#include "lib/simpson.h"
#include <cmath>
#include <iostream>
#include <fstream>
double my_sin(double value) {
return std::sin(value);
}
int main() {
std::cout << "Integrator maximum resolution?" << std::endl;
size_t N;
std::cin >> N;
std::cout << N << std::endl;
std::ofstream of("data/results.txt");
of.precision(15);
for(size_t i = 2; i <= N; ++i) {
of << i << "\t" << simpsonIntegral(my_sin, 0, M_PI, i) << std::endl;
}
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment