Está en la página 1de 2

Ejercicio 1.11.

2
Build a MATLAB simulation of 50 trial of the experiment of example 1.27. Your
output should be a pair of 50 x 1 vectors C an H. For the i th trial, Hi will record
whether it was heads (Hi = 1) or tails (Hi = 0), and Ci E {1,2} will record which
coin was picked.
Solution:
Rather than just solve the problem for 50 trials, we can write a function that
generates vectors C and H for an arbitrary number of trial n. The code for this
task is.
Function [C,H] = conjunto(n);
C=ceil(2*rand(n,1));
P= 1 (C/4);
H=(rand(n,1) <P;
The first line produce the nx1 vector C such that C(i) indicates whether coin 1
or coin 2 is chosen for trial I, Next, we generate the vector P such that
P(i)=0.75 if C(i)=1; otherwise, if C(i)=2, then P(i)=0.5. As a result, H(i) is the
simulate result of a coin flip with heads, corresponding to H(i)=1, occurring
with probability P(i).

Construir una simulacin MATLAB de 50 ensayos del experimento del ejemplo


1.27. El resultado debe ser un par de 50 x 1 C un H. vectores Para el ensayo isima, Hi registrar si se trataba de cabezas (Hi = 1) o cruz (Hi = 0), y Ci E
{1,2} se registro que la moneda fue detenido.

solucin:
En lugar de simplemente resolver el problema de 50 ensayos, se puede escribir una funcin
que genera vectores de C y H para un nmero arbitrario de ensayo n. El cdigo para esta
tarea.
Function [C,H] = conjunto(n);
C=ceil(2*rand(n,1));
P= 1 (C/4);
H=(rand(n,1) <P;

La primera lnea producir el vector C nx1 tal que C (i) indica si una moneda o moneda de 2
es elegida para el ensayo I, A continuacin, generar el vector P tal que P (i) = 0,75, si C (i)
= 1; de lo contrario, si C (i) = 2, entonces P (i) = 0,5. Como resultado, H (i) es el resultado
de simular un cara o cruz con la cabeza, lo que corresponde a la H (i) = 1, que ocurre con
probabilidad P (i).

También podría gustarte