In[ ]:= Print["Name-Gaganpreet Kaur
College Roll no-2020CHM1093
University Roll no-20068557036
Course-BSC Chemistry (H),2nd Year
Semester-4th
Paper Name-Numerical Methods
Paper Code-32355402
Practical File"]
Name-Gaganpreet Kaur
College Roll no-2020CHM1093
University Roll no-20068557036
Course-BSC Chemistry (H),2nd Year
Semester-4th
Paper Name-Numerical Methods
Paper Code-32355402
Practical File
In[ ]:= Print["TOPIC-1:BISECTION METHOD:The bisection method is
an approximation method to find the roots of the given equation by
repeatedly dividing the interval. This method will divide the interval
until the resulting interval is found, which is extremely small."]
TOPIC-1:BISECTION METHOD:The bisection method is an approximation method to find the
roots of the given equation by repeatedly dividing the interval. This method will
divide the interval until the resulting interval is found, which is extremely small.
In[ ]:= Print["(i)-Perform 10 iterations of bisection
method to obtain root of f (x)=x^3-3x^2+1 in the interval[0,1]."]
(i)-Perform 10 iterations of bisection
method to obtain root of f (x)=x^3-3x^2+1 in the interval[0,1].
2 Numerical Methods Practical file @(2020CHM1093).nb
list = {{"i", "a", "b", "m", "f[a]", "f[b]", "f[m]"}};
f[x_] := x ^ 3 - 3 x ^ 2 + 1;
a = 0;
b = 1;
Fori = 1, i ≤ 10, i = i + 1, m = Na + b 2, 7;
values = i, a, b, a + b 2., f[a], f[b], fa + b 2.;
If[f[a] * f[m] < 0, b = m, a = m];
AppendTo[list, values]
Grid[list, Dividers → All]
Print"After ", i - 1, " iterations,in the interval[",
a, ",", b, "] approx root value is = ", a + b 2
i a b m f[a] f[b] f[m]
1 0 1 0.5 1 -1 0.375
2 0.5000000 1 0.75 0.375000 -1 - 0.265625
3 0.5000000 0.7500000 0.625 0.375000 - 0.265625 0.0722656
4 0.6250000 0.7500000 0.6875 0.072266 - 0.265625 - 0.0930176
Out[ ]= 5 0.6250000 0.6875000 0.65625 0.072266 - 0.093018 - 0.0093689
6 0.6250000 0.6562500 0.640625 0.072266 - 0.009369 0.0317116
7 0.6406250 0.6562500 0.648438 0.031712 - 0.009369 0.0112357
8 0.6484375 0.6562500 0.652344 0.011236 - 0.009369 0.000949323
9 0.6523438 0.6562500 0.654297 0.000949 - 0.009369 - 0.00420583
10 0.6523438 0.6542969 0.65332 0.000949 - 0.004206 - 0.00162726
After 10 iterations,in the interval[0.6523438,0.6533203] approx root value is = 0.6528320
In[ ]:= Print["(ii)-Perform 10 iterations of bisection
method to obtain root of f (x)=x^3-x-10 in the interval[0,1]."]
(ii)-Perform 10 iterations of bisection
method to obtain root of f (x)=x^3-x-10 in the interval[0,1].
Numerical Methods Practical file @(2020CHM1093).nb 3
In[ ]:= list = {{"i", "a", "b", "m", "f[a]", "f[b]", "f[m]"}};
f[x_] := x ^ 3 - x - 10;
a = 0;
b = 1;
Fori = 1, i ≤ 10, i = i + 1, m = Na + b 2, 7;
values = i, a, b, a + b 2., f[a], f[b], fa + b 2.;
If[f[a] * f[m] < 0, b = m, a = m];
AppendTo[list, values]
Grid[list, Dividers → All]
Print"After ", i - 1, " iterations,in the interval[",
a, ",", b, "] approx root value is = ", a + b 2
i a b m f[a] f[b] f[m]
1 0 1 0.5 - 10 - 10 - 10.375
2 0.5000000 1 0.75 - 10.3750000 - 10 - 10.3281
3 0.7500000 1 0.875 - 10.328125 - 10 - 10.2051
4 0.8750000 1 0.9375 - 10.205078 - 10 - 10.1135
Out[ ]= 5 0.9375000 1 0.96875 - 10.113525 - 10 - 10.0596
6 0.9687500 1 0.984375 - 10.059601 - 10 - 10.0305
7 0.9843750 1 0.992188 - 10.030521 - 10 - 10.0154
8 0.9921875 1 0.996094 - 10.015442 - 10 - 10.0078
9 0.9960938 1 0.998047 - 10.007767 - 10 - 10.0039
10 0.9980469 1 0.999023 - 10.003895 - 10 - 10.002
After 10 iterations,in the interval[0.9990234,1] approx root value is = 0.9995117
In[ ]:= Print["(iii)-Perform 5 iterations of bisection method
to obtain root of f (x)=3x^4+x^3+12x+4 in the interval[0,-1]."]
(iii)-Perform 5 iterations of bisection method
to obtain root of f (x)=3x^4+x^3+12x+4 in the interval[0,-1].
In[ ]:= list = {{"i", "a", "b", "m", "f[a]", "f[b]", "f[m]"}};
f[x_] := 3 x ^ 4 + x ^ 3 + 12 x + 4;
a = 0;
b = - 1;
Fori = 1, i ≤ 5, i = i + 1, m = Na + b 2, 7;
values = i, a, b, a + b 2., f[a], f[b], fa + b 2.;
If[f[a] * f[m] < 0, b = m, a = m];
AppendTo[list, values]
Grid[list, Dividers → All]
Print"After ", i - 1, " iterations,in the interval[",
a, ",", b, "] approx root value is = ", a + b 2
i a b m f[a] f[b] f[m]
1 0 -1 - 0.5 4 -6 - 1.9375
2 0 - 0.5000000 - 0.25 4 - 1.937500 0.996094
Out[ ]=
3 - 0.2500000 - 0.5000000 - 0.375 0.996094 - 1.937500 - 0.493408
4 - 0.2500000 - 0.3750000 - 0.3125 0.996094 - 0.493408 0.248093
5 - 0.3125000 - 0.3750000 - 0.34375 0.248093 - 0.493408 - 0.123731
After 5 iterations,in the interval[-0.3125000,-0.3437500] approx root value is = -0.3281250
4 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= Print[
"TOPIC-2 False Position Method/Secant Method/Regula Falsi Method(Error Tolerance): The
method is the trial and error technique of using test values for the variable and
then adjusting the test value according to the outcome."]
TOPIC-2 False Position Method/Secant Method/Regula Falsi Method(Error
Tolerance): The method is the trial and error technique of using test values
for the variable and then adjusting the test value according to the outcome.
In[ ]:= Print["(i)-Find the root of f (x)=x^3-3
x^2+1 using false position method to find the root of in the
interval[0,1].Perform maximum 8 iterations with the absolute tolerance of 0.00001"]
(i)-Find the root of f (x)=x^3-3 x^2+1 using false position method to find the root of in the
interval[0,1].Perform maximum 8 iterations with the absolute tolerance of 0.00001
In[ ]:= list = {{"i", "a", "b", "s", "f[a]", "f[b]", "f[s]"}};
f[x_] := x ^ 3 - 3 x ^ 2 + 1;
a = 0;(*left end point of the interval*)
b = 1;(*right end point of the interval*)
max = 8;(*number of iterations*)
ϵ = 0.00001;
Fori = 1, i ≤ max, i ++, s = Na * f[b] - b * f[a] f[b] - f[a], 12;
val = {i, a, b, s, f[a], f[b], f[s]};
AppendTo[list, val];
If[Abs[f[s]] < ϵ, Break[]];
If[f[a] * f[s] < 0, b = s, a = s];
Grid[list, Dividers → All]
If[max ⩵ i - 1, Print["Upto ", max, "iterations |f[s]|≮", ϵ],
Print["After ", i, " we get the result, that is, |f[s]|<", ϵ]]
i a b s f[a] f[b] f[s]
1 0 1 0.500000000000 1 - 1 0.37500000000
2 0.500000000000 1 0.63636363636 0.37500000000 - 1 0.0428249437
Out[ ]= 3 0.63636363636 1 0.6512968300 0.0428249437 -1 0.003709331
4 0.6512968300 1 0.652585505 0.003709331 -1 0.00031166
5 0.652585505 1 0.65269375 0.00031166 -1 0.0000261
6 0.65269375 1 0.6527028 0.0000261 -1 2. × 10-6
After 6 we get the result, that is, |f[s]|<0.00001
In[ ]:= Print["(ii)-Find the cube root of 2 using secant method, in the interval[0,1].Perform
maximum of 8 iterations with the absolute tolerance of 0.0001"]
(ii)-Find the cube root of 2 using secant method, in the
interval[0,1].Perform maximum of 8 iterations with the absolute tolerance of 0.0001
Numerical Methods Practical file @(2020CHM1093).nb 5
list = {{"i", "a", "b", "x", "f[a]", "f[b]", "f[x]"}};
f[x_] := x ^ 3 - 2;
a = 0;(*left end point of the interval*)
b = 1;(*right end point of the interval*)
max = 4;
ϵ = 0.00001;
Fori = 1, i ≤ max, i ++, x = Na * f[b] - b * f[a] f[b] - f[a], 8;
val = {i, a, b, x, f[a], f[b], f[x]};
AppendTo[list, val];
If[Abs[f[s]] < ϵ, Break[]];
a = b;
b = x;
Grid[list, Dividers → All]
If[max ⩵ i - 1, Print["Upto ", max, "iterations |f[s]|≮", ϵ],
Print["After ", i, " we get the result, that is, |f[s]|<", ϵ]]
i a b x f[a] f[b] f[x]
1 0 1 2.0000000 -2 -1 6.000000
Out[ ]= 2 1 2.0000000 1.1428571 -1 6.000000 - 0.507289
3 2.0000000 1.1428571 1.209677 6.000000 - 0.507289 - 0.22986
4 1.1428571 1.209677 1.2650 - 0.507289 - 0.22986 0.0245
Upto 4iterations |f[s]|≮0.00001
In[ ]:= Print["(iii)-Find the root of f (x)== x^3 -
5 x + 1 using false position method to find the root of in the
interval[0.1].Perform maximum 3 iterations with the absolute tolerance of 0.000001"]
(iii)-Find the root of f (x)== x^3 - 5
x + 1 using false position method to find the root of in the
interval[0.1].Perform maximum 3 iterations with the absolute tolerance of 0.000001
6 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= list = {{"i", "a", "b", "s", "f[a]", "f[b]", "f[s]"}};
f[x_] := x ^ 3 - 5 x + 1;
a = 0;
b = 1;
max = 3;
ϵ = 0.000001;
Fori = 1, i ≤ max, i = i + 1, s = Na * f[b] - b * f[a] f[b] - f[a], 12;
val = {i, a, b, s, f[a], f[b], f[s]};
AppendTo[list, val];
If[Abs[f[s]] < ϵ, Break[]];
If[f[a] * f[b] < 0, b = s, a = s];
Print"After ", i - 1, " iterations,in the interval[", a, ",", b,
"] approx root value is = ", s = Na * f[b] - b * f[a] f[b] - f[a], 12
Grid[list, Dividers → ALL]
If[max ⩵ i - 1, Print["Upto ", max, "iterations |f[s]|≮", ϵ],
Print["After ", i, " we get the result, that is, |f[s]|<", ϵ]]
After 3 iterations,in the interval[0,0.201654334550] approx root value is = 0.20163991609
i a b s f[a] f[b] f[s]
1 0 1 0.250000000000 1 -3 - 0.23437500000
Out[ ]=
2 0 0.250000000000 0.202531645570 1 - 0.23437500000 - 0.00435056862
3 0 0.202531645570 0.201654334550 1 - 0.00435056862 - 0.00007150598
Upto 3iterations |f[s]|≮1. × 10-6
In[ ]:= Print[
"NEXT TOPIC-GUASS JACOBI METHOD:An iterative algorithm for determining the solutions
of a strictly diagonally dominant system of linear equations. Each diagonal
element is solved for, and an approximate value is plugged
in. The process is then iterated until it converges."]
NEXT TOPIC-GUASS JACOBI METHOD:An iterative algorithm for determining the solutions of a
strictly diagonally dominant system of linear equations. Each diagonal element is solved
for, and an approximate value is plugged in. The process is then iterated until it converges.
In[ ]:= Print["(i)-Solve the system of equations
26 x1+2x2+2x3=12.6
3x1+27x2+2 x3=-14.3
2x1+3x2+17x3=6.0
using the Gauss-Jacobi Method.Use the initial approximation
x1=0,x2=0,x3=0"]
(i)-Solve the system of equations
26 x1+2x2+2x3=12.6
3x1+27x2+2 x3=-14.3
2x1+3x2+17x3=6.0
using the Gauss-Jacobi Method.Use the initial approximation
x1=0,x2=0,x3=0
Numerical Methods Practical file @(2020CHM1093).nb 7
In[ ]:= list = {{"iteration", "x1", "x2", "x3"}};
x1[0] = 0;
x2[0] = 0;
x3[0] = 0;
Fori = 1, i ≤ 10, i ++, x1[i] = 12.6 - 2 x2[i - 1] - 2 x3[i - 1] 26;
x2[i] = - 14.3 - 3 x1[i - 1] - x3[i - 1] 27;
x3[i] = 6 - 2 x1[i - 1] - 3 x2[i - 1] 17;
val = {i, x1[i], x2[i], x3[i]};
AppendTo[list, val]
Grid[list, Dividers → All]
iteration x1 x2 x3
6
1 0.484615 - 0.52963
17
2 0.498207 - 0.596548 0.389392
3 0.50055 - 0.599408 0.399602
4 0.499985 - 0.600046 0.399831
Out[ ]=
5 0.500017 - 0.599992 0.40001
6 0.499999 - 0.600002 0.399997
7 0.5 - 0.6 0.400001
8 0.5 - 0.6 0.4
9 0.5 - 0.6 0.4
10 0.5 - 0.6 0.4
In[ ]:= Print["(ii)Solve the system of equations
4 x1+x2+x3=2
x1+5 x2+2 x3=-6
x1+2 x2+3 x3=-4
using the Gauss-Jacobi Method.Use the initial approximation
x1=0.5,x2=-0.5,
x3=-0.5"]
(ii)Solve the system of equations
4 x1+x2+x3=2
x1+5 x2+2 x3=-6
x1+2 x2+3 x3=-4
using the Gauss-Jacobi Method.Use the initial approximation
x1=0.5,x2=-0.5,
x3=-0.5
8 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= list = {{"iteration", "x1", "x2", "x3"}};
x1[0] = 0.5;
x2[0] = - 0.5;
x3[0] = - 0.5;
Fori = 1, i ≤ 10, i ++, x1[i] = 2 - x2[i - 1] - x3[i - 1] 4;
x2[i] = - 6 - x1[i - 1] - 2 x3[i - 1] 5;
x3[i] = - 4 - x1[i - 1] - 2 x2[i - 1] 3;
val = {i, x1[i], x2[i], x3[i]};
AppendTo[list, val]
Grid[list, Dividers → All]
iteration x1 x2 x3
1 0.75 - 1.1 - 1.16667
2 1.06667 - 0.883333 - 0.85
3 0.933333 - 1.07333 - 1.1
4 1.04333 - 0.946667 - 0.928889
Out[ ]= 5 0.968889 - 1.03711 - 1.05
6 1.02178 - 0.973778 - 0.964889
7 0.984667 - 1.0184 - 1.02474
8 1.01079 - 0.987037 - 0.982622
9 0.992415 - 1.00911 - 1.01224
10 1.00534 - 0.993588 - 0.9914
In[ ]:= Print["(iii)Solve the system of equations
25x1+5x2+x3=106.8
64x1+8x2+x3=177.2
144x1+12 x2+x3=279.2
using the Gauss-Jacobi Method.Use the initial approximation
x1=1,x2=2,
x3=5"]
(iii)Solve the system of equations
25x1+5x2+x3=106.8
64x1+8x2+x3=177.2
144x1+12 x2+x3=279.2
using the Gauss-Jacobi Method.Use the initial approximation
x1=1,x2=2,
x3=5
Numerical Methods Practical file @(2020CHM1093).nb 9
In[ ]:= list = {{"i", "x1[i]", "x2[i]", "x3[i]", "Error"}};
x1[0] = 1;
x2[0] = 2;
x3[0] = 5;
Fori = 1, i ≤ 4, i = i + 1,
x1[i] = N106.8 - 5 x2[i - 1] - x3[i - 1] 25, 8;
x2[i] = N177.2 - 64 x1[i] - x3[i - 1] 8, 8;
x3[i] = N279.2 - 144 x1[i] - 12 x2[i], 8;
err[i - 1] = {{x1[i] - x1[i - 1]}, {x2[i] - x2[i - 1]}, {x3[i] - x3[i - 1]}};
values = {i, x1[i], x2[i], x3[i], err[i - 1] // MatrixForm};
AppendTo[list, values];
Grid[list, Dividers → All]
i x1[i] x2[i] x3[i] Error
2.672
1 3.672 - 7.851 - 155.356 - 9.851
- 160.356
8.38444
2 12.0564 - 54.882 - 798.343 - 47.031
Out[ ]= - 642.987
35.1257
3 47.1821 - 255.514 - 3448.86 - 200.632
- 2650.51
146.147
4 193.329 - 1093.38 - 14 439.7 - 837.862
- 10 990.8
In[ ]:= Print["NEXT TOPIC-GUASS ELIMINATION :The row reduction algorithm
for solving linear equations systems. It consists of a sequence of
operations performed on the corresponding matrix of coefficients."]
NEXT TOPIC-GUASS ELIMINATION :The row reduction algorithm for solving linear equations systems.
It consists of a sequence of operations performed on the corresponding matrix of coefficients.
In[ ]:= Print["(i)-Solve the system of equations
x1+10x2-x3=3
2x1+x3+20x3=7
10x1-x2+2x3=4
using Guass elimination with partial pivoting "]
(i)-Solve the system of equations
x1+10x2-x3=3
2x1+x3+20x3=7
10x1-x2+2x3=4
using Guass elimination with partial pivoting
In[ ]:= A = {{1, 10, - 3}, {2, 1, 20}, {10, - 1, 2}};
A // MatrixForm
Out[ ]//MatrixForm=
1 10 - 3
2 1 20
10 - 1 2
10 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= X = {x1, x2, x3};
(*X={{x1},{x2},{x3}}*)X // MatrixForm
Out[ ]//MatrixForm=
x1
x2
x3
In[ ]:= b = {{3}, {7}, {4}};
b // MatrixForm
Out[ ]//MatrixForm=
3
7
4
In[ ]:= aug = ArrayFlatten[{{A, b}}];
aug // MatrixForm
Out[ ]//MatrixForm=
1 10 - 3 3
2 1 20 7
10 - 1 2 4
In[ ]:= Max[Abs[Take[aug, All, 1]]]
Out[ ]= 10
In[ ]:= row1 = aug〚1〛;
row2 = aug〚3〛;
aug〚1〛 = row2;
aug〚3〛 = row1;
aug // MatrixForm
Out[ ]//MatrixForm=
10 - 1 2 4
2 1 20 7
1 10 - 3 3
In[ ]:= aug〚2〛 = aug〚2〛 - aug〚2, 1〛 aug〚1, 1〛 × aug〚1〛;
aug〚3〛 = aug〚3〛 - aug〚3, 1〛 aug〚1, 1〛 × aug〚1〛;
aug // MatrixForm
Out[ ]//MatrixForm=
10 - 1 2 4
6 98 31
0
5 5 5
101
0 - 16 13
10 5 5
In[ ]:= Max[Abs[Take[aug, - 2, {2}]]]
101
Out[ ]=
10
Numerical Methods Practical file @(2020CHM1093).nb 11
In[ ]:= row2 = aug〚2〛;
row3 = aug〚3〛;
aug〚2〛 = row3;
aug〚3〛 = row2;
aug // MatrixForm
Out[ ]//MatrixForm=
10 - 1 2 4
0 101 - 16 13
10 5 5
6 98 31
0
5 5 5
In[ ]:= aug〚3〛 = aug〚3〛 - aug〚3, 2〛 aug〚2, 2〛 × aug〚2〛;
aug // MatrixForm
Out[ ]//MatrixForm=
10 - 1 2 4
0 101 - 16 13
10 5 5
2018 595
0 0
101 101
In[ ]:= upper = Take[aug, All, 3];
upper // MatrixForm
Out[ ]//MatrixForm=
10 - 1 2
0 101 - 16
10 5
2018
0 0
101
In[ ]:= c = Take[aug, All, - 1];
c // MatrixForm
Out[ ]//MatrixForm=
4
13
5
595
101
In[ ]:= m = Solve[upper.X ⩵ c]
759 354 595
Out[ ]= x1 → , x2 → , x3 →
2018 1009 2018
In[ ]:= x1 = x1 /. m〚1〛;
x2 = x2 /. m〚1〛;
x3 = x3 /. m〚1〛;
X
759 354 595
Out[ ]= , ,
2018 1009 2018
In[ ]:= A.X
Out[ ]= {3, 7, 4}
12 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= Print["(ii)-Solve the system of equations
x1 + 2 x2 + 3 x3 = 1,
2 x1 + 6 x2 + 10 x3 = 0,
3 x1 + 14 x2 + 28 x3 = -8
using Guass elimination with partial pivoting "]
(ii)-Solve the system of equations
x1 + 2 x2 + 3 x3 = 1,
2 x1 + 6 x2 + 10 x3 = 0,
3 x1 + 14 x2 + 28 x3 = -8
using Guass elimination with partial pivoting
In[ ]:= A = {{1, 2, 3}, {2, 6, 10}, {3, 14, 28}};
A // MatrixForm
Out[ ]//MatrixForm=
1 2 3
2 6 10
3 14 28
In[ ]:= x = {{x1}, {x2}, {x3}};
x // MatrixForm
Out[ ]//MatrixForm=
x1
x2
x3
In[ ]:= B = {{1}, {0}, {- 8}};
B // MatrixForm
Out[ ]//MatrixForm=
1
0
-8
In[ ]:= aug = ArrayFlatten[{{A, B}}];
aug // MatrixForm
Out[ ]//MatrixForm=
1 2 3 1
2 6 10 0
3 14 28 - 8
In[ ]:= Max[Abs[Take[aug, All, 1]]]
Out[ ]= 3
In[ ]:= row1 = aug[[1]];
row3 = aug[[3]];
aug[[1]] = row3;
aug[[3]] = row1;
aug // MatrixForm
Out[ ]//MatrixForm=
3 14 28 - 8
2 6 10 0
1 2 3 1
Numerical Methods Practical file @(2020CHM1093).nb 13
aug[[2, 1]]
In[ ]:= aug[[2]] = aug[[2]] - aug[[1]];
aug[[1, 1]]
aug[[3, 1]]
aug[[3]] = aug[[3]] - aug[[1]];
aug[[1, 1]]
aug // MatrixForm
Out[ ]//MatrixForm=
3 14 28 - 8
0 - 10 - 26 16
3 3 3
0 -8 - 19 11
3 3 3
In[ ]:= Max[Abs[Take[aug, - 2, {2, 2}]]]
10
Out[ ]=
3
aug[[3, 2]]
In[ ]:= aug[[3]] = aug[[3]] - aug[[2]];
aug[[2, 2]]
aug // MatrixForm
Out[ ]//MatrixForm=
3 14 28 -8
0 - 10 - 26 16
3 3 3
3
0 0 -3
5 5
In[ ]:= upper = Take[aug, All, 3];
upper // MatrixForm
Out[ ]//MatrixForm=
3 14 28
0 - 10 - 26
3 3
3
0 0
5
In[ ]:= c = Take[aug, All, - 1];
c // MatrixForm
Out[ ]//MatrixForm=
-8
16
3
-3
5
In[ ]:= m = Solve[upper.x ⩵ c]
Out[ ]= {{x1 → 2, x2 → 1, x3 → - 1}}
In[ ]:= Print["(iii)-Solve the system of equations
2x2+5x3 = 7,
7x1+x2-2x3 = 6,
2x1+3x2+8x3 =13
using Guass elimination without partial pivoting "]
14 Numerical Methods Practical file @(2020CHM1093).nb
(iii)-Solve the system of equations
2x2+5x3 = 7,
7x1+x2-2x3 = 6,
2x1+3x2+8x3 =13
using Guass elimination without partial pivoting
"
In[ ]:= A = {{1, 2, 5}, {7, 1, - 2}, {2, 3, 8}};
A // MatrixForm
Out[ ]//MatrixForm=
1 2 5
7 1 -2
2 3 8
In[ ]:= X = {x1, x2, x3};(*X={{x1},{x2},{x3}}*)
X // MatrixForm
Out[ ]//MatrixForm=
x1
x2
x3
In[ ]:= b = {{7}, {6}, {13}};
b // MatrixForm
Out[ ]//MatrixForm=
7
6
13
In[ ]:= aug = ArrayFlatten[{{A, b}}];
aug // MatrixForm
Out[ ]//MatrixForm=
1 2 5 7
7 1 -2 6
2 3 8 13
In[ ]:= aug〚2〛 = aug〚2〛 - 7 aug〚1〛;
aug〚3〛 = aug〚3〛 - 2 aug〚1〛;
aug // MatrixForm
Out[ ]//MatrixForm=
1 2 5 7
0 - 13 - 37 - 43
0 -1 -2 -1
aug〚2〛
In[ ]:= aug〚3〛 = aug〚3〛 - ;
13
aug // MatrixForm
Out[ ]//MatrixForm=
1 2 5 7
0 - 13 - 37 - 43
11 30
0 0
13 13
Numerical Methods Practical file @(2020CHM1093).nb 15
In[ ]:= upper = Take[aug, All, 3];
upper // MatrixForm
Out[ ]//MatrixForm=
1 2 5
0 - 13 - 37
11
0 0
13
In[ ]:= c = Take[aug, All, - 1];
c // MatrixForm
Out[ ]//MatrixForm=
7
- 43
30
13
In[ ]:= Solve[upper.X ⩵ c]
25 49 30
Out[ ]= x1 → , x2 → - , x3 →
11 11 11
In[ ]:= Print["NEXT TOPIC-NEWTON-RAPHSON METHOD:A way to quickly find a
good approximation for the root of a real-valued function f ( x ) = 0
f(x) = 0 f(x)=0. It uses the idea that a continuous and differentiable
function can be approximated by a straight line tangent to it."]
NEXT TOPIC-NEWTON-RAPHSON METHOD:A way to quickly find a good approximation for the root of a
real-valued function f ( x ) = 0 f(x) = 0 f(x)=0. It uses the idea that a continuous
and differentiable function can be approximated by a straight line tangent to it.
In[ ]:= Print["(i)-Perform 4 iterations of the Newton-Raphson method
to find the smallest positive root of the equation f(x)=x^3-17"]
(i)-Perform 4 iterations of the Newton-Raphson
method to find the smallest positive root of the equation f(x)=x^3-17
In[ ]:= list = {{"i", "x0", "x1", "f[x0]", "f[x1]"}};
f[x_] := x ^ 3 - 17;
x0 = 2;
f[x0]
Fori = 1, i ≤ 4, i ++, x1 = Nx0 + , 8;
f '[x0]
values = {i, x0, x1, f[x0], f[x1]};
x0 = x1;
AppendTo[list, values];
Grid[list, Dividers → ALL]
i x0 x1 f[x0] f[x1]
1 2 1.2500000 -9 - 15.0468750
Out[ ]= 2 1.2500000 - 1.9600000 - 15.0468750 - 24.52954
3 - 1.9600000 - 4.088413 - 24.52954 - 85.3383
4 - 4.088413 - 5.79023 - 85.3383 - 211.128
Print["(ii)-Perform 4 iterations of the
Newton-Raphson method to solve xLog[x]-12.34 with x0=10"]
16 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= list = {{"i", "x0", "x1", "f[x0]", "f[x1]"}};
f[x_] := x * Log[x] - 12.34;
x0 = 10;
f[x0]
Fori = 1, i ≤ 4, i ++, x1 = Nx0 + , 8;
f '[x0]
values = {i, x0, x1, f[x0], f[x1]};
x0 = x1;
AppendTo[list, values];
Grid[list, Dividers → ALL]
i x0 x1 f[x0] f[x1]
1 10 13.2356 10.6859 21.8464
Out[ ]= 2 13.2356 19.333 21.8464 44.9207
3 19.333 30.6714 44.9207 92.6583
4 30.6714 51.619 92.6583 191.24
In[ ]:= Print["(iii)-Perform 4 iterations of the Newton-Raphson method
to find the smallest positive root of the equation f(x)=x^3-5x+1"]
(iii)-Perform 4 iterations of the Newton-Raphson
method to find the smallest positive root of the equation f(x)=x^3-5x+1
In[ ]:= list = {{"i", "x0", "x1", "f[x0]", "f[x1]"}};
f[x_] := x * Sin[x] + Cos[x];
x0 = π;
f[x0]
Fori = 1, i ≤ 5, i ++, x1 = Nx0 + , 8;
f '[x0]
values = {i, x0, x1, f[x0], f[x1]};
x0 = x1;
AppendTo[list, values];
Grid[list, Dividers → ALL]
i x0 x1 f[x0] f[x1]
1 π 3.4599025 -1 - 2.032583
2 3.4599025 4.0784428 - 2.032583 - 3.878315
Out[ ]=
3 4.0784428 5.683852 - 3.878315 - 2.38050
4 5.683852 5.17663 - 2.38050 - 4.18100
5 5.17663 3.37278 - 4.18100 - 1.7462
In[ ]:= Print[
"NEXT TOPIC-GUASS JORDAN:The Gauss-Jordan Method is similar to Gaussian Elimination,
except that the entries both above and below each pivot are targeted (zeroed out). "]
NEXT TOPIC-GUASS JORDAN:The Gauss-Jordan Method is similar to Gaussian Elimination,
except that the entries both above and below each pivot are targeted (zeroed out).
Numerical Methods Practical file @(2020CHM1093).nb 17
In[ ]:= Print["(i)-Solve the system of equations:
x1 + 2 x2 + 3 x3 = 1
x1 + 6 x2 + 10 x3 = 0
x1 + 14 x2 + 28 x3 = -8
(with partial pivoting )"]
(i)-Solve the system of equations:
x1 + 2 x2 + 3 x3 = 1
x1 + 6 x2 + 10 x3 = 0
x1 + 14 x2 + 28 x3 = -8
(with partial pivoting )
In[ ]:= A = {{1, 2, 3}, {2, 6, 10}, {3, 14, 28}};
A // MatrixForm
Out[ ]//MatrixForm=
1 2 3
2 6 10
3 14 28
In[ ]:= X = {{x1}, {x2}, {x3}};
X // MatrixForm
Out[ ]//MatrixForm=
x1
x2
x3
In[ ]:= B = {{1}, {0}, {- 8}};
B // MatrixForm
Out[ ]//MatrixForm=
1
0
-8
In[ ]:= aug = ArrayFlatten[{{A, B}}];
aug // MatrixForm
Out[ ]//MatrixForm=
1 2 3 1
2 6 10 0
3 14 28 - 8
In[ ]:= Max[Abs[Take[aug, All, 1]]]
Out[ ]= 3
In[ ]:= row1 = aug[[1]];
row3 = aug[[3]];
aug[[1]] = row3;
aug[[3]] = row1;
aug // MatrixForm
Out[ ]//MatrixForm=
3 14 28 - 8
2 6 10 0
1 2 3 1
18 Numerical Methods Practical file @(2020CHM1093).nb
aug[[2, 1]]
In[ ]:= aug[[2]] = aug[[2]] - aug[[1]];
aug[[1, 1]]
aug[[3, 1]]
aug[[3]] = aug[[3]] - aug[[1]];
aug[[1, 1]]
aug // MatrixForm
Out[ ]//MatrixForm=
3 14 28 - 8
0 - 10 - 26 16
3 3 3
0 -8 - 19 11
3 3 3
In[ ]:= Max[Abs[Take[aug, - 2, {2, 2}]]]
10
Out[ ]=
3
aug[[3, 2]]
In[ ]:= aug[[3]] = aug[[3]] - aug[[2]];
aug[[2, 2]]
aug // MatrixForm
Out[ ]//MatrixForm=
3 14 28 -8
0 - 10 - 26 16
3 3 3
3
0 0 -3
5 5
In[ ]:= upper = Take[aug, All, 3];
upper // MatrixForm
Out[ ]//MatrixForm=
3 14 28
0 - 10 - 26
3 3
3
0 0
5
In[ ]:= c = Take[aug, All, - 1];
c // MatrixForm
Out[ ]//MatrixForm=
-8
16
3
-3
5
In[ ]:= m = Solve[upper.X ⩵ c]
Out[ ]= {{x1 → 2, x2 → 1, x3 → - 1}}
In[ ]:= Print["(ii)Find the inverse of the matrix using Guass jordan method"]
(ii)Find the inverse of the matrix using Guass jordan method
Numerical Methods Practical file @(2020CHM1093).nb 19
In[ ]:= A = {{2, 2, 2}, {2, 1, 1}, {1, 3, 5}};
id = IdentityMatrix[3];
inv = ArrayFlatten[{{A, id}}];
RowReduce[inv];
Print["Inverse of A is ", Take[RowReduce[inv], All, - 3] // MatrixForm]
- 12 1 0
9
Inverse of A is 4
-2 - 12
- 54 1 1
2
In[ ]:= Print["(iii)-Solve the system of equations:
(without partial pivoting )"]
(iii)-Solve the system of equations:
(without partial pivoting )
1 1 1
In[ ]:= 4 3 -1 ;
3 5 3
A = {{1, 1, 1}, {4, 3, - 1}, {3, 5, 3}};
A // MatrixForm
Out[ ]//MatrixForm=
1 1 1
4 3 -1
3 5 3
In[ ]:= id = IdentityMatrix[3];
id // MatrixForm
Out[ ]//MatrixForm=
1 0 0
0 1 0
0 0 1
In[ ]:= inv = ArrayFlatten[{{A, id}}];
inv // MatrixForm
Out[ ]//MatrixForm=
1 1 1 1 0 0
4 3 -1 0 1 0
3 5 3 0 0 1
In[ ]:= inv〚2〛 = inv〚2〛 - 4 inv〚1〛;
inv〚3〛 = inv〚3〛 - 3 inv〚1〛;
inv // MatrixForm
Out[ ]//MatrixForm=
1 1 1 1 0 0
0 -1 -5 -4 1 0
0 2 0 -3 0 1
In[ ]:= inv〚1〛 = inv〚1〛 + inv〚2〛;
inv〚3〛 = inv〚3〛 + 2 inv〚2〛;
inv // MatrixForm
Out[ ]//MatrixForm=
1 0 -4 -3 1 0
0 -1 -5 -4 1 0
0 0 - 10 - 11 2 1
20 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= inv〚2〛 = - 1 inv〚2〛;
inv // MatrixForm
Out[ ]//MatrixForm=
1 0 -4 -3 1 0
0 1 5 4 -1 0
0 0 - 10 - 11 2 1
In[ ]:= inv〚3〛 = 1 inv〚3, 3〛 inv〚3〛;
inv // MatrixForm
Out[ ]//MatrixForm=
1 0 -4 -3 1 0
0 1 5 4 -1 0
11 1
0 0 1 - - 1
10 5 10
In[ ]:= inv〚1〛 = inv〚1〛 + 4 inv〚3〛;
inv〚2〛 = inv〚2〛 - 5 inv〚3〛;
inv // MatrixForm
Out[ ]//MatrixForm=
7 1
1 0 0 -2
5 5 5
0 1 0 -3 0 1
2 2
11
0 0 1 -1 - 1
10 5 10
In[ ]:= Print["Inverse of A is ", Take[inv, All, - 3] // MatrixForm]
7 1
5 5
- 25
Inverse of A is - 32 0 1
2
11
10
- 15 1
- 10
In[ ]:= Print["NEXT TOPIC-GUASS-SEIDAL METHOD: An
iterative method used to solve a system of linear equations."]
NEXT TOPIC-GUASS-SEIDAL METHOD: An iterative method used to solve a system of linear equations.
In[ ]:= Print["(i)-Solve the system of equations:
45x1 + 2x2 + 3 x3 = 58
-3x1 + 22x2 + 2x3 = 47
5x1 + x2 + 20x3 = 67
and find the residual vector for the system
Take the initial approximations as x1 = 0, x2 = 0, x3 = 0"]
(i)-Solve the system of equations:
45x1 + 2x2 + 3 x3 = 58
-3x1 + 22x2 + 2x3 = 47
5x1 + x2 + 20x3 = 67
and find the residual vector for the system
Take the initial approximations as x1 = 0, x2 = 0, x3 = 0
Numerical Methods Practical file @(2020CHM1093).nb 21
In[ ]:= list = {{"iteration", "x1", "x2", "x3", "Residual vector"}};
A = {{45, 2, 3}, {- 3, 22, 2}, {5, 1, 20}};
b = {{58}, {47}, {67}};
x1[0] = 0;
x2[0] = 0;
x3[0] = 0;
Fori = 1, i ≤ 3, i ++,
- 2 x2[i - 1] + 58 - 3 x3[i - 1]
x1[i] = N ;
45
x2[i] = N47 + 3 x1[i] - 2 x3[i - 1] 22;
x3[i] = N67 - x2[i] - 5 x1[i] 20;
r[i - 1] = b - A.{{x1[i - 1]}, {x2[i - 1]}, {x3[i - 1]}};
val = {i, x1[i], x2[i], x3[i], r[i - 1] // MatrixForm};
AppendTo[list, val]
Grid[list, Dividers → All]
iteration x1 x2 x3
Residual vector
58
1 1.28889 2.31212 2.91217 47
67
Out[ ]=
- 13.3608
2 0.991983 2.00689 3.00166 - 5.82434
0.
0.341996
3 0.999583 1.99979 3.00011 - 0.178976
0.
In[ ]:= Print[
"(ii)-Solve the system of equations by Gauss-Seidel method applied to the system of
equations
3x1 – 6x2 + 2x3 = 23
– 4x1 + x2 – x3 = – 8
x1 – 3x2 + 7x3 = 17
Take the initial approximations as x1 = 0.9, x2 = – 3.1, x3 = 0.9"]
(ii)-Solve the system of equations by Gauss-Seidel method applied to the system of
equations
3x1 – 6x2 + 2x3 = 23
– 4x1 + x2 – x3 = – 8
x1 – 3x2 + 7x3 = 17
Take the initial approximations as x1 = 0.9, x2 = – 3.1, x3 = 0.9
22 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= list = {{"iteration", "x1", "x2", "x3"}};
x1[0] = 0.9;
x2[0] = - 3.1;
x3[0] = 0.9;
Fori = 1, i ≤ 3, i ++,
6 x2[i - 1] + 23 - 2 x3[i - 1]
x1[i] = N ;
3
x2[i] = N- 8 + 4 x1[i] + x3[i - 1];
x3[i] = N17 + 3 x2[i] - x1[i] 7;
val = {i, x1[i], x2[i], x3[i]};
AppendTo[list, val]
Grid[list, Dividers → All]
iteration x1 x2 x3
1 0.866667 - 3.63333 0.747619
Out[ ]=
2 - 0.0984127 - 7.64603 - 0.83424
3 - 7.06924 - 37.1112 - 12.4663
In[ ]:= Print[
"(iii)Solve the following system of equations using the Gauss-Seidel iteration method:
27x1 + 6x2 – x3 = 85,
x1+ x2+ 54x3= 110,
6x1 + 15x2 + 2x3 = 72
Take the initial approximations as x1 = 0, x2 =0, x3 = 0"]
(iii)Solve the following system of equations using the Gauss-Seidel iteration method:
27x1 + 6x2 – x3 = 85,
x1+ x2+ 54x3= 110,
6x1 + 15x2 + 2x3 = 72
Take the initial approximations as x1 = 0, x2 =0, x3 = 0
In[ ]:= list = {{"iteration", "x1", "x2", "x3"}};
x1[0] = 0;
x2[0] = 0;
x3[0] = 0;
Fori = 1, i ≤ 3, i ++,
- 6 x2[i - 1] + 85 + x3[i - 1]
x1[i] = N ;
27
x2[i] = N110 - x1[i] - 54 x3[i - 1];
x3[i] = N72 - 15 x2[i] - 6 x1[i] 2;
val = {i, x1[i], x2[i], x3[i]};
AppendTo[list, val]
Grid[list, Dividers → All]
iteration x1 x2 x3
1 3.14815 106.852 - 774.833
Out[ ]=
2 - 49.2942 42 000.3 - 314 818.
3 - 20 990.2 1.70213 × 10 - 1.27597 × 108
7
Numerical Methods Practical file @(2020CHM1093).nb 23
In[ ]:= Print[
"NEXT TOPIC-Trapezoidal Rule:In this method the area under the curve is evaluated, then
the total area is divided into small trapezoids instead of rectangles. This rule
is used for approximating the definite integrals where
it uses the linear approximations of the functions."]
NEXT TOPIC-Trapezoidal Rule:In this method the area under the curve is evaluated, then the total
area is divided into small trapezoids instead of rectangles. This rule is used for
approximating the definite integrals where it uses the linear approximations of the functions.
In[ ]:= Print"(i)Find the approximate value of I =∫ 1/(1+x)ⅆx using the
trapezium rule with 4 equal sub-intervals, in given interval[0,1].
Using the exact solution, find the Absolute errors, Relative errors. "
(i)Find the approximate value of I =∫1/(1+x)ⅆx using the
trapezium rule with 4 equal sub-intervals, in given interval[0,1].
Using the exact solution, find the Absolute errors, Relative errors.
In[ ]:= a = 0;
b = 1;
n = 4;
h = b - a n
1
Out[ ]=
4
In[ ]:= f[x_] := 1 1 + x
sum = Sum[f[i], {i, a + h, b - h, h}]
214
Out[ ]=
105
In[ ]:= trp = h 2 f[a] + 2 sum + f[b] // N
Out[ ]= 0.697024
In[ ]:= Print["Approximate value of given integral=", trp]
Approximate value of given integral=0.697024
In[ ]:= exact = Integrate[f[x], {x, a, b}] // N
Out[ ]= 0.693147
In[ ]:= error = Abs[exact - trp]
Out[ ]= 0.00387663
exact - trp
In[ ]:= relative = Abs
exact
Out[ ]= 0.00559279
24 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= Print"(ii)Find the approximate value of I =∫ 1/(5+3x)ⅆx
using the trapezium rule with 8 equal sub-intervals, in given
interval[1,2]. Using the exact solution, find the Relative errors. "
(ii)Find the approximate value of I =∫1/(5+3x)ⅆx using the trapezium rule with 8 equal
sub-intervals, in given interval[1,2]. Using the exact solution, find the Relative errors.
In[ ]:= a = 1;
b = 2;
n = 8;
h = b - a n
1
Out[ ]=
8
In[ ]:= f[x_] := 1 5 + 3 x
sum = Sum[f[i], {i, a + h, b - h, h}] // N
Out[ ]= 0.741485
In[ ]:= trp = h 2 f[a] + 2 sum + f[b] // N
Out[ ]= 0.10618
In[ ]:= Print["Approximate value of given integral=", trp]
Approximate value of given integral=0.10618
In[ ]:= exact = Integrate[f[x], {x, a, b}] // N
Out[ ]= 0.106151
exact - trp
In[ ]:= relative = Abs
exact
Out[ ]= 0.000270769
In[ ]:= Print"(iii)Find the approximate value of I =∫ 1/(x^2+6x+10)ⅆx
using the trapezium rule with 4 equal sub-intervals, in given
interval[0,1]. Using the exact solution, find the Relative errors "
(iii)Find the approximate value of I =∫1/(x^2+6x+10)ⅆx using the trapezium rule with 4 equal
sub-intervals, in given interval[0,1]. Using the exact solution, find the Relative errors
In[ ]:= a = 0;
b = 1;
n = 4;
h = b - a n
1
Out[ ]=
4
Numerical Methods Practical file @(2020CHM1093).nb 25
In[ ]:= f[x_] := 1 x ^ 2 + 6 x + 10
sum = Sum[f[i], {i, a + h, b - h, h}] // N
Out[ ]= 0.228348
In[ ]:= trp = h 2 f[a] + 2 sum + f[b] // N
Out[ ]= 0.07694
In[ ]:= Print["Approximate value of given integral=", trp]
Approximate value of given integral=0.07694
In[ ]:= exact = Integrate[f[x], {x, a, b}] // N
Out[ ]= 0.0767719
exact - trp
In[ ]:= relative = Abs
exact
Out[ ]= 0.00218969
In[ ]:= Print[
"NEXT TOPIC-SIMPSON'S 1/3 RULE:The 1/3 rule can be used for the remainding subintervals
without changing the order of the error term (conversely, the 3/8 rule
can be used with a composite 1/3 rule for odd-numbered subintervals)."]
NEXT TOPIC-SIMPSON'S 1/3 RULE:The 1/3 rule can be used for the remainding
subintervals without changing the order of the error term (conversely, the
3/8 rule can be used with a composite 1/3 rule for odd-numbered subintervals).
In[ ]:= Print"(i)Find the approximate value of I =∫ 1/(x^2+6x+10)ⅆx using the
Simpson's 1/3 rule with 4 equal sub-intervals, in given interval[0,1]. "
(i)Find the approximate value of I =∫1/(x^2+6x+10)ⅆx using the
Simpson's 1/3 rule with 4 equal sub-intervals, in given interval[0,1].
In[ ]:= a = 0;
b = 1;
n = 4;
b - a
h=
2n
1
Out[ ]=
8
In[ ]:= f[x_] := 1 x ^ 2 + 6 x + 10
oddsum = Sum[f[i], {i, a + h, b - h, 2 h}] // N
Out[ ]= 0.306752
In[ ]:= evensum = Sum[f[i], {i, a + 2 h, b - 2 h, 2 h}] // N
Out[ ]= 0.228348
In[ ]:= simpson = h 3 f[a] + 4 * oddsum + 2 * evensum + f[b]
Out[ ]= 0.0767719
26 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= Print"(ii)Find the approximate value of I =∫ ⅇ-x^2 dx
using the Simpson's 1/3 rule with 4 equal sub-intervals, in given
interval[0,2]. Using the exact solution, find the Relative errors. "
(ii)Find the approximate value of I =∫ ⅇ-x^2 dx using the Simpson's 1/3 rule with 4 equal
sub-intervals, in given interval[0,2]. Using the exact solution, find the Relative errors.
In[ ]:= a = 0;
b = 2;
n = 4;
b - a
h= // N
2n
Out[ ]= 0.25
In[ ]:= f[x_] := ⅇ-x^2
oddsum = Sum[f[i], {i, a + h, b - h, 2 h}] // N
Out[ ]= 1.76558
In[ ]:= evensum = Sum[f[i], {i, a + 2 h, b - 2 h, 2 h}] // N
Out[ ]= 1.25208
In[ ]:= simpson = h 3 f[a] + 4 * oddsum + 2 * evensum + f[b]
Out[ ]= 0.882066
In[ ]:= Print["Approximate value of given integral=", simpson]
Approximate value of given integral=0.882066
In[ ]:= exact = Integrate[f[x], {x, a, b}] // N
Out[ ]= 0.882081
exact - simpson
In[ ]:= relative = Abs
exact
Out[ ]= 0.0000180033
1
In[ ]:= Print"(iii)Find the approximate value of I= 1/(1+x)ⅆxusing the Simpson’s 1/3 rule
0
with 8 equal subintervals.Using the exact solution, find the Absolute errors."
1
(iii)Find the approximate value of I= 1/(1+x)ⅆxusing the Simpson’s 1/3 rule
0
with 8 equal subintervals.Using the exact solution, find the Absolute errors.
In[ ]:= a = 0;
b = 1;
n = 8;
b - a
h= // N
2n
Out[ ]= 0.0625
Numerical Methods Practical file @(2020CHM1093).nb 27
In[ ]:= f[x_] := 1 1 + x
oddsum = Sum[f[i], {i, a + h, b - h, 2 h}] // N
Out[ ]= 5.54128
In[ ]:= evensum = Sum[f[i], {i, a + 2 h, b - 2 h, 2 h}] // N
Out[ ]= 4.80297
In[ ]:= simpson = h 3 f[a] + 4 * oddsum + 2 * evensum + f[b]
Out[ ]= 0.693148
In[ ]:= Print["Approximate value of given integral=", simpson]
Approximate value of given integral=0.693148
In[ ]:= exact = Integrate[f[x], {x, a, b}] // N
Out[ ]= 0.693147
In[ ]:= error = exact - simpson // N
Out[ ]= - 4.72259 × 10-7
In[ ]:= Print["SIMPSON'S 3/8 RULE"]
SIMPSON'S 3/8 RULE
1
In[ ]:= Print"(i)Find the approximate value of I =∫ dx
1 + x^2
using the Simpson's 3/8 rule with 9 equal sub-intervals, in given
interval[1,2]. Using the exact solution, find the Relative errors. "
1
(i)Find the approximate value of I =∫ dx using the Simpson's 3/8 rule with 9 equal
1 + x^2
sub-intervals, in given interval[1,2]. Using the exact solution, find the Relative errors.
28 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= a = 1;(*Enter the left end point*)
b = 2;(*Enter the right end point*)
n = 9;(*Enter the number of sub intervals to be formed*)
Print["The given interval is:[", a, " , ", b, "]"]
Print["The number of sub intervals to be formed n=", n]
b - a
h = N ;
n
Print["Step size=", h]
1
f[x_] := ;
1 + x^2
sum3odd = 0;
restsum = 0;
Fori = 1, i < n, i ++,
i
IfIntegerQ , sum3odd = sum3odd + 2 * f[a + i * h], restsum = restsum + 3 * f[a + i * h]
3
;
sum = 3 h 8 * f[a] + N[sum3odd] + N[restsum] + f[b];
ev = N[Integrate[f[x], {x, a, b}]];
Print["Approx value is:", sum]
Print["Exact value is:", ev]
Print["Absolute error is:", Abs[sum - ev]]
The given interval is:[1 , 2]
The number of sub intervals to be formed n=9
Step size=0.111111
Approx value is:0.32175
Exact value is:0.321751
Absolute error is:3.53484 × 10-7
In[ ]:= Print"(ii)Find the approximate value of I =∫ ⅇ-x^2 dx
using the Simpson's 3/8 rule with 4 equal sub-intervals, in given
interval[0,2]. Using the exact solution, find the Relative errors. "
(ii)Find the approximate value of I =∫ ⅇ-x^2 dx using the Simpson's 3/8 rule with 4 equal
sub-intervals, in given interval[0,2]. Using the exact solution, find the Relative errors.
Numerical Methods Practical file @(2020CHM1093).nb 29
In[ ]:= a = 0;(*Enter the left end point*)
b = 2;(*Enter the right end point*)
n = 4;(*Enter the number of sub intervals to be formed*)
Print["The given interval is:[", a, " , ", b, "]"]
Print["The number of sub intervals to be formed n=", n]
b - a
h = N ;
n
Print["Step size=", h]
1
f[x_] := ;
1 + x^2
sum3odd = 0;
restsum = 0;
Fori = 1, i < n, i ++,
i
IfIntegerQ , sum3odd = sum3odd + 2 * f[a + i * h], restsum = restsum + 3 * f[a + i * h]
3
;
sum = 3 h 8 * f[a] + N[sum3odd] + N[restsum] + f[b];
ev = N[Integrate[f[x], {x, a, b}]];
Print["Approx value is:", sum]
Print["Exact value is:", ev]
Print["Absolute error is:", Abs[sum - ev]]
The given interval is:[0 , 2]
The number of sub intervals to be formed n=4
Step size=0.5
Approx value is:1.07163
Exact value is:1.10715
Absolute error is:0.0355141
1 2
In[ ]:= Print"(iii)Using the Simpson’s 3/8 rule,evaluate I= ⅆx
5+3x 1
with 3 and 6 subintervals.Compare with the exact solution."
(iii)Using the Simpson’s 3/8 rule,evaluate
2 1
I= ⅆx with 3 and 6 subintervals.Compare with the exact solution.
1 5+3x
In[ ]:= Print["PART-(a)-For 3 intervals"]
PART-(a)-For 3 intervals
30 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= a = 1;(*Enter the left end point*)
b = 2;(*Enter the right end point*)
n = 3;(*Enter the number of sub intervals to be formed*)
Print["The given interval is:[", a, " , ", b, "]"]
Print["The number of sub intervals to be formed n=", n]
b - a
h = N ;
n
Print["Step size=", h]
1
f[x_] := ;
5+3x
sum3odd = 0;
restsum = 0;
Fori = 1, i < n, i ++,
i
IfIntegerQ , sum3odd = sum3odd + 2 * f[a + i * h], restsum = restsum + 3 * f[a + i * h]
3
;
sum = 3 h 8 * f[a] + N[sum3odd] + N[restsum] + f[b];
ev = N[Integrate[f[x], {x, a, b}]];
Print["Approx value is:", sum]
Print["Exact value is:", ev]
Print["Absolute error is:", Abs[sum - ev]]
The given interval is:[1 , 2]
The number of sub intervals to be formed n=3
Step size=0.333333
Approx value is:0.106155
Exact value is:0.106151
Absolute error is:4.05932 × 10-6
In[ ]:= Print["PART-(b)-For 6 intervals"]
PART-(b)-For 6 intervals
Numerical Methods Practical file @(2020CHM1093).nb 31
In[ ]:= a = 1;(*Enter the left end point*)
b = 2;(*Enter the right end point*)
n = 6;(*Enter the number of sub intervals to be formed*)
Print["The given interval is:[", a, " , ", b, "]"]
Print["The number of sub intervals to be formed n=", n]
b - a
h = N ;
n
Print["Step size=", h]
1
f[x_] := ;
5+3x
sum3odd = 0;
restsum = 0;
Fori = 1, i < n, i ++,
i
IfIntegerQ , sum3odd = sum3odd + 2 * f[a + i * h], restsum = restsum + 3 * f[a + i * h]
3
;
sum = 3 h 8 * f[a] + N[sum3odd] + N[restsum] + f[b];
ev = N[Integrate[f[x], {x, a, b}]];
Print["Approx value is:", sum]
Print["Exact value is:", ev]
Print["Absolute error is:", Abs[sum - ev]]
The given interval is:[1 , 2]
The number of sub intervals to be formed n=6
Step size=0.166667
Approx value is:0.106152
Exact value is:0.106151
Absolute error is:2.68932 × 10-7
In[ ]:= Print["EULER'S METHOD"]
EULER'S METHOD
32 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= Print"Question (i):
Solve the following differential equation using Euler' s Method with
dx x
step size h = 1 for values of x in the range[1,6] =1+ ;y(1)=1"
dy y
Question (i):
Solve the following differential equation using Euler' s Method
dx x
with step size h = 1 for values of x in the range[1,6] =1+ ;y(1)=1
dy y
In[ ]:= x0 = 1;
y0 = 1;
n = 5;
h = 1;
f[x_, y_] := 1 + x / y;
list = "i", "x", "y", {0, x0 , y0 };
Fori = 1, i ≤ n, i = i + 1, xi = N[xi-1 + h]; yi = Nyi-1 + h * f[xi-1 , yi-1 ];
values = i, xi , yi ;
AppendTolist, values
Gridlist, Dividers -> All
i x y
0 1 1
1 2. 3.
Out[ ]= 2 3. 4.66667
3 4. 6.30952
4 5. 7.94349
5 6. 9.57293
In[ ]:=
In[ ]:= Print"Question (ii):
Solve the following differential equation using Euler' s Method with
dx
step size h = 0.1 for values of x in the range[1,6] =1+xy;y(0)=2"
dy
Question (ii):
Solve the following differential equation using Euler' s Method
dx
with step size h = 0.1 for values of x in the range[1,6] =1+xy;y(0)=2
dy
Numerical Methods Practical file @(2020CHM1093).nb 33
In[ ]:= x0 = 0;
y0 = 2;
n = 10;
h = 0.1;
f[x_, y_] := 1 + x * y;
list = "i", "x", "y", {0, x0 , y0 };
Fori = 1, i ≤ n, i = i + 1, xi = xi-1 + h; yi = yi-1 + h * f[xi-1 , yi-1 ];
values = i, xi , yi ;
AppendTolist, values
Gridlist, Dividers -> All
i x y
0 0 2
1 0.1 2.1
2 0.2 2.221
3 0.3 2.36542
4 0.4 2.53638
Out[ ]=
5 0.5 2.73784
6 0.6 2.97473
7 0.7 3.25321
8 0.8 3.58094
9 0.9 3.96741
10 1. 4.42448
In[ ]:= Print"Question (iii):
Solve the following differential equation using Euler' s Method with step
dx
size h = 0.2 for values of x in the range[1,6] =-2xy^1;y(0)=1"
dy
Question (iii):
Solve the following differential equation using Euler' s Method with
dx
step size h = 0.2 for values of x in the range[1,6] =-2xy^1;y(0)=1
dy
In[ ]:= x0 = 0;
y0 = 1;
n = 5;
h = 0.2;
f[x_, y_] := -2 * x * y2 ;
list = "i", "x", "y", {0, x0 , y0 };
Fori = 1, i ≤ n, i = i + 1, xi = N[xi-1 + h]; yi = Nyi-1 + h * f[xi-1 , yi-1 ];
values = i, xi , yi ;
AppendTolist, values
Gridlist, Dividers -> All
i x y
0 0 1
1 0.2 1.
Out[ ]= 2 0.4 0.92
3 0.6 0.784576
4 0.8 0.636842
5 1. 0.50706
34 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= Print["MID POINT METHOD"]
MID POINT METHOD
In[ ]:= Print"Question (i):
Solve the following differential equation using Midpoint Method with
dx
step size h = 0.1 for values of x in the range [0,1] =x+2y;y(0)=0"
dy
Question (i):
Solve the following differential equation using Midpoint Method
dx
with step size h = 0.1 for values of x in the range [0,1] =x+2y;y(0)=0
dy
In[ ]:=
In[ ]:= x0 = 0;
y0 = 0;
n = 2;
h = 0.1;
f[x_, y_] := x + 2 y;
list = "i", "x", "y", {0, x0 , y0 };
h
Fori = 1, i ≤ n, i = i + 1, xi-1/2 = Nxi-1 + ;
2
h
yi-1/2 = Nyi-1 + * f[xi-1 , yi-1 ];
2
h
xi = Nxi-1/2 + ;
2
yi = Nyi-1 + h * f[xi-1/2 , yi-1/2 ]];
values = i, xi , yi ;
AppendTolist, values
Gridlist, Dividers -> All
i x y
0 0 0
Out[ ]=
1 0.1 0.005
2 0.2 0.0221
In[ ]:= Print"Question (ii):
Solve the following differential equation using Midpoint Method with
dx
step size h = 0.5 for values of x in the range [0,1] =x+y;y(0)=1"
dy
Question (ii):
Solve the following differential equation using Midpoint Method
dx
with step size h = 0.5 for values of x in the range [0,1] =x+y;y(0)=1
dy
Numerical Methods Practical file @(2020CHM1093).nb 35
In[ ]:= x0 = 0;
y0 = 1;
n = 2;
h = 0.5;
f[x_, y_] := x + y;
list = "i", "x", "y", {0, x0 , y0 };
h
Fori = 1, i ≤ n, i = i + 1, xi-1/2 = Nxi-1 + ;
2
h
yi-1/2 = Nyi-1 + * f[xi-1 , yi-1 ];
2
h
xi = Nxi-1/2 + ;
2
yi = Nyi-1 + h * f[xi-1/2 , yi-1/2 ]];
values = i, xi , yi ;
AppendTolist, values
Gridlist, Dividers -> All
i x y
0 0 1
Out[ ]=
1 0.5 1.75
2 1. 3.28125
In[ ]:= Print"Question (iii):
Solve the following differential equation using
Midpoint Method with step size h = 0.1 for values of x in the
dx
range [0,1] =1+x^2+y;y(0)=1. Determine y(0.1) and y(0.2)."
dy
Question (iii):
Solve the following differential equation using
Midpoint Method with step size h = 0.1 for values of x in
dx
the range [0,1] =1+x^2+y;y(0)=1. Determine y(0.1) and y(0.2).
dy
36 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= x0 = 0;
y0 = 1;
n = 2;
h = 0.1;
f[x_, y_] := 1 + x2 + y;
list = "i", "x", "y", {0, x0 , y0 };
h
Fori = 1, i ≤ n, i = i + 1, xi-1/2 = Nxi-1 + ;
2
h
yi-1/2 = Nyi-1 + * f[xi-1 , yi-1 ];
2
h
xi = Nxi-1/2 + ;
2
yi = Nyi-1 + h * f[xi-1/2 , yi-1/2 ]];
values = i, xi , yi ;
AppendTolist, values
Gridlist, Dividers -> All
i x y
0 0 1
Out[ ]=
1 0.1 1.21025
2 0.2 1.44463
In[ ]:= Print[
"HEUN'S METHOD:It considers the tangent lines to the solution curve at both ends of the
interval, one which overestimates, and one which underestimates the ideal vertical
coordinates. A prediction line must be constructed based on the right
end point tangent's slope alone, approximated using Euler's Method."]
HEUN'S METHOD:It considers the tangent lines to the solution curve at both
ends of the interval, one which overestimates, and one which underestimates
the ideal vertical coordinates. A prediction line must be constructed based on
the right end point tangent's slope alone, approximated using Euler's Method.
In[ ]:= Print"Question(i):Use Heun's method to approximate the
dy
solution of =x+y; y(0)=2 in [0,1] in the interval with h=0.5"
dx
Question(i):Use Heun's method to approximate the
dy
solution of =x+y; y(0)=2 in [0,1] in the interval with h=0.5
dx
Numerical Methods Practical file @(2020CHM1093).nb 37
In[ ]:= x0 = 0;
y0 = 2;
n = 2;
h = 0.5;
f[x_, y_] := x + y;
list = "i", "x", "y", {0, x0 , y0 };
Fori = 1, i ≤ n, i = i + 1, xi = N[xi-1 + h];
yistar = Nyi-1 + h * f[xi-1 , yi-1 ];
h
yi = Nyi-1 + * f[xi-1 , yi-1 ] + f[xi , yistar ];
2
values = i, xi , yi ;
AppendTolist, values
Gridlist, Dividers -> All
i x y
0 0 2
Out[ ]=
1 0.5 3.375
2 1. 5.92188
In[ ]:= Print"Question-(ii):Use Heun's method to approximate the
dy
solution of =-2x^2; y(0)=1 in [0,1] in the interval with h=0.5"
dx
Question-(ii):Use Heun's method to approximate the
dy
solution of =-2x^2; y(0)=1 in [0,1] in the interval with h=0.5
dx
In[ ]:= x0 = 0;
y0 = 1;
n = 2;
h = 0.5;
f[x_, y_] := -2 * x * y2 ;
list = "i", "x", "y", {0, x0 , y0 };
Fori = 1, i ≤ n, i = i + 1, xi = N[xi-1 + h];
yistar = Nyi-1 + h * f[xi-1 , yi-1 ];
h
yi = Nyi-1 + * f[xi-1 , yi-1 ] + f[xi , yistar ];
2
values = i, xi , yi ;
AppendTolist, values
Gridlist, Dividers -> All
i x y
0 0 1
Out[ ]=
1 0.5 0.75
2 1. 0.499512
38 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= Print"Question-(iii):Use Heun's method to approximate the
dy
solution of =1+xy; y(0)=2 in [0,1] in the interval with h=0.1"
dx
Question-(iii):Use Heun's method to approximate
dy
the solution of =1+xy; y(0)=2 in [0,1] in the interval with h=0.1
dx
In[ ]:= x0 = 0;
y0 = 2;
n = 3;
h = 0.1;
f[x_, y_] := 1 + x * y;
list = "i", "x", "y", {0, x0 , y0 };
Fori = 1, i ≤ n, i = i + 1, xi = N[xi-1 + h];
yistar = Nyi-1 + h * f[xi-1 , yi-1 ];
h
yi = Nyi-1 + * f[xi-1 , yi-1 ] + f[xi , yistar ];
2
values = i, xi , yi ;
AppendTolist, values
Gridlist, Dividers -> All
i x y
0 0 2
Out[ ]= 1 0.1 2.1105
2 0.2 2.24337
3 0.3 2.40163
In[ ]:= Print[
"NEWTON DIVIDED DIFFERENCE INTERPOLATION: It is a interpolation technique used when the
interval difference is not same for all sequence of values. Divided differences are
symmetric with respect to the arguments i.e independent of the order of arguments."]
NEWTON DIVIDED DIFFERENCE INTERPOLATION: It is a interpolation technique used when the
interval difference is not same for all sequence of values. Divided differences are
symmetric with respect to the arguments i.e independent of the order of arguments.
In[ ]:= Print["Question(i):Find a unique polynomial of
degree 4 or less such that f(-4)=1245,f(-1)=33, f(0)=5,f(2)=9
and f(5)=1335. Using Newton Divided difference interpolation."]
Question(i):Find a unique polynomial of degree 4 or less such that f(-4)=1245,f(-1)=33,
f(0)=5,f(2)=9 and f(5)=1335. Using Newton Divided difference interpolation.
In[ ]:= xval = {- 4, - 1, 0, 2, 5};
f = {1245, 33, 5, 9, 1335};
nop = Length[xval];
dd[p_] := Sumf[[s]] Product[If[i == s, 1, xval[[s]] - xval[[i]]], {i, 1, p}], {s, 1, p}
poly = Sum[Product[x - xval[[j]], {j, 1, p - 1}] × dd[p], {p, 1, nop}]
Out[ ]= 1245 - 404 (4 + x) + 94 1 + x (4 + x) - 14 x 1 + x (4 + x) + 3 - 2 + x x 1 + x (4 + x)
Numerical Methods Practical file @(2020CHM1093).nb 39
In[ ]:= Simplify[poly]
Out[ ]= 5 - 14 x + 6 x2 - 5 x3 + 3 x4
In[ ]:= Print["The unique polynomial is ", Simplify[poly]]
The unique polynomial is 5 - 14 x + 6 x2 - 5 x3 + 3 x4
In[ ]:= Print["Question(ii):Find a unique polynomial of degree 2 or less such that
f(0)=1,f(1)=3 and f(3)=55. Using Newton Divided difference interpolation."]
Question(ii):Find a unique polynomial of degree 2 or less such that
f(0)=1,f(1)=3 and f(3)=55. Using Newton Divided difference interpolation.
In[ ]:= xval = {0, 1, 3};
f = {1, 3, 55};
nop = Length[xval];
dd[p_] := Sumf[[s]] Product[If[i == s, 1, xval[[s]] - xval[[i]]], {i, 1, p}], {s, 1, p}
poly = Sum[Product[x - xval[[j]], {j, 1, p - 1}] × dd[p], {p, 1, nop}]
Out[ ]= 1 + 2 x + 8 - 1 + x x
In[ ]:= Simplify[poly]
Out[ ]= 1 - 6 x + 8 x2
In[ ]:= Print["The unique polynomial is ", Simplify[poly]]
The unique polynomial is 1 - 6 x + 8 x2
In[ ]:= Print["Question(iii):Find a unique polynomial of degree 2 or less such that
f(-1)=5,f(0)=1 and f(1)=1. Using Newton Divided difference interpolation."]
Question(iii):Find a unique polynomial of degree 2 or less such that
f(-1)=5,f(0)=1 and f(1)=1. Using Newton Divided difference interpolation.
In[ ]:= xval = {- 1, 0, 1};
f = {5, 1, 1};
nop = Length[xval];
dd[p_] := Sumf[[s]] Product[If[i == s, 1, xval[[s]] - xval[[i]]], {i, 1, p}], {s, 1, p}
poly = Sum[Product[x - xval[[j]], {j, 1, p - 1}] × dd[p], {p, 1, nop}]
Out[ ]= 5 - 4 1 + x + 2 x 1 + x
In[ ]:= Simplify[poly]
Out[ ]= 1 - 2 x + 2 x2
In[ ]:= Print["The unique polynomial is ", Simplify[poly]]
The unique polynomial is 1 - 2 x + 2 x2
In[ ]:= Print["LANGRANGE INTERPOLATION"]
LANGRANGE INTERPOLATION
40 Numerical Methods Practical file @(2020CHM1093).nb
In[ ]:= Print["Question(i):Given f(-2)=4,f(0)=2,f(2)=8 find the
quadratic interpolating polynomial using Lagrange interpolation."]
Question(i):Given f(-2)=4,f(0)=2,f(2)=8 find the
quadratic interpolating polynomial using Lagrange interpolation.
In[ ]:= y = {- 2, 0, 2};
f = {4, 2, 8};
nop = 3;
sum = 0;
x - y[[i]]
l[n_] := ProductIfn == i, f[[i]], , {i, 1, nop}
y[[n]] - y[[i]]
For[n = 1, n <= nop, n ++, sum = sum + l[n]]
langr = Expand[sum]
Out[ ]= 2 + x + x2
In[ ]:= Print["The polynomial is ", langr]
The polynomial is 2 + x + x2
In[ ]:= Print[
"Question(ii):Given f(0)=1,f(1)=3,f(3)=55 find the quadratic interpolating polynomial
using Lagrange interpolation. Also estimate the value of f at x=0.5 and x=2.5"]
Question(ii):Given f(0)=1,f(1)=3,f(3)=55 find the quadratic interpolating polynomial
using Lagrange interpolation. Also estimate the value of f at x=0.5 and x=2.5
In[ ]:= y = {0, 1, 3};
f = {1, 3, 55};
nop = 2;
sum = 0;
x - y[[i]]
l[n_] := ProductIfn == i, f[[i]], , {i, 1, nop}
y[[n]] - y[[i]]
For[n = 1, n <= nop, n ++, sum = sum + l[n]]
lang = Expand[sum]
Out[ ]= 1+2x
In[ ]:= Print["If number of point(nop)=3 then "]
If number of point(nop)=3 then
In[ ]:= y = {0, 1, 3};
f = {1, 3, 55};
nop = 3;
sum = 0;
x - y[[i]]
l[n_] := ProductIfn == i, f[[i]], , {i, 1, nop}
y[[n]] - y[[i]]
For[n = 1, n <= nop, n ++, sum = sum + l[n]]
lang = Expand[sum]
Out[ ]= 1 - 6 x + 8 x2
Numerical Methods Practical file @(2020CHM1093).nb 41
In[ ]:= Print["The polynomial is ", b[x]]
The polynomial is 1 - 6 x + 8 x2
In[ ]:= Print["The value at x=0.5 is ", b[0.5], " and at x=2.5 is ", b[2.5]]
The value at x=0.5 is 0. and at x=2.5 is 36.
In[ ]:= Print["Question(iii):Given f(2)=4,f(2.5)=5.5,find
the linear interpolating polynomial using Lagrange interpolation."]
Question(iii):Given f(2)=4,f(2.5)=5.5,find
the linear interpolating polynomial using Lagrange interpolation.
In[ ]:= y = {2, 2.5};
f = {4, 5.5};
nop = 2;
sum = 0;
x - y[[i]]
l[n_] := ProductIfn == i, f[[i]], , {i, 1, nop}
y[[n]] - y[[i]]
For[n = 1, n <= nop, n ++, sum = sum + l[n]]
lang = Expand[sum]
Out[ ]= - 2. + 3. x
In[ ]:= Print["The polynomial is ", lang]
The polynomial is -2. + 3. x
In[ ]:= Print["THANK YOU"]
THANK YOU
Set: Tag Real in 0.201654334550[x_] is Protected.