Matlab numerical integral avoid point

91 views Asked by At

I am using the integralN function to do a 4 dimensional integral of a complicated function (if you're interested I put it at the end of the post).

I am doing the integration starting at (r1, r2) = (1.8, 1.8). Analytically, this function is defined at r1 = r2 however, numerically Matlab treats everything with matching coordinates as NaN.

Is there any way I can avoid this behavior? As an FYI adding eps to one to the lower bounds doesn't work.

f1 =@(r) 5*r.^2 + 2;
f2 =@(r) 2*r.^2 + 12;

f1p = der(f1);
f2p = der(f2);

dF = @(r1,t1,r2,t2)((r1.*r2.*(f1(r1)-f2(r2)+(-r1+r2.*cos(t1-t2)).*f1p(r1)).*(f1(r1)-f2(r2)+(r2-r1.*cos(t1-t2)).*f2p(r2)))./(pi.*(r1.^2+r2.^2-2.*r1.*r2.*cos(t1-t2)+(f1(r1)-f2(r2)).^2).^2))

function df = der(f)
    syms x
    df = matlabFunction(diff(f(x)));
end
1

There are 1 answers

0
user1543042 On BEST ANSWER

I figured out a wrap around function that will force the correct output, where funMatch and funNoMatch are 4d functions and my limits were stored in Limit.

f = @(r1, t1, r2, t2) selective(funMatch, funNoMatch, r1 == r2, r1, t1, r2, t2);
res = integralN(f, Limit(1,1), Limit(1,2), Limit(2,1), Limit(2,2), Limit(3,1), Limit(3,2), Limit(4,1), Limit(4,2), 'RelTol',1e-1, 'AbsTol',1e-3);

function res = selective(funMatch, funNoMatch, cond, varargin)
    res = zeros(size(cond));

    Match = cellfun(@(x) x(cond), varargin, 'uni', 0);
    NoMatch = cellfun(@(x) x(~cond), varargin, 'uni', 0);

    res(cond) = funMatch(Match{:});
    res(~cond) = funNoMatch(NoMatch{:});
end