I need to get the histogram of z (i)
2 次查看(过去 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!