From 25f2a146e3cde58231bf76009470ce5702179e25 Mon Sep 17 00:00:00 2001 From: Eduardo Cueto Mendoza Date: Sat, 23 May 2020 18:18:32 -0600 Subject: [PATCH] [ADD] week three finishing notebook --- COVID_data_proc.ipynb | 40 +++ Week3_PR_Data.dat | 15 + Week3_PR_Template.ipynb | 588 ++++++++++++++++++++++++++++++++++ coursera_week2_function.ipynb | 64 ++-- 4 files changed, 675 insertions(+), 32 deletions(-) create mode 100644 Week3_PR_Data.dat create mode 100644 Week3_PR_Template.ipynb diff --git a/COVID_data_proc.ipynb b/COVID_data_proc.ipynb index b5a89ba..6d23384 100644 --- a/COVID_data_proc.ipynb +++ b/COVID_data_proc.ipynb @@ -2153,6 +2153,46 @@ ")" ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-2:2" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "-2:2" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "21" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "length(-5:.5:5)" + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/Week3_PR_Data.dat b/Week3_PR_Data.dat new file mode 100644 index 0000000..a908a56 --- /dev/null +++ b/Week3_PR_Data.dat @@ -0,0 +1,15 @@ +.1268004831284406 -1.6416953879765301 +.5013092807380928 -.9776975375269383 +1.5280121125586477 .5277112203195138 +1.7001225303407743 1.711524991194374 +1.9924936253216172 1.8910000148140624 +2.706075824201991 -.46342779446395 +2.9949319274309043 -.4435666186385725 +3.4918528112833935 -1.275179133203867 +3.501191722475427 -.6904995966451337 +4.459924502120439 -5.51613079927097 +4.936965850879389 -6.001703074115855 +5.023289852369695 -8.364169009651015 +5.042336980089736 -7.924477516763416 +5.507392850419521 -10.774823709545498 +5.568665171088307 -10.917187797703853 diff --git a/Week3_PR_Template.ipynb b/Week3_PR_Template.ipynb new file mode 100644 index 0000000..ee013df --- /dev/null +++ b/Week3_PR_Template.ipynb @@ -0,0 +1,588 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Week 3 - Fitting a curve" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Plots.GRBackend()" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# You may need this setup\n", + "using Plots\n", + "gr() # Activate the GR backend for use with Plots" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "using DelimitedFiles" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Import the supplied data representing 15 pairs to x- and y-values. " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "data_tofit = readdlm(\"Week3_PR_Data.dat\", '\\t');" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.1268004831284406, -1.6416953879765301]\n", + "[0.5013092807380928, -0.9776975375269383]\n", + "[1.5280121125586477, 0.5277112203195138]\n", + "[1.7001225303407743, 1.711524991194374]\n", + "[1.9924936253216172, 1.8910000148140624]\n", + "[2.706075824201991, -0.46342779446395]\n", + "[2.9949319274309043, -0.4435666186385725]\n", + "[3.4918528112833935, -1.275179133203867]\n", + "[3.501191722475427, -0.6904995966451337]\n", + "[4.459924502120439, -5.51613079927097]\n", + "[4.936965850879389, -6.001703074115855]\n", + "[5.023289852369695, -8.364169009651015]\n", + "[5.042336980089736, -7.924477516763416]\n", + "[5.507392850419521, -10.774823709545498]\n", + "[5.568665171088307, -10.917187797703853]\n" + ] + } + ], + "source": [ + "for i = 1:size(data_tofit)[1]\n", + " println(data_tofit[i,:])\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "x, y = data_tofit[:,1], data_tofit[:,2];" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Then we do a scatterplot, this gives us the points the line must go through." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Plot the x and y data points using a scatter plot of the x and y array variables\n", + "plot(x,y,line=:scatter,legend=:false)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For the line, we need a function, which we now define. Note that the parameters a, b, c need not be passed to the function: we will keep resetting them to try to improve the fit." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "parabfit (generic function with 1 method)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create a function called parabfit, with x as the argument, returning a*x^2 + b*x + c\n", + "function parabfit(x)\n", + " return a*x^2 + b*x + c\n", + "end" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's check that we do get a reasonable parabola. Choose your own interval [xmin, xmax] and parameters a, b, c. If it looks too much like a straight line, chance your choices until it does." + ] + }, + { + "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" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create variables a, b and c, assigning each the value 1\n", + "a = 1\n", + "b = 1\n", + "c = 1\n", + "\n", + "f = Array{Float64}(undef,length(-5:0.5:5))\n", + "\n", + "# Plot the function parabfit, for x values between -5 and 5 \n", + "i = 1\n", + "for j = -5:0.5:5\n", + " f[i] = parabfit(j) \n", + " i = i + 1\n", + "end\n", + "\n", + "plot(-5:0.5:5,f)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we choose a, b, c and plot the curve together with the points. \n", + "\n", + "Note that by looking at where the data points lie, we can deduce some of the properties for a, b, c, as follows.\n", + "\n", + "The plot must have a y-intersection that is close to 0, so c is close to 0. Also, the parabola is open downwards, so a must be negative. Finally, it has its maximum at a positive x, so b must be positive. \n", + "\n", + "Use plot() to start with the scatter plot and plot!() to add the curve for parabfit. (There are other ways to do this ...)" + ] + }, + { + "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", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# More plot!() tries.\n", + "rangez = 0:0.5:6\n", + "a,b,c = -1,4,-3\n", + "\n", + "f = Array{Float64}(undef,length(rangez))\n", + "\n", + "# Plot the function parabfit, for x values between -5 and 5 \n", + "i = 1\n", + "for j = rangez\n", + " f[i] = parabfit(j) \n", + " i = i + 1\n", + "end\n", + "plot(x,y,line=:scatter,legend=:false)\n", + "plot!(rangez,f)" + ] + }, + { + "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 +} diff --git a/coursera_week2_function.ipynb b/coursera_week2_function.ipynb index 0cfc33a..9e6026d 100644 --- a/coursera_week2_function.ipynb +++ b/coursera_week2_function.ipynb @@ -79,105 +79,105 @@ "\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",