Numeric integration of a matrix using integral2()
5 次查看(过去 30 天)
显示 更早的评论
I need to integrate the function F over the area 0<f<2*pi,0<p<a where:
a = 50
F = @(f,p) p.*exp(1i*k*p.*A)
k = 1
A = sin(pi/180*theta)'*cos(pi/180*phi-f)
theta = [-5:0.05:5]
phi = [0 45 90]
A is a (201x3)size matrix and so is F for earch pair (f,p). Tried integral2(F,0,2*pi,0,a) but it didn't work!
Then I made F not a handler anymore and did:
syms p f
fun=p.*exp(1i*k*p.*A)
int(fun,f,0,2*pi)
int(ans,p,0,a)
but it didn't work at all either plus computing time was intolerable! Would it be possible to resolve this still using integral2 way? I'm a MATLAB newcomer and stuck so far... Thanks in advance for any help!
1 个评论
Walter Roberson
2013-12-5
Some of the integration work can be made easier by treating it as a list of integrals, one for each phi, as some of them simplify when particular values of phi are substituted. Unfortunately, for other phi (e.g., 45) the integral stays messy, and there is no obvious way to convert it to a closed form.
回答(1 个)
Mike Hosea
2013-12-11
This is an advanced exercise for using integral2. I think this does what you wanted. The approach is to work with all scalar parameters to define the right integral and then evaluate it on a grid:
% A is a "vectorized" function of f. We will only call it with
% scalar phi and theta below.
A = @(phi,theta,f)sin(pi/180*theta).*cos(pi/180*phi-f);
% Define the integrand function with parameters k, phi, and theta. It is a
% vectorized function of the variables f and p. These can be any size as
% long as they are the same size. The function should only see scalar k,
% phi, and theta inputs.
F = @(f,p,k,phi,theta)p.*exp(1i*k*p.*A(phi,theta,f));
% Now define the integral as a function of phi and theta.
% This function only works with scalar phi and theta inputs.
a = 50;
k = 1;
ys = @(phi,theta)integral2(@(f,p)F(f,p,k,phi,theta),0,2*pi,0,a);
% Define phi and theta vectors.
theta = -5:0.05:5;
phi = [0 45 90];
% Form arrays that present each combination of phi and theta possible.
[phigrid,thetagrid] = ndgrid(phi,theta);
% Evaluate the integral function for each phi and theta combination.
y = arrayfun(ys,phigrid,thetagrid);
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!