Thread: double integral with trapezoid rule

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    1

    double integral with trapezoid rule

    Hello everyone

    I was trying to implement a program in which I can calculate a double integral as a function of x and y with the trapezium rule (that's the one I understood the best). However, in addition to not getting the result I want the program has a very large calculation time for very large n ranges.


    I thought first of calculating the integral of the function with respect to x, and then in the body of the program it calls it in relation to the parameters presented and calculate its value in function of y, but it did not work very well and I can not think of a way to do it.


    Could someone help me to implement the program correctly and more efficiently?

    here is the code as far as i did:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    double f(double x, double y, double ay,double  by, double ny);
    
    
    int main()
    {
    
    
      int i;
      int nx = 0;
    
    
      double ax, bx;
      double sumx = 0;
      double soma=0;
      double hx,hy;
      double x,y;
      double ay,by;
      int ny;
    
    
    
    
    
    
      printf("inferior limit ax = ");
      scanf("%lf", &ax);
      printf("upper limit  bx (b>a) = ");
      scanf("%lf", &bx);
      printf("interval nx (n>1) n = ");
      scanf("%d", &nx);
    
    
    hx = (bx - ax) / (nx - 1);
    
    
    
    
    
    
      x = ax;
    
    
    printf("inferior limit ay = ");
      scanf("%lf", &ay);
      printf("upper limit by (b>a) = ");
      scanf("%lf", &by);
      printf("interval ny (n>1) n = ");
      scanf("%d", &ny);
    
    
    
    
    
    
    
    
    
    
      for (i = 1; i <= nx - 1; i++)
      {
    
    
    
    
      sumx = sumx + f(x,y,ay,by,ny) + f(x + hx,y,ay,by,ny);
      x = x + hx;
    
    
      }
    
    
      sumx = sumx * (hx / 2);
    
    
      printf("O resultado da soma e': %lf\n", sumx);
    
    
      return 0;
    } // end main()
    
    
    //
    //
    //
    double f(double x, double y, double ay,double  by, double ny)
    {
    
    
    double func;
    int j=0;
    double hy;
    
    
    double sumy=0;
    
    
      func= sin(x+y) / ((x*x) + y);
    
    
      hy = (by - ay) / (ny -1);
       y= ay;
    
    
      for (j =1; j <= ny - 1; j++)
    
    
      {
    
    
            sumy = sumy + sin(x + (y + hy)/ (x*x) + y+ hy);
            y = y +hy;
    
    
      }
    
    
      return sumy = sumy * (hy / 2);
    
    
    
    
    } // end f()
    the limits of integration are
    0.1 <y< 0.9
    0.2 <x<0.5

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Give some examples of reasonable input and the correct output.
    And what exactly is the equation? Is it:
    sin( x + y/(x*x) + y )
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to evaluate a double integral?
    By eenerd1993 in forum C Programming
    Replies: 1
    Last Post: 10-07-2014, 03:20 AM
  2. Double integral help
    By Telume9 in forum C Programming
    Replies: 6
    Last Post: 12-22-2011, 03:31 PM
  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. integral of 1/x
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 03-19-2004, 07:47 AM
  5. trapezoid code
    By blindleaf in forum C Programming
    Replies: 0
    Last Post: 03-19-2003, 03:11 PM

Tags for this Thread