diff --git a/3ano/1semestre/mpei/aula01/MPEI_2024_2025_Guiao_1.pdf b/3ano/1semestre/mpei/aula01/MPEI_2024_2025_Guiao_1.pdf new file mode 100644 index 0000000..3990d8d Binary files /dev/null and b/3ano/1semestre/mpei/aula01/MPEI_2024_2025_Guiao_1.pdf differ diff --git a/3ano/1semestre/mpei/aula01/ex1.m b/3ano/1semestre/mpei/aula01/ex1.m new file mode 100644 index 0000000..4198422 --- /dev/null +++ b/3ano/1semestre/mpei/aula01/ex1.m @@ -0,0 +1,21 @@ +% Matriz 3x10000 em que cada coluna representa uma experiência com os 3 +% resultados (cada valor está entre 0..1) +experiencias = rand(3,10000); +% Matriz 3x10000 com valores 1 ou 0 dependente se for maior que 0.5 +lancamentos = experiencias > 0.5; +% Vetor 1x10000 com a soma de cada linha (número de resultados obtidos) +resultados = sum(lancamentos,1); +% Vetor 1x10000 com 1 se o resultado for 2 (o pretendido) +sucessos = resultados == 2; +% Determina a % de valores 1 +P = sum(sucessos) / 10000 + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +N = 1e5; % Número de experiências +p = 0.5; % prob de cara +k = 2; % número de caras +n = 3; % número de lançamentos +lancamentos = rand(n,N) > p; +sucessos = sum(lancamentos) == k; +P = sum(sucessos) / N \ No newline at end of file diff --git a/3ano/1semestre/mpei/aula01/ex2.m b/3ano/1semestre/mpei/aula01/ex2.m new file mode 100644 index 0000000..a08b828 --- /dev/null +++ b/3ano/1semestre/mpei/aula01/ex2.m @@ -0,0 +1,7 @@ +N = 1e5; % Número de experiências +p = 0.5; % prob de cara +k = 6; % número de caras +n = 15; % número de lançamentos +lancamentos = rand(n,N) > p; +sucessos = sum(lancamentos) == k; +P = sum(sucessos) / N \ No newline at end of file diff --git a/3ano/1semestre/mpei/aula01/ex3.m b/3ano/1semestre/mpei/aula01/ex3.m new file mode 100644 index 0000000..f289819 --- /dev/null +++ b/3ano/1semestre/mpei/aula01/ex3.m @@ -0,0 +1,7 @@ +N = 1e5; % Número de experiências +p = 0.5; % prob de cara +k = 6; % número de caras +n = 15; % número de lançamentos +lancamentos = rand(n,N) > p; +sucessos = sum(lancamentos) >= k; +P = sum(sucessos) / N \ No newline at end of file diff --git a/3ano/1semestre/mpei/aula01/ex4.m b/3ano/1semestre/mpei/aula01/ex4.m new file mode 100644 index 0000000..7fc61ed --- /dev/null +++ b/3ano/1semestre/mpei/aula01/ex4.m @@ -0,0 +1,22 @@ +function P = ProbCara(nLan, nCara, nExp) + lancamentos = rand(nLan, nExp) > 0.5; + sucessos = sum(lancamentos) == nCara; + P = sum(sucessos) / nExp; +end + +function Calc(n, color) + span = 1:n; + graph = zeros(n, 1); + + for i = 1 : length(span) + graph(i) = ProbCara(n, i, 1e5); + end + + stem(span, graph, color) +end + +hold on +Calc(20, 'red') +Calc(40, 'blue') +Calc(100, 'green') +hold off \ No newline at end of file diff --git a/3ano/1semestre/mpei/aula01/ex_consolidacao_matlab.mlx b/3ano/1semestre/mpei/aula01/ex_consolidacao_matlab.mlx new file mode 100644 index 0000000..6d80f24 Binary files /dev/null and b/3ano/1semestre/mpei/aula01/ex_consolidacao_matlab.mlx differ diff --git a/3ano/1semestre/mpei/aula02/114184_guiao_2.zip b/3ano/1semestre/mpei/aula02/114184_guiao_2.zip new file mode 100644 index 0000000..8126085 Binary files /dev/null and b/3ano/1semestre/mpei/aula02/114184_guiao_2.zip differ diff --git a/3ano/1semestre/mpei/aula02/MPEI_2024_2025_Guiao_2_Probabilidades.pdf b/3ano/1semestre/mpei/aula02/MPEI_2024_2025_Guiao_2_Probabilidades.pdf new file mode 100644 index 0000000..1460695 Binary files /dev/null and b/3ano/1semestre/mpei/aula02/MPEI_2024_2025_Guiao_2_Probabilidades.pdf differ diff --git a/3ano/1semestre/mpei/aula02/part1/CalculateProbEqual.m b/3ano/1semestre/mpei/aula02/part1/CalculateProbEqual.m new file mode 100644 index 0000000..9fa6d1f --- /dev/null +++ b/3ano/1semestre/mpei/aula02/part1/CalculateProbEqual.m @@ -0,0 +1,5 @@ +function P = CalculateProbEqual(partial, total, experiences) + results = rand(total, experiences) > 0.5; + successes = sum(results) == partial; + P = sum(successes) / experiences; +end \ No newline at end of file diff --git a/3ano/1semestre/mpei/aula02/part1/CalculateProbMaxEq.m b/3ano/1semestre/mpei/aula02/part1/CalculateProbMaxEq.m new file mode 100644 index 0000000..5438ce1 --- /dev/null +++ b/3ano/1semestre/mpei/aula02/part1/CalculateProbMaxEq.m @@ -0,0 +1,5 @@ +function P = CalculateProbMaxEq(partial, total, experiences) + results = rand(total, experiences) > 0.5; + successes = sum(results) <= partial; + P = sum(successes) / experiences; +end \ No newline at end of file diff --git a/3ano/1semestre/mpei/aula02/part1/CalculateProbMinEq.m b/3ano/1semestre/mpei/aula02/part1/CalculateProbMinEq.m new file mode 100644 index 0000000..67c2383 --- /dev/null +++ b/3ano/1semestre/mpei/aula02/part1/CalculateProbMinEq.m @@ -0,0 +1,5 @@ +function P = CalculateProbMinEq(partial, total, experiences) + results = rand(total, experiences) > 0.5; + successes = sum(results) >= partial; + P = sum(successes) / experiences; +end \ No newline at end of file diff --git a/3ano/1semestre/mpei/aula02/part1/CalculateProbMinMaxEq.m b/3ano/1semestre/mpei/aula02/part1/CalculateProbMinMaxEq.m new file mode 100644 index 0000000..e9f7eb3 --- /dev/null +++ b/3ano/1semestre/mpei/aula02/part1/CalculateProbMinMaxEq.m @@ -0,0 +1,6 @@ +function P = CalculateProbMinMaxEq(min, max, total, experiences) + results = rand(total, experiences) > 0.5; + successes = sum(results) >= min; + successes = successes <= max; + P = sum(successes) / experiences; +end \ No newline at end of file diff --git a/3ano/1semestre/mpei/aula02/part1/ex1.m b/3ano/1semestre/mpei/aula02/part1/ex1.m new file mode 100644 index 0000000..6d47b86 --- /dev/null +++ b/3ano/1semestre/mpei/aula02/part1/ex1.m @@ -0,0 +1,9 @@ +a = CalculateProbMinEq(1,2,1e5); +d = CalculateProbMinEq(2,2,1e5); +e = CalculateProbEqual(1,4,1e5); +f = CalculateProbMinMaxEq(1,2,4,1e5); + +fprintf("a) %.3f\n", a) +fprintf("d) %.3f\n", d) +fprintf("e) %.3f\n", e) +fprintf("f) %.3f\n", f) \ No newline at end of file diff --git a/3ano/1semestre/mpei/aula02/part1/ex2a.m b/3ano/1semestre/mpei/aula02/part1/ex2a.m new file mode 100644 index 0000000..fcd28b5 --- /dev/null +++ b/3ano/1semestre/mpei/aula02/part1/ex2a.m @@ -0,0 +1,12 @@ +N = 1e5; % Numero de experiencias + +dardos = randi(100,20,N); % Gerar uma matriz 20xN com cada elemento pertencente [1,100] +res = zeros(1, N); % Criar uma matriz linha de 0 + +for i = 1:N + % unique -> retorna 1 se o campo de dardos selecionado apenas for '1' + res(i) = length(unique(dardos(:,i))) == 20; % Verificar para coluna +end + +probSimul = sum(res) / N; % Calcular a probabilidade +fprintf("Prob. Simulação: %.4f\n", probSimul); \ No newline at end of file diff --git a/3ano/1semestre/mpei/aula02/part1/ex2b.m b/3ano/1semestre/mpei/aula02/part1/ex2b.m new file mode 100644 index 0000000..703178f --- /dev/null +++ b/3ano/1semestre/mpei/aula02/part1/ex2b.m @@ -0,0 +1,12 @@ +N = 1e5; % Numero de experiencias + +dardos = randi(100,20,N); % Gerar uma matriz 20xN com cada elemento pertencente [1,100] +res = zeros(1, N); % Criar uma matriz linha de 0 + +for i = 1:N + % unique -> retorna 1 se o campo de dardos selecionado apenas for '1' + res(i) = not(length(unique(dardos(:,i))) == 20); % Verificar para coluna +end + +probSimul = sum(res) / N; % Calcular a probabilidade +fprintf("Prob. Simulação: %.4f\n", probSimul); \ No newline at end of file diff --git a/3ano/1semestre/mpei/aula02/part1/ex2c.m b/3ano/1semestre/mpei/aula02/part1/ex2c.m new file mode 100644 index 0000000..d80a953 --- /dev/null +++ b/3ano/1semestre/mpei/aula02/part1/ex2c.m @@ -0,0 +1,45 @@ +N = 1e5; % Numero de experiencias +dArr = 10:10:100; + + +prob_array = zeros(length(dArr), 1); +for d = 1:length(dArr) + dardos = randi(1000,dArr(d),N); + res = zeros(1,N); + + for i = 1:N + res(i) = not(length(unique(dardos(:,i))) == dArr(d)); + end + + prob = sum(res) / N; + + prob_array(d) = prob; +end + +subplot(2,1,1); +plot(dArr, prob_array, "-o"); +title('m = 1000'); +xlabel('Numero de dardos'); +ylabel("Probabilidade"); +grid on + +prob_array = zeros(length(dArr), 1); +for d = 1:length(dArr) + dardos = randi(100000,dArr(d),N); + res = zeros(1,N); + + for i = 1:N + res(i) = not(length(unique(dardos(:,i))) == dArr(d)); + end + + prob = sum(res) / N; + + prob_array(d) = prob; +end + +subplot(2,1,2); +plot(dArr, prob_array, "-o"); +title('m = 100000'); +xlabel('Numero de dardos'); +ylabel("Probabilidade"); +grid on \ No newline at end of file diff --git a/3ano/1semestre/mpei/aula02/part1/ex2d.m b/3ano/1semestre/mpei/aula02/part1/ex2d.m new file mode 100644 index 0000000..12199e2 --- /dev/null +++ b/3ano/1semestre/mpei/aula02/part1/ex2d.m @@ -0,0 +1,24 @@ +N = 1e5; % Numero de experiencias +m = [200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000]; + +prob_array = zeros(1, length(m)); +for i = 1:length(m) + m_value = m(i); + dardos = randi(m_value,100,N); + res = zeros(1,N); + + for j = 1:N + res(j) = not(length(unique(dardos(:,j))) == 100); + end + + prob = sum(res) / N; + + prob_array(i) = prob; +end + +figure; +plot(m, prob_array, "-o"); +title('Muitos graficos'); +xlabel('Numero de alvos'); +ylabel("Probabilidade"); +grid on \ No newline at end of file diff --git a/3ano/1semestre/mpei/aula02/part1/ex3ab.m b/3ano/1semestre/mpei/aula02/part1/ex3ab.m new file mode 100644 index 0000000..cdc9f84 --- /dev/null +++ b/3ano/1semestre/mpei/aula02/part1/ex3ab.m @@ -0,0 +1,26 @@ +N = 500; +T = 1000; +n_keys = 10; +prob_array = zeros(1,n_keys); + +for n = 1:n_keys + prob = 0; + for i = 1:N + hash_exp = randi([0,T-1],n,T); + colisions = 0; + for j = 1:T + if n > length(unique(hash_exp(:,j))) + colisions = colisions + 1; + end + end + prob = prob + colisions / T; + end + prob_array(n) = prob / N; +end + +fprintf("a) = %.4f\n", prob_array(10)); + +plot(1:n_keys, prob_array, "-o") +title("Ex3 b)"); +xlabel("Numero de keys"); +ylabel("Prob. de colisao"); \ No newline at end of file diff --git a/3ano/1semestre/mpei/aula02/part1/ex3c.m b/3ano/1semestre/mpei/aula02/part1/ex3c.m new file mode 100644 index 0000000..b0521e3 --- /dev/null +++ b/3ano/1semestre/mpei/aula02/part1/ex3c.m @@ -0,0 +1,25 @@ +N = 50; +T = 100:100:1000; +n_keys = 50; +prob_array = zeros(1,length(T)); + +for i = 1:length(T) + t = T(i); + prob = 0; + for n = 1:N + hash_exp = randi([1,t-1],n_keys,t); + colisions = 0; + for j = 1:t + if n_keys > length(unique(hash_exp(:,j))) + colisions = colisions + 1; + end + end + prob = prob + colisions / t; + end + prob_array(i) = prob / N; +end + +plot(T, prob_array, "-o") +title("Ex3 c)"); +xlabel("Tamanho do array"); +ylabel("Prob. de colisao"); \ No newline at end of file diff --git a/3ano/1semestre/mpei/aula02/part1/ex4.m b/3ano/1semestre/mpei/aula02/part1/ex4.m new file mode 100644 index 0000000..ea1e07c --- /dev/null +++ b/3ano/1semestre/mpei/aula02/part1/ex4.m @@ -0,0 +1,17 @@ +function n = Calc(p) + N = 1e5; + for pessoas = 2 : 366 + a = randi(365, pessoas, N); + for i = 1:N + res(i) = length(unique(a(:, i))) < pessoas; + end + prob = sum(res)/N; + if (prob > p) + n = pessoas; + break + end + end +end + +fprintf("a) = %.4f\n", Calc(0.5)) +fprintf("b) = %.4f\n", Calc(0.9)) diff --git a/3ano/1semestre/mpei/aula02/part2/ex1.m b/3ano/1semestre/mpei/aula02/part2/ex1.m new file mode 100644 index 0000000..26b5e46 --- /dev/null +++ b/3ano/1semestre/mpei/aula02/part2/ex1.m @@ -0,0 +1,46 @@ +N = 1e5; +p_paridade = 0.5; +lancamentos = 2; +num_faces = 6; +p_serX = 1/6; + +rolls = randi(num_faces, lancamentos, N); + +soma9 = 0; +soma = sum(rolls); +for i = 1 : N + if soma(i) == 9 + soma9 = soma9 + 1; + end +end +p_a = soma9/N; + +soma_par = 0; +roll2 = rolls(2 : N+1); +for i = 1 : N + if mod(roll2(i), 2) == 0 + soma_par = soma_par + 1; + end +end +p_b = soma_par/N; + +ser5 = 0; +for i = 1 : N + if rolls(1, i) == 5 || rolls(2, i) == 5 + ser5 = ser5 + 1; + end +end +p_c = ser5/N; + +diff1 = 0; +for i = 1 : N + if rolls(1, i) ~= 1 && rolls(2, i) ~= 1 + diff1 = diff1 + 1; + end +end +p_d = diff1/N; + +fprintf("P(A) = %.4f\n", p_a) +fprintf("P(B) = %.4f\n", p_b) +fprintf("P(C) = %.4f\n", p_c) +fprintf("P(D) = %.4f\n", p_d) \ No newline at end of file