Derived Function Handle does not support integration operation, frustrating!

조회 수: 1 (최근 30일)
Try this piece of code, which works fine.
a=1;b=2;
% A two-variate function
f2= @(x,y) x+y;
derivedF2=@(x) integral(@(y) f2(x,y), a,b);
% Test the evaluation of the derived function handle
derivedF2(0);
% Test the integration of the derived function handle
% integralVal=integral(derivedF2,a,b);
% integralVal=integral(@(x) derivedF2(x),a,b);
% Test plotting of the derived function handle
figure(11);
ezplot(@(x) arrayfun(derivedF2,x));
But if you uncomment the lines starting with integralVal. The code breaks.
Apparently, the derived function handle does not support integration operation, or have I missed something?
  댓글 수: 2
Walter Roberson
Walter Roberson 2017년 7월 12일
You have not defined a or b, so the code will not run.
Richard
Richard 2017년 7월 14일
Sorry, I should have tested it before I posted.

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

채택된 답변

Steven Lord
Steven Lord 2017년 7월 12일
When I ran this line of code (with the typo corrected, using derivedF2 instead of derivedF):
integralVal=integral(derivedF,a,b);
the error message I received said the following. I manually broke the message across two lines, so you don't need to scroll to read the whole thing.
Error using integralCalc/finalInputChecks (line 515)
Output of the function must be the same size as the input. If FUN is an
array-valued integrand, set the 'ArrayValued' option to true.
When I did that:
>> integralVal=integral(derivedF2,a,b, 'ArrayValued', true)
integralVal =
3
I used a = 1, b = 2.
I also recommend checking if integral2 will do what you need rather than calling integral twice.
  댓글 수: 1
Richard
Richard 2017년 7월 14일
Thanks! Yeah, the integral2 works fine. Thank you very much for your answer!

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 7월 12일
integralVal = integral(derivedF2, a, b, 'ArrayValued', true);
Or
integralVal = integral( @(X) arrayfun(derivedF2, X), a, b);
The basic problem is that integral() passes a vector as the first argument and the objective function must return a value for each member of the vector, and that happens at both levels when you have a nested integral() call.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by