MATLAB Answers


Nested Numerical Integral in Matlab

Asked by Ignas
on 11 Dec 2012

Hello, I am fairly new in using Matlab and was wondering if a nested numerical integral was possible. I have seen a number of other questions here where the outer variable of integration appears in the limits of the inner integral but the function being integrated over just depends on one variable. So I was wondering how or if it's possible to do, say:

z = integral( e^(-integral(f(x,y),x,0,1)),y,0,1)


on 11 Dec 2012

Do you want to solve this for any general function f(x,y)?

on 12 Dec 2012

If not, what is your f(x,y) function? Can you find g(.),h(.) such that f(x,y)=g(x)*h(y)?

on 12 Dec 2012

This is just an example of what I want to do, which is to take the integral of an integral of a function of two variables with a non-linear operation between them. Ultimately what I want to do is solve:

integral( det([f11 f12; f21 f22]) ,y,ymin,ymax)

where f11 = integral( a(x,y),x,xmin,xmax ) f12 = integral( b(x,y),x,xmin,xmax ) ... But functionally the example with the exponential is the same. And to answer your question, I generally won't be able to split f(x,y) into two products like that.


No products are associated with this question.

2 Answers

Answer by Teja Muppirala
on 12 Dec 2012
 Accepted answer

Rather than trying to do it all in one expression, it's much simpler if you break it up into two parts.

Step 1. Make the inner part a separate function and save it to a file.

function F = innerF(y)
F11 = integral(@(x) exp(x+y) ,0,1);
F21 = integral(@(x) exp(x-y) ,0,1);
F12 = integral(@(x) sin(x+y) ,0,1);
F22 = integral(@(x) cos(x-y) ,0,1);
F = det([F11 F12; F21 F22]);

Step 2. From the command line, call INTEGRAL to do the outer integral

integral(@innerF, 0, 1, 'ArrayValued', true)

  1 Comment

on 20 Dec 2012

Thank you! Sorry my reply took so long.

Answer by Roger Stafford
on 12 Dec 2012

The functions 'dbsquad' and 'quad2d' are designed to numerically solve just your kind of problem.  The former uses the the kind of fixed integration limits that you have described and the latter allows variable limits.  Be sure to read their descriptions carefully so you can define the integrand function properly.

Roger Stafford


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!