dih-jupyter/5. Jupyter - całkowanie numeryczne.ipynb

1746 lines
313 KiB
Plaintext
Raw Normal View History

2021-02-18 01:06:06 +01:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pola figur: całkowanie analityczne i metodą Monte Carlo\n",
"\n",
"W jaki sposób można obliczyć pole figury o zadanym kształcie, który jest trudny do analitycznego opisu?\n",
"W tym pliku przedyskutujemy sposób obliczania pól figur oparty na metodach losowych z wykorzystaniem prostych spacerów losowych. Porównamy wyniki z obliczeniami analitycznymi na konkretnym przykładzie.\n",
"\n",
"<div class=\"alert alert-block alert-info\"> \n",
"Aby wzbogacić nasz repertuar matematyczny wykorzystamy w tym pliku funkcjonalność SageMath - obszernej biblioteki, która rozwija metody symboliczne Pythona. Do uruchomienia tego pliku będziesz potrzebować jądra \"SageMath\" w wersji co najmniej 8.9. Bez instalacji ten plik można rekompilować np. w przeglądarkowej wersji SageMath pod adresem cocalc.com.</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Problem</h2>\n",
"<div class=\"alert alert-block alert-success\"> \n",
"Oblicz z dokładnością $0.01$ pole wewnątrz następującej figury.</div>"
]
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAegAAAEFCAYAAAAoinRQAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi40LCBodHRwOi8vbWF0cGxvdGxpYi5vcmcv7US4rQAAIABJREFUeJzt3Xlc1NX+P/DXsCOy5AaiuOae1xVxX7Ioy7VMy3IrK3O5l8xK85aaa1leK7FvesvlambXLdOuRaVokeUC7qgpCi5IqIG4DALn98f7NxACyjLD+czM6/l4zGOYYZh5f5iZz/tzzuec9zEppRSIiIjIUFx0B0BEREQFMUETEREZEBM0ERGRATFBExERGRATNBERkQExQRMRERkQEzQREZEBMUETEREZEBM0ERGRATFBExERGRATNBERkQG56Q6gtHJycnD+/Hn4+vrCZDLpDoeIiKhQSilcvXoVwcHBcHEpQbtYaRIdHa169+6tqlevrgCoDRs2lOjvk5KSFABeeOGFF154sYtLUlJSifKcthb0tWvX0KJFC4wcORKPP/54if/e19cXAJCUlAQ/Pz9rh0dERGQV6enpCAkJyc1bxaUtQffq1Qu9evUq9d9burX9/PyYoImIyPBKejrWbs5Bm81mmM3m3Nvp6ekaoyEiIrItuxnFPWfOHPj7++deQkJCdIdERERkM3aToCdPnoy0tLTcS1JSku6QiIiIbMZuurg9PT3h6empOwwiIqJyYTctaCIiImeirQWdkZGB33//Pfd2QkIC4uLiUKlSJdSqVUtXWERERIagLUHv2bMHPXr0yL09YcIEAMDw4cOxbNkyTVEREREZg7YE3b17dyildL08ERGRofEcNBERkQExQRMRERkQEzQREZEBMUETEREZkN0l6MjISDRt2hShoaG6QyEiIrIZk7LTodTp6enw9/dHWloaV7MiIiLDKm2+srsWNBERkTNggiYiIjIgJmgiIiIDYoImIiIyICZoIiIiA2KCJiIiMiAmaCIiIgNigiYiIjIgu0vQrCRGRETOgJXEiIiIbIiVxIiIiBwIEzQREZEBMUETEREZEBM0ERGRATFBExERGRATNBERkQExQRMRERkQEzQREZEB2V2CZiUxIiJyBqwkRkREZEOsJEZERORAmKCJiIgMiAmaiIjIgJigiYiIDIgJmoiIyICYoImIiAyICZqIiMiAmKCJiIgMyO4SNCuJERGRM2AlMSIiIhtiJTEiIiIHwgRNRERkQEzQREREBsQETUREZEBM0ERERAZktQS9aNEi1K1bF15eXmjTpg127txZ5GOXLVsGk8lU4HLz5k1rhUNERGTXrJKg16xZg4iICEyZMgWxsbHo0qULevXqhcTExCL/xs/PDxcuXMh38fLyskY4REREds/NGk8yf/58PPfccxg1ahQAYMGCBfj222/x8ccfY86cOYX+jclkQlBQkDVenhyIUsDVq8Dly3K5ehW4eVMuSgEuLoCrK1ChAlCxIuDnB1StCgQEyO+IjOL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CDeV/oTp044EQp2fl//738Pyz/mwoVlHrjDaUuX9YdHW3frlTHjvkHfU2erFRKiu7I9MvKUmrBAvm8Wj63ixc7R0+CTk6ToBcuXKiaNGmiGjZsyARtQ19/rVTlynnduR99xC6x2+XkKPXdd0q1a5e/+3D+fGltU/k6ejR/V66Xl1ITJzIxFyYhQakePfL+VwMG8ODSlpwmQVuwBW0bOTky5cjyxW3VSnZ8VDTLec5mzfL+b40bK7V1q+7InMOffyr18ssyEtsyInvMGOkBoqJlZ8s4End3+b81aMDvuq2UNl9xCQHKde0aMHgw8OabcnvsWKlc1Lix3riMzmSSoib79wNLlsho4Ph
"text/plain": [
"Graphics object consisting of 1 graphics primitive"
]
},
"execution_count": 129,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x,y=var('x,y')\n",
"implicit_plot((x**2+y**2)**2-2*(x**2-y**2),(x,-2,2),(y,-2,2),figsize=[5,5],ymax=1,ymin=-1,xmin=-2,xmax=2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Obliczenia analityczne\n",
"\n",
"W naszym pierwszym podejściu obliczymy pole wewnątrz \"ósemki\" w sposób dokładny (analityczny). W tym celu musimy rozwiazać uwikłane równanie opisujące krzywą."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Rozwiązanie równania\n",
"Najpierw \"rozwiązujemy\" nasze równanie uwikłane.\n",
"\n",
"$$(x^2+y^2)^2=2(x^2-y^2)$$"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"solv=solve((x**2+y**2)**2-2*(x**2-y**2)==0,(x,y))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\left[x = r_{1}, y = \\sqrt{-r_{1}^{2} - \\sqrt{4 \\, r_{1}^{2} + 1} - 1}\\right], \\left[x = r_{2}, y = -\\sqrt{-r_{2}^{2} - \\sqrt{4 \\, r_{2}^{2} + 1} - 1}\\right], \\left[x = r_{3}, y = \\sqrt{-r_{3}^{2} + \\sqrt{4 \\, r_{3}^{2} + 1} - 1}\\right], \\left[x = r_{4}, y = -\\sqrt{-r_{4}^{2} + \\sqrt{4 \\, r_{4}^{2} + 1} - 1}\\right]\\right]</script></html>"
],
"text/plain": [
"[[x == r1, y == sqrt(-r1^2 - sqrt(4*r1^2 + 1) - 1)], [x == r2, y == -sqrt(-r2^2 - sqrt(4*r2^2 + 1) - 1)], [x == r3, y == sqrt(-r3^2 + sqrt(4*r3^2 + 1) - 1)], [x == r4, y == -sqrt(-r4^2 + sqrt(4*r4^2 + 1) - 1)]]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pretty_print(solv)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-success\"> \n",
"<p>Powyższy obiekt przechowuje wszystke \"lokalne\" rozwiązania naszego równania. Wykorzystamy je do napisania równań funkcji, które kawałkami opisują kształt naszej \"ósemki\" (precyzyjnie: lemniskaty Bernoulliego).</p>\n",
"\n",
"<p><b>UWAGA:</b> dwie spośród podanych wyżej funkcji rozwiązań nie opisują rzeczywistych części wykresu - ignorujemy je.</p></div>"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"y1=solv[2][1].rhs() #wybieramy prawą stronę trzeciego równania\n",
"r=y1.variables()[0] #odnosimy się do jego wewnętrznej zmiennej symbolicznej\n",
"f=y1.subs({r:x}) #i podstawiamy ustaloną przez nas od początku zmienną x\n",
"\n",
"#sprawdzamy równość podstawienia\n",
"assert bool(f==sqrt(-x^2 + sqrt(4*x^2 + 1) - 1))\n",
"\n",
"#to będzie nasza \"rozwiązana\" funkcja\n",
"funkcja=sqrt(-x^2 + sqrt(4*x^2 + 1) - 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### W jakim prostokącie mieści się wykres lemniskaty?\n",
"\n",
"Obliczmy teraz formalnie liczby $a$ i $b$ takie, że zbiór punktów\n",
"\n",
"$$L=\\{(x,y)\\in\\mathbb{R}^2: (x^2+y^2)^2=2(x^2-y^2)\\}$$\n",
"spełnia\n",
"\n",
"$$\\max_{(x,y)\\in L} x = - \\min_{(x,y)\\in L} x = a$$\n",
"\n",
"oraz \n",
"\n",
"$$\\max_{(x,y)\\in L} y = - \\min_{(x,y)\\in L} y = b$$\n",
"\n",
"Wartość parametru $a$ przy nam się przy obliczaniu pola (całki) metodą analityczną. Parametr $b$ będzie nam potrzebny również przy obliczaniu pola numerycznie metodą Monte Carlo."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Liczbę $a$ wyznaczamy określając dziedzinę funkcji $f(x) = \\sqrt{\\sqrt{4x^2+1}-x^2-1}$"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(x^2 + 1)^2 - 4*x^2 - 1 == 0"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#UWAGA: w SageMath można używać zarówno ** jak i ^ jako symbolu potęgowania (w zwykłym Python symbol ^ oznacza\n",
"#operację XOR; w SageMath ta operacja jest oznaczona jako ^^)\n",
"eq1=0==-x^2 + sqrt(4*x^2 + 1) - 1 #formułujemy zerowanie argumenty pod zewnętrznym pierwiastkiem\n",
"\n",
"#wykonujemy przekształcenia na równaniu eq1, aby doprowadzić je do wygodnej wielomianowej postaci\n",
"eq1=eq1.add_to_both_sides(x^2+1)\n",
"eq1=eq1*eq1 #podniesienie obu stron do kwadratu\n",
"eq1=eq1.add_to_both_sides(-(4*x^2+1))\n",
"eq1"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[x == -sqrt(2), x == sqrt(2), x == 0]"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve(eq1,x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Zatem nasz parametr $a$ wynosi $\\sqrt{2}$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Liczbę $b$ obliczymy wyznaczając maksimum osiągane przez funkcję $f(x)$ na przedziale $(0,\\sqrt{2})$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-success\"> \n",
"Obliczamy pochodną funkcji $f$ i zauważamy, że mianownik jest dodatni. Do określenia przebiegu funkcji użyjemy informacji o znaku licznika.</div>"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"pochodnaf=funkcja.derivative(x).factor()\n",
"licznik=pochodnaf.numerator()\n",
"mianownik=pochodnaf.denominator()"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}-x {\\left(\\sqrt{4 \\, x^{2} + 1} - 2\\right)}</script></html>"
],
"text/plain": [
"-x*(sqrt(4*x^2 + 1) - 2)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#licznik\n",
"pretty_print(licznik)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\sqrt{4 \\, x^{2} + 1} \\sqrt{-x^{2} + \\sqrt{4 \\, x^{2} + 1} - 1}</script></html>"
],
"text/plain": [
"sqrt(4*x^2 + 1)*sqrt(-x^2 + sqrt(4*x^2 + 1) - 1)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#mianownik\n",
"pretty_print(mianownik)"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAASEAAAEfCAYAAAAObXDGAAAABHNC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fLTt+PEWDpBY1KRJwFtvAX/5C9DBpr/EitlUF/2FC0BUFB8NvXs3H21LbF9jI//F8/PPQGkp9XxKjeRqQu05eRIYMoQvmH7oECUge+LkxF/Jmpt5O2Bjo9gREUPYRBI6cgQIC+O7YBw6xJeCIPbFx4cPYCwu5vMAiXRIPgkVFvLFr4KC+EJktBKf/Ro2jG/LtHIlHxFPpEHybUKOjmqMGdMFW7fy8SPEvjHGxw4dOcK3mqZfStZPErPoGWO4fv06tFqt7lxGBv/zCy/UYP163hZEG+gRgHfXDx4MTJ/Oh2g4SL6+Lx0KhUKv7d/vJoma0G+1HkKIdTNm4wlJJKG2akKHDv2C2NgBOHXqFHx9fdu876mnnsLx48cN/tr9vl5TUwM/Pz9UV1e3+wMX8nxrLl9K33tiIpCZyZeJ7d/f8uUb83xr/XvXt3xjakKSeB2TyWTocc9ueKGh/L8KhaLdH4ijo6NRX9Pn6wDQpUsXsz3f2suXwve+Zg3vLX31VeCHH/63dK81/+yt/e/9fuUbw6bfluPj4436mj5fF1K2rZdvLd+7XM7XDT9/no+qtnT5xnxdyn/vRrP8ztOmUV1dzQCw6upqi5etVqsZAKZWqy1ettjlS/F7z8hgDGDs66/FKd9UpPiz14djcnJysuVTn3BarRarVq1CYmIiOouwQJCjoyOGDx8OJydx3mjFLF9q3/vAgfy17JNP+GRmFxfLlm9KUvvZ60MSDdNtMdc20MQ2XbgA/O53wOTJvLGaWA+bbhMi5Df+/sD
"text/plain": [
"Graphics object consisting of 1 graphics primitive"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#wykres licznika pochodnej\n",
"plot(licznik,(x,0,sqrt(2)),figsize=[3,3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-success\"> \n",
"Z postaci funkcji $-x(\\sqrt{4x^2+1}-2)$ wynika, że jest dodatnia dla $x<c$ i ujemna dla $x>c$, gdzie $c$ jest jedynym miejscem zerowym tej funkcji. Obliczmy zatem $c$.</div>"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\n",
"x == 1/2*sqrt(3)\n",
"]\n"
]
}
],
"source": [
"with assuming(x>0):\n",
" print(solve(licznik==0,x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Czyli funkcja $f(x)$ jest rosnąca na przedziale $\\left(0,\\frac{\\sqrt{3}}{2}\\right)$ i malejąca na przedziale $\\left(\\frac{\\sqrt{3}}{2},\\sqrt{2}\\right)$. \n",
"\n",
"<div class=\"alert alert-block alert-success\"> \n",
"W punkcie $x=\\frac{\\sqrt{3}}{2}$ funkcja $f(x)$ osiąga maksimum równe $b=1/2$.</div>"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"1/2"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"funkcja.subs({x:sqrt(3)/2})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Wizualizacja pudełka ograniczającego kształt lemniskaty"
]
},
{
"cell_type": "code",
"execution_count": 231,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAkkAAADmCAYAAADBVREmAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi40LCBodHRwOi8vbWF0cGxvdGxpYi5vcmcv7US4rQAAIABJREFUeJzs3Xd4k2UXwOFf0rRN9y7IkI3IEARkyh5lI1OmooCoIDgQRVFxoKIiIAKi4ECGUJBZ9hIERIbI3iAgqzudaZrm++MRP0cRCk3ejHNfVy6gI+8BktPzPuM8OpvNZkMIIYQQQvyNXusAhBBCCCGckRRJQgghhBD5kCJJCCGEECIfUiQJIYQQQuRDiiQhhBBCiHxIkSSEEEIIkQ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sxa3oAv/7iQ9nyuTw+JJ0efbLcMhc6okhy4xrT/mw22LTOl04tI+n9UARJSXo++TyZXYev8tzL6S5TIAEkZyUz/9B8krOStQ6lQLy8oHGzHD75PIX9J68ycbqK//mnwqhRoQijhodw+KAsvdPCnl17GDlsJO+OfZeubbre0RZq4dxSknV8NiWARjWjeahVFJvX+/Lo4Ax+2HuVFRsTeGRgpksWSOBaudHHB7r1yiJuSwJL1yVQuZqFN14OoU6VIkwcH0iaSdYkFJT89LgNNhusX+PLpPeD+PUXH2rVyWHu94k0aWGWdTEaCgyy0bNvFj37ZnHxvBex8/yY83UAc78OoE59MwOeyKBdp2y32T7s7GrXrc2GnRu0DkPY0dHDBr6aEcDiBX5Yc3V0eCiL9yam0LBxjltP8zg7nY4/Wwv
"text/plain": [
"Graphics object consisting of 12 graphics primitives"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#rysowanie lemniskaty ze zmodyfikowaną skalą\n",
"pup=plot(f,(x,-sqrt(2),sqrt(2)),xmin=-sqrt(2)-0.2,xmax=sqrt(2)+0.2,ticks=[[-sqrt(2),-sqrt(2)/2,0,sqrt(2)/2,sqrt(2)],[-1/2,0,1/2]],tick_formatter=[sqrt(2)/2,1/2])\n",
"pdown=plot(-f,(x,-sqrt(2),sqrt(2)),ticks=[[-sqrt(3)/2,0,sqrt(3)/2],[-1/2,0,1/2]])\n",
"lemniskata=pdown+pup\n",
"\n",
"#pudełko dookoła lemniskaty\n",
"pudelko=plot(1/2,(x,-sqrt(2),sqrt(2)),color='red',fill='axis',fillcolor='green',fillalpha=0.1)+line([(-sqrt(2),-1/2),(-sqrt(2),1/2)],color='red')\n",
"pudelko+=plot(-1/2,(x,-sqrt(2),sqrt(2)),color='red')+line([(sqrt(2),-1/2),(sqrt(2),1/2)],color='red')\n",
"\n",
"#linie maksimów (przerywane, zielone)\n",
"linie=line([(sqrt(3)/2,0),(sqrt(3)/2,1/2)],linestyle=\"--\",color=\"green\")\n",
"linie+=line([(-sqrt(3)/2,0),(-sqrt(3)/2,1/2)],linestyle=\"--\",color=\"green\")\n",
"\n",
"#dodatkowe etykiety\n",
"teksty=text(\"$\\\\frac{\\\\sqrt{3}}{2}$\",(sqrt(3)/2+0.05,-0.05),color='green')\n",
"teksty+=text(\"$\\\\frac{-\\\\sqrt{3}}{2}$\",(-sqrt(3)/2-0.05,-0.05),color='green')\n",
"\n",
"#końcowy wykres\n",
"show(pudelko+lemniskata+teksty+linie,figsize=[6,6],aspect_ratio=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Teraz możemy obliczyć wartość pola wewnątrz figury jako sumę czterech całek (każda odpowiada jednej ćwiartce figury)."
]
},
{
"cell_type": "code",
"execution_count": 227,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYMAAACaCAYAAABLyiT1AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi40LCBodHRwOi8vbWF0cGxvdGxpYi5vcmcv7US4rQAAIABJREFUeJztnXd8T9f/x5/3kz0kIiERYgRBBLVqhIoV1C61qkbVTNqiqlX6LR2Ctkqb2JQus1ZLjVaNlqKILdEYiREkshMZn8/9/XF+0lIjic9MzvPxuI8PH/ee874+997XPee8h6KqqopEIpFISjQaUxsgkUgkEtMjxUAikUgkUgwkEolEIsVAIpFIJEgxkEgkEglSDCQSiUSCFAOJRCKRIMVAIpFIJEgxkEgkEglSDCQSiUSCFAOJRCKRIMVAIkFVVVJTU5FpuiQlGSkGkhJPWloarq6upKWlmdoUicRkWJvagOJEXh5kZkJurvhzXh7odGBnBw4OYG8PNjamtlIisQzy8iA7W2w5OWKztgZbW3FP2dmJ+0lRTG1p8UCKQQFJT4foaLh48Z/tyhW4fRsSEyEpCVJSntyOrS2ULQvlyolPT0/w9obq1aFGDbGVLy8vcEnxRVXFfRMTA5cvw6VL4jMuTuV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yZQBiY2PRmOvdZCLy8sT03f/+p+Lilsucr2J5LrgAZfckBkdRYOSEG7iXzWXWu5W4fVvl668Vs4z8N8s4g0WLYPRo4er41lvFM6mcpaOqsHUrzJkjRGLRIuGFZEkUh9xEp0/D4CEqkceh/yu
"text/plain": [
"Graphics object consisting of 8 graphics primitives"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plg=plot(f,(x,-sqrt(2),0),fill='min',fillcolor='blue', fillalpha=.2)\n",
"pld=plot(-f,(x,-sqrt(2),0),fill='max',fillcolor='red', fillalpha=.2)\n",
"ppg=plot(f,(x,0,sqrt(2)),fill='min',fillcolor='green', fillalpha=.2)\n",
"ppd=plot(-f,(x,0,sqrt(2)),fill='max',fillcolor='yellow', fillalpha=.2)\n",
"show(plg+pld+ppg+ppd,figsize=[4,4],aspect_ratio=1)"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.0000000070692856"
]
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Pole wewnątrz lemniskaty\n",
"2*integrate(funkcja,(x,-sqrt(2),sqrt(2))).n()"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2.0000000070692856, 1.4825283125400555e-06)"
]
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#szybsza metoda numerycznego całkowania z kontrolą błędu (wartość całki, błąd)\n",
"integral_numerical(2*funkcja,-sqrt(2),sqrt(2),max_points=100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hmm, być może wartość pola wynosi $2$, więc wartość całki z $f(x)$ wynosi $1$ w przedziale $(-\\sqrt{2},\\sqrt{2})$. Sprawdźmy to !"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3> Hipoteza</h3>\n",
"<div class=\"alert alert-block alert-info\"> \n",
"\n",
"$$P=\\int_{0}^{\\sqrt{2}}\\sqrt{\\sqrt{4x^2 + 1}-x^2 - 1}\\: dx = \\frac{1}{2}$$</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Wyznaczymy najpierw parametryzację promieniem i kątem $(r,t)$ dla naszego równania krzywej Bernoulliego.\n",
"\n",
"Korzystamy ze standardowej zamiany zmiennych\n",
"\n",
"$$x=r\\cos(t),\\quad y=r\\sin(t)$$"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(r^2 + 4*sin(t)^2 - 2)*r^2"
]
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r,t=var('r,t')\n",
"((x**2+y**2)**2-2*(x**2-y**2)).subs({x:r*cos(t),y:r*sin(t)}).simplify_full().factor()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-success\"> \n",
"Zatem równanie uwikłane lemniskaty we współrzędnych biegunowych $(r,t)$ jest postaci\n",
"\n",
"$$r^2+4\\sin(t)^2-2=0$$</div>"
]
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[r == -sqrt(-4*sin(t)^2 + 2), r == sqrt(-4*sin(t)^2 + 2)]"
]
},
"execution_count": 132,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve(r^2 + 4*sin(t)^2 - 2==0,r)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Zatem możemy sparametryzować naszą krzywą następującymi równaniami\n",
"\n",
"$x=r \\cos(t), \\quad y=r \\sin(t)$, gdzie\n",
"\n",
"$r=\\pm \\sqrt{2-4 \\sin(t)^2}=\\pm \\sqrt{2\\cos(t)}=\\pm \\sqrt{4\\cos(t)^2-2}$"
]
},
{
"cell_type": "code",
"execution_count": 225,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAASEAAACCCAYAAAAJ4n7vAAAABHNCSVQICAgI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2Vqzt6d1b9Kb58ENg714uQEw8kWTHDjEvOGuWWA9m/kOLP/BIqB0oLBQ3lB44IF5XruTnYrHGiIDly4HoaOCtt4C3324fjfclLVZkpqXTAe+9J76hPDxEqwdugsWao1AAS5eKZnMREeL7JyHB/AuRWRehO9u7WpuTJ8Wo59AhsVI2Lo47GbLWWbgQ6NBBdEOorRUjZ3MuRHw6ZmZ0OtHbZ/Fi8YjiDz8Uz+hiTKr33hMtQZYuFf2KzJXR+wllZGRgyJAh6Ny5MxwcHDBw4EBs3bpV74QtWUmJuM3ijTeAuXNF+08uQExfr70mTsdiY0Vva7Ml9Zq+1H5Ce/bsoYyMDCooKKCioiJat24dKZVK2rlzZ6tjWsM6odRUok6diDw9iXJy5M6GWYraWqL584kUCqL0dLmzaZpJ+gk1NGjQIFq8ePFd97OGFdNlZUTPPy/uBZo6lejaNbkzYpZGpyN69lkilUo8BdfcGPUG1oZqa2tp9+7ddO+991JWVlar41rqSOjQIfHoYEdHon/9y/I76DH53LpFNGqU6NZ4lyaoJidpTkiffkIAUFZWho4dO0KlUmH8+PFYv349goKCmt2/qqoK5eXl9TZLQgS8846Y73FzAzQa8Tgdc76Cwdo3OzvxVNh77gEmTxbN7syFUfsJ3ebo6AiNRoMjR44gLi4OCxYsQE4Lzzex5H5C168DTz4pLqO+/jqwbx/Qo4fcWTFr4OoqVlafPCkufJgNKcOmtp6O3TZnzhw
"text/plain": [
"Graphics object consisting of 1 graphics primitive"
]
},
"execution_count": 225,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"parametric_plot((sqrt(2*cos(2*t))*cos(t),sqrt(2*cos(2*t))*sin(t)),(t,0,pi/4) ,figsize=[3,3])"
]
},
{
"cell_type": "code",
"execution_count": 224,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAR8AAADRCAYAAAD1wrY9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAADl0RVh0U29mdHdhc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Gg1vGnn6NODqCvTtC8yaxaV5RcWYdfJZtQr4/HNeMjFggNrRCPFAq1Y8zWP2bGDePK7/rWC1iWrBbJPP/v08zDlxIjBhgtrRCFGSrS0wcyYnoatX+Tbs++/Vjso4qs08n+vX+T+yY0eux2PpdVeE9UtP59ux7duBDz7g1pA1vm+tep5PXh7w2mv8HxcZaZ3/gcL6NGjAG00uXsyLVL29uTUkymZ2yWfuXG7Gfv010LCh2tEIYTiNhsv2HjrErfcuXXhOmiidWSWfw4e5vMHs2fzJIYQlevppLuX79NM8Py0kpPpuaFges+nzycoCnnqKZy4fPMj1d4WwZPn5/EE6dy7XFI+IABwc1I6qaqyyz+fDD3mrk7VrJfEI61CjBrfkv/uOZ+b36AGcO6d2VObDLJLPoUM8n2fePN4/XQhrMnIkb16o0QDdu3PHtDCD5JOdzetkevXiXQWEsEZt2gDHj/Pi6FGjeH7QX7vWVFuqJ58lS3jd1qpVcrslrJuDA68Nmz+fW/mjR/PecpbA6iYZXr0KtGvHG7gtXqxWFEKY3tatPJ+tTRtg2zbA3V3tiAxjNR3OU6YAjo68ME+I6mTkSJ5akpbGHdFxcWpHZHqqJZ/jx3mrmwULOAEJUd107sz7yD/2GODlxZUbqhPVks+MGbxjgJ+fWhEIob7GjXkR9fDhvKf8/PnVZ0KiKsnnp5/4+OQT6WQWonZtXsc4axZ/KPv
"text/plain": [
"Graphics object consisting of 1 graphics primitive"
]
},
"execution_count": 224,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"parametric_plot((-sqrt(2*cos(2*t))*cos(t),-sqrt(2*cos(2*t))*sin(t)),(t,-pi/4,pi/4),figsize=[3,3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Świetnie, zatem możemy naszą całkę $P$ przetransformować do postaci, gdzie będzie zależała wyłącznie od zmiennej $t$."
]
},
{
"cell_type": "code",
"execution_count": 276,
"metadata": {},
"outputs": [],
"source": [
"f1=funkcja.subs({x:sqrt(4*cos(t)**2-2)*cos(t)})\n",
"d1=x.subs({x:sqrt(4*cos(t)**2-2)*cos(t)}).derivative(t)"
]
},
{
"cell_type": "code",
"execution_count": 263,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\sqrt{-4 \\, \\cos\\left(t\\right)^{4} + 2 \\, \\cos\\left(t\\right)^{2} + \\sqrt{16 \\, \\cos\\left(t\\right)^{4} - 8 \\, \\cos\\left(t\\right)^{2} + 1} - 1}</script></html>"
],
"text/plain": [
"sqrt(-4*cos(t)^4 + 2*cos(t)^2 + sqrt(16*cos(t)^4 - 8*cos(t)^2 + 1) - 1)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"f1=f1.simplify_full()\n",
"pretty_print(f1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Zauważmy, że \n",
"\n",
"$$16\\cos(t)^4 - 8\\cos(t)^2 + 1 = (4\\cos(t)^2 - 1)^2$$\n",
"\n",
"oraz\n",
"\n",
"$4\\cos(t)^2 - 1\\geq 0$ skoro $t\\in(0,\\frac{\\pi}{4})$"
]
},
{
"cell_type": "code",
"execution_count": 285,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\sqrt{-4 \\, \\sin\\left(t\\right)^{4} + 2 \\, \\sin\\left(t\\right)^{2}}</script></html>"
],
"text/plain": [
"sqrt(-4*sin(t)^4 + 2*sin(t)^2)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"f1=sqrt(-4*cos(t)**4+2*cos(t)**2+(4*cos(t)**2-1)-1)\n",
"pretty_print(f1.simplify_full())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To wyrażenie uprościmy dalej\n",
"\n",
"$$\\sqrt{-4\\sin(t)^4+2\\sin(t)^2} = \\sin(t)\\sqrt{2(1-2\\sin(t)^2)}=\\sin(t)\\sqrt{4\\cos(t)^2-2}$$ na mocy naszych założeń o $t$."
]
},
{
"cell_type": "code",
"execution_count": 300,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}-2 \\, {\\left(4 \\, \\cos\\left(t\\right)^{2} - 1\\right)} \\sin\\left(t\\right)^{2}</script></html>"
],
"text/plain": [
"-2*(4*cos(t)^2 - 1)*sin(t)^2"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"f1=(sin(t)*sqrt(4*cos(t)**2-2))\n",
"d1=d1.factor()\n",
"pretty_print((f1*d1).simplify().expand().simplify_full())"
]
},
{
"cell_type": "code",
"execution_count": 301,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"-2*(4*cos(t)^2 - 1)*sin(t)^2"
]
},
"execution_count": 301,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(f1*d1).simplify().expand().simplify_full()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-success\"> \n",
"Zatem metodą dokładną pole wewnątrz lemniskaty jest czterokrotnym polem jednego parametryzowanego kawałka $P$. Parametryzacja jest zgodna z orientacją osi, gdy $t$ maleje od $\\frac{\\pi}{4}$ do $0$. Czyli\n",
"\n",
"$P = \\int_{x=0}^{x=\\sqrt{2}} \\sqrt{\\sqrt{4x^2 + 1}-x^2-1}\\: dx=\\int_{t=\\pi/4}^{t=0}-2(4\\cos(t)^2-1)\\cdot \\sin(t)^2 dt$ </div>"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1/2"
]
},
"execution_count": 133,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"integrate(-2*(4*cos(t)^2 - 1)*sin(t)^2,(t,pi/4,0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Problem</h3>\n",
"<i>Czy możemy obliczyć przybliżenie pola lemniskaty za pomocą metody nieanalitycznej?</i>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2> Metoda numeryczna 1:</h2>\n",
"<div class=\"alert alert-block alert-success\"> \n",
"Wygenerujemy zbiór punktów na płaszczyźnie w obszarze, w którym mieści się cała lemniskata.</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nasza lemniskata styka się z bokami prostokąta $A=[-\\sqrt{2},\\sqrt{2}]\\times [-1/2,1/2]$.\n",
"\n",
"Pole tego prostokąta wynosi $2\\sqrt{2}$. Wylosujemy punkty w obu przedziałach (jednostajnie) i zobaczymy ile ich potrzeba, aby uzyskać rozsądne przybliżenie."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>UWAGA:</b> W tym rozwiązaniu zastosujemy generator liczb pseudolosowych, który produkuje dla nas liczby z przedziału $[0,1]$ z rozkładem jednostajnym."
]
},
{
"cell_type": "code",
"execution_count": 222,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAASIAAAEgCAYAAAAQdn69AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi40LCBodHRwOi8vbWF0cGxvdGxpYi5vcmcv7US4rQAAIABJREFUeJzt3XlcVOX+B/APsqohqWyCQGApKrkhmgvaYpSZ2m3VXLjVrTRMyRbxUj+3FNPstoiay+12W8SbYpmZVzQ1Fa8aQqkopoigiYjhgCkg8P398QTDYXNmmJnnnJnv+/Wal/MMZ875svid5zyrAxERGGNMohayA2CMMU5EjDHpOBExxqTjRMQYk44TEWNMOk5EjDHpOBExxqTjRMQYk44TEWNMOk5EjDHprJKIfvzxR4wcORJ+fn5wcHDA119/bY3LMsY0wiqJ6I8//kDPnj2xdOlSk95PRCguLgZPi2PMNjlZ4yLDhw/H8OHDTX5/SUkJPDw8oNPp0KZNGzNGxh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hQE1B6D+eWXvOazmnXpInYqqbZ4sX4HE6Z9GlsswHw+/FCs8QsADzxg2UXPWfPVXW7VUsuvMjnstkYkzitmywcG8qLzanf6tNgc4MwZUXN9/33+ndkSu05EjDF1sNtbM8aYenAiYoxJp+pElJiYiG7duiEiIqLZ5/rlF+D//g9YtUqM1mWMqYddtBGdOCHmN1VPsJwyBfjoIzMHyRgzmaprROaSkqKc5b1xo7xYGGP12UUiCg1tuswYk8suEtH99wMffAD06QM88gj
"text/plain": [
"Graphics object consisting of 1 graphics primitive"
]
},
"execution_count": 222,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r=random() #zmienna o rozkładzie jednostajnym i wartościac w przedziale [0,1]\n",
"list_plot([random() for i in range(0,100)],figsize=[3,3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Generujemy losowy punkt w prostokącie $A$"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [],
"source": [
"def LosowyPunkt():\n",
" x=sqrt(2).n()*(2*random()-1)\n",
" y=random()-1/2\n",
" return (x,y)"
]
},
{
"cell_type": "code",
"execution_count": 221,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAR8AAAEfCAYAAAB4V8JNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi40LCBodHRwOi8vbWF0cGxvdGxpYi5vcmcv7US4rQAAIABJREFUeJzt3XlcVNX7B/APoAwgiAsKooaWGiJqLohLLrnQt8WlLDWX1PyWFpZkZVqZSylqZfVVrG+mZT/3by6paUZWaGpmJrlgrqhUIKAIKMoynN8fT8MwrLPcmTP3zvN+vebl3HHm3keEh3POPec5bkIIAcYYczB32QEwxlwTJx/GmBScfBhjUnDyYYxJwcmHMSYFJx/GmBScfBhjUnDyYYxJwcmHMSYFJx/GmBScfBhjUnDy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8lmyhGrQvvaa6a6Krk6vB6ZPp5bLxIlAXp5lny/bzfL0VC42S+TkAPn5cq6tNWVrI5c9dgatWgEzZ9LuFXYtai9stGqVEJTL6fHyy7aeUTvef9/0axMTY9nnv/pKCG9v+uyQIUIUFdknzqo8+yxd38tLiA0bjK9nZ2cLACI7O9vxQalYWpoQffsKERAgxOjRQuTny45IHpsXlh4+XPWxKzt1qurj6gwaBGRkUMvDHrsHVGfvXrrtCtB404QJtLkd72JhvcBAYM8e2VE4B5u7XWWr4/Xta+sZnVdGBtXbCQigyv7VDQAPHGj6gzp4sOXXrFVLTuIBaEyitNu3qeYQY4pQovm0YYMQTz0lxIcfCqHXK3F
"text/plain": [
"Graphics object consisting of 100 graphics primitives"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"li=[point(LosowyPunkt()) for _ in range(0,100)]\n",
"show(sum(li),figsize=[3,3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Teraz potrzebujemy funkcji, która wskaże czy zadany punkt znajduje się we wnętrzu lemniskaty lub nie."
]
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {},
"outputs": [],
"source": [
"def CzyWewnatrzLemniskaty(punkt):\n",
" x,y=punkt\n",
" f=sqrt(-x^2 + sqrt(4*x^2 + 1) - 1)\n",
" if abs(y)<=f:\n",
" return True\n",
" else:\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 146,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"CzyWewnatrzLemniskaty((1/4,1/8))"
]
},
{
"cell_type": "code",
"execution_count": 147,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 147,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"CzyWewnatrzLemniskaty((0,1))"
]
},
{
"cell_type": "code",
"execution_count": 198,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"image/png": "iVBOR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IIQQSBkIIIZAwEEIIgYSBEEIIJAyEEEIgYSCEEAIJAyGEEEgYCCGEQMJACCEEEgZCCCGQMBBCCIGEgRBCCCQMhBBCIGEghBACCQNRzE2YAG3b6v+dlGRsLUIYScJAFFvz58Mbb8CePfrXr71mbD1CGEnCQBRbhw/f+vWxY8bUIYQjkDAQxVaXLuDkdOPrTp2Mq0UIo2lKKWV0EUIYZcsWWLYsmUmTfElKSsLHx8fokoQwhISBKPaSk5Px9ZUwEMWbNBMJIYSQMBBCCCFhIES+XL4MQUEQHW2/Y/zxB5QvD2lp9juGEH8lYSBEPkyYAN27Q3Cw/vXZs/rXnp4QGAgvvgjZ2Xffx/vvQ7Nm4OEBfn5//3ndutC4MXz2mc3LF+KOJAyEyKOMDJg5E555Rv/aYoGuXfUr+G3bYOFCWLoURo+++36ys+GJJ+Cf/7zzNsOGwdSp+jGEKAhmowsQorD46Scwm6FpU/3rn3+GI0fg3DkoW1b/3iefwNCh+tX/nTomjR+vf549+87H6thRb5L65Zcb02UIYU9yZyBEHm3ZAg0b3vj6t9+gTp0bQQD6m3hW1o0pLu6Xiws89BBs3fpg+xEiryQMhMij6Ohb3/hjY6FUqVu38ffX38hjYx/8eOXK2fdBtRA3kzAQIo8yMsDN7dbvadrft1Pq9t/PL3d3SE9/8P0IkRcSBkLkUWAgXLly4+vSpf9+B3DlCuTk/P2O4X4kJkLJkg++HyHyQsJAiDwKC9MfGF/TtCkcOgQXL9743s8/g6srNGjw4Mc7dEg/phA
"text/plain": [
"Graphics object consisting of 9 graphics primitives"
]
},
"execution_count": 198,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lem=plot(f,(x,-sqrt(2),sqrt(2)),fill='min',fillcolor='yellow', fillalpha=.2,figsize=[4,4],aspect_ratio=1,ticks=[[],[]])\n",
"lem+=plot(-f,(x,-sqrt(2),sqrt(2)),fill='max',fillcolor='yellow', fillalpha=.2)\n",
"pt=point((1/4,1/8))+point((0,1))\n",
"teksty=text(\"(1/4,1/8)\",(1/4+0.4,1/8))+text(\"(0,1)\",(+0.2,1-0.1))\n",
"teksty+=text(\"Lemniskata $(x^2+y^2)^2=2(x^2-y^2)$\", (0.0,1.3), background_color=(1,1,0.8),fontsize=8)\n",
"\n",
"lem+pt+teksty"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Obliczanie pola polega na wygenerowaniu zestawu próbek punktów, dla których sprawdzamy czy należą do wnętrza lemniskaty."
]
},
{
"cell_type": "code",
"execution_count": 199,
"metadata": {},
"outputs": [],
"source": [
"def PoleLemniskaty(proba):\n",
" traf=0\n",
" for _ in range(0,proba):\n",
" if CzyWewnatrzLemniskaty(LosowyPunkt()):\n",
" traf+=1\n",
" return traf/proba*(2*sqrt(2).n()) #skalujemy ze względu na rozmiar prostokąta A"
]
},
{
"cell_type": "code",
"execution_count": 223,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAASAAAAEgCAYAAAAUg66AAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi40LCBodHRwOi8vbWF0cGxvdGxpYi5vcmcv7US4rQAAIABJREFUeJzt3X1UVPedP/D3OIzDRGECGh5GQNBTEGMCpmHxaSXbtBgqBB+SYNJYxMZkI7qarMc6VVdqo2h6JN2uE7PBYI2mIQ8iupukCVkVYhVHjcT4CB5ROERjzNJBYR15+Pz+8MeNU0QZmeEOzPt1zj2de/nO9/O9Oek733vv3Hs1IiIgIlJBP7UHQETeiwFERKphABGRahhARKQaBhARqYYBRESqYQARkWoYQESkGgYQEamGAUREqmEAEZFqvCqARAQNDQ3g7W9EnsGrAujKlSswGo24cuWK2kMhInhJAFksFowcORIJCQlqD4U8iN0OcDKsLo03PY6joaEBRqMRNpsN/v7+ag+HVCICPPccsGkTEBAAfPgh8E//5J5aV64Av/sdcPEiMHs28Mgj7qnTWzGAyOv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YrAohPRCTyTDwEIyLVeMUMiIg8EwOIiFTDACIi1XhFAPEkNJFn4kloIlKNV8yAiMgzMYCISDUMICJSDQOIiFTjFQHEq2BEnolXwYhINU7PgMrKypCWlgaTyQSNRoPi4uI7fsdisSA2NhYGgwExMTF4++23Hf7e3NyMlStXYvjw4fD19UVcXBz+0v680f8vJycHGo3
"text/plain": [
"Graphics object consisting of 1 graphics primitive"
]
},
"execution_count": 223,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list_plot([PoleLemniskaty(1000*k) for k in range(1,10)],figsize=[3,3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Przy wyborze niewielkiej liczby punktów próbkujących dokładność jest niezadowalająca. Jedną z przyczyn kłopotów z dobrym przybliżeniem jest też jakość próbek losowych pochodzących z funkcji `random`."
]
},
{
"cell_type": "code",
"execution_count": 209,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.82842712474619"
]
},
"execution_count": 209,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"PoleLemniskaty(5)"
]
},
{
"cell_type": "code",
"execution_count": 214,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"2.00818325856980"
]
},
"execution_count": 214,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"PoleLemniskaty(500)"
]
},
{
"cell_type": "code",
"execution_count": 207,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.00224356160783"
]
},
"execution_count": 207,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"PoleLemniskaty(50000)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Metoda numeryczna 2</h2>\n",
" <h3> Całkowanie z wykorzystaniem spacerów losowych</h3>\n",
"\n",
"<div class=\"alert alert-block alert-success\"> \n",
"W ostatniej części postawimy sobie ambitniejsze zadanie. Zakładamy na początek, że mamy do dyspozycji tylko prosty generator bitowy `genbit` (który zwraca wartości $0$ i $1$ z prawdopodobieństwem bliskim $50\\%$).\n",
"\n",
"Skonstruujemy na bazie generatora bitowego zmienną losową `step`, która z prawdopodobieństwem $1/4$ zwraca wartość $-1$, z prawdopodobieństwem $1/2$ zwraca wartość $0$ i z prawdopodobieństwem $1/4$ zwraca wartość $1$.\n",
"\n",
"Zmienna $K$ może posłużyć do konstrukcji spaceru losowego po prostej, w którym połowę czasu spędzamy stojąc w miejscu.</div>\n"
]
},
{
"cell_type": "code",
"execution_count": 233,
"metadata": {},
"outputs": [],
"source": [
"def genbit():\n",
" return choice([0,1])"
]
},
{
"cell_type": "code",
"execution_count": 239,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Częstość zer = 0.501960000000000\n",
"Częstość jedynek = 0.498040000000000\n"
]
}
],
"source": [
"proba=100000\n",
"probka=[genbit() for _ in range(0,proba)]\n",
"\n",
"print(\"Częstość zer = \"+str(probka.count(0)/proba*1.0))\n",
"print(\"Częstość jedynek = \"+str(probka.count(1)/proba*1.0))"
]
},
{
"cell_type": "code",
"execution_count": 240,
"metadata": {},
"outputs": [],
"source": [
"def step():\n",
" go=genbit()\n",
" if go==1:\n",
" left=genbit()\n",
" if left==1:\n",
" return -1\n",
" else:\n",
" return 1\n",
" else:\n",
" return 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Zbadajmy numerycznie rozkłąd zmiennej losowej `step`"
]
},
{
"cell_type": "code",
"execution_count": 246,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"P(step=-1)=0.240000000000000\n",
"P(step=0)=0.508000000000000\n",
"P(step=1)=0.252000000000000\n",
"\n"
]
}
],
"source": [
"proba=1000\n",
"li=[step() for i in range(0,proba)]\n",
"le=li.count(-1)\n",
"st=li.count(0)\n",
"ri=li.count(1)\n",
"ll=len(li)\n",
"print(''.join([\"P(step={})={}\\n\".format(x[0],x[1]*1.0/ll) for x in [(-1,le),(0,st),(1,ri)]]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dla ustalonej pozycji początkowej $\\{-1,0,1\\}$ konstruujemy dodatkową funkcję, która zwróci nam losową pozycję (stan) po zastosowaniu zmiennej `step`."
]
},
{
"cell_type": "code",
"execution_count": 247,
"metadata": {},
"outputs": [],
"source": [
"states=[-1,0,1]\n",
"def RandomStep(init):\n",
" s=step()\n",
" if s==0:\n",
" return init\n",
" else:\n",
" return states[(init+s+1)%3]"
]
},
{
"cell_type": "code",
"execution_count": 248,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"P(step=-1)=0.253000000000000\n",
"P(step=0)=0.255000000000000\n",
"P(step=1)=0.492000000000000\n",
"\n"
]
}
],
"source": [
"proba=1000\n",
"li=[RandomStep(1) for i in range(0,proba)]\n",
"le=li.count(-1)\n",
"st=li.count(0)\n",
"ri=li.count(1)\n",
"ll=len(li)\n",
"print(''.join([\"P(step={})={}\\n\".format(x[0],x[1]*1.0/ll) for x in [(-1,le),(0,st),(1,ri)]]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-success\"> \n",
"Teraz naszym celem jest skonstruowanie zmiennej losowej, która przyjmuje jeden ze stanów $\\{-1,0,1\\}$ z prawdopodobieństwem $1/3$. Nie możemy bezpośrednio do tego celu wykorzystać naszej poprzedniej zmiennej, ale korzystając z niej możemy skonstruować łańcuch Markowa, który będzie miał tę cechę, że jego rozkład graniczny będzie jednostajny.</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nasz łańcuch Markowa jest aperiodyczny i nierozkładalny, zatem posiada jedyny stan stacjonarny, do którego zbiega każdy stan początkowy. Stan stacjonarny odpowiada wektorowi własnemu (znormalizowanemu) podporządkowanemu wartości własnej $1$."
]
},
{
"cell_type": "code",
"execution_count": 250,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[1, 1/4, 1/4]"
]
},
"execution_count": 250,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m=matrix([[1/2,1/4,1/4],[1/4,1/2,1/4],[1/4,1/4,1/2]])\n",
"m.eigenvalues()"
]
},
{
"cell_type": "code",
"execution_count": 254,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAGECAYAAAA1Jpu+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi40LCBodHRwOi8vbWF0cGxvdGxpYi5vcmcv7US4rQAAIABJREFUeJzsnXdUFMn39p9hGIJkRBGRoCIImEAkKIqLuisqYljFNWfUVQxr2HXNec05Z7/mBJhgV8wBEERFQBQlmABBkuRh6v1jftTrSIYJhPqc04fp6eq6t4eeZ6pvVd3iEEIIGAwGg1HnkZO1AwwGg8GQDkzwGQwGo57ABJ/BYDDqCUzwGQwGo57ABJ/BYDDqCUzwGQwGo57ABJ/BYDDqCUzwGQwGo57ABJ/BYDDqCUzwGWUyduxYDBgwoFLnHD16FJqamnR/2bJl6NChg7hdqzI1zZ+aTlXuAUbNhAm+FKjvX5i5c+fC399f1m7USPbs2YN27dpBXV0d6urqcHBwwI0bN2TiS2xsLDgcDp49eyby/rZt23D06FGZ+MQQL/KydoBR91FVVYWqqmq16sjPz4eCgoKYPKo5NGvWDOvWrYOJiQkA4NixY3Bzc0NoaCgsLS2l5kd+fn6pxzQ0NKTmB0OysBZ+DSA+Ph5ubm5QVVWFuro6hg4disTERJEye/bsQcuWLaGgoAAzMzOcOHFC5DiHw8GePXvg4uICZWVlNG/eHOfPnxcp8/HjR7i7u0NLSwsNGzaEm5sbYmNj6fHCwkLMmTMHmpqaaNiwIebPn4+K5NY7evQoDA0N0aBBAwwcOBApKSkix38MofD5fHh6elI7CxYswJgxY0Segrp3747p06djzpw50NHRQa9evQAAmzdvRtu2baGiogIDAwNMmzYN3759E/FFU1MTXl5eMDU1hZKSEnr16oX3798X8/vEiRMwN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7OgLCAhA69atAQDW1tZ48+YNGjduDBMTE5FNQ0MDGhoa0NPTE6mDz+cjJCSkXP9LslsaGhoa0NXVRVBQEH2vsLAQoaGhZdoBgODgYPD5fGzatAn29vYwNTWlrdzv4fP5CA4OpvtRUVFIS0ujn0VVsLCwKPF/YGpqCi6XW+V6S4IQgry8vDLLDB8+HCdPnsSVK1cgJyeHvn370mPW1tYIDw8v9n82MTEpc6RT0bHvn1h+pFWrVlBWVi51mG1591kRVlZW+Ouvv/Do0SO0adMGp06dKvN6GeKBCb6USE9Px7Nnz0S2+Ph49OzZE+3atcOIESPw9OlTBAUFYfTo0XBycqKP3PPmzcPRo0exd+9evHnzBps3b8alS5cwd+5cERvnz5/H4cOH8fr1ayxduhRBQUGYPn06AGEYQEdHB25ubrh//z5iYmJw9+5dzJw5Ex8+fAAAzJw5E+vWrcPly5fx6tUrTJs2rdhokB8paqmtX78er1+/xs6dO+Hr61vmOTNmzMDatWvh7e2NqKgozJw5E6mpqeU+sbRs2RJ8Ph87duzAu3fvcOLECezdu7dYOR6PhxkzZiAwMBBPnz7FuHHjYG9vLxJGqix//PEH/P39sXLlSrx+/RrHjh3Dzp07i/0PKsvChQtx//59xMbGIiwsDH///Tfu3LmDESNGlHle0f2yevVq/Prrr1BSUqLHFixYgMePH+P333/Hs2fP8ObNG/j4+GDGjBll1tm4cWMoKyvD19cXiYmJSE9PL1ZGSUkJCxYswPz583H8+HG8ffsWAQEBOHToEPWrrPssJiYGf/31Fx4/foy4uDj8+++/eP36dbHGC0NCyK77oP4wZswYAqDYNmbMGEIIIXFxcaR///5ERUWFqKmpkSFDhpCEhASROnbv3k1atGhBeDweMTU1JcePHxc
"text/plain": [
"Looped digraph on 3 vertices"
]
},
"execution_count": 254,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#graf łańcucha Markowa\n",
"g = DiGraph({-1: [-1,0,1], 0: [-1,0,1],1: [-1,0,1]}); g"
]
},
{
"cell_type": "code",
"execution_count": 266,
"metadata": {},
"outputs": [],
"source": [
"pi_vec=m.eigenvectors_right()[0][1][0]*1/3"
]
},
{
"cell_type": "code",
"execution_count": 267,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 267,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m*pi_vec==pi_vec"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tempo zbieżności do rozkładu stacjonarnego jest dość duże w tym przypadku"
]
},
{
"cell_type": "code",
"execution_count": 269,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0.333333969116211 0.333333015441895 0.333333015441895]\n",
"[0.333333015441895 0.333333969116211 0.333333015441895]\n",
"[0.333333015441895 0.333333015441895 0.333333969116211]"
]
},
"execution_count": 269,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m^10*1.0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-success\"> \n",
"Poniżej konstruujemy funkcję, która przy zadanym stanie początkowym i odpowiednim czasie mieszania `mixing` generuje wartości zgodne z łańcuchem Markowa opisanym powyżej. Wykorzystanie `yield` pozwala nam utworzyć jeden proces, który będzie można wykorzystywać we wszystkich funkcjach.</div>"
]
},
{
"cell_type": "code",
"execution_count": 271,
"metadata": {},
"outputs": [],
"source": [
"def MarkovChain(seed,mixing):\n",
" state=seed\n",
" #mieszanie początkowe dochodzące do stanu jednostajnego rozkładu\n",
" for _ in range(0,mixing):\n",
" state=RandomStep(state)\n",
" while true:\n",
" state=RandomStep(state)\n",
" yield state"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sprawdzimy numerycznie własności naszego łańcucha Markowa"
]
},
{
"cell_type": "code",
"execution_count": 274,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0.336800000000000, 0.330300000000000, 0.332900000000000]"
]
},
"execution_count": 274,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m1=MarkovChain(-1,500) #start w stanie -1 i \"wygrzewanie\" łańcucha w 500 krokach startowych \n",
" #(aby zgubić początkowy rozkład)\n",
"le=0 #zliczanie wartości -1\n",
"st=0 #zliczanie wartości 0\n",
"ri=0 #zliczanie wartości 1\n",
"samp=50000 #rozmiar próby\n",
"for _ in range(0,samp):\n",
" v=next(m1)\n",
" if v==-1:\n",
" le+=1\n",
" else:\n",
" if v==0:\n",
" st+=1\n",
" else:\n",
" ri+=1\n",
"[x*1.0/samp for x in [le,st,ri]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-success\"> \n",
"Na bazie łańcucha Markowa `MarkovChain` możemy utworzyć spacer losowy po grafie odwzorowującym punkty o współrzędnych całkowitych na płaszczyźnie. Taki spacer posłuży nam do generowania próbek losowych punktów na płaszczyźnie (po normalizacji).</div>"
]
},
{
"cell_type": "code",
"execution_count": 275,
"metadata": {},
"outputs": [],
"source": [
"def RandomWalk(point):\n",
" x,y=point\n",
" mx=MarkovChain(0,500)\n",
" my=MarkovChain(0,500)\n",
" lipt=[(x,y)]\n",
" while true:\n",
" x+=next(mx) #modyfikacja zmiennej x za pomocą łańcucha mx\n",
" y+=next(my) #modyfikacja zmiennej y za pomocą łańcucha my\n",
" yield (x,y) #dodanie kolejnego punktu w spacerze "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Zobaczmy jak \"wędruje\" punkt po płaszczyźnie w spacerze losowym zadanym powyżej."
]
},
{
"cell_type": "code",
"execution_count": 286,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYMAAAEKCAYAAADw2zkCAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi40LCBodHRwOi8vbWF0cGxvdGxpYi5vcmcv7US4rQAAIABJREFUeJzsnXeYFEX6x7+7wC55leCyCKIiEgSRJIYTQZSgBAFPEAOeyhnQ01PUQx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h3ZwoagOi224LdR3Ex0Ukn8b4OH7ZP16wZp2nfPtj8EBE1bmzuSul3qViR6NVX/eejuDg6+YjHUrs20fHj/r738eNGd2O5+ygA+uCDD0peb9myhQDQ999/b/p8nz596KabbrLdfl5eHuXk5JQsWVlZumtpnNmzh6hmTaI33oh3TvxRLkoGy5YFt20ibgzMzubXlSrZp503D2jShLtMbtoENG0aXL7mzOF9RMpnn/FdOcAR1l4gAu6/P/I8qGjbFmjc2Hi9cyewYgU/T01VB3L99BMPRQEAPXoANWqEplm8GDhwgJ9v3eq+RGClcmXgww+5JHPBBfbp9uzZAwBIl4s0f77+/fffbT/39NNPY8yYMf4ypwmEgwe5JH7bbfz61lvjmx/PxNtGQQMQNW0azLaLi4kefZT3ce65/JiX5/yZPXuIWrTg4LONG4PJVzQpLia65x7+bl5KCMXFRPfdx5+bPJlo+3bjjltsJzvbeE8VjGdlwADznftvv/H7O3YQNWlC1LAh0ZYt6s+uX09Uty7ReecR7d+vTiMHlPktEYQDlpLB8uX
"text/plain": [
"Graphics object consisting of 1 graphics primitive"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"rw=RandomWalk((0,0))\n",
"lin=[(0,0)]\n",
"\n",
"for _ in range(0,1000):\n",
" pp=next(rw)\n",
" lin.append(pp)\n",
" \n",
"#show(pt)\n",
"show(line(lin),aspect_ratio=1,figsize=[4,4])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"W praktyce interesuje nas spacer nie po nieograniczonej płaszczyźnie, ale po organiczonym jej kawałku. Aby zastosować taki efekt, utożsamiamy brzegi pewnego kwadratu, aby uzyskać topologiczny model torusa, na którym realizuje się nasz spacer losowy."
]
},
{
"cell_type": "code",
"execution_count": 287,
"metadata": {},
"outputs": [],
"source": [
"def RandomWalkWithBounds(point,size):\n",
" x,y=point\n",
" mx=MarkovChain(0,500)\n",
" my=MarkovChain(0,500)\n",
" lipt=[(x,y)]\n",
" while true:\n",
" x+=next(mx) #modyfikacja zmiennej x za pomocą łańcucha mx\n",
" x=x%size\n",
" y+=next(my) #modyfikacja zmiennej y za pomocą łańcucha my\n",
" y=y%size\n",
" yield (x,y) #dodanie kolejnego punktu w spacerze "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Poniżej prześledzimy na mapie częstości (im ciemniejszy piksel tym więcej odwiedzin) jak często dany punkt jest odwiedzany w spacerze losowym. W granicy każdy punkt powinien być odwiedzany z tą samą częstotliwością."
]
},
{
"cell_type": "code",
"execution_count": 303,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAASYAAAEiCAYAAABUVX/MAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi40LCBodHRwOi8vbWF0cGxvdGxpYi5vcmcv7US4rQAAIABJREFUeJzt3X9U1FX+P/AnKoyIOCsivxSR/BmgpmKGmZIWSqtZmtmP07FsPWuh5UHPttTphJ02WjvH6pS5u22raRrtrlraD1NLMTNbRVHUMlNUVIj8NSjqIHi/f/R9zyeS9+s2zIxc9Pk4Z86Rec4dLm/Gl2/n/Zp7g5RSCkREBmnW2BMgIvo1FiYiMg4LExEZh4WJiIzDwkRExmFhIiLjsDARkXFYmIjIOCxMRGQcFiYiMk6TK0xvvvkmEhMT0bJlS/Tv3x9ffvllY08Jubm5CAoKqnOLiYm54vPYsGEDRo8ejbi4OAQFBeGDDz6okyulkJubi7i4OISGhiI9PR27d+9u9Hk9/PDDlx2/m266KeDzysvLw4ABAxAeHo6oqCjcdddd2Lt3b53HuN1uTJs2DZGRkQgLC8Odd96JI0eONPq80tPTLztm9913X0DnBQDz5s1D79690aZNG7Rp0wZpaWn49NNPPbm/jleTKkzvv/8+pk+fjmeeeQbbt2/HLbfcgszMTBw+fLixp4bk5GSUlZV5bsXFxVd8DlVVVejTpw/eeOONevPZs2djzpw5eOONN7BlyxbExMTg9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"text/plain": [
"Graphics object consisting of 1 graphics primitive"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"rozmiar=30\n",
"rw=RandomWalkWithBounds((rozmiar//2,rozmiar//2),rozmiar)\n",
"li1=[]\n",
"for _ in range(0,50000):\n",
" li1.append(next(rw))\n",
"\n",
"freq={(i,j):(li1.count((i,j))) for i in range(0,rozmiar) for j in range(0,rozmiar)}\n",
"m=max([freq[x] for x in freq])*1.0\n",
"M=matrix(RR,rozmiar,rozmiar,0)\n",
"for el in freq:\n",
" M[el]=freq[el]/m\n",
" \n",
"show(matrix_plot(M),figsize=[3,3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Teraz możemy zdefiniować funkcję, która przy zadanym kształcie będzie zliczała odwiedziny."
]
},
{
"cell_type": "code",
"execution_count": 304,
"metadata": {},
"outputs": [],
"source": [
"def ShapeFunction(x,y,S):\n",
" if S(x,y): #S jest warunkiem bycia wewnątrz kształtu\n",
" return 1\n",
" else:\n",
" return 0\n",
" \n",
"S1=(lambda x,y: y**2<=sqrt(4*x**2 + 1)-x**2 - 1) # wnętrze lemniskaty"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I jeszcze jedna techniczna funkcja skalująca."
]
},
{
"cell_type": "code",
"execution_count": 308,
"metadata": {},
"outputs": [],
"source": [
"def MapFunction(a,b,xbound,xsize,ybound,ysize): #(a,b) w [0,1]^2\n",
" return xbound+a*xsize,ybound+b*ysize"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-success\"> \n",
"Teraz możemy już przygotować schemat całkowania metodą Monte Carlo z wykorzystaniem spaceru losowego. Po zainicjalizowaniu spaceru zliczamy te punkty wewnątrz kształtu, które odwiedziliśmy w trakcie spaceru. Zgodnie ze spodziewanym rozkładem jednostajnym zliczamy częstość odwiedzonych punktów w stosunku do wszystkich i mnożąc przez rozmiar próbkowanej przestrzeni.</div>"
]
},
{
"cell_type": "code",
"execution_count": 309,
"metadata": {},
"outputs": [],
"source": [
"def CalkowanieMarkowa(start,steps,size,S,xbound,xsize,ybound,ysize):\n",
" s=0\n",
" rw=RandomWalkWithBounds(start,size)\n",
" for _ in range(0,steps):\n",
" a,b=next(rw)\n",
" x,y=MapFunction(a*1.0/size,b*1.0/size,xbound,xsize,ybound,ysize)\n",
" s+=ShapeFunction(x,y,S)\n",
" return s*(xsize)*(ysize)/steps"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Przy nawet stosunkowo małych rozdzielczościach próbkowania (przykład poniżej dla 30 i 40 punktów w każdym z kierunków) i niezbyt dużej liczbie iteracji, otrzymujemy zadowalające przybliżenie wyniku."
]
},
{
"cell_type": "code",
"execution_count": 315,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.82842712474619"
]
},
"execution_count": 315,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"CalkowanieMarkowa((15,15),5,30,S1,-sqrt(2).n(),2*sqrt(2).n(),-1/2,1)"
]
},
{
"cell_type": "code",
"execution_count": 316,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.94030100757589"
]
},
"execution_count": 316,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"CalkowanieMarkowa((15,15),500,30,S1,-sqrt(2).n(),2*sqrt(2).n(),-1/2,1)"
]
},
{
"cell_type": "code",
"execution_count": 324,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.00659933937994"
]
},
"execution_count": 324,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"CalkowanieMarkowa((15,15),50000,30,S1,-sqrt(2).n(),2*sqrt(2).n(),-1/2,1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\"> \n",
"Przy rozdzielczości $40\\times 40$ punktów kraty i $2000$ iteracji otrzymujemy, że średnia wartość obliczonego metodą Monte Carlo pola ma błąd poniżej 0.05, a mediana przypada na wartość, której odchylenie również mieści się w zakresie około $0.01$.</div>\n",
"\n",
"Na wykresie poniżej prezentujemy rozkład wartości pola dla 100 prób wykonanych obliczeń. "
]
},
{
"cell_type": "code",
"execution_count": 339,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.99339058470737\n",
"2.01808275350641\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAksAAAGECAYAAAAm62T+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi40LCBodHRwOi8vbWF0cGxvdGxpYi5vcmcv7US4rQAAIABJREFUeJzt3Xl0FFXexvGnSUgIEJo1QCAICgoRWQYiyqLIpoAg4zIjKMF1VALIixu4II5LFNRxHAKOiqCCoiCbGyMMkLDKGgFRQNk1gLJ0J4EkJKn3jztJ0yxtEpKuTvL9nNOHquqq9K8phMd7b93rsCzLEgAAAM6pgt0FAAAABDLCEgAAgA+EJQAAAB8ISwAAAD4QlgAAAHwgLAEAAPhAWAIAAPCBsAQAAOADYQkAAMAHwhIAAIAPhCUAAAAfSkVYsixLbrdbLGMHAAD8rVSEpdTUVDmdTqWmptpdCgAAKGdKRVgCAACwC2EJAADAB8ISAAAoNbKypAMHpOxs/30mYQkAAJQKO3dKTZtKUVFSy5bSr7/653MJSwAAoFQYO1bav99sb98uvfKKfz6XsAQAAEqFU6e897Oy/PO5AR2WEhISFB0drZiYGLtLAQAANhszRqpRw2zXry+NGuWfz3VYpWCmR7fbLafTKZfLpWrVqtldDgAAsMmRI9LPP0uXXSY5nf75zGD/fAwAAMCFq1XLvPwpoLvhAAAA7EZYAgAA8IGwBAAA4ANhCQAAwAfCEgAAgA+EJQAAAB8ISwAAAD4QlgAAAHwgLAEAAPhAWAIAAPAhoMMSC+kCAFB+ffGF9PbbUkqKvXWwkC4AAAg4o0dLr7xituvXlzZsML/aIaBblgAAQPn07rue7ZQU6euv7auFsAQAAAJOZKT3foMG9tQhEZYAAI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udYerq0eLF04ID3tWfuwz8ISwAAlLBTp6S8fpzcXM92nuxsqXlz72PNm0tDhpjH/yUpONh0ycH/AjosJSQkKDo6WjHMxQ4AKAWOHZMSE6Vff/UcGzrUzJhdp46ZFLJePemJJzzv/+1vUosWZgzSgAFSu3Zmcsnu3aW+fc14pYQE6dtvzZxK8D/GLAEAUAx++knq0sXMb1Slink67cQJqXdvzzkNGni60n766dwtSgg8Ad2yBABAaTFxoglKkmcNNpfL+5zT95s2JSiVFoQlAAAKybLMY/xPPimtWmWOhYV5n1O5sulGa9XKc2zMGP/ViOJDNxwAAIU0dqz0/PNmOzhYWr5cuvRSqWdPaeNGM//RN9+YY+npZhxTnToSQ3BLJ9aGAwCgkBYs8GxnZ0tff20Wql2/3ixaW6OGVOF/fTdVqkh9+thTJ4oH3XAAABRSixbe+3ljjxwOqVYtT1BC2UDLEgAAhTRpkpn/aPt26c9/lgYOtLsilCTGLAEAAPhAQyEAAIAPhQpL8fHxiomJUXh4uCIiIjRgwABt377d5zXvvPOOunTpoho1aqhGjRrq0aOH1q5de0FFAwDgL+npZvHap5+W9uyxuxrYoVBhKTExUXFxcVqzZo0WLVqk7Oxs9erVS+np6ee9ZtmyZRo4cKCWLl2q1atXq1GjRurVq5d++eWXCy4eAICS1r+/NHq09OKLUseO0pEjdlcEf7ugMUu//fabIiIilJiYqGuuuaZA1+Tk5KhGjRqaOHGiYgu4IiBjlgAAdkhPl6pW9T62cCFrtJU3F/Q0nOt/87bXrFmzwNecOHFCp06d8nlNZmamMjMz8/fdbnfRiwQAoIiqVDETTO7da/ZDQswyJShfijzA27IsjRo1Sp07d1bLli0LfN3o0aPVoEED9ejR47znxMf
"text/plain": [
"Graphics object consisting of 1 graphics primitive"
]
},
"execution_count": 339,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"li=[CalkowanieMarkowa((randint(0,40),randint(0,40)),2000,40,S1,-sqrt(2).n(),2*sqrt(2).n(),-1/2,1)*1.0 for i in [1..100]] \n",
"print sum(li)/len(li) #wartość średnia po wielu iteracjach\n",
"li.sort()\n",
"print li[floor(len(li)/2)] #wartość środkowa (mediana)\n",
"list_plot(li)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "SageMath 8.9",
"language": "sage",
"name": "sagemath"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}