{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Quiz 2 : Batch 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the value of $\\pi$ upto $5$ decimal places by doing the following steps.\n", "\n", "1. Write a **function** to compute arctan of a number $x$, $-1 < x < 1$ using the formula\n", " \\begin{equation}\n", " \\arctan(x) = x - \\frac{x^3}{3} + \\frac{x^5}{5} - \\dotsb\n", " \\end{equation}\n", " The function should have $x$ and a positive integer $N$ as input. $N$ is the number of terms of the series to compute. Call this function \n", " *compute_arctan*.\n", " \n", "1. Then use this function (with $N = 20$) to compute the value of pi using the following formula :\n", " \\begin{equation}\n", " \\pi = 4 * \\arctan\\left(\\frac{1}{2}\\right) + 4 * \\arctan\\left(\\frac{1}{3}\\right).\n", " \\end{equation}\n", " \n", "1. Print the answer to 5 decimal places." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Grading scheme.\n", "* Total points : 10 marks\n", "* -5 if any kind of loop (while or for) is not used to compute the sum\n", "* -2 if the logic is correct but the code is not running due to syntax error\n", "* -1 is not upto the specification of 5 decimal spaces." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def compute_arctan(x, N) :\n", " t = float(x)\n", " s = 0.0\n", " d = 1.0\n", " for i in range(N) :\n", " s += t/d\n", " t *= - x*x\n", " d += 2\n", " return s" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "val_of_pi = 4 * compute_arctan(1.0/2, 20) + 4 * compute_arctan(1.0/3, 20)\n", "print \"The value of pi is %7.5f.\" % val_of_pi" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of pi is 3.14159.\n" ] } ], "prompt_number": 2 } ], "metadata": {} } ] }