Merge pull request #146 from JRaiden16/patch-2

Update Simpson's_1-3rd_rule.c
This commit is contained in:
Ashwek Swamy 2019-02-11 05:58:20 +05:30 committed by GitHub
commit 1f9a5168c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,34 +1,41 @@
#include<conio.h>
#include<stdio.h> #include<stdio.h>
#include<math.h> #include<math.h>
float f(float x)
float f(float x)
{ {
return 1.0+x*x*x; return 1.0+x*x*x; //This is the expresion of the function to integrate?
} }
void main() void main()
{ {
int i,n; int i,n;
float a,b,h,x,s2,s3,sum,integral; float a,b,h,x,s2,s3,sum,integral;
printf("enter the lower limit of the integration");
sacnf("%f",&a); printf("enter the lower limit of the integration:");
printf("enter the upper limit of the integration"); scanf("%f",&a);
sacnf("%f",&b); printf("enter the upper limit of the integration:");
printf("enter the number of intervals"); scanf("%f",&b);
sacnf("%d",&n); printf("enter the number of intervals:");
scanf("%d",&n);
h=(b-a)/n; h=(b-a)/n;
sum=f(a)+f(b); sum=f(a)+f(b);
s2=s3=0.0; s2=s3=0.0;
for(i=1;i<n;i+=3)
for(i=1;i<n;i+=3)
{ {
x=a+i*h; x=a+i*h;
s3=s3+f(x)+f(x+h); s3=s3+f(x)+f(x+h);
} }
for(i=3;i<n;i+=3)
for(i=3;i<n;i+=3)
{ {
x=a+i*h; x=a+i*h;
s2=s2+f(x); s2=s2+f(x);
} }
intgral=(h/3.0)*(sum+2*s2+4*s3);
printf("\nvalue of the integral =%9.4f\n",integral); integral=(h/3.0)*(sum+2*s2+4*s3);
getch(); printf("\nValue of the integral = %9.4f\n",integral);
return 0;
} }