Wednesday, September 17, 2014

Trapezoidal Method of numerical integration in C

#include<stdio.h>
#include<conio.h>

float f(float x){
float r;
r=2*x+1;
return r;
}

void main(){

float output;
float a,b;
float h,s;
int i,j;
int n;
clrscr();
printf("===========================================\n");
printf("           TRAPEZOIDAL RULE\n");
printf("===========================================\n");
printf("Enter the integral limit:-\n");
printf("-------------------------------------------\n");
printf("Lower limit value a: \n");
scanf("%f",&a);
printf("Upper limit value b: \n");
scanf("%f",&b);
printf("-------------------------------------------\n");
printf("Enter number of total intervals: \n");
scanf("%d",&n);
h=0.0;
h=(b-a)/n;
printf("____________________________________________\n");
printf("Resultant value of h is %.2f \n",h);
s=0.0;
s=f(a)+f(b);
printf("Resultant value of f(a) + f(b) is %.2f \n",s);
for (i=1;i<=n-1;i++)
{
s=s+2*f(a+i*h);
}
printf("Resultant value of whole summation of f(-) is %.2f\n",s);
output=1;
output=(h/2)*s;
printf("Output value of the integral is %.2f \n",output);
printf("____________________________________________\n");
printf("             GOOD LUCK\n");
getch();
}

Simpson's One-Third Rule of numerical integration in C

#include<stdio.h>
#include<conio.h>

float f(float x){
float r;
r=2*x+1;
return r;
}

void main(){

float output;
float a,b;
float h,s;
int i,j;
int n;
clrscr();
printf("===========================================\n");
printf("           SIMPSON'S ONE-THIRD RULE\n");
printf("===========================================\n");
printf("Enter the integral limit:-\n");
printf("-------------------------------------------\n");
printf("Lower limit value a: \n");
scanf("%f",&a);
printf("Upper limit value b: \n");
scanf("%f",&b);
printf("-------------------------------------------\n");
printf("Enter number of total intervals: \n");
scanf("%d",&n);
h=0.0;
h=(b-a)/(2*n);
printf("____________________________________________\n");
printf("Resultant value of h is %.2f \n",h);
s=0.0;
s=f(a)+f(b)+4*f(a+h);
printf("Resultant value of f(a) + f(b) is %.2f \n",s);
for (i=3;i<2*n;i+=2)
{
s+=2*f(a+(i-1)*h)+4*f(a+i*h);
}
printf("Resultant value of whole summation of f(-) is %.2f\n",s);
output=1;
output=(h/3)*s;
printf("Output value of the integral is %.2f \n",output);
printf("____________________________________________\n");
printf("             GOOD LUCK\n");
getch();
}