Quad 2d giving inconsistant answers
Show older comments
Hello,
I'm trying to use quad2d to integrate a function whose value is 0 outside of the (-5,5)(-5,5) box. However, I get different answers if I tell quad to integrate that box specifically or a larger box around it. Any idea what's happening? I'm using R2009a. (Sorry for the inefficent Matlab coding, it's partly because I hacked the code to work with quad2d)
quad2d(@(x,y)(of_Nd_ExpHimmelblau(x,y)),-5,5,-5,5,'AbsTol',10^-14,'RelTol',10^-14)
ans =
38.008891501144596
quad2d(@(x,y)(of_Nd_ExpHimmelblau(x,y)),-5,7,-5,11,'AbsTol',10^-14,'RelTol',10^-14)
ans =
38.008544462736467
function outMat=of_Nd_ExpHimmelblau(x1,y1)
[N,D]=size(x1);
x=zeros(N*D,2);
x(:,1)=x1(:);
x(:,2)=y1(:);
beta=0.01;
[numSamples,numDim]=size(x);
xmin=-5;
xmax=5;
out=zeros(numSamples,1);
for jj=1:numSamples
if sum(x(jj,:)<xmin)>0 || sum(x(jj,:)>xmax)>0
out(jj)=0;
else
for ii=1:(numDim-1)
xx=x(jj,ii);
yy=x(jj,ii+1);
out(jj)=out(jj)+exp(-((xx^2+yy-11)^2+(xx+yy^2-7)^2)*beta);
end
end
end
outMat=zeros(N,D);
outMat(:)=out(:);
Accepted Answer
More Answers (1)
Brendan
on 4 Aug 2011
0 votes
Categories
Find more on Function Creation 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!