/*trapizoidal
rule*/
#include<stdio.h>
#include<math.h>
float
fun(float x)
{
float d;
d=sqrt(1-(x*x));
return(d);
}
void
main()
{
float
a,b,sum=0.0,integral=1.0,c=0.0,h;
int i,n;
printf("enter the lower and
upper limits:");
scanf("%f%f",&a,&b);
printf("enter the total
number of intervals:");
scanf("%d",&n);
label:
c=integral;
h=(b-a)/n;
sum=fun(a)+fun(b);
for(i=1;i<n;i++)
{
sum=sum+(2*fun(a+(i*h)));
}
integral=(h*0.5)*sum;
if(fabs((integral-c)/integral)>0.001)
{
n=n+10;
goto label;
}
printf("the value of the
integral is %f and is converged for %d intervals",integral,n);
}
Result:
enter the
lower and upper limits:0 1
enter the
total number of intervals:10
the value of
the integral is 0.784237 and is converged for 40 intervals
Press any key to continue
/*trapizoidal
rule*/
#include<stdio.h>
#include<math.h>
float
fun(float x)
{
float d;
d=(x*cos(x));
return(d);
}
void
main()
{
float a,b,sum=0.0,integral=1.0,c=0.0,h;
int i,n;
printf("enter the lower and
upper limits in degree:");
scanf("%f%f",&a,&b);
printf("enter the total
number of intervals:");
scanf("%d",&n);
a=a*(3.1415/180);
b=b*(3.1415/180);
label:
c=integral;
h=(b-a)/n;
sum=fun(a)+fun(b);
for(i=1;i<n;i++)
{
sum=sum+(2*fun(a+(i*h)));
}
integral=(h*0.5)*sum;
if(fabs((integral-c)/integral)>0.001)
{
n=n+10;
goto label;
}
printf("the value of the
integral is %f and is converged for %d intervals",integral,n);
}
Result:
enter the
lower and upper limits in degree:90 180
enter the
total number of intervals:10
the value of
the integral is -2.570212 and is converged for 20 intervals
Press any key to
continue
No comments:
Post a Comment
Thanks for comment.