a problem while using an anonymous function
古いコメントを表示
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 件のコメント
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.
採用された回答
その他の回答 (1 件)
Jos (10584)
2018 年 10 月 10 日
0 投票
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.
カテゴリ
ヘルプ センター および File Exchange で Poisson Distribution についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!