Скачиваний:
25
Добавлен:
01.05.2014
Размер:
2.55 Кб
Скачать
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream.h>


void main()
  {
     clrscr();
     int k;
     long int s;
     float a1,b1,epsf;
     double a,b,eps,x,x1,X1;
     double F(double);
     double PRM(double,double,double,int);
     double TRAP(double,double,double,int);
     double SIMPSON(double,double,double,int);
     cout<<"\n                 Welcome!\n\n";
     cout<<"I = sin(2x)*exp(-x^2),  [1,2]\n\n";
     printf("Input a: ");
     scanf("%f",&a1);
     a=a1;
     printf("Input b: ");
     scanf("%f",&b1);
     b=b1;
     printf("Input eps: ");
     scanf("%f",&epsf);
     eps=epsf;
     k=1;
     x1 = PRM(a,b,eps,k);
     do
	{
	   X1=x1;
	   k=k*2;
	   x1 = PRM(a,b,eps,k);
	   a1=fabs(x1-X1)/3;
	}
     while (a1>eps);
     printf("\n\tMetod pryamougolnikov:  I = %f    k=%d\n",X1,k);
     k=1;
     x1 = TRAP(a,b,eps,k);
     do
	{
	   X1=x1;
	   k=k*2;
	   x1 = TRAP(a,b,eps,k);
	   a1=fabs(x1-X1)/3;
	}
     while (a1>eps);
     printf("\tMetod trapecii:         I = %f    k=%d\n",X1,k);
     k=1;
     x1 = SIMPSON(a,b,eps,k);
     do
	{
	   X1=x1;
	   k=k*2;
	   x1 = SIMPSON(a,b,eps,k);
	   a1=fabs(x1-X1)/15;
	}
     while (a1>eps);
     printf("\tMetod Simpsona:         I = %f    k=%d\n",X1,k);
     cout<<"\nPress any key to exit, please.\n";
     getch();
  }


double F(double x)
   {
     double s;
     s = (sin(2*x)*exp(-x*x));
     return (s);
   }

double PRM(double L,double R,double Eps,int n)
   {
      double X,h=(R-L)/n;
      int i;
      float sum=0;
      if (Eps<=0.0) {puts("Nevernoe zadanie tochnosti\n");exit(1);}
      for(i=0;i<=n-1;i++)
	 {
	    X = L+i*h;     /* vichislenie serediny otrezka */
	    sum=sum+F(X+h/2);
	 }
      return(sum*h);
   }

double TRAP(double L,double R,double Eps,int n)
   {
      double X,h=(R-L)/n;
      int i;
      float sum=0;
      if (Eps<=0.0) {puts("Nevernoe zadanie tochnosti\n");exit(1);}
      for(i=0;i<=n-1;i++)
	 {
	    X = L+i*h;     /* vichislenie serediny otrezka */
	    sum=sum+F(X)+F(L+(i+1)*h);
	 }
      return(sum*h/2);
   }

double SIMPSON(double L,double R,double Eps,int n)
   {
      double X,h=(R-L)/n;
      int i;
      float sum=0;
      if (Eps<=0.0) {puts("Nevernoe zadanie tochnosti\n");exit(1);}
      for(i=0;i<=n-1;i+=2)
	 {
	    X = L+i*h;     /* vichislenie serediny otrezka */
	    sum=sum+F(X)+4*F(L+(i+1)*h)+F(L+(i+2)*h);
	 }
      return(sum*h/3);
   }

Соседние файлы в папке LR_7
  • #
    01.05.201430.21 Кб30lab7.xls
  • #
    01.05.20142.57 Кб23MAIN.BAK
  • #
    01.05.20142.55 Кб25Main.CPP
  • #
    01.05.201417.25 Кб21MAIN.OBJ
  • #
    01.05.20142.44 Кб22Main.~CPP
  • #
    01.05.2014262.14 Кб29Project1.tds
  • #
    01.05.2014139.78 Кб55ВМ_7.doc