Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The Trapezoidal Rule.

Status
Not open for further replies.

greyone

Programmer
Dec 14, 2000
200
0
0
CA
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(&quot;Enter the first integer => &quot;);
scanf(&quot;%d&quot;,&w);
printf(&quot;Enter the second integer => &quot;);
scanf(&quot;%d&quot;,&x);
printf(&quot;Enter the third integer => &quot;);
scanf(&quot;%d&quot;,&y);

printf(&quot;Enter the fourth integer => &quot;);
scanf(&quot;%d&quot;,&z);

printf(&quot;\n&quot;);printf(&quot;\n&quot;);
puts(&quot;Enter values of start and end of interval &quot;);
printf(&quot;\n&quot;);
printf(&quot;Enter the starting value => &quot;);
scanf(&quot;%lf&quot;,&x1);

printf(&quot;Enter the ending value=> &quot;);
scanf(&quot;%lf&quot;,&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(&quot;Intervals Area&quot;); printf(&quot;\n&quot;);
printf(&quot; 100 %f \n&quot;,answer1);
printf(&quot; 1000 %f \n&quot;,answer2);
printf(&quot;10000 %f \n&quot;,answer3); printf(&quot;\n&quot;);
}

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(&quot;\n&quot;);
}



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.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top