{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## Load Packages" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2020-09-25T11:59:02.029Z", "iopub.status.busy": "2020-09-25T11:59:01.320Z", "iopub.status.idle": "2020-09-25T11:59:08.538Z" } }, "outputs": [ { "data": { "text/plain": [ "printyellow (generic function with 1 method)" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" } ], "source": [ "using Printf, Distributions\n", "include(\"jlFiles/printmat.jl\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2020-09-25T11:59:08.640Z", "iopub.status.busy": "2020-09-25T11:59:08.559Z", "iopub.status.idle": "2020-09-25T11:59:25.236Z" } }, "outputs": [ ], "source": [ "using Plots\n", "gr(size=(480,480))\n", "default(fmt = :svg)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Find $\\pi$\n", "\n", "Find $\\pi$ by checking how often a pair of random variables are inside a circle. \n", "\n", "Clearly, this is not the smartest way to find $pi$, but the steps require some useful skills. Who knows, maybe it's smarter to remember how many letters there are in each of the words in \"How I wish I could calculate pi\" (3.141592).\n", "\n", "The next few cells will give you a useful starting point." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2020-09-25T11:59:40.900Z", "iopub.status.busy": "2020-09-25T11:59:40.890Z", "iopub.status.idle": "2020-09-25T11:59:42.059Z" } }, "outputs": [ { "data": { "image/svg+xml": "\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" }, "execution_count": 3, "metadata": { }, "output_type": "execute_result" } ], "source": [ "θ = range(0,2*pi,length=201) #angles (in radians) from 0 to 2*pi\n", "\n", "xCirc = cos.(θ)\n", "yCirc = sin.(θ)\n", "\n", "p1 = plot( xCirc,yCirc,\n", " legend=nothing,\n", " title = \"A circle with radius of 1 and center at (0,0)\" )\n", "display(p1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " x y x.^2 + y.^2 v\n", " 0.492 0.961 1.166 0 \n", " -0.118 -0.275 0.089 1 \n", " -0.227 0.742 0.602 1 \n", " -0.558 -0.404 0.475 1 \n", " 0.183 0.431 0.219 1 \n", " -0.769 -0.847 1.309 0 \n", " 0.526 0.731 0.811 1 \n", " 0.777 0.530 0.885 1 \n", " 0.427 -0.490 0.423 1 \n", " -0.940 0.348 1.005 0 \n", " 0.999 0.006 0.999 1 \n", " -0.833 0.594 1.046 0 \n", " -0.885 0.907 1.606 0 \n", " 0.665 0.225 0.492 1 \n", " -0.326 -0.178 0.138 1 \n", " 0.875 -0.778 1.372 0 \n", " 0.688 -0.440 0.667 1 \n", " 0.630 0.141 0.416 1 \n", " 0.092 0.742 0.559 1 \n", " -0.447 -0.630 0.596 1 \n", " -0.596 0.589 0.703 1 \n", " 0.532 0.403 0.446 1 \n", " -0.805 -0.740 1.195 0 \n", " -0.017 -0.562 0.316 1 \n", " -0.899 -0.723 1.332 0 \n", "\n" ] } ], "source": [ "N = 25\n", "\n", "dist = Uniform(-1,1)\n", "x = rand(dist,N) #draw uniformly [-1,1] distributed numbers\n", "y = rand(dist,N)\n", "\n", "v = (x.^2 + y.^2) .<= 1 #true if inside\n", "\n", "printmat(x,y,x.^2 + y.^2,v,colNames=[\"x\",\"y\",\"x.^2 + y.^2\",\"v\"],width=14)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2020-09-25T11:59:43.020Z", "iopub.status.busy": "2020-09-25T11:59:43.009Z", "iopub.status.idle": "2020-09-25T11:59:43.481Z" } }, "outputs": [ { "data": { "image/svg+xml": "\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" }, "execution_count": 5, "metadata": { }, "output_type": "execute_result" } ], "source": [ "p1 = plot( xCirc,yCirc,\n", " legend = nothing,\n", " title = \"What fraction is inside the circle?\",\n", " xlabel = \"x\",\n", " ylabel = \"y\" )\n", "scatter!(x[v],y[v],color=:blue)\n", "scatter!(x[.!v],y[.!v],color=:red)\n", "display(p1)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## Hints\n", "\n", "Draw many more random numbers (1_000_000 or more) and calculate $pi$ as the fraction of points inside the circle times the area of the box.\n", "\n", "The area of the box is $2x2=4$, and a pair is inside or on the circle if $x^2 + y^2 \\le 1$" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2020-09-25T11:59:43.499Z", "iopub.status.busy": "2020-09-25T11:59:43.491Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 0.785 3.141 π\n", "\n" ] } ], "source": [ "N = 25_000_000\n", "\n", "x = rand(dist,N)\n", "y = rand(dist,N)\n", "\n", "FreqInCircle = mean((x.^2 + y.^2) .<= 1)\n", "\n", "piEstimate1 = FreqInCircle*4\n", "\n", "printmat(FreqInCircle,piEstimate1,pi)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 8, "metadata": { }, "output_type": "execute_result" } ], "source": [ "1 > 2" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ ] } ], "metadata": { "@webio": { "lastCommId": null, "lastKernelId": null }, "anaconda-cloud": { }, "kernelspec": { "display_name": "Julia 1.6", "env": { "JULIA_DEPOT_PATH": "/home/user/.julia/:/ext/julia/depot/", "JULIA_PROJECT": "/home/user/.julia/environment/v1.6" }, "language": "julia", "metadata": { "cocalc": { "description": "The Julia Programming Language", "priority": 10, "url": "https://julialang.org/" } }, "name": "julia-1.6", "resource_dir": "/ext/jupyter/kernels/julia-1.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.6.2" }, "nteract": { "version": "0.25.0" } }, "nbformat": 4, "nbformat_minor": 4 }