How do I get random variables generated from a distribution in [-1, 1]?

1 visualizzazione (ultimi 30 giorni)
Hi Everyone, I have h represent the direct problem for the matrix (2N,1) , N=30,
I tried to get noise. It is required to multiply h by (1+0.01*\epsilon), where \epsilon is a random variables generated from a distribution in [-1, 1]. So I write the noise as
h.*(1+0.01*(2*rand(2*N,1)-1));
but i think there is something wrong, i attached the figure that represnt h with noise and without noise.

Risposta accettata

Torsten
Torsten il 28 Mag 2023
Modificato: Torsten il 28 Mag 2023
The curve for the "noisy h" looks wrong although your formula for the "noisy h" is correct.
The noise is
h.*0.01*(2*rand(2*N,1)-1)
, the modified "noisy h" is
h + h.*0.01*(2*rand(2*N,1)-1) = h.*(1+0.01*(2*rand(2*N,1)-1))
  2 Commenti
Torsten
Torsten il 29 Mag 2023
I don't know what your problem is. Maybe it's wrong because h is a row vector instead of a column vector as required ?
N = 30;
x = linspace(0,5,2*N).';
h = 0.1*sqrt(x);
h_noise = h.*(1+0.01*(2*rand(2*N,1)-1));
plot(x,[h,h_noise])
grid on

Accedi per commentare.

Più risposte (1)

John D'Errico
John D'Errico il 28 Mag 2023
The MATLAB crystal ball is foggy today. You give us a tiny fragment of code, and then tell us that something strange happens. PROBABLE USER ERROR.
This fragment generates random numbers on the interval [-1,1].
2*rand(2*N,1)-1
But this fragment generates random numbers on a different interval:
h.*(1+0.01*(2*rand(2*N,1)-1))
What is that interval? Think about what multiplying a uniform random number does. Think about what adding a constant to that uniform number does.
For example, RAND generates numbers randomly and uniformly in the interval (0,1). Then think about what a*RAND does? It will now be uniform and random on the interval (0,a). Do you understand that?
Similarly, what does adding a constant do to rand? That is, a+rand will be uniform on the interval (a,1+a). Now look carefully at those expressions. Start with the first.
2*rand(2*N,1)-1
This maps rand first into the interval (0,2), then by subtracting 1, the result will be uniform on the interval (-1,1).
Now look at what you have. You multiply by 0.1, which maps that (-1,1) interval into (-0.01,0.01). Then you add 1. So now the random numbers are mapped into (0.99,1.01). Finally, you multiply by h. So the second code fragment generates uniform random numbers on the interval (0.99*h,1.01*h).
As for what you did wrong with those numbers, that is between you and your computer, since you have refrained from telling us anything at all.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by