Path: news.mathworks.com!not-for-mail
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$1@newscl01ah.mathworks.com>
References: <709f2305-2da2-4e70-9bcb-3c2bea819160@ra8g2000pbc.googlegroups.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-01-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1338174486 13275 172.30.248.46 (28 May 2012 03:08:06 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 28 May 2012 03:08:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:769190

pmassicotte <pmassicotte@hotmail.com> wrote in message <709f2305-2da2-4e70-9bcb-3c2bea819160@ra8g2000pbc.googlegroups.com>...
> 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