필터 지우기
필터 지우기

I need to get the histogram of z (i)

조회 수: 1 (최근 30일)
Melissa Ouafi
Melissa Ouafi 2022년 3월 14일
편집: Torsten 2022년 3월 14일
n=input('donner le nombre de points ')
k=0;
for i=1:n
u1=rand;
u2=rand;
x=2*u1;
y=2*u2;
if(y<=[(4*x)/(x^2 +1)]-[x*(x-12)])
k=k+1;
end
end
Surface = k/n;
disp('la surface est:')
Surface
for i=1:n
z(i)=k/i;
disp('la valeure de z(i)')
z(i)
end
  댓글 수: 3
Melissa Ouafi
Melissa Ouafi 2022년 3월 14일
I am trying To do the Monte Carlo method To have the area of this function : (4x/x^2+1)-(x(x-12)),z(i) is the area of this function when we change n,So I need to get the histogram of z(i) when n Will be changed.
Torsten
Torsten 2022년 3월 14일
편집: Torsten 2022년 3월 14일
Which area ? The area under the curve y(x) = (4*x)/(x^2 +1)-x*(x-12) in the interval [0;2] ?
Try
f=@(x)4*x./(x.^2 +1)-x.*(x-12);
figure(1)
plot(0:0.01:2,f(0:0.01:2))
n = 2000;
ntrials = 10000;
for j = 1:ntrials
x = 2*rand(n,1);
y = f(2)*rand(n,1);
k(j) = numel(y(y<=f(x)))/n;
end
integral_approx = mean(k)*2*f(2)
figure(2)
histogram(k*2*f(2),'Normalization','pdf')

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

답변 (1개)

Image Analyst
Image Analyst 2022년 3월 14일
z(i) is a single variable so taking a histogram of that will have only one bin. You want a histogram of z, not z(i). So at the end of your code add
histogram(z);

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by