Problem with number of intervals chosen by quadgk

I am running the following code being called in nested loops that vary n and k:
g = @(x) alpha(x, n, fun);
I = @(x) x.*g(R.*x).*besselj(n,j(n+1,k).*x);
[int, err] = quadgk(I,0,1);
with:
function res = alpha(r, n, fun)
func = @(theta) feval(fun,r,theta).*cos(n.*theta);
res = (1/pi) .* quadgk(func,-pi,pi);
end
and the function passed as fun is:
function y = f(r,th)
y = (4-r.^2).*sin(th).^2;
end
Now the problem I have is that the first call of quadgk chooses a differen number of points than the call in alpha. This causes me to get a matrix dimension error if f since r and th are different sizes. Is there any way to work around this problem?
p.s. I know I can give them a max number of points, but that doesn't help since they could still choose different numbers under that max.

Answers (1)

Hi Nicolas,
since the "r" in the function alpha is the variable, it looks to me, as if you have to rewrite alpha to loop on r, something like
function res = alpha(r, n, fun)
res = zeros(size(r));
for i=1:length(r)
func = @(theta) fun(r(i), theta).*cos(n.*theta);
res(i) = 1/pi * quadgk(func, -pi, pi);
end
end
Titus

This question is closed.

Tags

Asked:

on 10 Dec 2011

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!