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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Chapter 4 : Truncation Errors and the Taylor Series"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example: 4.1 Page No:79"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The value of f at x=0 : 1.2\n",
"The value of f at x=1 due to zero order approximation : 1.2\n",
"Truncation error : -1.0\n",
"----------------------------------------------\n",
"The value of first derivative of f at x=0 : -0.4\n",
"The value of f at x=1 due to first order approximation : 0.8\n",
"Truncation error : -0.6\n",
"----------------------------------------------\n",
"The value of second derivative of f at x=0 : -1.8\n",
"The value of f at x=1 due to second order approximation : -0.1\n",
"Truncation error : 0.3\n",
"----------------------------------------------\n",
"The value of third derivative of f at x=0 : -0.9\n",
"The value of f at x=1 due to third order approximation : -0.25\n",
"Truncation error : 0.45\n",
"----------------------------------------------\n",
"The value of fourth derivative of f at x=0 : -2.4\n",
"The value of f at x=1 due to fourth order approximation : -0.35\n",
"Truncation error : 0.55\n",
"----------------------------------------------\n"
]
}
],
"source": [
"from math import factorial\n",
"from scipy.misc import derivative\n",
"def f(x):\n",
" y=-0.1*x**4-0.15*x**3-0.5*x**2-0.25*x+1.2#\n",
" return y\n",
"xi=0#\n",
"xf=1#\n",
"h=xf-xi#\n",
"fi=f(xi)##function value at xi\n",
"ffa=f(xf)##actual function value at xf\n",
"\n",
"#for n=0, i.e, zero order approximation\n",
"ff=fi#\n",
"Et_1=ffa-ff##truncation error at x=1\n",
"print \"The value of f at x=0 :\",fi\n",
"print \"The value of f at x=1 due to zero order approximation :\",ff\n",
"print \"Truncation error :\",Et_1\n",
"print \"----------------------------------------------\"\n",
"\n",
"#for n=1, i.e, first order approximation\n",
"def f1(x):\n",
" y=derivative(f,x)\n",
" return y\n",
"f1i=f1(xi)##value of first derivative of function at xi\n",
"f1f=fi+f1i*h##value of first derivative of function at xf\n",
"Et_2=ffa-f1f##truncation error at x=1\n",
"print \"The value of first derivative of f at x=0 :\",f1i\n",
"print \"The value of f at x=1 due to first order approximation :\",f1f\n",
"print \"Truncation error :\",Et_2\n",
"print \"----------------------------------------------\"\n",
"\n",
"\n",
"#for n=2, i.e, second order approximation\n",
"def f2(x):\n",
" y=derivative(f1,x)\n",
" return y\n",
"f2i=f2(xi)##value of second derivative of function at xi\n",
"f2f=f1f+f2i*(h**2)/factorial(2)##value of second derivative of function at xf\n",
"Et_3=ffa-f2f##truncation error at x=1\n",
"print \"The value of second derivative of f at x=0 :\",f2i\n",
"print \"The value of f at x=1 due to second order approximation :\",f2f\n",
"print \"Truncation error :\",Et_3\n",
"print \"----------------------------------------------\"\n",
"\n",
"#for n=3, i.e, third order approximation\n",
"def f3(x):\n",
" y=derivative(f2,x)\n",
" return y\n",
"f3i=f3(xi)##value of third derivative of function at xi\n",
"f3f=f2f+f3i*(h**3)/factorial(3)##value of third derivative of function at xf\n",
"Et_4=ffa-f3f##truncation error at x=1\n",
"print \"The value of third derivative of f at x=0 :\",f3i\n",
"print \"The value of f at x=1 due to third order approximation :\",f3f\n",
"print \"Truncation error :\", Et_4\n",
"print \"----------------------------------------------\"\n",
"\n",
"#for n=4, i.e, fourth order approximation\n",
"def f4(x):\n",
" y=derivative(f3,x)\n",
" return y\n",
"f4i=f4(xi)##value of fourth derivative of function at xi\n",
"f4f=f3f+f4i*(h**4)/factorial(4)##value of fourth derivative of function at xf\n",
"Et_5=ffa-f4f##truncation error at x=1\n",
"print \"The value of fourth derivative of f at x=0 :\",f4i\n",
"print \"The value of f at x=1 due to fourth order approximation :\",f4f\n",
"print \"Truncation error :\",Et_5\n",
"print \"----------------------------------------------\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 4.2: Page No:82"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The value of f at x=1 due to zero order approximation : 0.707106781187\n",
"% relative error : -41.4213562373\n",
"----------------------------------------------\n",
"The value of f at x=1 due to first order approximation : 0.551333569463\n",
"% relative error : -10.2667138927\n",
"----------------------------------------------\n",
"The value of f at x=1 due to second order approximation : 0.534175415889\n",
"% relative error : -6.83508317772\n",
"----------------------------------------------\n",
"The value of f at x=1 due to third order approximation : 0.535435376789\n",
"% relative error : -7.08707535775\n",
"----------------------------------------------\n",
"The value of f at x=1 due to fourth order approximation : 0.535504768061\n",
"% relative error : -7.10095361216\n",
"----------------------------------------------\n",
"The value of f at x=1 due to fifth order approximation : 0.535501917392\n",
"% relative error : -7.10038347839\n",
"----------------------------------------------\n",
"The value of f at x=1 due to sixth order approximation : 0.535501819651\n",
"% relative error : -7.10036393016\n",
"----------------------------------------------\n"
]
}
],
"source": [
"from math import pi,cos,factorial\n",
"from scipy.misc import derivative\n",
"def f(x):\n",
" y=cos(x)\n",
" return y\n",
"xi=pi/4#\n",
"xf=pi/3#\n",
"h=xf-xi#\n",
"fi=f(xi)##function value at xi\n",
"ffa=f(xf)##actual function value at xf\n",
"\n",
"#for n=0, i.e, zero order approximation\n",
"ff=fi#\n",
"et1=(ffa-ff)*100/ffa##percent relative error at x=1\n",
"print \"The value of f at x=1 due to zero order approximation :\",ff\n",
"print \"% relative error :\",et1\n",
"print \"----------------------------------------------\"\n",
"\n",
"#for n=1, i.e, first order approximation\n",
"def f1(x):\n",
" y=derivative(f,x)\n",
" return y\n",
"f1i=f1(xi)##value of first derivative of function at xi\n",
"f1f=fi+f1i*h##value of first derivative of function at xf\n",
"et2=(ffa-f1f)*100/ffa##% relative error at x=1\n",
"print \"The value of f at x=1 due to first order approximation :\",f1f\n",
"print \"% relative error :\",et2\n",
"print \"----------------------------------------------\"\n",
"\n",
"\n",
"#for n=2, i.e, second order approximation\n",
"def f2(x):\n",
" y=derivative(f1,x)\n",
" return y\n",
"f2i=f2(xi)##value of second derivative of function at xi\n",
"f2f=f1f+f2i*(h**2)/factorial(2)##value of second derivative of function at xf\n",
"et3=(ffa-f2f)*100/ffa##% relative error at x=1\n",
"print \"The value of f at x=1 due to second order approximation :\",f2f\n",
"print \"% relative error :\",et3\n",
"print \"----------------------------------------------\"\n",
"\n",
"\n",
"#for n=3, i.e, third order approximation\n",
"def f3(x):\n",
" y=derivative(f2,x)\n",
" return y\n",
"f3i=f3(xi)##value of third derivative of function at xi\n",
"f3f=f2f+f3i*(h**3)/factorial(3)##value of third derivative of function at xf\n",
"et4=(ffa-f3f)*100/ffa##% relative error at x=1\n",
"print \"The value of f at x=1 due to third order approximation :\",f3f\n",
"print \"% relative error :\",et4\n",
"print \"----------------------------------------------\"\n",
"\n",
"\n",
"#for n=4, i.e, fourth order approximation\n",
"def f4(x):\n",
" y=derivative(f3,x)\n",
" return y\n",
"f4i=f4(xi)##value of fourth derivative of function at xi\n",
"f4f=f3f+f4i*(h**4)/factorial(4)##value of fourth derivative of function at xf\n",
"et5=(ffa-f4f)*100/ffa##% relative error at x=1\n",
"print \"The value of f at x=1 due to fourth order approximation :\",f4f\n",
"print \"% relative error :\",et5\n",
"print \"----------------------------------------------\"\n",
"\n",
"\n",
"#for n=5, i.e, fifth order approximation\n",
"f5i=(f4(1.1*xi)-f4(0.9*xi))/(2*0.1)##value of fifth derivative of function at xi (central difference method)\n",
"f5f=f4f+f5i*(h**5)/factorial(5)##value of fifth derivative of function at xf\n",
"et6=(ffa-f5f)*100/ffa##% relative error at x=1\n",
"print \"The value of f at x=1 due to fifth order approximation :\",f5f\n",
"print \"% relative error :\",et6\n",
"print \"----------------------------------------------\"\n",
"\n",
"\n",
"#for n=6, i.e, sixth order approximation\n",
"def f6(x):\n",
" y=derivative(f5,x)\n",
" return y\n",
"f6i=(f4(1.1*xi)-2*f4(xi)+f4(0.9*xi))/(0.1**2)##value of sixth derivative of function at xi (central difference method)\n",
"f6f=f5f+f6i*(h**6)/factorial(6)##value of sixth derivative of function at xf\n",
"et6=(ffa-f6f)*100/ffa##% relative error at x=1\n",
"print \"The value of f at x=1 due to sixth order approximation :\",f6f\n",
"print \"% relative error :\", et6\n",
"print \"----------------------------------------------\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 4.3 : Page No:85"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Input value of m:4\n",
"Input value of h:5\n",
"\n",
"Remainder: 21 \n",
"The value by first order approximation: 1275\n",
"True Value at x2: 1296\n"
]
}
],
"source": [
"from math import pi,cos,factorial\n",
"m=input(\"Input value of m:\")\n",
"h=input(\"Input value of h:\")\n",
"def f(x):\n",
" y=x**m\n",
" return y\n",
"x1=1#\n",
"x2=x1+h#\n",
"fx1=f(x1)#\n",
"fx2=fx1+m*(fx1**(m-1))*h#\n",
"if m==1:\n",
" R=0#\n",
"elif m==2 :\n",
" R=2*(h**2)/factorial(2)#\n",
" \n",
"elif m==3:\n",
" R=(6*(x1)*(h**2)/factorial(2))+(6*(h**3)/factorial(3))#\n",
" \n",
"elif m==4:\n",
" R=(12*(x1**2)*(h**2)/factorial(2))+(24*(x1)*(h**3)/factorial(3))+(24*(h**4)/factorial(4))\n",
" \n",
"print \"\\nRemainder:\",fx2,\"\\nThe value by first order approximation:\",R\n",
"print \"True Value at x2:\",f(x2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 4.4: Page No:92"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Input h:1.232323\n",
"For h= 1.232323\n",
"and percent error= -2.70944264922 Derivative at x by forward difference method= 114.60931875\n",
"and percent error= -0.178591334206 Derivative at x by backward difference method= 85.854151746\n",
"and percent error= -1.44401699172 Derivative at x by central difference method= 14.3775835022\n"
]
}
],
"source": [
"from scipy.misc import derivative\n",
"def f(x):\n",
" y=-0.1*(x**4)-0.15*(x**3)-0.5*(x**2)-0.25*(x)+1.2\n",
" return y\n",
"x=0.5#\n",
"h=input(\"Input h:\")\n",
"x1=x-h#\n",
"x2=x+h#\n",
"#forward difference method\n",
"fdx1=(f(x2)-f(x))/h##derivative at x\n",
"et1=abs((fdx1-derivative(f,x))/derivative(f,x))*100#\n",
"#backward difference method\n",
"fdx2=(f(x)-f(x1))/h##derivative at x\n",
"et2=abs((fdx2-derivative(f,x))/derivative(f,x))*100#\n",
"#central difference method\n",
"fdx3=(f(x2)-f(x1))/(2*h)##derivative at x\n",
"et3=abs((fdx3-derivative(f,x))/derivative(f,x))*100#\n",
"print \"For h=\",h\n",
"print \"and percent error=\",fdx1,\"Derivative at x by forward difference method=\",et1\n",
"print \"and percent error=\",fdx2,\"Derivative at x by backward difference method=\",et2\n",
"print \"and percent error=\",fdx3,\"Derivative at x by central difference method=\",et3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 4.5: Page No: 95"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"true value is between : 15.4275 and 15.8225\n"
]
}
],
"source": [
"from scipy.misc import derivative\n",
"def f(x):\n",
" y=x**3\n",
" return y\n",
"x=2.5#\n",
"delta=0.01#\n",
"deltafx=abs(derivative(f,x))*delta#\n",
"fx=f(x)#\n",
"print \"true value is between : \",fx-deltafx,\"and\",fx+deltafx"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 4.6: Page No: 96"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The value of y is between: 0.528721343471 and 0.596278656529\n",
"ymin is calculated at lower extremes of F, L, E, I values as = 0.524066539965\n",
"ymax is calculated at higher extremes of F, L, E, I values as = 0.602846335915\n"
]
}
],
"source": [
"def f(F,L,E,I):\n",
" y=(F*(L**4))/(8*E*I)\n",
" return y\n",
"Fbar=50##lb/ft\n",
"Lbar=30##ft\n",
"Ebar=1.5*(10**8)##lb/ft**2\n",
"Ibar=0.06##ft**4\n",
"deltaF=2##lb/ft\n",
"deltaL=0.1##ft\n",
"deltaE=0.01*(10**8)##lb/ft**2\n",
"deltaI=0.0006##ft**4\n",
"ybar=(Fbar*(Lbar**4))/(8*Ebar*Ibar)#\n",
"def f1(F):\n",
" y=(F*(Lbar**4))/(8*Ebar*Ibar)\n",
" return y\n",
"def f2(L):\n",
" y=(Fbar*(L**4))/(8*Ebar*Ibar)\n",
" return y\n",
"def f3(E):\n",
" y=(Fbar*(Lbar**4))/(8*E*Ibar)\n",
" return y\n",
"def f4(I):\n",
" y=(Fbar*(Lbar**4))/(8*Ebar*I)\n",
" return y\n",
"\n",
"deltay=abs(derivative(f1,Fbar))*deltaF+abs(derivative(f2,Lbar))*deltaL+abs(derivative(f3,Ebar))*deltaE+abs(derivative(f4,Ibar))*deltaI#\n",
"\n",
"print \"The value of y is between:\",ybar-deltay,\"and\",ybar+deltay\n",
"ymin=((Fbar-deltaF)*((Lbar-deltaL)**4))/(8*(Ebar+deltaE)*(Ibar+deltaI))#\n",
"ymax=((Fbar+deltaF)*((Lbar+deltaL)**4))/(8*(Ebar-deltaE)*(Ibar-deltaI))#\n",
"print \"ymin is calculated at lower extremes of F, L, E, I values as =\",ymin\n",
"print \"ymax is calculated at higher extremes of F, L, E, I values as =\",ymax"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 4.7 : Page No:98"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The condition number of function for x = 0.18201112073 is : 1.72787595947\n",
"The condition number of function for x = 0.0160083243793 is : 1.58650429006\n"
]
}
],
"source": [
"from math import pi,tan\n",
"from scipy.misc import derivative\n",
"def f(x):\n",
" y=tan(x)\n",
" return y\n",
"x1bar=(pi/2)+0.1*(pi/2)#\n",
"x2bar=(pi/2)+0.01*(pi/2)#\n",
"#computing condition number for x1bar\n",
"condnum1=x1bar*derivative(f,x1bar)/f(x1bar)#\n",
"print \"The condition number of function for x =\",condnum1,\"is :\",x1bar\n",
"if abs(condnum1)>1:\n",
" print \"Function is ill-conditioned for x =\",x1bar\n",
"\n",
"#computing condition number for x2bar\n",
"condnum2=x2bar*derivative(f,x2bar)/f(x2bar)#\n",
"print \"The condition number of function for x =\",condnum2,\"is :\",x2bar\n",
"if abs(condnum2)>1:\n",
" print \"Function is ill-conditioned for x =\",x2bar"
]
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|