Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "math.h"
- using namespace std;
- double fnc (double x){
- return x*x*x + cos(x)*cos(x) + 2*x;
- }
- double modul(double x){ //взятие модуля
- if(x<0){
- return -1*x;
- }
- else{
- return x;
- }
- }
- double fnc2(double x){
- return (0.25*(x*x*x*x + sin(2*x))) + (0.5*x) + (x*x);
- }
- int main()
- {
- double a,b;// границы интегрирования
- double e;// заданная точность
- int n; //начальное число шагов
- cout<<"granici intervala:";
- cin>>a>>b;
- cout<<"tochnost':";
- cin>>e;
- cout<<"nachal'noe chislo shagov:";
- cin>>n;
- double c = b-a; //вычисление длины интервала
- double h; //ширина шага
- double h2; //ширина шага для следующей итерации
- double ocenka=10;
- double summ1,summ2; // приближённое значение интеграла
- double summ3; // точное значение интеграла
- while (ocenka>=e){
- h = c/n;
- h2= c/(2*n);
- summ1=0;
- summ2=0;
- for (int i=0;i<n;i++){
- summ1+= 0.5*h*(fnc(i*h)+fnc(h*(i+1)));
- }
- for(int l=0;l<2*n;l++){
- summ2+=0.5*h2*(fnc(l*h2)+fnc(h2*(l+1)));
- }
- ocenka= (1.0/3.0)*modul(summ2 - summ1);
- n*=2;
- }
- summ3=fnc2(b) - fnc2(a);
- cout<<"priblizhennoe znachenie:"<<summ2<<endl;
- cout<<"polucheno pri "<<n<<" shagov"<<endl;
- cout<<"tochnoe znachenie:"<<summ3<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement