Numerical Integration with functions as borders
15 views (last 30 days)
Show older comments
Hi. I am new to this Forum and I have a question to which I did yet not find an appropriate answer. Maybe someone can help.
I would like to solve an triple integral which has a function as border (I am not sure if this is the right word for the boundaries of an integral, but let's assume it is). The border-function itself depends on the variable which will be integrated with the 2nd integral.
For instance, look at this integral:
My problem is not analyticly solvable (using symbolic variables ans int()-function), so I need a numeric solution. Working with quad() and function-handles as borders doesn't work either. So how to integrate?
Thank you for your help,
Manu
2 Comments
Sean de Wolski
on 5 Oct 2011
Should it be h1(phi) to h2(phi)? Above looks like you're only integrating at a specific point.
Accepted Answer
Mike Hosea
on 5 Oct 2011
QUAD2D accepts function handles for limits. Take care that you make them "vectorized" (usually just use .*, .+, and ./ instead of *, +, and /).
>> h1 = @(phi)-2 + sin(phi); % or whatever
>> h2 = @(phi)2 - cos(phi); % or whatever
>> B = @(xi,b)quad2d(@(phi,x)(1./(h1(phi) - h2(phi))).*(1./(b-(xi-x).^2).^(3/2)),-pi,pi,h1,h2);
>> B(5,100)
ans =
-0.010761692235804
-- Mike
function q = quad3d(f,x1,x2,y1x,y2x,z1xy,z2xy)
q = quadgk(@inner,x1,x2);
function q = inner(x)
q = arrayfun( ...
@(xs,y1s,y2s) ...
quad2d(@(y,z)f(xs*ones(size(y)),y,z),y1s,y2s, ...
@(y)z1xy(xs*ones(size(y)),y), ...
@(y)z2xy(xs*ones(size(y)),y)), ...
x,y1x(x),y2x(x));
end
end
3 Comments
Mike Hosea
on 6 Oct 2011
You said the limits had to be functions, so I didn't code it to accept scalars. The call for your example should be
I3 = @(xi,yi,zi)quad3d(@(r,phi,z)(xi+yi+zi+r+phi+z),0,1,@(r)zeros(size(r)),@(r)ones(size(r)),@(r,phi)zeros(size(r)),@(r,phi)ones(size(r))); I3(0,0,0)
This returns 1.5 on my computer.
More Answers (3)
the cyclist
on 5 Oct 2011
I think you should be able to do this with dblquad(). To take care of the variable limits, define a rectangular area (with fixed limits) that you know contains the entire range. Then, include an extra multiplicative term in your integrand that becomes zero when you are outside the true limits of your inner integral.
I'm pretty sure that a similar question was asked and answered here before, so maybe do a search on "dblquad", "limit", etc. [Have to admit I did not find it myself, but I did not search thoroughly.]
Matt Tearle
on 5 Oct 2011
You could do this as an iterated integral using quad (twice). Hooray for Fubini's Theorem! Here's the volume of a hemisphere, calculated as a double integral over the unit circle:
% Define upper and lower limits
limupr = @(x) sqrt(1-x.^2);
limlwr = @(x) -sqrt(1-x.^2);
% Define integrand
f = @(x,y) sqrt(1-x.^2-y.^2);
% Do inner integral w.r.t. y:
% I(x) = integrate_l(x)^u(x) f(x,y) dy
int1 = @(x) quad(@(y) f(x,y),limlwr(x),limupr(x));
% Make sure we can evaluate int1 at a vector of x values
g = @(x) arrayfun(int1,x);
% Integrate w.r.t. x
I = quad(g,-1,1)
3 Comments
Mike Hosea
on 6 Oct 2011
Unfortunately, there is not. It's a little tricky to extend QUAD2D, so I'll add the code to my original answer (so that it will be readably formatted).
Manuel
on 6 Oct 2011
2 Comments
Matt Tearle
on 7 Oct 2011
So, just to be clear: you're all set now? You got it to work?
If so, it seems like the key point was using quad2d to have function handle limits on the inner integral, so please accept Mike's answer. Thx. (And if not, let us know what's still outstanding.)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!