From cc6ba42c25440f19d38fa90ea7737b7272422496 Mon Sep 17 00:00:00 2001 From: Eduardo Cueto Mendoza Date: Fri, 22 May 2020 13:37:10 -0600 Subject: [PATCH] [ADD] New week 3 code --- 01_Coursera_3.ipynb | 1431 ++++++++++++++++++++++++++++++++++++++++++- 02_Coursera_3.ipynb | 351 +++++++++++ 2 files changed, 1779 insertions(+), 3 deletions(-) create mode 100644 02_Coursera_3.ipynb diff --git a/01_Coursera_3.ipynb b/01_Coursera_3.ipynb index f002c4b..b0b2768 100644 --- a/01_Coursera_3.ipynb +++ b/01_Coursera_3.ipynb @@ -24,17 +24,1442 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "updateSIR (generic function with 1 method)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "function updateSIR(popnvector)\n", " susceptibles = popnvector[1];\n", " infecteds = popnvector[2];\n", " removeds = popnvector[3];\n", - " newS = \n", + " newS = susceptibles - λ*susceptibles*infecteds*dt\n", + " newI = infecteds + λ*susceptibles*infecteds*dt - γ*infecteds*dt\n", + " newR = removeds + γ*infecteds*dt\n", + " return [newS newI newR]\n", "end" ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1×3 Array{Float64,2}:\n", + " 975.0 34.5 20.5" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dt = 0.5;\n", + "λ = 1/200; γ = 1/10\n", + "\n", + "s,i,r = 1000.,10,20\n", + "vect = [s i r]\n", + "\n", + "updateSIR(vect)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "# Values for the test run\n", + "λ = 1/20000\n", + "γ = 1/10\n", + "dt = 0.5\n", + "tfinal = 610\n", + "s0 = 10000.0\n", + "i0 = 4.0\n", + "r0 = 0.0\n", + "\n", + "\n", + "nsteps = round(Int64,tfinal/dt)\n", + "resultvals = Array{Float64}(undef,nsteps+1,3)\n", + "timevec = Array{Float64}(undef,nsteps+1)\n", + "resultvals[1,:] = [s0, i0, r0]\n", + "timevec[1] = 0.\n", + "\n", + "\n", + "for step = 1:nsteps\n", + " resultvals[step+1,:] = updateSIR(resultvals[step,:])\n", + " timevec[step+1] = timevec[step] + dt\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Plots.GRBackend()" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "using Plots\n", + "gr()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "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" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "plot(timevec,resultvals)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "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" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "plot(timevec,resultvals,\n", + " title = \"Example of SIR results\",\n", + " xlabel = \"Epidemic day\",\n", + " ylabel = \"Population size\",\n", + " label = [\"Succeptible\" \"Infected\" \"Removed\"]\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "# Values for the test run\n", + "λ = 2.3e-8 #0.0005\n", + "γ = 1/20.\n", + "dt = 0.5\n", + "tfinal = 610\n", + "s0 = 22.0e6 #150.0\n", + "i0 = 4.0\n", + "r0 = 0.0\n", + "\n", + "\n", + "nsteps = round(Int64,tfinal/dt)\n", + "resultvals = Array{Float64}(undef,nsteps+1,3)\n", + "timevec = Array{Float64}(undef,nsteps+1)\n", + "resultvals[1,:] = [s0, i0, r0]\n", + "timevec[1] = 0.\n", + "\n", + "\n", + "for step = 1:nsteps\n", + " resultvals[step+1,:] = updateSIR(resultvals[step,:])\n", + " timevec[step+1] = timevec[step] + dt\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "svals = resultvals[:,1];\n", + "ivals = resultvals[:,2];" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "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" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "plot(svals,ivals,\n", + " title = \"First look at I vs S plot\",\n", + " xlabel = \"Succeptible\",\n", + " ylabel = \"Infected\"\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.1" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1/10" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/02_Coursera_3.ipynb b/02_Coursera_3.ipynb new file mode 100644 index 0000000..70c40fc --- /dev/null +++ b/02_Coursera_3.ipynb @@ -0,0 +1,351 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4-element Array{Any,1}:\n", + " #undef\n", + " #undef\n", + " #undef\n", + " #undef" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tempvar = Array{Any}(undef,4)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4-element Array{Any,1}:\n", + " \"Hello...word\"\n", + " \"Hello...word\"\n", + " \"Hello...word\"\n", + " \"Hello...word\"" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fill!(tempvar, \"Hello...word\")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4-element Array{Any,1}:\n", + " \"Hello...word\"\n", + " \"Hello...word\"\n", + " \"Hello...word\"\n", + " \"Hello...word\"" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tempvar" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4-element Array{Any,1}:\n", + " 777\n", + " 777\n", + " 777\n", + " 777" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fill!(tempvar,777)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "approxcos (generic function with 1 method)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function approxcos(x)\n", + " outval = Array{Any}(undef,size(x))\n", + " \n", + " ii=0\n", + " for aa in x\n", + " y = 1 - aa^2/2 + aa^4/24 - aa^6/720 + aa^8/(56*720)\n", + " ii = ii + 1\n", + " outval[ii] = y\n", + " end\n", + " return outval\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "x1 = 4*rand(10)\n", + "\n", + "x2 = range(0., stop=4., step=0.01)\n", + "\n", + "y1 = approxcos(x1)\n", + "y2 = cos.(x2);" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Plots.GRBackend()" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "using Plots\n", + "gr()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "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" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "scatter(x1,y1,legend=:false, title=\"Ilustrating 6-th order approximation to cos\")\n", + "\n", + "plot!(x2,y2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 1.2.0", + "language": "julia", + "name": "julia-1.2" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "1.2.0" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}