Search code examples
matlabnumerical-integration

Double integral of the equation in Matlab


How to implement this equation

enter image description here

in Matlab,

where: A and B are mxm matrices.

for example:

A = [3.45 32.54 78.2; 8.4 33.1 4.66; 68.2 9.336 33.87 ]

B = [6.45 36.54 28.24; 85.4 323.1 74.66; 98.2 93.336 38.55 ]

my code:

  f1 = @(A) (abs(A) ).^2;  
  f2 = @(B) (abs(B) ).^2; 
  Q = integral2( f1, 0, 1, 0, 1) * integral2(f2, 0, 1, 0, 1);

But when i run the code I got the error "Too many input arguments.".

What is the problem in the code?


Solution

  • After clarification of your question, let me change my post.

    What you are after is numerical integration of a function that was already sampled on a fixed grid, and the function values are stored in matrices A and B that are two dimensional M by M. I suppose that you have the associated gridpoints as well, suppose they are denoted xc and yc. Then, if you have sufficiently fine sampling of a smooth function, the integral approaches:

    xc = linspace(0,1,M);
    yc = linspace(0,1,M);
    Q = trapz(xc,trapz(yc, abs(A).^2)) * trapz(xc,trapz(yc, abs(B).^2 ));
    

    To test that, I made a simple example that evaluates the surface of a circle, i.e. formula

    so to do that with trapezoidal method with N samples for r and M samples for phi, we have,

    r = 2;       % Pick a value for r
    M = 100;     % Pick M samples for the angular coordinate from 0 to 2*pi
    N = 101;     % Pick N samples for the radial coordinate from 0 to r
    phic = linspace(0,2*pi,M);   % take M samples uniformly for example
    rc = linspace(0,r,N);        % take N samples uniformly for example
    integrand = repmat(rc,M,1);  % Make MxN matrix, phi along rows, r along columns
    I = trapz(rc, trapz(phic, integrand));
    

    So for the case r=2, it gives indeed 4*pi.