function of a variable obtained from integrating a two variable function

조회 수: 1 (최근 30일)
I have a function that depends on two variables, say x and y; f(x,y). I want to integrate with respect to x only and get a new function that depends only on y; i.e., g(y) = @(y) integral(@(y)f(x,y),lim1,lim2), where lim1 and lim2 are the limits for for x. I want to use g(y) later to carry out other operations. I cannot find a way around this problem.

채택된 답변

Matt J
Matt J 2014년 9월 16일
편집: Matt J 2014년 9월 16일
I think you've answered your own question. Why can't you create an anonymous function for g() just as you did in your post
g = @(y) integral(@(x)f(x,y),lim1,lim2)
and carry that around for reuse?
  댓글 수: 1
Mike Hosea
Mike Hosea 2014년 9월 16일
You can make it more general (work with array inputs) like so
g = @(y1)arrayfun(@(y)integral(@(x)f(x,y),lim1,lim2),y1);
For example:
f = @(x,y)exp(-hypot(x,y));
lim1 = -inf;
lim2 = inf;
g = @(y1)arrayfun(@(y)integral(@(x)f(x,y),lim1,lim2),y1);
y = linspace(-10,10);
plot(y,g(y));

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

추가 답변 (1개)

Mauricio
Mauricio 2014년 9월 16일
편집: Matt J 2014년 9월 16일
Well you are right but I did not expressed myself correctly. The problem is the following: In the code below I have the function "Base(z)" which depends on "z", which I need to integrate with respect to z. This function "Base" depends in turn on another function called RR which I have to integrate with respect to one variable a to obtain "fun", the output of "Base(z)". Unfortunately, I keep on getting the error message you see below. Note: I can evaluate "Base(z)" for any z, but I cannot integrate "Base(z)" with respect to z. Thanks for your help
function out = Princip
clc;
function fun=Base(z)
RR = @(a,b)(z.*3.^a)./(b+1);
fun = integral(@(a)RR(a,5),0,10);
end
out = integral(@Base,1,10);
end
Error Message:
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.
Error in integralCalc/iterateScalarValued (line 315)
finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in Princip (line 18)
out = integral(@Base,1,10);
  댓글 수: 2
Matt J
Matt J 2014년 9월 16일
Set the 'ArrayValued' option to true, as the error message instructs (in all your calls to integral()).
Mike Hosea
Mike Hosea 2014년 9월 16일
Or make the Base function satisfy the requirements of INTEGRAL without the ArrayValued flag, i.e. to accept an array input and return an array of the same size.
function fun=Base(z)
fun = zeros(size(z));
for k = 1:numel(fun)
RR = @(a,b)(z(k).*3.^a)./(b+1);
fun(k) = integral(@(a)RR(a,5),0,10);
end
end

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

카테고리

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