Integral2 and anonymous function in a matrix

2 views (last 30 days)
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);

Accepted Answer

Walter Roberson
Walter Roberson on 12 Nov 2017
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 Comment
Zaharaddeen Hussaini
Zaharaddeen Hussaini on 13 Nov 2017
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

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!