URI:
   DIR Return Create A Forum - Home
       ---------------------------------------------------------
       Essentials of Scientific Computing 2013
  HTML https://esc2013.createaforum.com
       ---------------------------------------------------------
       *****************************************************
   DIR Return to: Programming Questions
       *****************************************************
       #Post#: 4--------------------------------------------------
       Numerical Integration
   DIR By: mathnathan
       Date: July 3, 2013, 1:16 pm
       ---------------------------------------------------------
       Here is the code we wrote to approximate the integral of the
       function f(x) = -x2+4 from 0 to 2 using the left hand rule.
       --- Code ---
       
       #include <stdio.h>
       
       int main() {
       
       float a = 0;
       float b = 2;
       float n = 2;
       
       // Our derived quatities. These depend on the paramenters
       double step = (b-a)/n;
       double xi = a;
       
       // Initialize the sum so the += operator will work
       double sum = 0;
       
       // Loop over all n rectangles! i starts at 0 and counts
       all the way up to n
       for( int i=0; i<n; i++ ) {
       sum += (-(xi*xi)+4)*step; // Calculate the area of
       rectangle i, and add it to the cumulative sum
       xi += step; // Increment which xi we're at so we can
       evaluate the function at that point (getting the height)
       }
       
       printf( "sum = %f\n", sum );
       
       return 0;
       }
       
       --- End Code ---
       The first three lines contain all of the parameters of
       integration
       float a = 0;
       float b = 2;
       float n = 2;
       The first 2, a and b, are the bounds of integration and the last
       one, n, is the number of rectangles used to approximate the
       integral.
       The next 2 lines are our derived quantities. These depend on the
       parameters.
       double step = (b-a)/n;
       double xi = a;
       The step size is how far you step to get to the next rectangle,
       it is also the width of the rectangle, but in scientific
       computing, we call this value the step, or the delta x, the
       change in x between each iteration. xi is the ith rectangle. It
       is the current rectangle being processed. It starts at a, the
       beginning boundary of our integration.
       We then initialize the sum to 0 so that we can use the +=
       operator. Without the initialization sum=0 the program will try
       to add the area of the ith rectangle to sum during the first
       iteration. But without initialiazing it, sum does not have a
       value assigned to it so there is nothing to add the area to! The
       program will crash.
       double sum = 0;
       We finally begin the loop.
       for( int i=0; i<n; i++ ) { ... }
       The loop initializes i to 0, checks to see if i is less than n,
       if it is it will execute the code in the brackets and then
       increment i by 1. It then checks again to see if i is less than
       n, if it is it will execute the code in the brackets and then
       increment i by 1. This process continues until i is no longer
       less than n. So basically we run n iterations of the loop, or
       the loop runs n times! One iteration for each rectangle!
       Every time we run the code inside the loop, we simply calculate
       the area of the ith rectangle, f(x)*step, and add it to the
       cumulative sum. Then just move xi to the next rectangle by
       adding the step size or rectangle width to it.
       sum += (-(xi*xi)+4)*step;
       xi += step;
       The more rectangles used, the more accurate your approximation
       will be. (there is a limit to how accurate the left hand rule
       can become when using a discrete approximation like this)
       *****************************************************
       Page 1 of 1