Nested Numerical Integral in Matlab

조회 수: 36 (최근 30일)
Ignas
Ignas 2012년 12월 11일
편집: Walter Roberson 2025년 2월 7일 23:26
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)
  댓글 수: 3
Babak
Babak 2012년 12월 12일
If not, what is your f(x,y) function? Can you find g(.),h(.) such that f(x,y)=g(x)*h(y)?
Ignas
Ignas 2012년 12월 12일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Teja Muppirala
Teja Muppirala 2012년 12월 12일
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)
  댓글 수: 2
Ignas
Ignas 2012년 12월 20일
Thank you! Sorry my reply took so long.
Sundar Aditya
Sundar Aditya 2017년 1월 28일
Hi,
I was wondering how the syntax would change if the limits of x were a function of y. Specifically, the expression I need to evaluate is of the form integral( e^( -integral( f(x,y),x,0,g(y) ) ),y,0,1). This is what I tried:
integral(@(x) exp(-integral(@(x,y) f,0,@(y) y)),0,1)
where f is the function handle for f(x,y). I get the following error message:
Function 'subsindex' is not defined for values of class 'function_handle'.
I'm unable to figure out what I'm doing wrong. Any help will be greatly appreciated.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Richard
Richard 2017년 7월 18일
Seems to me that this is what you are looking for. I assumed a random expression for f function since you did not specify it.
f =@(x,y) x+y;
integral(@(y) exp(-integral(@(x) f(x,y),0,y)),0,1,'ArrayValued',true)
  댓글 수: 2
Paul
Paul 2021년 9월 27일
Many thanks; I had the same problem and your suggestion solved it.
Bronwyn
Bronwyn 2025년 2월 7일 22:41
I had a similar problem (nasty integrals summed in the denominator of another integral) and this worked perfectly! Thanks!

댓글을 달려면 로그인하십시오.


Roger Stafford
Roger Stafford 2012년 12월 12일
편집: Walter Roberson 2025년 2월 7일 23:26
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

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by