How to find double integral in MATLAB

조회 수: 2 (최근 30일)
may
may 2013년 9월 16일
Given mean1, mean2, sigma1,sigma2, and u, I want to find the following integral:
for example: mean1=0, mean2=0, sigma1=0.2, sigma2=1, and u=0.4
F = @(x,y)normpdf(x, mean1, sigma1).*normpdf(y, mean2, sigma2).*dirac(x*y-u);
answer= integral2(F,-Inf,Inf,-Inf,Inf);
but I get the following error
|Error using integralCalc/finalInputChecks (line 511) Input function must return 'double' or 'single' values. Found 'function_handle'.|
I would appreciate if you could help me to fix my code. Thank you.
  댓글 수: 11
Walter Roberson
Walter Roberson 2013년 9월 17일
If you define a true function you can use conditional logic for it.
function r = F(x)
r = zeros(size(x));
r(x==0) = whatever it should be;
nzx = x(x ~=0);
r(x~=0) = normpdf(nxz, mean1, sigma1).*normpdf(u./nzx, mean2, sigma2).*(1./abs(nzx);
end
may
may 2013년 9월 17일
Nice answer! Thank a lot!

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

답변 (2개)

Sean de Wolski
Sean de Wolski 2013년 9월 16일
Running your example I get an error saying the matrix dimensions must agree. Changing the x*y to x.*y inside of dirac allows it to run and returns an answer of zero
F = @(x,y)normpdf(x, mean1, sigma1).*normpdf(y, mean2, sigma2).*dirac(x.*y-u);
answer= integral2(F,-Inf,Inf,-Inf,Inf);
  댓글 수: 1
may
may 2013년 9월 16일
thank you, but now I get zero for all the inputs i am trying.

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


Walter Roberson
Walter Roberson 2013년 9월 16일
Which dirac are you using? It appears you might be using dirac from the Symbolic toolbox, You should experiment with
class(dirac(5)) %for example
You might need something like double(dirac(x*y-u)) in your code instead of a plan dirac() call.
  댓글 수: 1
Walter Roberson
Walter Roberson 2013년 9월 16일
You should be using symbolic integration instead of numeric integration. symbolic integration is int() in the Symbolic Toolbox

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by