Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Ira_4m.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h" //for visual studio 2010
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include "gnuplot_i.hpp"
- #include <conio.h>
- #include <windows.h>
- #define cyrillic setlocale(LC_CTYPE, "rus")
- using namespace std;
- char* p;
- double f(double x);
- string parse_path(char* path);
- void concat(vector<char> & v , string s);
- double trapezoidal_rule(double a , double b , int n){
- double h = (b - a) / n;
- double sum = 0.0;
- for(int i = 0 ; i < n ; ++i){
- double x = a + h * double(i);
- double xx = a + h * double(i + 1);
- sum += f(x) + f(xx);
- }
- sum *= h;
- sum /= 2.0;
- return sum;
- }
- double simpson(double a , double b , int n){
- double h = (b - a) / n;
- double sum = 0.0;
- for(int i = 1 ; i < n ; ++i){
- double x = a + h * (double)i;
- int c = 2 + 2 * (i % 2);
- sum += (double)c * f(x);
- }
- sum += f(a) + f(b);
- sum *= h;
- sum /= 3.;
- return sum;
- }
- double rectangle_method(double a , double b , int n){
- double h = (b - a) / n;
- double sum = 0.0;
- for(int i = 0 ; i < n ; ++i){
- double x = a + h * (double)i;
- sum += f(0.5 * (x + x + h));
- }
- return h * sum;
- }
- void plot(double a , double b , int n , double (*method)(double a , double b , int n) , string head){
- string parse_p = parse_path(p);
- vector<char> path(parse_p.begin() , parse_p.end());
- path.push_back('\0');
- concat(path , "temp.txt");
- double h = (b - a) / n;
- vector<pair<double , double>> segments;
- segments.push_back(make_pair(a , 0.));
- for(int i = 1 ; i <= n ; ++i){
- double x = a + h * (double)i;
- double xx = a + h * (double)(i - 1);
- double segans = method(xx , x , 100);
- segments.push_back(make_pair(x , segans));
- segments[segments.size() - 1].second += segments[segments.size() - 2].second;
- }
- FILE * file = fopen(&path[0] , "w");
- fprintf(file , "# X Y\n");
- for(int i = 0 ; i < segments.size() ; ++i){
- fprintf(file , " %lf %lf\n" , segments[i].first , segments[i].second);
- }
- fclose(file);
- Gnuplot gnu("lines");
- string fi = "set style line 1 lc rgb '#0060ad' lt 2 lw 2 pt 0 ps 1.5";
- string se = &path[0]; se = "plot '" + se; se += "' with linespoints ls 1";
- gnu.set_grid();
- gnu.set_title(head);
- gnu.plot_points(se , fi);
- FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
- cout << endl << "Press any key to continue..." << endl;
- _getch();
- gnu.remove_tmpfiles();
- remove(&path[0]);
- }
- double f(double x){
- return x * x;
- }
- int main(int argc , char* argv[]){
- cout.precision(10);
- cout.setf(ios::fixed);
- cyrillic;
- p = argv[0];
- double a , b;
- int n;
- cin >> a >> b >> n;
- cout << "Метод трапеций: " << trapezoidal_rule(a , b , n) << endl;
- cout << "Формула Симпсона: " << simpson(a , b , n) << endl;
- cout << "Метод средних прямоугольников: " << rectangle_method(a , b , n) << endl;
- plot(a , b , n , &trapezoidal_rule , "График методом трапеций");
- plot(a , b , n , &simpson , "График формулой Симпсона");
- plot(a , b , n , &rectangle_method , "График методом средних прямоугольников");
- system("pause");
- return 0;
- }
- string parse_path(char* path){
- string s = path;
- size_t pos = s.find_last_of('\\');
- if(pos != string::npos){
- return s.substr(0 , pos + 1);
- }
- if(s[s.size() - 1] != '\\')s += '\\';
- return s;
- }
- void concat(vector<char> & v , string s){
- v.pop_back();
- for(int i = 0 ; i < s.size() ; ++i){
- v.push_back(s[i]);
- }
- v.push_back('\0');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement