Hi I want to implement the trpezoidal rule which is explained as follows. I have written the code for it but it seems to give me the wrong output. Please help.
The code is as follows:
#include <stdio.h>
double trapezoidal(int a,int b,int c,int d,double lowx,double highx,int intervals);
main()
{
int w,x,y,z;
double x1,x2,answer1,answer2,answer3;
printf("Enter the first integer => "
;
scanf("%d",&w);
printf("Enter the second integer => "
;
scanf("%d",&x);
printf("Enter the third integer => "
;
scanf("%d",&y);
printf("Enter the fourth integer => "
;
scanf("%d",&z);
printf("\n"
;printf("\n"
;
puts("Enter values of start and end of interval "
;
printf("\n"
;
printf("Enter the starting value => "
;
scanf("%lf",&x1);
printf("Enter the ending value=> "
;
scanf("%lf",&x2);
answer1=trapezoidal(w,x,y,z,x1,x2,100);
answer2=trapezoidal(w,x,y,z,x1,x2,1000);
answer3=trapezoidal(w,x,y,z,x1,x2,10000);
puts("Intervals Area"
; printf("\n"
;
printf(" 100 %f \n",answer1);
printf(" 1000 %f \n",answer2);
printf("10000 %f \n",answer3); printf("\n"
;
}
double trapezoidal(int a,int b,int c,int d,double lowx,double highx,int intervals)
{
double answer,answer1,answer2,answer3,X,f,Y,Z;
int i;
X=(highx-lowx)/(2*intervals);
Y=(highx-lowx)/intervals;
Z=lowx+Y;
answer1=a*lowx*lowx*lowx - b*lowx*lowx + c*lowx +d;
for(i=Z; i<=highx; i=i+Y)
{
f=a*i*i*i-b*i*i+c*i+d;
answer2=2*f;
}
answer3=a*highx*highx*highx - b*highx*highx + c*highx + d;
answer=X*(answer1+answer2+answer3);
return(answer); printf("\n"
;
}
The Trapezoidal Rule.
Integrating f(x)dx over the interval a to b=
((b-a)/(2n))[f(x0) + 2f(x1)+ 2f(x2) + 2(fx3) +..... +2f(x(n-1))+f(xn)]
0,1,2......n are the intervals. The number of intervals is the number of trapezoids.
The width of every trapezoid is (b-a)/n.
a=x0<x1<x2<x3......<xn=b means that the value of x increases from a to b in the interval and as I understand it increases (b-a)/n times.
The code is as follows:
#include <stdio.h>
double trapezoidal(int a,int b,int c,int d,double lowx,double highx,int intervals);
main()
{
int w,x,y,z;
double x1,x2,answer1,answer2,answer3;
printf("Enter the first integer => "
scanf("%d",&w);
printf("Enter the second integer => "
scanf("%d",&x);
printf("Enter the third integer => "
scanf("%d",&y);
printf("Enter the fourth integer => "
scanf("%d",&z);
printf("\n"
puts("Enter values of start and end of interval "
printf("\n"
printf("Enter the starting value => "
scanf("%lf",&x1);
printf("Enter the ending value=> "
scanf("%lf",&x2);
answer1=trapezoidal(w,x,y,z,x1,x2,100);
answer2=trapezoidal(w,x,y,z,x1,x2,1000);
answer3=trapezoidal(w,x,y,z,x1,x2,10000);
puts("Intervals Area"
printf(" 100 %f \n",answer1);
printf(" 1000 %f \n",answer2);
printf("10000 %f \n",answer3); printf("\n"
}
double trapezoidal(int a,int b,int c,int d,double lowx,double highx,int intervals)
{
double answer,answer1,answer2,answer3,X,f,Y,Z;
int i;
X=(highx-lowx)/(2*intervals);
Y=(highx-lowx)/intervals;
Z=lowx+Y;
answer1=a*lowx*lowx*lowx - b*lowx*lowx + c*lowx +d;
for(i=Z; i<=highx; i=i+Y)
{
f=a*i*i*i-b*i*i+c*i+d;
answer2=2*f;
}
answer3=a*highx*highx*highx - b*highx*highx + c*highx + d;
answer=X*(answer1+answer2+answer3);
return(answer); printf("\n"
}
The Trapezoidal Rule.
Integrating f(x)dx over the interval a to b=
((b-a)/(2n))[f(x0) + 2f(x1)+ 2f(x2) + 2(fx3) +..... +2f(x(n-1))+f(xn)]
0,1,2......n are the intervals. The number of intervals is the number of trapezoids.
The width of every trapezoid is (b-a)/n.
a=x0<x1<x2<x3......<xn=b means that the value of x increases from a to b in the interval and as I understand it increases (b-a)/n times.