summaryrefslogtreecommitdiff
path: root/Higher_Engineering_Mathematics_by_B._S._Grewal/chapter24_2.ipynb
blob: 9dd44a2c2d5c78690531f144e7d82fcc7883febe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 24: Numerical Methods"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Example 24.1, page no. 636"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Finding roots of this equation by bisection method\n",
      "f(2) is -ve and f(3) is +ve so root lies between 2 and 3\n",
      "The root is:  2.6875\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "x = numpy.poly([0])\n",
    "p = x**3-4*x-9\n",
    "print \"Finding roots of this equation by bisection method\"\n",
    "print 'f(2) is -ve and f(3) is +ve so root lies between 2 and 3'\n",
    "l = 2.\n",
    "m = 3.\n",
    "def f(x):\n",
    "    y = x**3-4*x-9\n",
    "    return y\n",
    "for i in range(1,5):\n",
    "    k = 1.0/2.*(l+m)\n",
    "    if(f(k)<0):\n",
    "        l = k\n",
    "    else:\n",
    "        m = k\n",
    "print \"The root is: \", k"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Example 24.3, page no. 638"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f(x)=xeˆx−cos(x)\n",
      "We are required to find the roots of f(x) by the method of false position \n",
      "f(0)=−ve and f(1)=+ve so s root lie between 0 and 1 \n",
      "finding the roots by false position method \n",
      "The root of the equation is :\n",
      "0.517747878322\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "print \"f(x)=xeˆx−cos(x)\"\n",
    "def f(x):\n",
    "    y = x*math.e**(x)-math.cos(x)\n",
    "    return y\n",
    "print \"We are required to find the roots of f(x) by the method of false position \"\n",
    "print \"f(0)=−ve and f(1)=+ve so s root lie between 0 and 1 \"\n",
    "print \"finding the roots by false position method \"\n",
    "l = 0.0\n",
    "m = 1.0\n",
    "for i in range(1,11):\n",
    "    k = l-(m-l)*f(l)/(f(m)-f(l))\n",
    "    if(f(k)<0):\n",
    "        l = k\n",
    "    else:\n",
    "        m = k\n",
    "print \"The root of the equation is :\"\n",
    "print k"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 24.4, page no. 638"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f(x) = x∗math.log(x)−1.2\n",
      "We are required to find the roots of f(x) by the method of false position \n",
      "f(2)=−ve and f(3)=+ve so s root lie between 2 and 3 \n",
      "finding the roots by false position method \n",
      "The root of the equation is :  2.74063625664\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "print \"f(x) = x∗math.log(x)−1.2\"\n",
    "def f(x):\n",
    "    y = x*math.log10(x)-1.2\n",
    "    return y\n",
    "print \"We are required to find the roots of f(x) by the method of false position \"\n",
    "print \"f(2)=−ve and f(3)=+ve so s root lie between 2 and 3 \"\n",
    "print \"finding the roots by false position method \"\n",
    "l = 2.\n",
    "m = 3.\n",
    "for i in range(1,4):\n",
    "    k = l-(m-l)*f(l)/(f(m)-f(l))\n",
    "    if(f(k)<0):\n",
    "        l = k\n",
    "    else:\n",
    "        m = k\n",
    "print \"The root of the equation is : \",k"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 24.5, page no. 639"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "To find the roots of f(x) = 3x−cos(x)−1 by newtons method \n",
      "f(0)=−ve and f(1) is +ve so a root lies between 0 and 1 \n",
      "Let us take x0 =0.6 as the root is closer to 1 \n",
      "Root is given by r=x0−f(xn)/der(f(xn))\n",
      "Approximated root in each steps are \n",
      "0.607290551153\n",
      "0.607096741973\n",
      "0.607101775605\n"
     ]
    }
   ],
   "source": [
    "import math,numpy\n",
    "from scipy.misc import derivative\n",
    "\n",
    "print \"To find the roots of f(x) = 3x−cos(x)−1 by newtons method \"\n",
    "print \"f(0)=−ve and f(1) is +ve so a root lies between 0 and 1 \"\n",
    "l = 0\n",
    "m = 1\n",
    "def f(x):\n",
    "    y = 3*x-math.cos(x)-1\n",
    "    return y\n",
    "x0 = 0.6\n",
    "print \"Let us take x0 =0.6 as the root is closer to 1 \"\n",
    "print \"Root is given by r=x0−f(xn)/der(f(xn))\"\n",
    "print \"Approximated root in each steps are \"\n",
    "for i in range(1,4):\n",
    "    k = x0-f(x0)/derivative(f,x0)\n",
    "    print k\n",
    "    x0 = k"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 24.6, page no. 640"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "To find square root of 28 by newtons method let x=sqrt(28) ie xˆ2−28=0 \n",
      "To find the roots by newtons method\n",
      "f(5)=−ve and f(6) is +ve so a root lies between 5 and 6 \n",
      "Let us take x0 = 5.5 \n",
      "Root is given by rn=xn−f(xn)/der(f(xn))\n",
      "Approximated root in each steps are\n",
      "5.29545454545\n",
      "5.29150409676\n",
      "5.29150262213\n",
      "5.29150262213\n"
     ]
    }
   ],
   "source": [
    "from scipy.misc import derivative\n",
    "\n",
    "print \"To find square root of 28 by newtons method let x=sqrt(28) ie xˆ2−28=0 \"\n",
    "def f(x):\n",
    "    y = x**2-28\n",
    "    return y\n",
    "print \"To find the roots by newtons method\"\n",
    "print \"f(5)=−ve and f(6) is +ve so a root lies between 5 and 6 \"\n",
    "l = 5\n",
    "m = 6\n",
    "print \"Let us take x0 = 5.5 \"\n",
    "print \"Root is given by rn=xn−f(xn)/der(f(xn))\"\n",
    "print \"Approximated root in each steps are\"\n",
    "x0 = 5.5\n",
    "for i in range(1,5):\n",
    "    k = x0-f(x0)/derivative(f,x0)\n",
    "    print k\n",
    "    x0 = k"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 24.7, page no. 641"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "To find square root of 28 by newtons method let x=sqrt(28) ie xˆ2−28=0 \n",
      "To find the roots by newtons method\n",
      "f(5)=−ve and f(6) is +ve so a root lies between 5 and 6 \n",
      "Let us take x0 = 5.5 \n",
      "Root is given by rn=xn−f(xn)/der(f(xn))\n",
      "Approximated root in each steps are\n",
      "5.29545454545\n",
      "5.29150409676\n",
      "5.29150262213\n",
      "5.29150262213\n"
     ]
    }
   ],
   "source": [
    "from scipy.misc import derivative\n",
    "\n",
    "print \"To find square root of 28 by newtons method let x=sqrt(28) ie xˆ2−28=0 \"\n",
    "def f(x):\n",
    "    y = x**2-28\n",
    "    return y\n",
    "print \"To find the roots by newtons method\"\n",
    "print \"f(5)=−ve and f(6) is +ve so a root lies between 5 and 6 \"\n",
    "l = 5\n",
    "m = 6\n",
    "print \"Let us take x0 = 5.5 \"\n",
    "print \"Root is given by rn=xn−f(xn)/der(f(xn))\"\n",
    "print \"Approximated root in each steps are\"\n",
    "x0 = 5.5\n",
    "for i in range(1,5):\n",
    "    k = x0-f(x0)/derivative(f,x0)\n",
    "    print k\n",
    "    x0 = k"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}