From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Double integral on vector data
Date: Mon, 28 May 2012 03:08:06 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 31
Message-ID: <jpuq6m$cur$>
References: <>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1338174486 13275 (28 May 2012 03:08:06 GMT)
NNTP-Posting-Date: Mon, 28 May 2012 03:08:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:769190

pmassicotte <> wrote in message <>...
> I would like to calculate a double integral using trapz function.
> You can find the code bellow to reproduce the calculations.
> Data:
> Ex1 = Excitation data. First column
> Step 1: Create the surface from witch I want to calculate the double integral
> Comp1 = Em1(:,2) * Ex1(:,2)';
> Step 2: Meshgrid
> [X,Y] = meshgrid(Ex1(:,1), Em1(:,1));
> Step 3: Integrate
> V1 = trapz( Y(:,1), (trapz(X(1,:),Comp1, 2)) )
- - - - - - - - - - -
  I haven't reproduced your calculation, but it looks as though it would give you the correct answer.  However, there are two respects in which your code could be simplified.

  First, there is no need whatever to compute the 'meshgrid' function as you have.  You could just as well have written:

 V1 = trapz(Em1(:,1),trapz(Ex1(:,1),Comp1,2));

without doing the 'meshgrid' at all.  It doesn't accomplish anything for you here.

  Second, as you have defined 'Comp1' this problem doesn't have to be done as a double integral.  The two vectors Ex1(:,2) and Em1(:,2) can be factored out separately in the integrals so as to obtain the product of two single integrals:

 V1 = trapz(Em1(:,1),Em1(:,2)) * trapz(Ex1(:,1),Ex1(:,2));

which involves much less computation.  This is true because in calculus

 int( int(f(x)*g(y) dy) dx = int(f(x) dx) * int(g(y) dy)

over a rectangular x-y area.

Roger Stafford