a problem while using an anonymous function

조회 수: 1 (최근 30일)
Eliraz Nahum
Eliraz Nahum 2018년 10월 10일
댓글: Eliraz Nahum 2018년 10월 11일
hello,
I can't understand what's wrong. I will appreciate any help. I have a problem while trying to using the integral function with the handle of an anonymous function. I am adding a photo of the output error.
clear all
close all
clc
lamda=5;
x=[1:1:30];
p=zeros(1,length(x));
poiss_PDF=zeros(1,length(x));
poiss_int=zeros(1,length(x));
step=0;
poiss_pdf=@(x) (1/factorial(x))*(lamda^x)*exp(-lamda);
for x_i=x
step=step+1;
p(step)=poiss_pdf(x_i);
poiss_PDF(step)=poisspdf(x_i,lamda);
poiss_int(step)=integral(poiss_pdf,x(1),x(end));
end
plot(x,p,'b')
title (['Poisson PDF (calculated) with the parameter lamda= ',num2str(lamda)])
xlabel('x')
ylabel ('Distribution')
xlim([x(1) x(end)])
hold on
plot (lamda,poiss_pdf(lamda),'dr')
figure
plot(x,poiss_PDF,'g')
title (['Poisson PDF (from Matlab) with the parameter lamda= ',num2str(lamda)])
xlabel('x')
ylabel ('Distribution')
xlim([x(1) x(end)])
hold on
plot (lamda,poisspdf(lamda,lamda),'dr')
figure
plot(x,poiss_int,'k')
title (['Poisson integral (calculated) with the parameter lamda= ',num2str(lamda)])
xlabel('x')
ylabel ('Probability')
xlim([x(1) x(end)])
hold on
plot (lamda,poiss_pdf(lamda),'dr')
  댓글 수: 2
jonas
jonas 2018년 10월 10일
편집: jonas 2018년 10월 10일
The problem is that factorial only works for integers, so your function is only defined in those points. This is not my field of expertise, but wikipedia tells me you can use a gamma-function instead.
Bruno Luong
Bruno Luong 2018년 10월 10일
Why you are using integral to handle discrete PDF like Poisson? For discrete you need to do a sum.
Or transform the discrete PDF to a sum of weighted dirac, not sur integral can handle though.

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

채택된 답변

the cyclist
the cyclist 2018년 10월 10일
편집: the cyclist 2018년 10월 10일
Replace
poiss_pdf=@(x) (1/factorial(x))*(lamda^x)*exp(-lamda);
with
poiss_pdf=@(x) (1./gamma(x+1)).*(lamda.^x).*exp(-lamda);
and your code will execute to completion. I did not try to figure out if the output is sensible.
Note that I did two things here:
  • Replaced your factorial with the appropriate gamma function
  • Used element-wise operations in the function, which I'm pretty sure is what you intended
  댓글 수: 3
the cyclist
the cyclist 2018년 10월 10일
편집: the cyclist 2018년 10월 10일
Because internally, the integral function creates a vector from x(1) to x(end), and evaluates your function using that vector.
Eliraz Nahum
Eliraz Nahum 2018년 10월 11일
Ok, understood now. Thanks

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

추가 답변 (1개)

Jos (10584)
Jos (10584) 2018년 10월 10일
You do not show the whole error message!
But I also assume that the problem is indeed in the use of factorial inside the function poiss_pdf that is being passed to integral. The function factorial(x) is only defined for integer values of x. You can use gamma(x+1) instead. See the documentation of gamma for more details.

카테고리

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

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by