# I need to get the histogram of z (i)

1 view (last 30 days)
Melissa Ouafi on 14 Mar 2022
Edited: Torsten on 14 Mar 2022
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
Torsten on 14 Mar 2022
Edited: Torsten on 14 Mar 2022
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')

Image Analyst on 14 Mar 2022
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);

R2021a

### Community Treasure Hunt

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

Start Hunting!