Integral2 and anonymous function in a matrix

4 次查看(过去 30 天)
I cannot seem to get integration over a function right. Any explanation why the section of code doesn't run. Even after changing ymax to scalar, matrix exceed dimension error is alway there.
lxx = 2 * RR;
lyy= RH.* cos(eT); % et is [35*6] matrix
sigma_tot = sqrt((D*(0.0045.^2+0.-0025.^2+sigma_bq.^2+0.0065.^2))); % sigma_tot returns a [35*6] matrix
fun = @(lx,ly) exp ( - ((lx.^2 + ly.^2)./(2*sigma_tot.^2)));
int_i = integral2(fun,0,lxx,0,lyy);

采纳的回答

Walter Roberson
Walter Roberson 2017-11-12
You indicate that sigma_tot is 35 x 6, and fun uses sigma_tot so fun returns a 35 x 6 matrix.
Integral2 can only integrate scalar functions.
"The function fun must accept two arrays of the same size and return an array of corresponding values. It must perform element-wise operations."
To operate over the entire sigma_tot array to get a 35 x 6 output, you will need to arrayfun over sigma_tot:
fun = @(lx,ly, sigtot) exp ( - ((lx.^2 + ly.^2)./(2*sigtot.^2)));
int_i = arrayfun(@(sigtot) integral2( @(x,y) fun(x,y,sigtot),0,lxx,0,lyy), sigma_tot);
  1 个评论
Zaharaddeen Hussaini
Thanks. Had no idea Integral2 can only integrate scalar functions and would never have discovered arrayfun. I take it I do the same thing for argument lyy which is a also a matrix of equal size.
lyy= RH.* cos(eT); % et is [35*6] matrix

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by