Thread: Trapezoidal Rule Adding Values

  1. #1
    Registered User hungrymouth's Avatar
    Join Date
    Oct 2012
    Posts
    19

    Trapezoidal Rule Adding Values

    So I want to approximate the area of the integral using the trapezoidal method. The function has limits from 1 to 2 and the function is sqrt(x^3-1)dx. So the number of rectangles this function has is 10 with the width of 1/10. This is what I have so far in my code. I have calculated from 1 to 2 with this formula and used the while loop.

    width/2[f(x)+2f(x1)+2f(x2)+2f(x3)...+f(n) and so on)

    so since the width is 1/10, It's just increasing by .1 so it's going from 1,1.1,1.2,1.3 all the way to 2

    Now that I have those values calculated. All I have to do is add all those values and divide it by width/2 which in this case is 1/20. How would I do that?

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    int main()
    {
        double x;
        double number;
        number = 2.0;
        
        for(x = 1 ; x < number ; x=x+0.1)
        {
           cout<< "\n\n\tThe value is" << 2*sqrt(x*x*x-1);
           
               
        }
    
        cout<<"\n\n\n";
    
        return 0;
    }
    Last edited by hungrymouth; 02-25-2013 at 02:10 PM.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Well you just store all the results in the loop in a variable?? when the loop exits you can then divide by whatever value required
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trapezoidal rule
    By howlin in forum C Programming
    Replies: 8
    Last Post: 03-06-2012, 12:02 PM
  2. The trapezoidal rule question
    By paranoidgnu in forum C Programming
    Replies: 9
    Last Post: 04-24-2011, 09:00 AM
  3. Simpson's rule and Trapezoidal rule from fixed array
    By timwonderer in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2010, 03:14 PM
  4. composite trapezoidal rule
    By Sam Robinson in forum C Programming
    Replies: 0
    Last Post: 05-19-2003, 10:01 AM
  5. Arrays and the trapezoidal rule
    By ChemistryStar4 in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2003, 09:16 PM