I need to get the histogram of z (i)
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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
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);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Histograms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!