Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include "graphics.h"
- #include <iostream>
- #include <math.h>
- #include <cstdlib>
- #include <string>
- #include <fstream>
- using namespace std;
- double func(double x); //Вычисляет значение функции f(x)=x*e^(x)*sin(x)
- double f2(double x, double h); //Вторая производная
- int main(int argc, char * argv[])
- {
- int n = 48;
- double a = 0, b = 1, h1, e = 0.001, S, f2max;
- double i;
- int w = 1900, h = 1000;
- int height = h - 100;
- int length = w - 50;
- setlocale(LC_ALL, "RUSSIAN");
- cout << "Нахождение определенного интеграла функции f(x)=x*e^(x)*sin(x) на отрезке [0;1]. Отрезок разбивается на 48 частей." << endl;
- while (1)
- {
- h1 = (b - a) / n;
- S = h1 * (func(a) + func(b)) / 2;
- for (i = 0; i < n; i++)
- {
- S = S + func(a + h1 * i)*h1;
- }
- f2max = f2(a, h1);
- for (i = 0; i < n; i++)
- {
- if (f2(a + h1 * i, h1) > f2max) f2max = f2(a + h1 * i, h1);
- }
- double check = f2max * pow(b - a, 3) / (12 * n*n);
- if (check > e)
- {
- n = n + 5;
- }
- else
- {
- cout << "Значение интеграла = " << S << endl;
- break;
- }
- }
- cout << "Нажмите Enter, чтобы вывести диаграмму." << endl;
- system("pause");
- int mastx = length / n;
- double maxfunc = func(a);
- for (i = 1; i < n; i++)
- {
- if (func(a + i * h1) > maxfunc)
- maxfunc = func(a + i * h1);
- }
- if (func(b) > maxfunc)
- maxfunc = func(b);
- int masty = height / maxfunc;
- char conv[128];
- initwindow(w, h, "Diagrama");
- bar(0, 0, w, h);
- rectangle(0, 0, w, h);
- setcolor(0);
- setbkcolor(15);
- line(30, 30, 30, 30 + height);
- line(30, 30, 25, 40);
- line(30, 30, 35, 40);
- line(30, 30 + height, 20 + length, 30 + height);
- line(20 + length, 30 + height, length + 15, 25 + height);
- line(20 + length, 30 + height, length + 15, 35 + height);
- outtextxy(length + 20, 45 + height, "x");
- outtextxy(40, 30, "f(x)");
- for (i = 0; i < 1; i+=0.2)
- {
- line(30 + i * length, 20 + height, 30 + i * length, 40 + height);
- sprintf(conv, "%f", i);
- outtextxy(30 + i * length, 45 + height, conv);
- double k = 30 + height - func(i) * masty;
- line(20, 30 + height - func(i) * masty, 40, 30 + height - func(i) * masty);
- sprintf(conv, "%f", func(i));
- outtextxy(5, 30 + height - func(i) * masty, conv);
- }
- for (i = 0; i < n; i++)
- {
- line(30 + i * mastx, 30 + height, 30 + i * mastx, 30 + height - func(a+i*h1) * masty);
- line(30 + i * mastx, 30 + height - func(a + i * h1) * masty, 30 + (i + 1) * mastx, 30 + height - func(a + i * h1) * masty);
- line(30 + (i + 1) * mastx, 30 + height, 30 + (i + 1) * mastx, 30 + height - func(a + i * h1) * masty);
- }
- getch();
- while (!kbhit())
- {
- delay(200);
- }
- getch();
- closegraph();
- cout << "Нажмите Enter, чтобы вывести график функции." << endl;
- system("pause");
- initwindow(w, h, "Diagrama");
- bar(0, 0, w, h);
- rectangle(0, 0, w, h);
- setcolor(0);
- setbkcolor(15);
- line(30, 30, 30, 30 + height);
- line(30, 30, 25, 40);
- line(30, 30, 35, 40);
- line(30, 30 + height, 20 + length, 30 + height);
- line(20 + length, 30 + height, length + 15, 25 + height);
- line(20 + length, 30 + height, length + 15, 35 + height);
- outtextxy(length + 20, 45 + height, "x");
- outtextxy(40, 30, "f(x)");
- for (i = 0; i < 1; i += 0.2)
- {
- line(30 + i * length, 20 + height, 30 + i * length, 40 + height);
- sprintf(conv, "%f", i);
- outtextxy(30 + i * length, 45 + height, conv);
- double k = 30 + height - func(i) * masty;
- line(20, 30 + height - func(i) * masty, 40, 30 + height - func(i) * masty);
- sprintf(conv, "%f", func(i));
- outtextxy(5, 30 + height - func(i) * masty, conv);
- }
- for (i = 0; i < n; i++)
- {
- line(30 + i * mastx, 30 + height - func(a + i * h1) * masty, 30 + (i + 1) * mastx, 30 + height - func(a + (i+1) * h1) * masty);
- }
- getch();
- while (!kbhit())
- {
- delay(200);
- }
- getch();
- closegraph();
- system("pause");
- return 0;
- }
- double func(double x) //Вычисляет значение функции f(x)=x*e^(x)*sin(x)
- {
- return x * exp(x)*sin(x);
- }
- double f2(double x, double h) //Вторая производная
- {
- return (func(x + h) - 2 * func(x) + func(x - h)) / (h * h);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement