Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
help on triple integral

Subject: help on triple integral

From: Carla Soares

Date: 28 Jun, 2010 17:38:06

Message: 1 of 5

Hi! Can someone help me on a triple integral?
I have something like this:
integrand is f(x,y,z)
int over z from a to g(x,y)
int over y from b to f(x)
int over x from c to d
I've tried using triplequad and quad2d but always get error messages.
Thanks a lot in advance!

Subject: help on triple integral

From: Roger Stafford

Date: 4 Jul, 2010 02:13:04

Message: 2 of 5

"Carla Soares" <carla.soares@gmail.com> wrote in message <i0amlt$e1b$1@fred.mathworks.com>...
> Hi! Can someone help me on a triple integral?
> I have something like this:
> integrand is f(x,y,z)
> int over z from a to g(x,y)
> int over y from b to f(x)
> int over x from c to d
> I've tried using triplequad and quad2d but always get error messages.
> Thanks a lot in advance!
- - - - - - - - -
  I can think of a way of using quad2d for finding two-dimensional integrals of cross sections for fixed x values and then using a one-dimensional quadrature routine (quad, quadgk, or quadl) for integrating over these cross sections from x=c to x=d.

  You would need to write a function F(x), which accepts x as a vector, to act as the integrand of the 1d quadrature routine. Using a for-loop, for each element xi of the vector, F(xi) would be the value given by quad2d for the integral of f(xi,y,z) over the cross section b<=y<=f(xi) and a<=z<=g(xi,y). As long as guad2d is being called on with a single fixed xi, it can solve such a cross section integral if it is called in a correct manner.

  Admittedly this won't be as efficient as a routine that is specifically designed for triple integration, but I see no reason why it shouldn't work.

  Another possibility is to find an appropriate change of variables that would change your region of integration into a three-dimensional rectangle and then use triplequad. Being able to do that would depend on the nature of your y and z upper limit functions, f(x) and g(x,y). (Your f(x) upper limit function is different from your f(x,y,z).)

Roger Stafford

Subject: help on triple integral

From: Carla Soares

Date: 5 Jul, 2010 16:48:19

Message: 3 of 5

Thanks for your reply! I'm sorry, but I don't get it completly... I now have:

    for x=-A:R-A;
        int1 = @(y,z) (-alpha*(cdf(max(0,max(0,max(0,R-max(0,A+x))-max(0,A+x+y))-max(0,A+x+y+z))))).*pdf(z).*pdf(x).*pdf(y);
        ymin = -1e5;
        ymax = max(0,R-max(0,A+x));
        zmax = @(y) max(0,max(0,R-max(A+x))-max(0,A+x+y));
        int2 = quad2d(int1,ymin,ymax,ymin,zmax);
    end

int1 is my original integrand with the 3 variables. A and R are changing values in another loop and cdf and pdf are functions handles for the cdf and pdf of the normal.
But this gives me a value (and I guess only for the last assumed value of x), I don't see how I get to integrate over x...
Sorry if this is very basic, but I'm not managing to figure it out...

Subject: help on triple integral

From: Roger Stafford

Date: 5 Jul, 2010 23:26:04

Message: 4 of 5

"Carla Soares" <carla.soares@gmail.com> wrote in message <i0t2cj$m4p$1@fred.mathworks.com>...
> Thanks for your reply! I'm sorry, but I don't get it completly... I now have:
>
> for x=-A:R-A;
> int1 = @(y,z) (-alpha*(cdf(max(0,max(0,max(0,R-max(0,A+x))-max(0,A+x+y))-max(0,A+x+y+z))))).*pdf(z).*pdf(x).*pdf(y);
> ymin = -1e5;
> ymax = max(0,R-max(0,A+x));
> zmax = @(y) max(0,max(0,R-max(A+x))-max(0,A+x+y));
> int2 = quad2d(int1,ymin,ymax,ymin,zmax);
> end
>
> int1 is my original integrand with the 3 variables. A and R are changing values in another loop and cdf and pdf are functions handles for the cdf and pdf of the normal.
> But this gives me a value (and I guess only for the last assumed value of x), I don't see how I get to integrate over x...
> Sorry if this is very basic, but I'm not managing to figure it out...
- - - - - - - -
  What I had in mind is a function, call it F(X) (or whatever you like,) which can accept a vector X of x values. Within this function there should be a for-loop that selects each value xi out of the vector X one-at-a-time. The number of trips through the for-loop is entirely determined by the length of vector X sent to the function each time it is called. Also needed is a subfunction within it which uses this scalar xi as a parameter along with the matrices, Y and Z, which will be the arguments passed to it by quad2d, in order to evaluate the desired integrand for xi and each pair of y,z values in Y and Z. In addition there should be two other subfunctions which use xi as a parameter and evaluate the upper limits of z using matrix Y as an argument and the upper limit of y using only parameter xi. At each pass through the for-loop the quad2d function is called on with those
integrand and limit functions as function handles. The result of each call should be the scalar double integral of the integrand over the cross section at the particular x value xi. This result should be placed in a vector, FF, and when the for-loop is finished, the vector FF(X) is passed back as an output. (Note that this for-loop has nothing to do with variations in your parameters R and A.)

  Then outside the above function, the function quad, quadl, or quadgk, whichever you choose, should call on the F function as it integrates from c to d. Note that these three quadrature functions each pass vectors to their integrand functions rather than make calls with single scalar values, which is the reason that F has to be able to receive vectors.

  In the code you showed me, there is no evidence of the for-loop I have described here. Your for-loop appears to be varying x in a fixed way as given by -A:R-A and not as a vector which has been passed to it from quad (or quadl or quadgk.) There ought to be a piece of code that looks in outline roughly like this:

function FF = F(X)
 FF = zeros(size(X)); % Allocate space for FF
 for k = 1:length(X)
  xi = X(k);
  % Then use xi as parameter in integrand & limit subfunctions
  FF(k) = quad2d(.... Calling on these subfunctions ....)
 end
end

  Then outside, the F function is to be called by quad, quadl, or quadgk to integrate this cross section F from c to d, and in so doing the vector X is repeatedly passed automatically to F so as to cover that interval.

Roger Stafford

Subject: help on triple integral

From: Carla Soares

Date: 14 Jul, 2010 16:01:07

Message: 5 of 5

thanks a lot! I think it works!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us