From: Walter Roberson on
Chen wrote:
> The x and y are given domain. Let's say x~(-10,10), y ~(10,100).

That's nice, but it doesn't answer even one of the questions I asked.

Does the N x N matrix of values for f(x,y) *define* f(x,y), or does the matrix
represent a *sampling* of a function at discrete grid points? If it represents
a *sampling* of a function, then is the function known to have continuous
first derivatives? Is it known to have continuous second derivatives as well?

Your matrix for f(x,y) is N x N, but the x and y ranges you give are not the
same: one has a range of 20 and the other has a range of 90. That implies
finding the area of rectangles rather than squares, a fact which would affect
numerical integration formula. You are using the ~ operator, which in
statistics means "is distributed according to", but you have a bunch of
(x,y,f) triples as implied by "is distributed according to" then you would not
be able to put them into a regular NxN grid (well, not meaningfully). But
still you used the ~ statistical operator: is the implication that you _do_
have a rectangular grid but that the grid spacing is irregular?
From: Chen on
Hi walter,

Your questions are really good. Those conditions should be taken care of when you are doing puring mathematical calculation (1st and 2nd derivative). But I am only doing this for engineering use. The functions are basicly well behaved .
Sorry about the wrong use of ~. when I said x~(-10,10), I meant that x is from -10 to 10.
Let's put it this way. I want to integrate a function f(x,y)*p(x) over x and y.
(1)
If f(x,y) = cos(x)*sin(y) and p(x) = cos(2*x) (!! they are functions here ); . Theoretically, I can integrate this with no trouble.
(2)
But now the thing is that I need to integrate discrete data set of f(x,y) and p(x) (!! They are discrete numbers here). and both x and y are 1 X N vectors. Then the problem becomes tricky to me. Now the f(x,y) is a N X N matrix and p(x) is a 1 X N vector.

I hope you understand what i am saying..
From: Roger Stafford on
"Chen Chen" <chenchen_ee(a)yahoo.com> wrote in message <i3pfrd$aca$1(a)fred.mathworks.com>...
> I just simplify the problem i want to solve to this imple double mathematical problem. I thought it would be confusing if I explained the whole thing.
> Sorry I didn't state the problem clearly. Here is a specific version.
> These numbers are given sets of data. That is: x and y are two sets of data which are given at the first place. Let's say both the x and y are 1 X N vectors. f(x,y) is given too and it's an N X N matrix. q(x) is a function depending on x only and I know the function already. So q(x) is a 1 X N vectors with known numbers.
> My problem is that I don't know how to integrate f(x,y)*f(x) over x and y numerically. I hope I expained it clearly this time.
>
> Chen
- - - - - - - - - -
With discrete-valued variables you cannot use any of matlab's "quad" type integration routines which require functions. However you can use an iterated form of trapz which does trapezoidal integration. Let X be a column vector of monotone x values in which X(1) = a and X(N) = b. Let Y be a column vector of monotone y values for which Y(1) = c and Y(M)= d. Let F be an N x M array in which

F(i,j) = f(X(i),Y(j)). (Your f(x,y) )

Let Q be a column vector in which Q(i) = q(X(i)) (your q(x) ). Then do this:

I = trapz(X,Q.*trapz(Y,F.').');

This will give the two-dimensional "trapezoidal" approximation to your desired double integral over the stated rectangle.

The approximation made is this. In each rectangular cell with corners at

(X(i),Y(j), (X(i+1),Y(j), (X(i),Y(j+1), and (X(i+1),Y(j+1),

the integral is approximated by the average of the integrand q(x)*f(x,y) evaluated at these four corners multiplied by the (signed) area of the rectangle. The final value I is the sum of these.

This integration can be considered as a first-order approximation to the exact double integral. I think if you look hard enough there may be some higher order integration routines listed on the file exchange which could possess higher accuracy, though if so, I don't remember where they are located.

Roger Stafford
From: Walter Roberson on
Chen wrote:
> Hi walter,
>
> Your questions are really good. Those conditions should be taken care
> of when you are doing puring mathematical calculation (1st and 2nd
> derivative). But I am only doing this for engineering use.

Which numeric integration routines you can use is affected by how many layers
of continuity you assume.
From: Chen on
Hi Roger,

Thank you for your reply. I tried your method and it seems like not working right.
I tried this:
f(x,y) = cos(x).*sin(y) and q(x) = cos(x).x is from 0 to pi/2,y is from pi/2 to 2*pi. The exact integral value of f(x,y)*q(x) over x and y is - pi/4.

The code is :
%%% Integration test

x = linspace(0,pi/2,100);
y = linspace(pi/2,2*pi,100);

% method 1,using quad2d
func = @(x,y) cos(x).^2.*sin(y);
I1 = quad2d(func,0,pi/2,pi/2,2*pi);

% method 2,using double trapz
[X Y] = meshgrid(x,y);
f = cos(X).*sin(Y);
q = cos(x);
I2 = trapz(x,q.*trapz(y,f.').');

%%%%% End of code %%%%%%%%%

The results are: I1 = -0.7854(which is - pi/4) and I2 = 5.2042e-017 (which is zero actually).

Did I understand your method wrong?
Thanks a lot

-Chen