Azzera filtri
Azzera filtri

I need to get the histogram of z (i)

2 visualizzazioni (ultimi 30 giorni)
Melissa Ouafi
Melissa Ouafi il 14 Mar 2022
Modificato: Torsten il 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
  3 Commenti
Melissa Ouafi
Melissa Ouafi il 14 Mar 2022
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 il 14 Mar 2022
Modificato: Torsten il 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')

Accedi per commentare.

Risposte (1)

Image Analyst
Image Analyst il 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);

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by