Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...(I) have been able to get my problems solved from past messages and also new posts that other users have responded to promptly..."

Geography

Where in the world do Tek-Tips members come from?

The Trapezoidal Rule.

greyone (Programmer)
24 Feb 01 14:16
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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close