{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Lab 8 Taylor Series" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.9999987317275388\n" ] } ], "source": [ "from math import factorial\n", "\n", "approx = 1\n", "x = 3.14\n", "for i in range(1,20):\n", " term = ((-1)**i) * ((x**(i*2)) / (factorial(i*2)))\n", " approx = approx + term\n", "print(approx)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.9999987317275395" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "math.cos(3.14)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Volume in drive C is Windows\n", " Volume Serial Number is B899-AB94\n", "\n", " Directory of C:\\Users\\student\\Desktop\n", "\n", "11/20/2019 09:58 AM .\n", "11/20/2019 09:58 AM ..\n", "11/20/2019 09:35 AM .ipynb_checkpoints\n", "11/20/2019 09:57 AM __pycache__\n", "11/20/2019 08:56 AM 1,457 Kaltura Capture.lnk\n", "05/13/2016 12:33 PM 210 MSDS Online.url\n", "11/20/2019 09:56 AM 352 myfuncs.py\n", "08/01/2018 03:14 PM Safety Information\n", "11/20/2019 08:49 AM 1,490 Week10_notes_numerical_methods.ipynb\n", "11/20/2019 09:58 AM 4,418 Week9_Notes_Taylor_Series_Lab_Intro.ipynb\n", " 5 File(s) 7,927 bytes\n", " 5 Dir(s) 321,499,787,264 bytes free\n" ] } ], "source": [ "%ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### In a file called myfuncs.py\n", "\n", "```\n", "# myfuncs.py\n", "\"\"\"\n", "A python that contains a function that calculates the approximation of the cosine function using a taylor series\n", "\"\"\"\n", "\n", "from math import factorial\n", "\n", "def approx_cos(x,n_terms):\n", " approx = 1\n", " for i in range(1,n_terms+1):\n", " term = ((-1)**i) * ((x**(i*2)) / (factorial(i*2)))\n", " approx = approx + term\n", " \n", " return approx\n", "```" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "import myfuncs" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.9999987317275388" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from myfuncs import approx_cos\n", "\n", "approx_cos(3.14,19)\n", "\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "after 1 terms, the difference between cos(x) and aprox func is 2.9298012682724606\n", "after 2 terms, the difference between cos(x) and aprox func is 1.1206867383942072\n", "after 3 terms, the difference between cos(x) and aprox func is 0.2105196466234821\n", "after 4 terms, the difference between cos(x) and aprox func is 0.023858254692953773\n", "after 5 terms, the difference between cos(x) and aprox func is 0.0018181048161521884\n", "after 6 terms, the difference between cos(x) and aprox func is 9.976362487795765e-05\n", "after 7 terms, the difference between cos(x) and aprox func is 4.134263480115585e-06\n", "after 8 terms, the difference between cos(x) and aprox func is 1.3403493681618528e-07\n", "after 9 terms, the difference between cos(x) and aprox func is 3.493543787591591e-09\n", "after 10 terms, the difference between cos(x) and aprox func is 7.481359975969326e-11\n", "after 11 terms, the difference between cos(x) and aprox func is 1.3391510123028638e-12\n", "after 12 terms, the difference between cos(x) and aprox func is 2.1094237467877974e-14\n", "after 13 terms, the difference between cos(x) and aprox func is 4.440892098500626e-16\n", "after 14 terms, the difference between cos(x) and aprox func is 6.661338147750939e-16\n", "after 15 terms, the difference between cos(x) and aprox func is 6.661338147750939e-16\n", "after 16 terms, the difference between cos(x) and aprox func is 6.661338147750939e-16\n", "after 17 terms, the difference between cos(x) and aprox func is 6.661338147750939e-16\n", "after 18 terms, the difference between cos(x) and aprox func is 6.661338147750939e-16\n", "after 19 terms, the difference between cos(x) and aprox func is 6.661338147750939e-16\n" ] } ], "source": [ "for n in range(1,20):\n", " approx = approx_cos(3.14,n)\n", " exact = math.cos(3.14)\n", " print(f'after {n} terms, the difference between cos(x) and aprox func is {abs(exact-approx)}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }