⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 num_int.cpp

📁 Numerical Integration Algorithm.
💻 CPP
字号:


//Numerical Integration(Trapezoidal Rule,Simpson's 1/3 Rule,Simpson's 3/8 Rule)

#include<stdio.h>
#include<conio.h>
void main()
{
 float a,b,x[20],y[20],s=0,h;
 int i,w,n;
 printf("\n\t\tNumerical Integration\n");
 printf("\n1.Trapezoidal Rule");
 printf("\n2.Simpson's 1/3 Rule");
 printf("\n3.Simpson's 3/8 Rule");
 printf("\n\nEnter the method you want to use");
 scanf("%d",&w);
 printf("\n\nHow many set of values do u want:");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
 	printf("Enter x[%d],y[%d]",i+1,i+1);
   scanf("%f%f",&x[i],&y[i]);
 }
 h=x[1]-x[0];
 printf("Enter the lower and upper limits:");
 scanf("%f%f",&a,&b);
 switch(w)
 {
 	case 1:
   for(i=0;i<n;i++)
   {
   	if(x[i]>a && x[i]<b)
      	s=s+2*y[i];
      if(x[i]==a || x[i]==b)
      	s=s+y[i];
   }
   s=s*.5*h;
   break;

   case 2:
   for(i=0;i<n;i++)
   {
   	if(x[i]>a && x[i]<b)
      {
      	s=s+4*y[i];
         i=i+1;
         if(i<n && x[i]<b)
         	s=s+2*y[i];
      }
      if(x[i]==a || x[i]==b)
      	s=s+y[i];
   }
   s=s*h/3;
   break;

   case 3:
   for(i=0;i<n;i++)
   {
   	if(x[i]>a && x[i]<b)
      {
      	s=s+3*y[i];
         i=i+1;
         if(i<n && x[i]<b)
         	s=s+3*y[i];
         i++;
         if(i<n && x[i]<b)
         	s=s+2*y[i];
      }
      if(x[i]==a || x[i]==b)
      	s=s+y[i];
   }
   s=s*h*3/8;
   break;

   default:
   printf("Sorry, your chosen method do not exist!!!");
   exit(0);
 }
 printf("\nThe required integration is:%f",s);
 getch();
}
   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -