Numerical Methods Practical File
Numerical Methods Practical File
PRACTICAL FILE
5. BISECTION METHOD
7. ARRAY
8. DEFINING FUNCTIONS
9. SECANT METHOD
17. INTEGRATION
n = 25
Absolute value of n is 25
n = - 78
Absolute value of n is 78
n = 686 407
n = - 6 274 065
n = 54
54 is even
2
n = 97
97 is odd
n = 78 943
78 943 is odd
n = 3 658 790
SUM OF A SERIES
n = 10
n = 62
n = 5
k = 2
n = 40
k = 2
n = 5
k = 3
n = 75
k = 3
FACTORIAL OF A NUMBER
n=6
Factorial of n is 720
n = 15
n = 30
Factorial of n is 265 252 859 812 191 058 636 308 480 000 000
PRACTICAL-3
DIVISIBILITY , PRIMALITY AND POWER OF NUMBERS
DIVISIBILITY BY 3
n = 30
30 is divisible by 3.
n = 17
17 is not divisible by 3.
2
n = 7 754 982
DIVISIBILITY BY 4
n = 48
48 is divisible by 4.
n = 57
57 is not divisible by 4.
n = 9 845 678
n = 23
23 is prime
n = 46
46 is composite
n = 1
n = 2017
2017 is prime
n = 5 633 480
POWERS OF A NUMBER
n = 3
m = 1000
27
81
243
729
n = 2
m = 500
16
32
64
128
256
PRACTICAL-4
FINDING THE ROOTS OF A QUADRATIC EQUATION
x1 = - 1
x2 = - 1
1
x1 = - 1 + ⅈ 3
2
1
x2 = - 1 + ⅈ 3
2
PRACTICAL-5
BISECTION METHOD
i a b c f [a ] f [b ] f [c ]
0 1. 2. 1.5 - 1. 9. 2.375
1 1. 1.5 1.25 - 1. 2.375 0.328125
2 1. 1.25 1.125 - 1. 0.328125 - 0.419922
3 1.125 1.25 1.1875 - 0.419922 0.328125 - 0.067627
4 1.1875 1.25 1.21875 - 0.067627 0.328125 0.124725
2
i a b c f [a ] f [b ] f [c ]
0 1. 2. 1.5 - 2. 61. 8.39063
1 1. 1.5 1.25 - 2. 8.39063 0.814697
2 1. 1.25 1.125 - 2. 0.814697 - 0.972713
3 1.125 1.25 1.1875 - 0.972713 0.814697 - 0.195847
4 1.1875 1.25 1.21875 - 0.195847 0.814697 0.277085
i a b c f [a ] f [b ] f [c ]
0 1. 2. 1.5 - 1. 9. 2.375
1 1. 1.5 1.25 - 1. 2.375 0.328125
2 1. 1.25 1.125 - 1. 0.328125 - 0.419922
3 1.125 1.25 1.1875 - 0.419922 0.328125 - 0.067627
4 1.1875 1.25 1.21875 - 0.067627 0.328125 0.124725
5 1.1875 1.21875 1.20313 - 0.067627 0.124725 0.0271797
6 1.1875 1.20313 1.19531 - 0.067627 0.0271797 - 0.0205646
7 1.19531 1.20313 1.19922 - 0.0205646 0.0271797 0.00322217
i a b c f [a ] f [b ] f [c ]
0 1. 2. 1.5 - 2. 61. 8.39063
1 1. 1.5 1.25 - 2. 8.39063 0.814697
2 1. 1.25 1.125 - 2. 0.814697 - 0.972713
3 1.125 1.25 1.1875 - 0.972713 0.814697 - 0.195847
4 1.1875 1.25 1.21875 - 0.195847 0.814697 0.277085
5 1.1875 1.21875 1.20313 - 0.195847 0.277085 0.0329448
6 1.1875 1.20313 1.19531 - 0.195847 0.0329448 - 0.0833201
7 1.19531 1.20313 1.19922 - 0.0833201 0.0329448 - 0.025661
8 1.19922 1.20313 1.20117 - 0.025661 0.0329448 0.00352277
PRACTICAL-6
METHOD OF FALSE POSITION
i a b c f [a ] f [b ] f [c ]
0 1. 2. 1.57143 - 4. 3. - 1.36443
1 1.57143 2. 1.70541 - 1.36443 3. - 0.247745
2 1.70541 2. 1.72788 - 0.247745 3. - 0.0393396
3 1.72788 2. 1.7314 - 0.0393396 3. - 0.00611067
4 1.7314 2. 1.73195 - 0.00611067 3. - 0.000945921
2
i a b c f [a ] f [b ] f [c ]
0 0.4 0.48 0.420867 - 3.32232 9.41454 - 2.48159
1 0.420867 0.48 0.433203 - 2.48159 9.41454 - 1.73804
2 0.433203 0.48 0.440496 - 1.73804 9.41454 - 1.15359
3 0.440496 0.48 0.444808 - 1.15359 9.41454 - 0.73541
4 0.444808 0.48 0.447358 - 0.73541 9.41454 - 0.455924
i a b c f [a ] f [b ] f [c ]
0 1. 2. 1.57143 - 4. 3. - 1.36443
1 1.57143 2. 1.70541 - 1.36443 3. - 0.247745
2 1.70541 2. 1.72788 - 0.247745 3. - 0.0393396
3 1.72788 2. 1.7314 - 0.0393396 3. - 0.00611067
4 1.7314 2. 1.73195 - 0.00611067 3. - 0.000945921
5 1.73195 2. 1.73204 - 0.000945921 3. - 0.000146349
i a b c f [a ] f [b ] f [c ]
0 0.4 0.48 0.420867 - 3.32232 9.41454 - 2.48159
1 0.420867 0.48 0.433203 - 2.48159 9.41454 - 1.73804
2 0.433203 0.48 0.440496 - 1.73804 9.41454 - 1.15359
3 0.440496 0.48 0.444808 - 1.15359 9.41454 - 0.73541
4 0.444808 0.48 0.447358 - 0.73541 9.41454 - 0.455924
5 0.447358 0.48 0.448866 - 0.455924 9.41454 - 0.27755
6 0.448866 0.48 0.449757 - 0.27755 9.41454 - 0.167038
7 0.449757 0.48 0.450284 - 0.167038 9.41454 - 0.0998235
8 0.450284 0.48 0.450596 - 0.0998235 9.41454 - 0.0594023
9 0.450596 0.48 0.45078 - 0.0594023 9.41454 - 0.0352587
PRACTICAL-7
ARRAY
{ 56, 7, 34, 8, 5 }
{ 2, 4, 6, 8, 10 }
SORTING OF AN ARRAY
{ 2, 6, 7, 18, 34 }
{ 32, 15, 6, 5, 1 }
In[24]:= r[x_ ] = x ^ 3 - 2 x - 5;
falsepositioneb [r, 2, 3, 0.005 ]
i a b c r [a ] r [b ] r [c ]
0 2. 3. 2.05882 - 1. 16. - 0.3908
1 2.05882 3. 2.08126 - 0.3908 16. - 0.147204
2 2.08126 3. 2.08964 - 0.147204 16. - 0.0546765
3 2.08964 3. 2.09274 - 0.0546765 16. - 0.0202029
4 2.09274 3. 2.09388 - 0.0202029 16. - 0.00745051
5 2.09388 3. 2.09431 - 0.00745051 16. - 0.00274567
PRACTICAL-9
SECANT METHOD
In[4]:= f[x_ ] = x ^ 3 - 2 x - 5;
secant [f, 3, 2, 4]
i c f [c ]
0 2.05882 - 0.3908
1 2.09656 0.0224281
2 2.09451 - 0.000456805
3 2.09455 - 5.15785 × 10 -7
4 2.09455 1.18856 × 10 -11
2
In[9]:= g[x_ ] = x ^ 3 - 2 x - 5;
secanteb [g, 3, 2, 0.0005 ]
i c g [c ]
0 2.05882 - 0.3908
1 2.09656 0.0224281
2 2.09451 - 0.000456805
PRACTICAL-10
NEWTON'S METHOD
In[2]:= r[x_ ] = x ^ 7 - 3;
newton [r, 4, 1, 3 ^ (1 / 7)]
i pn | pn - p | r [pn ]
0 1. 0.169931 - 2.
1 1.28571 0.115783 2.8078
2 1.19692 0.026986 0.519231
3 1.17169 0.00175824 0.0317026
4 1.16994 7.89547 × 10 -6 0.000141725
In[7]:= v[x_ ] = x ^ 7 - 3;
newtoneb [v, 1, 0.00005 ]
i pn v [pn ]
0 1. - 2.
1 1.28571 2.8078
2 1.19692 0.519231
3 1.17169 0.0317026
4 1.16994 0.000141725
5 1.16993 2.86925 × 10 -9
Out[5]= 1 - 3 x + 2 x2 + x3
CALCULATING ERROR
2 x π - 2 2 π + 4 - 1 + 2 x
Lagrange Polynomial is = -
π2
Error is = 0.0152638
PRACTICAL-14
NEWTON INTERPOLATING POLYNOMIAL
In[2]:= x = {- 7, - 5, - 4, - 1};
f = {10, 5, 2, 10 };
divideddiff [x, f, 1, 4]
19
Out[4]=
72
In[6]:= x = {- 7, - 5, - 4, - 1};
f = {10, 5, 2, 10 };
poly [y _] = Simplify [newtonpoly [x, f]]
1
1700 + 1253 y + 292 y + 19 y
2 3
Out[8]=
72
2
CALCULATING ERROR
Out[12]= - 0.335749
2 y π - 2 2 π + 4 - 1 + 2 y
Newton Polynomial is = -
π 2
Error is = 0.0152638
PRACTICAL-15
CALCULATING 1st DERIVATIVE
In[4]:= f[x_] = 1 + x + x ^ 3;
forwarddiffderivative [f, 1, 1]
i h Approximate value error
0 1 8. 4.
1 0.1 4.31 0.31
2 0.01 4.0301 0.0301
3 0.001 4.003 0.003001
4 0.0001 4.0003 0.00030001
PRACTICAL-16
CALCULATING 2nd DERIVATIVE
In[4]:= f[x_] = E ^ x;
centraldiffderivative [f, 0, 1]
i h Approximate value error
0 1 1.08616 0.0861613
1 0.1 1.00083 0.000833611
2 0.01 1.00001 8.33336 × 10 -6
3 0.001 1. 8.34065 × 10 -8
PRACTICAL-17
INTEGRATION
TRAPEZOIDAL RULE
In[2]:= f[x_] = 1 / x;
trapezoidalrule [f, 1, 2]
In[4]:= f[x_] = x ^ 2;
trapezoidalrule [f, 3, 4]
SIMPSON'S RULE
In[7]:= g[x_] = 1 / x;
simpsonsrule [g, 1, 2]
In[9]:= g[x_] = x ^ 2;
simpsonsrule [g, 3, 4]
In[2]:= f[t_ , w _] := 1 + w / t;
euler [f, 1, 1, 6, 0.5 ]
ti wti
1. 1.
1.5 2.
2. 3.16667
2.5 4.45833
3. 5.85
3.5 7.325
4. 8.87143
4.5 10.4804
5. 12.1448
5.5 13.8593
6. 15.6193
2
In[4]:= f[t_ , w _] := t / w;
euler [f, 1, 0, 5, 0.5 ]
ti wti
0. 1.
0.5 1.
1. 1.25
1.5 1.65
2. 2.10455
2.5 2.57971
3. 3.06426
3.5 3.55377
4. 4.04621
4.5 4.5405
5. 5.03604
TO CALCULATE ERROR
In[11]:= z[t_ , w _] := 1 + w / t;
g[t_] := t * (1 + Log [t]);
euler [z, 1, 1, 6, 0.5 ]
ti wti xti error
1. 1. 1. 0.
1.5 2. 2.1082 0.108198
2. 3.16667 3.38629 0.219628
2.5 4.45833 4.79073 0.332393
3. 5.85 6.29584 0.445837
3.5 7.325 7.88467 0.55967
4. 8.87143 9.54518 0.673749
4.5 10.4804 11.2683 0.787991
5. 12.1448 13.0472 0.902348
5.5 13.8593 14.8761 1.01679
6. 15.6193 16.7506 1.13129
In[14]:= z[t_ , w _] := t / w;
g[t_] := Sqrt [t ^ 2 + 1];
euler [z, 1, 0, 5, 0.5 ]
ti wti xti error
0. 1. 1. 0.
0.5 1. 1.11803 0.118034
1. 1.25 1.41421 0.164214
1.5 1.65 1.80278 0.152776
2. 2.10455 2.23607 0.131523
2.5 2.57971 2.69258 0.112875
3. 3.06426 3.16228 0.0980191
3.5 3.55377 3.64005 0.0862816
4. 4.04621 4.12311 0.0768979
4.5 4.5405 4.60977 0.0692745
5. 5.03604 5.09902 0.0629814