Thread: Triple integral problem..

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    36

    Exclamation Triple integral problem..

    Hi,
    I need to write a code for the triple integral of some quantity. I wrote program for the single integral between two limits, How can I modify this code to find out the triple integral of the same.?
    Any Help would be appreciated.


    Thanks

    Code:
    voidmain(void) 
    { double i, a, b, sum = 0;
     printf(
       "\n integration of a function between two limits.");
     printf("\n\n first limit:");
     scanf("%f", &a);
     printf("\n second limit:");
     scanf("%f", &b);
     if (a > b) 
     {
      i = a;
      a = b;
      b = i;
     }
    
     for (i = a; i < b; i += (b - a) / N) 
     {
      /* Define your function below, and include the suitable header files */
      y = x * x + 2 * x - 4;
      sum += y * (b - a) / N;
     }
    
     printf("\n\nValue of integration is:%.3f", sum);
     getch();
     return;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Mastering how to paste code would be a start.
    For example, using "paste as text" in your browser, or perhaps "copy text only" from your IDE. Pasting a bunch of font/size tags into cboard generates a mess.

    Badly formatted and uncompilable code won't gather much of a response.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by shivagonal View Post
    for (i = a; i < b; i += (b - a) / N)
    That will not do what you think it does, especially if b is near zero, and a is very negative. You may end up with N, N-1, or infinite number of steps.

    The correct way to do xN steps over some interval x=[xmin,xmax] inclusive, with x0 and x1 being the current step endpoints, is
    Code:
        double xmin, xmax, x0, x1;
        long xi, xN;
    
        x1 = xmin;
        for (xi = 1L; xi <= xN; xi++) {
            const double pmin = (double)(xN - xi) / (double)xN;
            const double pmax = (double)xi / (double)xN;
            x0 = x1;
            x1 = xmin*pmin + xmax*pmax;
    
            /* xi=[1, xN] inclusive, current step is from x0 to x1. */
        }
    Although x1-x0 is almost constant, it does vary a very little bit in some cases, so better use x1-x0 for the step length instead of precalculating some h and assuming it is good enough.

    Numerical operations, especially using floating-point numbers, are trickier than the base math.

    For a multidimensional integral, you just need nested loops, one for each variable. The number of steps used per dimension depends on the function being integrated, but if without any information about that, I'd start with a total number of "steps" (cells), and divvy it up so that all steps end up the same size: cells roughly cubic.

    I find the rest of the code unreadable.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    That's totally the wrong approach for higher dimensional integration. The complexity becomes prohibitive, since it's the cube of your grid size. Try monte carlo integration instead. Heck it's easier to code IMHO.

    Monte Carlo integration - Wikipedia, the free encyclopedia

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    36

    Question

    I need to compute the divergence of the force vector which is attached below. where fvw = -rho*del_u*V. to find del_u there is another formula. The volume integral here is triple integral of the divergence of fvw. Will Monte Carlo integration help to solve these two?

    Thanks in advance
    Attached Images Attached Images Triple integral problem..-capture-jpg 

  6. #6
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    When you are evaluating a triple integral using a regular rectangular grid, the number of calculations you need to do increases as the cube of the number of steps along each axis. For example, if you take just 100 steps along each axis, you have to evaluate the integrand 100×100×100 = 1,000,000 times. Yet, 100 steps along each axis is very, very coarse, and is not likely to give you a very good answer.

    Monte Carlo integration is a method where you instead sample the integrand randomly. It is not easy to say which one is better for three dimensions, but if you have five or more dimensions (in which case 100 samples along each axis requires 10,000,000,000 evaluations of the integrand), Monte Carlo integration yields much better answers at the same number of evaluations done.

    For three dimensions, if the integrand is smooth, I'd probably try Monte Carlo integration. If it has small regions where the integrand is very large in magnitude, an adaptive approach might work much better. I'd start by dividing the volume into largeish cells, then evaluating each cell whether the integrand is flat enough within the cell (so it can be approximated by a constant value), or if the cell should be halved along each axis (in 3D, resulting in eight subcells) and each checked. This way you spend most computational resources in regions that are likely to affect the result most. (Again, this only applies to up to three or four dimensions; at five or more, I'd use Monte Carlo integration.)

    By the way, why the heck would you give one part of the formula, then say that [but part of it is actually something else]. That's idiotic. It's worse than useless, as it just says "it's like this but different".

  7. #7
    Registered User
    Join Date
    Jan 2015
    Posts
    36
    Quote Originally Posted by Nominal Animal View Post
    When you are evaluating a triple integral using a regular rectangular grid, the number of calculations you need to do increases as the cube of the number of steps along each axis. For example, if you take just 100 steps along each axis, you have to evaluate the integrand 100×100×100 = 1,000,000 times. Yet, 100 steps along each axis is very, very coarse, and is not likely to give you a very good answer.

    Monte Carlo integration is a method where you instead sample the integrand randomly. It is not easy to say which one is better for three dimensions, but if you have five or more dimensions (in which case 100 samples along each axis requires 10,000,000,000 evaluations of the integrand), Monte Carlo integration yields much better answers at the same number of evaluations done.

    For three dimensions, if the integrand is smooth, I'd probably try Monte Carlo integration. If it has small regions where the integrand is very large in magnitude, an adaptive approach might work much better. I'd start by dividing the volume into largeish cells, then evaluating each cell whether the integrand is flat enough within the cell (so it can be approximated by a constant value), or if the cell should be halved along each axis (in 3D, resulting in eight subcells) and each checked. This way you spend most computational resources in regions that are likely to affect the result most. (Again, this only applies to up to three or four dimensions; at five or more, I'd use Monte Carlo integration.)

    By the way, why the heck would you give one part of the formula, then say that [but part of it is actually something else]. That's idiotic. It's worse than useless, as it just says "it's like this but different".
    Thanks for the reply. you gave good suggestions. I will do programming according to the Monte Carlo for 2D and 3D cases.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem with const integral data
    By JackValentine01 in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2009, 09:45 AM
  2. Triple structure
    By dpp in forum C++ Programming
    Replies: 13
    Last Post: 06-25-2009, 11:42 PM
  3. Problem with Dawson's integral
    By lordbubonicus in forum C Programming
    Replies: 5
    Last Post: 11-25-2007, 03:44 PM
  4. Problem with Pythagorean triple etc.
    By -Prime- in forum C Programming
    Replies: 10
    Last Post: 10-14-2006, 01:50 AM
  5. triple monitor set up
    By Josh Kasten in forum Tech Board
    Replies: 15
    Last Post: 09-19-2004, 01:47 PM