{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Solutions to Quiz 06" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Imports\n", "import math" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note to the T.A.s** : Only the output of this file should match with the student's answers. Note that they are free to use calculators to do the problems. This code is just so that I don't have to do the computations myself." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Solution to Problem 1 for each batch\n", "def solve1(f, b, e, l) :\n", " \"\"\"Solves problem 1\n", " f : the function\n", " b : integrate from\n", " e : integrate to\n", " l : list of number of divisions of the interval to apply\n", " trapezoidal rule.\n", " \"\"\"\n", " for d in l :\n", " print \"Computing using %d divisions.\\n__________________________\" \\\n", " % d\n", " h = float(e - b)/d\n", " integral = 0\n", " for i in range(d) :\n", " print \"Computing trapezoid %d...\" % (i + 1)\n", " base1 = b + i * h\n", " base2 = b + (i+1) * h\n", " f1 = f(base1)\n", " f2 = f(base2)\n", " print \"Corners of the trapezoid :\", (base1, 0), (base2, 0),\\\n", " (base2, f2), (base1, f1),\n", " a = 0.5 * (f2 + f1) * h\n", " print \"\\tArea of the trapezoid :\", a\n", " integral += a\n", " print \"Integral with %d divisions = %g.\" % (d, integral)\n", " print \"-\"*40\n", " print\n", " return None" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "# Solution to problem 2 for each batch\n", "def solve2(f, x, lh) :\n", " \"\"\" Solves problem 2\n", " f : Function to differentiate\n", " x : point at which we want the derivative\n", " lh : lists of deviations, h to consider\n", " \"\"\"\n", " \n", " for h in lh :\n", " fxph = f(x + h) # f (of) x plus h\n", " fxmh = f(x - h) # f (of) x minus h\n", " dfwh = float(fxph - fxmh)/(2*h) # derivative (of) f with h\n", " print \"h = %6g, f(x + h) = %15g, f(x - h) = %15g\" % (h, fxph, fxmh)\n", " print \"Approximate derivative = (f(x + h) - f(x - h))/(2h) = %g\" % dfwh\n", " print \"-\"*40\n", " print\n", " return None" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "# Common variables\n", "l = [2, 3, 4]\n", "lh = [.5, .001]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Solutions for batch 1" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Solution to problem 1\"\n", "print \"_____________________\"\n", "solve1(lambda x : x*x, 0, 1, l)\n", "print\n", "print \"Solution to problem 2\"\n", "print \"_____________________\"\n", "solve2(lambda x : x**4, 1, lh)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Solution to problem 1\n", "_____________________\n", "Computing using 2 divisions.\n", "__________________________\n", "Computing trapezoid 1...\n", "Corners of the trapezoid : (0.0, 0) (0.5, 0) (0.5, 0.25) (0.0, 0.0) \tArea of the trapezoid : 0.0625\n", "Computing trapezoid 2...\n", "Corners of the trapezoid : (0.5, 0) (1.0, 0) (1.0, 1.0) (0.5, 0.25) \tArea of the trapezoid : 0.3125\n", "Integral with 2 divisions = 0.375.\n", "----------------------------------------\n", "\n", "Computing using 3 divisions.\n", "__________________________\n", "Computing trapezoid 1...\n", "Corners of the trapezoid : (0.0, 0) (0.3333333333333333, 0) (0.3333333333333333, 0.1111111111111111) (0.0, 0.0) \tArea of the trapezoid : 0.0185185185185\n", "Computing trapezoid 2...\n", "Corners of the trapezoid : (0.3333333333333333, 0) (0.6666666666666666, 0) (0.6666666666666666, 0.4444444444444444) (0.3333333333333333, 0.1111111111111111) \tArea of the trapezoid : 0.0925925925926\n", "Computing trapezoid 3...\n", "Corners of the trapezoid : (0.6666666666666666, 0) (1.0, 0) (1.0, 1.0) (0.6666666666666666, 0.4444444444444444) \tArea of the trapezoid : 0.240740740741\n", "Integral with 3 divisions = 0.351852.\n", "----------------------------------------\n", "\n", "Computing using 4 divisions.\n", "__________________________\n", "Computing trapezoid 1...\n", "Corners of the trapezoid : (0.0, 0) (0.25, 0) (0.25, 0.0625) (0.0, 0.0) \tArea of the trapezoid : 0.0078125\n", "Computing trapezoid 2...\n", "Corners of the trapezoid : (0.25, 0) (0.5, 0) (0.5, 0.25) (0.25, 0.0625) \tArea of the trapezoid : 0.0390625\n", "Computing trapezoid 3...\n", "Corners of the trapezoid : (0.5, 0) (0.75, 0) (0.75, 0.5625) (0.5, 0.25) \tArea of the trapezoid : 0.1015625\n", "Computing trapezoid 4...\n", "Corners of the trapezoid : (0.75, 0) (1.0, 0) (1.0, 1.0) (0.75, 0.5625) \tArea of the trapezoid : 0.1953125\n", "Integral with 4 divisions = 0.34375.\n", "----------------------------------------\n", "\n", "\n", "Solution to problem 2\n", "_____________________\n", "h = 0.5, f(x + h) = 5.0625, f(x - h) = 0.0625\n", "Approximate derivative = (f(x + h) - f(x - h))/(2h) = 5\n", "----------------------------------------\n", "\n", "h = 0.001, f(x + h) = 1.00401, f(x - h) = 0.996006\n", "Approximate derivative = (f(x + h) - f(x - h))/(2h) = 4\n", "----------------------------------------\n", "\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Solutions for batch 2" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Solution to problem 1\"\n", "print \"_____________________\"\n", "solve1(lambda x : 1.0/x, 1, 2, l)\n", "print\n", "print \"Solution to problem 2\"\n", "print \"_____________________\"\n", "solve2(lambda x : x*x*(x-1), 1, lh)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Solution to problem 1\n", "_____________________\n", "Computing using 2 divisions.\n", "__________________________\n", "Computing trapezoid 1...\n", "Corners of the trapezoid : (1.0, 0) (1.5, 0) (1.5, 0.6666666666666666) (1.0, 1.0) \tArea of the trapezoid : 0.416666666667\n", "Computing trapezoid 2...\n", "Corners of the trapezoid : (1.5, 0) (2.0, 0) (2.0, 0.5) (1.5, 0.6666666666666666) \tArea of the trapezoid : 0.291666666667\n", "Integral with 2 divisions = 0.708333.\n", "----------------------------------------\n", "\n", "Computing using 3 divisions.\n", "__________________________\n", "Computing trapezoid 1...\n", "Corners of the trapezoid : (1.0, 0) (1.3333333333333333, 0) (1.3333333333333333, 0.75) (1.0, 1.0) \tArea of the trapezoid : 0.291666666667\n", "Computing trapezoid 2...\n", "Corners of the trapezoid : (1.3333333333333333, 0) (1.6666666666666665, 0) (1.6666666666666665, 0.6000000000000001) (1.3333333333333333, 0.75) \tArea of the trapezoid : 0.225\n", "Computing trapezoid 3...\n", "Corners of the trapezoid : (1.6666666666666665, 0) (2.0, 0) (2.0, 0.5) (1.6666666666666665, 0.6000000000000001) \tArea of the trapezoid : 0.183333333333\n", "Integral with 3 divisions = 0.7.\n", "----------------------------------------\n", "\n", "Computing using 4 divisions.\n", "__________________________\n", "Computing trapezoid 1...\n", "Corners of the trapezoid : (1.0, 0) (1.25, 0) (1.25, 0.8) (1.0, 1.0) \tArea of the trapezoid : 0.225\n", "Computing trapezoid 2...\n", "Corners of the trapezoid : (1.25, 0) (1.5, 0) (1.5, 0.6666666666666666) (1.25, 0.8) \tArea of the trapezoid : 0.183333333333\n", "Computing trapezoid 3...\n", "Corners of the trapezoid : (1.5, 0) (1.75, 0) (1.75, 0.5714285714285714) (1.5, 0.6666666666666666) \tArea of the trapezoid : 0.154761904762\n", "Computing trapezoid 4...\n", "Corners of the trapezoid : (1.75, 0) (2.0, 0) (2.0, 0.5) (1.75, 0.5714285714285714) \tArea of the trapezoid : 0.133928571429\n", "Integral with 4 divisions = 0.697024.\n", "----------------------------------------\n", "\n", "\n", "Solution to problem 2\n", "_____________________\n", "h = 0.5, f(x + h) = 1.125, f(x - h) = -0.125\n", "Approximate derivative = (f(x + h) - f(x - h))/(2h) = 1.25\n", "----------------------------------------\n", "\n", "h = 0.001, f(x + h) = 0.001002, f(x - h) = -0.000998001\n", "Approximate derivative = (f(x + h) - f(x - h))/(2h) = 1\n", "----------------------------------------\n", "\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Solutions for batch 3" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Solution to problem 1\"\n", "print \"_____________________\"\n", "solve1(lambda x : 3*x*x + 2*x, 0, 1, l)\n", "print\n", "print \"Solution to problem 2\"\n", "print \"_____________________\"\n", "solve2(lambda x : math.sin(x), math.pi/3.0, lh)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Solution to problem 1\n", "_____________________\n", "Computing using 2 divisions.\n", "__________________________\n", "Computing trapezoid 1...\n", "Corners of the trapezoid : (0.0, 0) (0.5, 0) (0.5, 1.75) (0.0, 0.0) \tArea of the trapezoid : 0.4375\n", "Computing trapezoid 2...\n", "Corners of the trapezoid : (0.5, 0) (1.0, 0) (1.0, 5.0) (0.5, 1.75) \tArea of the trapezoid : 1.6875\n", "Integral with 2 divisions = 2.125.\n", "----------------------------------------\n", "\n", "Computing using 3 divisions.\n", "__________________________\n", "Computing trapezoid 1...\n", "Corners of the trapezoid : (0.0, 0) (0.3333333333333333, 0) (0.3333333333333333, 1.0) (0.0, 0.0) \tArea of the trapezoid : 0.166666666667\n", "Computing trapezoid 2...\n", "Corners of the trapezoid : (0.3333333333333333, 0) (0.6666666666666666, 0) (0.6666666666666666, 2.6666666666666665) (0.3333333333333333, 1.0) \tArea of the trapezoid : 0.611111111111\n", "Computing trapezoid 3...\n", "Corners of the trapezoid : (0.6666666666666666, 0) (1.0, 0) (1.0, 5.0) (0.6666666666666666, 2.6666666666666665) \tArea of the trapezoid : 1.27777777778\n", "Integral with 3 divisions = 2.05556.\n", "----------------------------------------\n", "\n", "Computing using 4 divisions.\n", "__________________________\n", "Computing trapezoid 1...\n", "Corners of the trapezoid : (0.0, 0) (0.25, 0) (0.25, 0.6875) (0.0, 0.0) \tArea of the trapezoid : 0.0859375\n", "Computing trapezoid 2...\n", "Corners of the trapezoid : (0.25, 0) (0.5, 0) (0.5, 1.75) (0.25, 0.6875) \tArea of the trapezoid : 0.3046875\n", "Computing trapezoid 3...\n", "Corners of the trapezoid : (0.5, 0) (0.75, 0) (0.75, 3.1875) (0.5, 1.75) \tArea of the trapezoid : 0.6171875\n", "Computing trapezoid 4...\n", "Corners of the trapezoid : (0.75, 0) (1.0, 0) (1.0, 5.0) (0.75, 3.1875) \tArea of the trapezoid : 1.0234375\n", "Integral with 4 divisions = 2.03125.\n", "----------------------------------------\n", "\n", "\n", "Solution to problem 2\n", "_____________________\n", "h = 0.5, f(x + h) = 0.999722, f(x - h) = 0.520296\n", "Approximate derivative = (f(x + h) - f(x - h))/(2h) = 0.479426\n", "----------------------------------------\n", "\n", "h = 0.001, f(x + h) = 0.866525, f(x - h) = 0.865525\n", "Approximate derivative = (f(x + h) - f(x - h))/(2h) = 0.5\n", "----------------------------------------\n", "\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Solutions for batch 4" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Solution to problem 1\"\n", "print \"_____________________\"\n", "solve1(lambda x : 1/(x*x), 1, 2, l)\n", "print\n", "print \"Solution to problem 2\"\n", "print \"_____________________\"\n", "solve2(lambda x : math.log(x), 1, lh)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Solution to problem 1\n", "_____________________\n", "Computing using 2 divisions.\n", "__________________________\n", "Computing trapezoid 1...\n", "Corners of the trapezoid : (1.0, 0) (1.5, 0) (1.5, 0.4444444444444444) (1.0, 1.0) \tArea of the trapezoid : 0.361111111111\n", "Computing trapezoid 2...\n", "Corners of the trapezoid : (1.5, 0) (2.0, 0) (2.0, 0.25) (1.5, 0.4444444444444444) \tArea of the trapezoid : 0.173611111111\n", "Integral with 2 divisions = 0.534722.\n", "----------------------------------------\n", "\n", "Computing using 3 divisions.\n", "__________________________\n", "Computing trapezoid 1...\n", "Corners of the trapezoid : (1.0, 0) (1.3333333333333333, 0) (1.3333333333333333, 0.5625) (1.0, 1.0) \tArea of the trapezoid : 0.260416666667\n", "Computing trapezoid 2...\n", "Corners of the trapezoid : (1.3333333333333333, 0) (1.6666666666666665, 0) (1.6666666666666665, 0.3600000000000001) (1.3333333333333333, 0.5625) \tArea of the trapezoid : 0.15375\n", "Computing trapezoid 3...\n", "Corners of the trapezoid : (1.6666666666666665, 0) (2.0, 0) (2.0, 0.25) (1.6666666666666665, 0.3600000000000001) \tArea of the trapezoid : 0.101666666667\n", "Integral with 3 divisions = 0.515833.\n", "----------------------------------------\n", "\n", "Computing using 4 divisions.\n", "__________________________\n", "Computing trapezoid 1...\n", "Corners of the trapezoid : (1.0, 0) (1.25, 0) (1.25, 0.64) (1.0, 1.0) \tArea of the trapezoid : 0.205\n", "Computing trapezoid 2...\n", "Corners of the trapezoid : (1.25, 0) (1.5, 0) (1.5, 0.4444444444444444) (1.25, 0.64) \tArea of the trapezoid : 0.135555555556\n", "Computing trapezoid 3...\n", "Corners of the trapezoid : (1.5, 0) (1.75, 0) (1.75, 0.32653061224489793) (1.5, 0.4444444444444444) \tArea of the trapezoid : 0.0963718820862\n", "Computing trapezoid 4...\n", "Corners of the trapezoid : (1.75, 0) (2.0, 0) (2.0, 0.25) (1.75, 0.32653061224489793) \tArea of the trapezoid : 0.0720663265306\n", "Integral with 4 divisions = 0.508994.\n", "----------------------------------------\n", "\n", "\n", "Solution to problem 2\n", "_____________________\n", "h = 0.5, f(x + h) = 0.405465, f(x - h) = -0.693147\n", "Approximate derivative = (f(x + h) - f(x - h))/(2h) = 1.09861\n", "----------------------------------------\n", "\n", "h = 0.001, f(x + h) = 0.0009995, f(x - h) = -0.0010005\n", "Approximate derivative = (f(x + h) - f(x - h))/(2h) = 1\n", "----------------------------------------\n", "\n" ] } ], "prompt_number": 8 } ], "metadata": {} } ] }