Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DefiniteIntegral.h"
- DefiniteIntegral::DefiniteIntegral(pointFunc f,double leftBorder, double rightBorder) : f(f), leftBorder(leftBorder), rightBorder(rightBorder) {}
- DefiniteIntegral::DefiniteIntegral(pointFunc f, double leftBorder, double rightBorder, double eps) : f(f), leftBorder(leftBorder), rightBorder(rightBorder), eps(eps) {}
- // Class public interface methods
- double DefiniteIntegral::calculate(const char* method)
- {
- if ( strcmp(toLowerCase(method), "left recktangle") == 0 || strcmp(toLowerCase(method), "leftrecktangle") == 0 )
- return this->leftRecktangleMethod(this->calculateAmountOfIterations(&DefiniteIntegral::leftRecktangleMethod));
- else if (strcmp(toLowerCase(method), "right recktangle") == 0 || strcmp(toLowerCase(method), "rightrecktangle") == 0)
- return this->rightRecktangleMethod(this->calculateAmountOfIterations(&DefiniteIntegral::rightRecktangleMethod));
- else if (strcmp(toLowerCase(method), "center recktangle") == 0 || strcmp(toLowerCase(method), "centerrecktangle") == 0)
- return this->centerRecktangleMethod(this->calculateAmountOfIterations(&DefiniteIntegral::centerRecktangleMethod));
- else if (strcmp(toLowerCase(method), "trapeze") == 0)
- return this->trapezeMethod(this->calculateAmountOfIterations(&DefiniteIntegral::trapezeMethod));
- else if (strcmp(toLowerCase(method), "newton") == 0)
- return this->newtonMethod(this->calculateAmountOfIterations(&DefiniteIntegral::newtonMethod));
- else if (strcmp(toLowerCase(method), "simpson") == 0)
- return this->simpsonMethod(this->calculateAmountOfIterations(&DefiniteIntegral::simpsonMethod));
- else {
- std::cout << "Error: there is no such method of calculating integral" << std::endl;
- system("pause>nul");
- exit(1);
- }
- return 0.0;
- }
- int DefiniteIntegral::getAmountOfIterations(const char * method)
- {
- if ( strcmp(toLowerCase(method), "left recktangle") == 0 || strcmp(toLowerCase(method), "leftrecktangle") == 0 )
- return this->calculateAmountOfIterations(&DefiniteIntegral::leftRecktangleMethod);
- else if ( strcmp(toLowerCase(method), "right recktangle") == 0 || strcmp(toLowerCase(method), "rightrecktangle") == 0 )
- return this->calculateAmountOfIterations(&DefiniteIntegral::rightRecktangleMethod);
- else if( strcmp(toLowerCase(method), "center recktangle") == 0 || strcmp(toLowerCase(method), "centerrecktangle") == 0 )
- return this->calculateAmountOfIterations(&DefiniteIntegral::centerRecktangleMethod);
- else if ( strcmp(toLowerCase(method), "trapeze") == 0 )
- return this->calculateAmountOfIterations(&DefiniteIntegral::trapezeMethod);
- else if ( strcmp(toLowerCase(method), "newton") == 0 )
- return this->calculateAmountOfIterations(&DefiniteIntegral::newtonMethod);
- else if ( strcmp(toLowerCase(method), "simpson") == 0 )
- return this->calculateAmountOfIterations(&DefiniteIntegral::simpsonMethod);
- else {
- std::cout << "Error: there is no such method of calculating integral" << std::endl;
- system("pause>nul");
- exit(1);
- }
- return -1;
- }
- // Method to calculate amount of iterations
- int DefiniteIntegral::calculateAmountOfIterations(double(DefiniteIntegral::*method)(int amountOfIterations))
- {
- double P = 0;
- if (method == &DefiniteIntegral::leftRecktangleMethod ||
- method == &DefiniteIntegral::rightRecktangleMethod) {
- P = 1.0;
- }
- else if (method == &DefiniteIntegral::centerRecktangleMethod ||
- method == &DefiniteIntegral::trapezeMethod) {
- P = 2.0;
- }
- else if (method == &DefiniteIntegral::newtonMethod ||
- method == &DefiniteIntegral::simpsonMethod) {
- P = 4.0;
- }
- else {
- std::cout << "Error: can't calculate amount of iterations, there is no such method in the class" << std::endl;
- system("pause>nul");
- exit(1);
- }
- int n = 10; // start amount of iterations
- const double k = 1 / (pow(2, P) - 1);
- while ( k * abs( (this->*method)(n) - (this->*method)(2 * n) ) > eps )
- n *= 10;
- return n;
- }
- // Methods for integral calcutation
- double DefiniteIntegral::leftRecktangleMethod(int amountOfIterations)
- {
- if (amountOfIterations <= 0) {
- std::cout << "Error: amount of iterations must be positive number" << std::endl;
- system("pause>nul");
- exit(1);
- }
- double h = abs(rightBorder - leftBorder) / amountOfIterations;
- double result = 0.0;
- double x = leftBorder;
- for (int i = 0; i != amountOfIterations; ++i) {
- result += f(x);
- x += h;
- }
- return (result * h);
- }
- double DefiniteIntegral::rightRecktangleMethod(int amountOfIterations)
- {
- if (amountOfIterations <= 0) {
- std::cout << "Error: amount of iterations must be positive number" << std::endl;
- system("pause>nul");
- exit(1);
- }
- double h = abs(rightBorder - leftBorder) / amountOfIterations;
- double result = 0.0;
- double x = leftBorder;
- for (int i = 1; i != amountOfIterations + 1; ++i) {
- result += f(x);
- x += h;
- }
- return (result * h);
- }
- double DefiniteIntegral::centerRecktangleMethod(int amountOfIterations)
- {
- if (amountOfIterations <= 0) {
- std::cout << "Error: amount of iterations must be positive number" << std::endl;
- system("pause>nul");
- exit(1);
- }
- double h = abs(rightBorder - leftBorder) / amountOfIterations;
- double result = 0.0;
- double x = leftBorder + (h / 2.0);
- for (int i = 0; i != amountOfIterations; ++i) {
- result += f(x);
- x += h;
- }
- return (result * h);
- }
- double DefiniteIntegral::trapezeMethod(int amountOfIterations)
- {
- if (amountOfIterations <= 0) {
- std::cout << "Error: amount of iterations must be positive number" << std::endl;
- system("pause>nul");
- exit(1);
- }
- double h = abs(rightBorder - leftBorder) / amountOfIterations;
- double result = 0.0;
- double x = leftBorder;
- for (int i = 1; i != amountOfIterations; ++i) {
- result += f(x);
- x += h;
- }
- result += ( this->f( this->leftBorder ) + this->f( this->rightBorder ) ) / 2;
- return ( result * h );
- }
- double DefiniteIntegral::simpsonMethod(int amountOfIterations)
- {
- double h = (rightBorder - leftBorder) / ( amountOfIterations * 2 );
- double x = leftBorder + h;
- double result = 0, result2 = 0, result4 = 0;
- for (int i = 0; i != amountOfIterations; ++i) {
- result4 += f(x);
- x += h;
- result2 += f(x);
- x += h;
- }
- result = 2 * result2 + 4 * result4 + (f(leftBorder) - f(rightBorder));
- return result * h / 3.0;
- }
- double DefiniteIntegral::newtonMethod(int amountOfIterations)
- {
- double h = (rightBorder - leftBorder) / ( amountOfIterations * 3 );
- double result2 = 0, result3 = 0, result = 0;
- double x = leftBorder + h;
- for (int i = 0; i != amountOfIterations; ++i) {
- result3 += f(x);
- x += h;
- result3 += f(x);
- x += h;
- result2 += f(x);
- x += h;
- }
- result = 2 * result2 + 3 * result3 + f(leftBorder) - f(rightBorder);
- return ( 3.0 / 8.0 ) * result * h;
- }
- // Not a class members
- char * toLowerCase(const char* str)
- {
- char * ptr = (char*)malloc(strlen(str));
- ptr = strcpy(ptr, str);
- for (char * tmpPtr = ptr; *tmpPtr; ++tmpPtr)
- *tmpPtr = char(tolower(*tmpPtr));
- return ptr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement