Translate

Bu Blogda Ara

17 Nisan 2018 Salı

Integration Methods and Their Comparison







#include <stdio.h>
#include <math.h>
#define yan printf("\t")
#define in printf("\n")
double fpower(double x);// The function where type of integration is wanted to be check.
double leftrectangular(double(*f)(double),double a,double b,int n);// left rectangular method
double rirectangular(double(*f)(double),double a,double b,int n);// right rectangular method
double simpson(double(*f)(double),double a,double b,int n);//Simpson's Method
int main(){
int n;
double initial,final;
printf("Initial Value of Integral");
scanf("%lf",&initial);
printf("Final value of integral");
scanf("%lf",&final);

printf("\t\t\t\tLeft Rectangular\tRight Rectangular\tSimpson's Method'");
in;

for(n=10;n<=100;n+=10){
printf("Number of panel %d",n);
yan;
yan;
leftrectangular(fpower,initial,final,n);
yan;
yan;
rirectangular(fpower,initial,final,n);
yan;
yan;
simpson(fpower,initial,final,n);
in;

}


return 0;
}
double fpower(double x){
double y;
y=16*160*exp(-10*x);


return y;

}

double leftrectangular(double(*f)(double),double a,double b,int n){
double h;
int i;
h=(b-a)/n;
double answer=0;

for(i=0;i<=n;i++)
answer=answer+f(a+i*h);


answer=answer-f(b);

printf("%.10lf",h*answer);
return (h*answer);

}


double rirectangular(double(*f)(double),double a,double b,int n){

double h;
int i;


h=(b-a)/n;
double answer=0;

for(i=0;i<=n;i++)
answer=answer+f((b)-i*h);


answer=answer-f(a);

printf("%.10lf",h*answer);
return (h*answer);

}






double simpson(double(*f)(double),double a,double b,int n){

double answer;
double h;
int i;
double x;
answer=f(a);
h=(b-a)/n;

for(i=1;i<=n;i++){
x=a+i*h;
answer+=4*f(x-h/2)+2*f(x);
}

answer=answer-f(b);
answer=(h*answer)/6;


printf("%.10lf",answer);


return answer;



}



Hiç yorum yok:

Yorum Gönder