http://fiscomp.if.ufrgs.br/index.php?title=M%C3%A9todo_de_Monte_Carlo_e_transforma%C3%A7%C3%B5es&feed=atom&action=history
Método de Monte Carlo e transformações - Histórico de revisão
2024-03-29T09:41:29Z
Histórico de revisões para esta página neste wiki
MediaWiki 1.39.4
http://fiscomp.if.ufrgs.br/index.php?title=M%C3%A9todo_de_Monte_Carlo_e_transforma%C3%A7%C3%B5es&diff=7351&oldid=prev
Jhordan em 20h09min de 29 de abril de 2022
2022-04-29T20:09:01Z
<p></p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="pt-BR">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Edição anterior</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Edição das 17h09min de 29 de abril de 2022</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l77">Linha 77:</td>
<td colspan="2" class="diff-lineno">Linha 77:</td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><pre></div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><pre></div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>#Cálculo da área de um círculo</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>#Cálculo da área de um círculo</div></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>import matplotlib.pyplot as plt #Plotar gráfico</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>import matplotlib.pyplot as plt <ins style="font-weight: bold; text-decoration: none;"> </ins>#Plotar gráfico</div></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>import numpy as np <del style="font-weight: bold; text-decoration: none;"> </del>#Funções matemáticas</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>import numpy as np <ins style="font-weight: bold; text-decoration: none;"> </ins>#Funções matemáticas</div></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>import random <del style="font-weight: bold; text-decoration: none;"> </del>#Números aleatórios</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>import random <ins style="font-weight: bold; text-decoration: none;"> </ins>#Números aleatórios</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>r=1 #Raio do círculo</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>r=1 <ins style="font-weight: bold; text-decoration: none;"> </ins>#Raio do círculo</div></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>Np=1000000 #Número de pontos</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>Np=1000000 <ins style="font-weight: bold; text-decoration: none;"> </ins>#Número de pontos</div></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>hitx=[];hity=[];missx=[];missy=[] #<del style="font-weight: bold; text-decoration: none;">Resultados</del></div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>hitx=[];hity=[];missx=[];missy=[] #<ins style="font-weight: bold; text-decoration: none;">Gurdar resultados</ins></div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>for n in range(Np):</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>for n in range(Np):</div></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> x=random.random()*2-1 <del style="font-weight: bold; text-decoration: none;"> </del>#Pontos aleatórios gerados entre -1 e +1 </div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> x=random.random()*2-1 <ins style="font-weight: bold; text-decoration: none;"> </ins>#Pontos aleatórios gerados entre -1 e +1 </div></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> y=random.random()*2-1 <del style="font-weight: bold; text-decoration: none;"> </del>#Pontos aleatórios gerados entre -1 e +1</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> y=random.random()*2-1 <ins style="font-weight: bold; text-decoration: none;"> </ins>#Pontos aleatórios gerados entre -1 e +1</div></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> if(np.sqrt(x**2+y**2)<=r): <del style="font-weight: bold; text-decoration: none;"> </del>#Se o ponto caiu dentro do círculo</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> if(np.sqrt(x**2+y**2)<=r): <ins style="font-weight: bold; text-decoration: none;"> </ins>#Se o ponto caiu dentro do círculo</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> hitx.append(x);hity.append(y)</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> hitx.append(x);hity.append(y)</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> else:</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> else:</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> missx.append(x);missy.append(y)</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> missx.append(x);missy.append(y)</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>#Plotar <del style="font-weight: bold; text-decoration: none;">curvas</del></div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>#Plotar <ins style="font-weight: bold; text-decoration: none;">o círculo</ins></div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>x=np.arange(-1,1,1E-5)</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>x=np.arange(-1,1,1E-5)</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>plt.plot(x,-np.sqrt(r-x**2),'-k')</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>plt.plot(x,-np.sqrt(r-x**2),'-k')</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l100">Linha 100:</td>
<td colspan="2" class="diff-lineno">Linha 100:</td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>plt.plot(missx,missy,'ob')</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>plt.plot(missx,missy,'ob')</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>a_ret=2*2 #Área do retângulo</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">#Cálcular a área</ins></div></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>a_cir=a_ret*len(hitx)/Np <del style="font-weight: bold; text-decoration: none;"> </del>#A_ret *(hit/N)</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>a_ret=2*2 <ins style="font-weight: bold; text-decoration: none;"> </ins>#Área do retângulo<ins style="font-weight: bold; text-decoration: none;">: geramos pontos entr -1 e +1 na duas dimensões, então é um quadrado de lado 2</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>a_cir=a_ret*len(hitx)/Np <ins style="font-weight: bold; text-decoration: none;"> </ins>#A_ret *(hit/N)</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>print('A área do círculo estimada é: {:.2f}'.format(a_cir))</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>print('A área do círculo estimada é: {:.2f}'.format(a_cir))</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>print('A área do círculo exata é: {:.2f}'.format(np.pi*r**2))</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>print('A área do círculo exata é: {:.2f}'.format(np.pi*r**2))</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></pre></div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></pre></div></td></tr>
</table>
Jhordan
http://fiscomp.if.ufrgs.br/index.php?title=M%C3%A9todo_de_Monte_Carlo_e_transforma%C3%A7%C3%B5es&diff=7350&oldid=prev
Jhordan em 19h56min de 29 de abril de 2022
2022-04-29T19:56:09Z
<p></p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="pt-BR">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Edição anterior</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Edição das 16h56min de 29 de abril de 2022</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l76">Linha 76:</td>
<td colspan="2" class="diff-lineno">Linha 76:</td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><pre></div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><pre></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">#Cálculo da área de um círculo</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">import matplotlib.pyplot as plt #Plotar gráfico</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">import numpy as np #Funções matemáticas</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">import random #Números aleatórios</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">r=1 #Raio do círculo</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">Np=1000000 #Número de pontos</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">hitx=[];hity=[];missx=[];missy=[] #Resultados</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">for n in range(Np):</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"> x=random.random()*2-1 #Pontos aleatórios gerados entre -1 e +1 </ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"> y=random.random()*2-1 #Pontos aleatórios gerados entre -1 e +1</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"> if(np.sqrt(x**2+y**2)<=r): #Se o ponto caiu dentro do círculo</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"> hitx.append(x);hity.append(y)</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"> else:</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"> missx.append(x);missy.append(y)</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">#Plotar curvas</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">x=np.arange(-1,1,1E-5)</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">plt.plot(x,-np.sqrt(r-x**2),'-k')</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">plt.plot(x,np.sqrt(r-x**2),'-k')</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">#Plotar pontos</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">plt.plot(hitx,hity,'or')</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">plt.plot(missx,missy,'ob')</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">a_ret=2*2 #Área do retângulo</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">a_cir=a_ret*len(hitx)/Np #A_ret *(hit/N)</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">print('A área do círculo estimada é: {:.2f}'.format(a_cir))</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">print('A área do círculo exata é: {:.2f}'.format(np.pi*r**2))</ins></div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></pre></div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></pre></div></td></tr>
</table>
Jhordan
http://fiscomp.if.ufrgs.br/index.php?title=M%C3%A9todo_de_Monte_Carlo_e_transforma%C3%A7%C3%B5es&diff=7349&oldid=prev
Jhordan: Criou página com '<span id="transformação-linear"></span> == Transformação linear == Sorteando um número aleatório <math display="inline">x\in\left[0,1\right)</math> então fazemos uma t...'
2022-04-29T19:42:50Z
<p>Criou página com '<span id="transformação-linear"></span> == Transformação linear == Sorteando um número aleatório <math display="inline">x\in\left[0,1\right)</math> então fazemos uma t...'</p>
<p><b>Página nova</b></p><div><span id="transformação-linear"></span><br />
== Transformação linear ==<br />
<br />
Sorteando um número aleatório <math display="inline">x\in\left[0,1\right)</math> então fazemos uma transformação para obter um número <math display="inline">y\in\left[ a,b\right)</math>. Isto é, obtemos a seguinte transformação <math display="inline">f:\left[ 0,1\right)\rightarrow\left[ a,b\right)</math> da seguinte forma:<br />
<br />
<math display="block">y\left(x\right)=a+\left(b-a\right)x</math><br />
<br />
Se todos os números entre <math display="inline">0</math> e <math display="inline">1</math> tinham igual probabilidade de serem sorteados, após a transformação todos os números entre <math display="inline">a</math> e <math display="inline">b</math> também possuem igual probabilidade, pois <math display="inline">y</math> varia com <math display="inline">x</math> de forma linear, isto é, a distribuição uniforme de números é mantida.Por sua vez, a distribuição uniforme significa que a probabiliade de obter um número entre <math display="inline">x</math> e <math display="inline">x+dx</math> é dada pela função densidade de probabilidade da seguinte forma:<br />
<br />
<math display="block">p\left(x\right)dx=\begin{cases}<br />
dx, & 0<x<1\\<br />
0, & \text{outra forma}<br />
\end{cases}</math><br />
<br />
Sendo <math display="inline">p\left(x\right)=p</math> constante, temos que: <math display="block">\int_{-\infty}^{+\infty}dx=p\int_{0}^{1}dx=p=1</math><br />
<br />
Pela normalização. Mas se ampliarmos o intervalo dos números possíveis para entre <math display="inline">a</math> e <math display="inline">b</math>:<br />
<br />
<math display="block">\int_{-\infty}^{+\infty}p'dx=\int_{a}^{b}p'dx=p'\int_{a}^{b}dx=p'\left(b-a\right)=1</math><br />
<br />
Então agora <math display="inline">p\left(y\right)=p'=1/\left(b-a\right)</math>. Isto é distribuição de probabilidade continua constante, mas com uma menor probabilidade de sortear um número <math display="inline">y</math> qualquer, quando em comparação de sortear um <math display="inline">x</math> qualquer.<br />
<br />
<span id="transformação-não-linear"></span><br />
== Transformação não-linear ==<br />
<br />
O mesmo não ocorre com uma transformação não linear. Por exemplo se <math display="inline">y\left(x\right)=4x^{2}</math>, derivando temos que:<br />
<br />
<math display="block">\frac{dy}{dx}=8x\quad\longrightarrow\quad dy=8xdx</math><br />
<br />
Diferente do caso anterior que tínhamos apenas a transformação linear <math display="inline">dy=\left(b-a\right)dx</math>. Podemos ver ainda usando a própria definição de derivada:<br />
<br />
<math display="block">\begin{align}<br />
y\left(x+dx\right)-y\left(x\right) & =4\left(x+dx\right)^{2}-4x^{2}\\<br />
& =4x^{2}+4xdx+dx^{2}-4x^{2}\\<br />
& =4\cdot2xdx+dx^{2}\end{align}</math><br />
<br />
E sendo <math display="inline">dx</math> um diferencial então <math display="inline">dx^{2}\approx0</math>, logo <math display="inline">dy=8xdx</math>. Agora a distribuição de probabilidade é alterada com a transformação. Para uma transformação <math display="inline">y\left(x\right)</math> qualquer, como a probabilidade se conserva, ainda temos:<br />
<br />
<math display="block">\left|p\left(y\right)dy\right|=\left|p\left(x\right)dx\right|</math><br />
<br />
Considrando que <math display="inline">y=y\left(x\right)</math> tem inversa, então <math display="inline">x=x\left(y\right)</math> então:<br />
<br />
<math display="block">\begin{align}<br />
\left|p\left(y\right)dy\right|= & \left|p\left(x\right)dx\right|\\<br />
p\left(y\right)\left|dy\right|= & p\left(x\right)\left|dx\right|\\<br />
p\left(y\right)= & p\left(x\right)\left|\frac{dx}{dy}\right|\end{align}</math><br />
<br />
Sendo <math display="inline">y\left(x\right)=-\ln\left(x\right)</math> então reescrevendo <math display="inline">x=e^{-y}</math>, logo <math display="inline">\frac{dx}{dy}=-e^{y}</math> Então temos:<br />
<br />
<math display="block">p\left(y\right)=p\left(x\right)e^{-y}</math><br />
<br />
E sendo nossa ditribuição em <math display="inline">x</math> uniforme com <math display="inline">p\left(x\right)=p=1</math> como vimos anteriormente, ficamos com:<br />
<br />
<math display="block">p\left(y\right)=e^{-y}</math><br />
<br />
Para a transformação <math display="inline">y=-\ln\left(x\right)</math>. O mais comum é que saibamos a distribuição de probabilidade <math display="inline">p\left(y\right)</math> que queremos, e uma vez que:<br />
<br />
<math display="block">p\left(y\right)=\left|\frac{dx}{dy}\right|</math><br />
<br />
E integramos então para encontrar <math display="inline">x\left(y\right)</math>.<br />
<br />
<span id="método-da-rejeição"></span><br />
== Método da rejeição ==<br />
<br />
Nem sempre o <math display="inline">p\left(y\right)</math> desejado é fácil de definir matematicamente. O método da rejeição é um método rústico para obtermos <math display="inline">p\left(y\right)</math>.<br />
<br />
* Desenhar a função <math display="inline">p\left(y\right)</math> desejada dentro dos limites <math display="inline">a\leq y<b</math> e <math display="inline">0\leq p\left(y\right)\leq p_{max}</math>.<br />
* Geramos um ponto aleatório <math display="inline">\left(y,p\left(y\right)\right)</math>, se estiver abaixo da curva desejada é aceito.<br />
<br />
Se gerarmos pontos aleatórios em grande quantidade, a razão de pontos aceitos para cada <math display="inline">y</math> em relação à todos o pontos aleatórios gerados neste <math display="inline">y</math>, nos dá uma estimativa de <math display="inline">p\left(y\right)</math> neste ponto, em relação do <math display="inline">p_{max}</math>. Isto é, se para um <math display="inline">y</math> qualquer, metade dos pontos gerado aleatoriamente foram válidos, então <math display="inline">p\left(y\right)\approx\frac{p_{max}}{2}</math>.<br />
<br />
<span id="cálulo-de-integrais-definidas"></span><br />
== Cálulo de integrais definidas ==<br />
<br />
Em uma ideia bastante análoga à anterior, aqui utilizamos a ideia que a integral definida é cálculo da área sob a curva. Definindo novamente limites <math display="inline">x_{min}\leq x\leq x_{max}</math> e <math display="inline">y_{min}\leq y\leq y_{max}</math> no qual a região que queremos calcular está contida, geramos uma série de pontos aleatórios, se gerado pontos suficientes, a razão de pontos que foram gerados dentro da área que queremos calcular em relação ao total de pontos gerados, será a mesma razão da área que queremos calcular em relação à área total definida pelos limites.<br />
<br />
<pre><br />
</pre></div>
Jhordan