I am generating random numbers with the rejection sampling, like in this picture. How can I see my generated random numbers as points, like in this picture?

 Réponse acceptée

Star Strider
Star Strider le 18 Oct 2020
One approach is to use the inpolygon function:
x = linspace(0, 10);
y = exp(-(x-5).^2)*0.2;
r = rand(1000,2).*[10 0.25];
[in,on] = inpolygon(r(:,1),r(:,2), x, y);
figure
plot(x,y)
hold on
plot(r(:,1), r(:,2),'.r')
plot(r(in,1), r(in,2), '.g')
hold off
producing:
Experiment with it to get the result you want.
.

6 commentaires

Thank you for your respond, I tried. What do I wrong?
clear all;
clf
t0=0.5; T=1; b=3;
for v_T = 1:length(T) % In case I want to see different combinations (T=1:3)
for v_b = 1:length(b)
T_A = T(v_T)
b_A = b(v_b)
a = linspace(0, 10);
pdf2p = @(x) (b_A/T_A).*(x/T_A).^(b_A-1).*(exp(-(x/T_A).^(b_A)));
r = rand(1000,2).*[10 0.25];
[in,on] = inpolygon(r(:,1),r(:,2), a, pdf2p);
h= figure
fplot(pdf2p,[0,10], 'LineWidth',2, "Color", 'b')
hold on
plot(r(:,1), r(:,2),'.r')
plot(r(in,1), r(in,2), '.g')
ax = gca;
ax.XLim=[0,2];
ax.YLim=[0,2];
grid on; %Gitter anzeigen
box on; %Rahmen zeigen
xlabel('Lebensdauer t');
ylabel('Dichtefunktion f(x)');
legend('2 P')
saveas(h,sprintf('T%db%d.png',T_A, b_A)) % I save every combination individually
end
end
Star Strider
Star Strider le 18 Oct 2020
My pleasure!
I tried. What do I wrong?
I have no idea. I cannot run that because I have none of the data. I do not know the results of that code.
It would likely be preferable to use plot rather than fplot for this. I have nothing against using fplot, however it is likely not appropriate here.
Also, saving each plot in each iteration of the inner loop as an image is going to quickly consume disk space.
If you provide the necessary data (specifically ‘T’ and ‘b’), I can probably provide a solution. If they are long vectors, use the save function to put them in a .mat file and attach it to your original Question or to a Comment to it. Otherwise, just post them as text here.
Allright thank you. The loop is not important. But the code doesnt work. Actually I want to change your given function with my given function. So I want to replace:
y = exp(-(x-5).^2)*0.2;
with:
pdf = @(x) (b/T).*(x/T).^(b-1).*(exp(-(x/T).^(b)));
Like this:
clear all
t0=0.5; T=1; b=3;
x = linspace(0, 10);
pdf = (b/T).*(x/T).^(b-1).*(exp(-(x/T).^(b)));
r = rand(1000,2).*[10 0.25];
[in,on] = inpolygon(r(:,1),r(:,2), x, pdf);
figure
plot(x,y)
hold on
plot(r(:,1), r(:,2),'.r')
plot(r(in,1), r(in,2), '.g')
hold off
But this is my graph:
In the picture above, its the same function like mine (pdf).
It appears that the function plots correctly, the only problem being the random dots. (I was going on the data in the plot image you posted.)
This is the reason it is extremely important to post the exact problem you want help with. The example image helps significantly. I will go by it to define my code in the absence of other information.
You will need to adjust the vector elements that multiply the rand matrix. This is defined as:
r = rand(1000,2).*[x_limit y_limit];
so here:
r = rand(1000,2).*[2 1.2];
to create the dots defining their x-values from 0 to 2 and y-values from 0 to 1.2.
Again using my function because I still do not have your parameters:
x = linspace(0, 2);
y = exp(-(x-1).^2*20)*1.15;
r = rand(1000,2).*[2 1.2];
[in,on] = inpolygon(r(:,1),r(:,2), x, y);
figure
plot(x,y)
hold on
plot(r(:,1), r(:,2),'.r')
plot(r(in,1), r(in,2), '.g')
hold off
producing:
Note that the plot x-limits and y-limits have changed as well.
Use your own function, and change the rand multiplication vector as necessary to get the result you want.
.
Mustafa Vural
Mustafa Vural le 18 Oct 2020
I understand now, thank you very much! I appreciate it!
Star Strider
Star Strider le 18 Oct 2020
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by