Numerical integration with a variable integration limit

조회 수: 5 (최근 30일)
JohnM
JohnM 2016년 9월 28일
답변: JohnM 2016년 9월 29일
Is there a way to implement the following:
f2=@(z) integral (@(x) f1(x,z) ,0 ,z);
P2=@(z) integral(f2,z,Inf);
With f2 and P2 as two separate anonymous functions? It seems z in f2 must be a scalar (not a vector), so a for loop is required to make multiple evaluations at z I can deal with that, but Matlab will not evaluate P2, giving the error: Error using integral (line 85) A and B must be floating-point scalars.
It appears that
P2Alt=@(z) integral2(@(x,z) f1(x,z), 0, z, z, Inf);
accomplishes the desired end result (z must be a scalar), but I would prefer the function f2, separate.

답변 (2개)

Ujjal Dey
Ujjal Dey 2016년 9월 29일
At first, try with "int" command and form a final function of your desired variable. Ex: f2=@(x) int (f1(x,z),z);
  댓글 수: 1
JohnM
JohnM 2016년 9월 29일
Hi Ujjal. I should have mentioned in my post that I don't have the symbolic math toolbox, so invoking int is not an option for me.

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


JohnM
JohnM 2016년 9월 29일
After giving this some thought, the following appears to be one way to solve the problem:
f2=@(z) integral(@(x) f1(x,z), 0, z);
f2Vect=@(z) arrayfun(@(z)f2(z),z); %vectorize f2
zval=0:0.1:3;
P2=zeros(1,length(zval)); %Preallocate
for i=1:length(zval)
P2(i)=integral(f2Vect,zval(i),Inf); %Evaluate the integral of f2
end

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by