简体   繁体   English

Matlab中矩阵的数值积分

[英]numerical integration of matrix in Matlab

I have to numerically evaluate, in Matlab, the integral of the product of two functions A(x,y) and B(x,y). 我必须在Matlab中以数值方式评估两个函数A(x,y)和B(x,y)的乘积的积分。 These two functions are in 2-dimensional array form. 这两个函数是二维数组形式。 The integral should read as following 积分应如下所示

$ C(x,z)= \\ int_ {a} ^ {b} dy'A(x,y')B(y',z)$

I understand that the function such as "trapz" are for numerical integration of arrays of data, but I do not understand ho to adapt to my case. 我知道诸如“ trapz”之类的功能用于对数据数组进行数值积分,但是我不了解如何适应我的情况。

thank you 谢谢

Giuseppe 朱塞佩

To guide you I will build some (square) matrix functions 为了指导您,我将构建一些(正方形)矩阵函数

%// Problem size.
n = 3;
%// Auxiliary constant matrices.
D1 = rand(n, n);
D2 = rand(n, n);
D3 = rand(n, n);
D4 = rand(n, n);
%// Matrix functions and product.
A = @(x,y) x^2*D1.^2 + y*D2;
B = @(y,z) (log(y)+z)*D3.^3 - z*sin(D4);
P = @(x,y,z) A(x,y)*B(y,z);

In this way P is a function of three variables, now we will integrate it in y, when x and z are both 0. 这样, P是三个变量的函数,现在我们将在x和z均为0时将其集成到y中。

a = 0;
b = 1;
integral(@(y) P(0,y,0), a, b, 'ArrayValued', true)

To generalize the solution to arbitrary domains we can build a new function in (x,z) 为了将解决方案推广到任意域,我们可以在(x,z)中建立一个新函数

Int = @(x,z) integral(@(y) P(x,y,z), a, b, 'ArrayValued', true);

and define a mesh 并定义一个网格

x = linspace(-1, 1, 11);
z = linspace(-2, 2, 21);
[X, Z] = meshgrid(x, z);

Now we can evaluate the integral on the whole mesh. 现在我们可以评估整个网格上的积分。

C = arrayfun(Int, X(:), Z(:), 'UniformOutput', false);

In this way C will contain all the integrals, stored in a 1D array of cells. 这样, C将包含所有积分,并存储在单元的一维数组中。

As a check we can get the result at (x,z) = (0,0) by calling 作为检查,我们可以通过调用来获得(x,z)=(0,0)的结果

C{sub2ind([11 21], 6, 11)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM