{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Week 9 Notes: Numeric Methods using loops" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.9999980926513672\n" ] } ], "source": [ "approx = 0\n", "\n", "for i in range(1,20):\n", " term = 1/2**i\n", " approx = approx + term\n", "\n", "print(approx)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.7853956633974299\n" ] } ], "source": [ "approx = 0\n", "\n", "for i in range(0,100000):\n", " term = (-1)**i * (1/(2*i+1))\n", " approx = approx + term\n", "\n", "print(approx)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7853981633974483" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "math.pi/4" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a number1.57079\n", "0.9999999999799855\n" ] } ], "source": [ "from math import factorial\n", "\n", "approx = 0\n", "x_str = input('Enter a number')\n", "x = float(x_str)\n", "for i in range(10):\n", " term = ((-1)**i) * ((x**(2*i+1)) / (factorial(2*i+1) ))\n", " #approx = approx + term\n", " approx += term\n", " \n", "print(approx)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9999999999799858" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.sin(1.57079)" ] }, { "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.0" } }, "nbformat": 4, "nbformat_minor": 2 }