Implementation of Trapezoidal Rule on function I=e^x dx?
#include<stdio.h>
#include<math.h>
double y(double x)
{
return (exp(x));
}
main()
{
float x=0,h,ans=0,l=0.0;
int n,i;
printf("\nEnter the initial value: ");
scanf("%f",&x);
printf("\nEnter the final value: ");
scanf("%f",&l);
printf("\nEnter the number of integrals: ");
scanf("%d",&n);
h=(l-x)/(n*1.0);
for(i=0;i<=n;i++)
{
if(i==0 || i==n)
ans+=y((i*h)+x);
else
ans+=2*y((i*h)+x);
}
ans=ans*h/2;
printf("\nIntegration Result=%f\n",ans);
}
#include<stdio.h>
#include<math.h>
double y(double x)
{
return (exp(x));
}
main()
{
float x=0,h,ans=0,l=0.0;
int n,i;
printf("\nEnter the initial value: ");
scanf("%f",&x);
printf("\nEnter the final value: ");
scanf("%f",&l);
printf("\nEnter the number of integrals: ");
scanf("%d",&n);
h=(l-x)/(n*1.0);
for(i=0;i<=n;i++)
{
if(i==0 || i==n)
ans+=y((i*h)+x);
else
ans+=2*y((i*h)+x);
}
ans=ans*h/2;
printf("\nIntegration Result=%f\n",ans);
}
OUTPUT:-