Professional Documents
Culture Documents
DU Notes
: Rishab
Course A: B.Sc.
Family of Learning
(H) Mathematics
Semester : V
Examination Roll No. : 18035563037
Roll No : 8185
EMail ID : rishab188185@keshav.du.ac.in
Date of submission : 31th November 2020
INDEX
S.No. Title
1. Practical 1 Harmonic sum of finite series
2. Practical 2 Bisection method
3. Practical 3 False position method
4. Practical 4 Secant method
5. Practical 5 Newton Raphson method
6. Practical 6 LU Decomposition
7. Practical 7 Gauss Jacobi method
8. Practical 8 Gauss Seidel method
9. Practical 9 SOR method
10. Practical 10 Lagrange form of interpolating polynomial
11. Practical 11 Newton interpolation
12. Practical 12 Trapezoidal rule
13. Practical 13 Simpson’s rule
14. Practical 14 Euler method
15. Practical 15 Runge-Kutta method
https://dunotes.wixsite.com/dunotes
2 rishab_8185.nb
Practical - 1
3
The harmonic sum of first 2 number is :
2
11
The harmonic sum of first 3 number is :
6
25
The harmonic sum of first 4 number is :
12
137
The harmonic sum of first 5 number is :
60
Practical - 2
BISECTION METHOD
Ques1
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 3
3
1th iteration value is :
2
1
Estimated error in 1th iteration is :
2
5
2th iteration value is :
4
1
Estimated error in 2th iteration is :
4
11
3th iteration value is :
8
1
Estimated error in 3th iteration is :
8
21
4th iteration value is :
16
1
Estimated error in 4th iteration is :
16
43
5th iteration value is :
32
1
Estimated error in 5th iteration is :
32
The approximate root is :1.34375
https://dunotes.wixsite.com/dunotes
4 rishab_8185.nb
15
10
Out[ ]= 5
-5
Ques2
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 5
https://dunotes.wixsite.com/dunotes
6 rishab_8185.nb
1.0
0.8
0.6
0.4
Out[ ]=
0.2
-0.2
-0.4
Ques3
In[ ]:=
f[x_] := Cos[x] - x ⅇx ;
a = 0;
b = 1;
ϵ = 0.001;
Nmax = 20; "Maximum number of iterations";
Iff[a] * f[b] > 0,
Print["These values do not satisfy the IVP, so change the initial value"],
a+b
Fori = 1, i ≤ Nmax, i ++, c = ;
2
b-a
IfAbs < ϵ, Return[c],
2
Print[i, "th iteration value is :", c];
b-a
Print"Estimated error in ", i, "th iteration is :", ;
2
If[f[a] * f[c] < 0, b = c, a = c];
Print["The approximate root is :", N[c]];
Plot[f[x], {x, 0, 1}]
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 7
1
1th iteration value is :
2
1
Estimated error in 1th iteration is :
2
3
2th iteration value is :
4
1
Estimated error in 2th iteration is :
4
5
3th iteration value is :
8
1
Estimated error in 3th iteration is :
8
9
4th iteration value is :
16
1
Estimated error in 4th iteration is :
16
17
5th iteration value is :
32
1
Estimated error in 5th iteration is :
32
33
6th iteration value is :
64
1
Estimated error in 6th iteration is :
64
67
7th iteration value is :
128
1
Estimated error in 7th iteration is :
128
133
8th iteration value is :
256
1
Estimated error in 8th iteration is :
256
265
9th iteration value is :
512
1
Estimated error in 9th iteration is :
512
531
Out[ ]= Return
1024
The approximate root is :0.518555
https://dunotes.wixsite.com/dunotes
8 rishab_8185.nb
1.0
0.5
Out[ ]=
-0.5
-1.0
-1.5
-2.0
Ques4
In[ ]:=
f[x_] := x3 - 5 x + 1;
a = 0;
b = 1;
ϵ = 0.001;
Nmax = 10; "Maximum number of iterations";
Iff[a] * f[b] > 0,
Print["These values do not satisfy the IVP, so change the initial value"],
a+b
Fori = 1, i ≤ Nmax, i ++, c = ;
2
b-a
IfAbs < ϵ, Return[c],
2
Print[i, "th iteration value is :", c];
b-a
Print"Estimated error in ", i, "th iteration is :", ;
2
If[f[a] * f[c] < 0, b = c, a = c];
Print["The approximate root is :", N[c]];
Plot[f[x], {x, 0, 1}]
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 9
1
1th iteration value is :
2
1
Estimated error in 1th iteration is :
2
1
2th iteration value is :
4
1
Estimated error in 2th iteration is :
4
1
3th iteration value is :
8
1
Estimated error in 3th iteration is :
8
3
4th iteration value is :
16
1
Estimated error in 4th iteration is :
16
7
5th iteration value is :
32
1
Estimated error in 5th iteration is :
32
13
6th iteration value is :
64
1
Estimated error in 6th iteration is :
64
25
7th iteration value is :
128
1
Estimated error in 7th iteration is :
128
51
8th iteration value is :
256
1
Estimated error in 8th iteration is :
256
103
9th iteration value is :
512
1
Estimated error in 9th iteration is :
512
207
Out[ ]= Return
1024
The approximate root is :0.202148
https://dunotes.wixsite.com/dunotes
10 rishab_8185.nb
Out[ ]= -1
-2
-3
Practical - 3
Que-1
In[ ]:=
f[x_] := Cos[x];
a = 0;
b = 2;
ϵ = 0.00000001;
Nmax = 10; "Maximum number of iterations";
Iff[a] * f[b] > 0,
Print["These values do not satisfy the IVP, so change the initial value"],
Fori = 1, i ≤ Nmax, i ++,
a * f[b] - b * f[a]
x = N ;
f[b] - f[a]
If[f[x] * f[b] > 0, b = x, a = x];
If[Abs[b - a] < ϵ, Return[x]];
Print[i, "th iteration value is :", N[x]];
Print["Estimated error is :", N[b - a]];
Print["Approximate root is :", N[x]];
Print["Estimated error is :", N[b - a]];
Plot[f[x], {x, - 1, 3}]
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 11
1.0
0.5
Out[ ]=
-1 1 2 3
-0.5
-1.0
Que 2
In[ ]:=
f[x_] := Exp[- x] - x;
a = 0;
b = 2;
ϵ = 0.00000001;
Nmax = 20; "Maximum number of iterations";
Iff[a] * f[b] > 0,
Print["These values do not satisfy the IVP, so change the initial value"],
Fori = 1, i ≤ Nmax, i ++,
a * f[b] - b * f[a]
x = N ;
f[b] - f[a]
If[f[x] * f[b] > 0, b = x, a = x];
If[Abs[b - a] < ϵ, Return[x]];
Print[i, "th iteration value is :", N[x, 6]];
Print"Estimated error is :", Nb - a, 6;
Print["Approximate root is :", N[x]];
Print["Estimated error is :", N[b - a]];
Plot[f[x], {x, - 3, 4}]
https://dunotes.wixsite.com/dunotes
12 rishab_8185.nb
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 13
20
15
Out[ ]= 10
-3 -2 -1 1 2 3 4
-5
Que 3
Ques 3: Determine the positive real root of Log(x 2 )=0.7 using 10 iterations of regular falsi method with
initial guess a=0.5 and b=2. Stop at the error of 1%.
https://dunotes.wixsite.com/dunotes
14 rishab_8185.nb
-1 1 2 3
-2
Out[ ]=
-4
-6
-8
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 15
In[ ]:=
f[x_] := x5 + 2 x - 1;
a = 0;
b = 1;
ϵ = 0.00000001;
Nmax = 10; "Maximum number of iterations";
Iff[a] * f[b] > 0,
Print["These values do not satisfy the IVP, so change the initial value"],
Fori = 1, i ≤ Nmax, i ++,
a * f[b] - b * f[a]
x = N ;
f[b] - f[a]
If[f[x] * f[b] > 0, b = x, a = x];
If[Abs[b - a] < ϵ, Return[x]];
Print[i, "th iteration value is :", N[x]];
Print["Estimated error is :", N[b - a]];
Print["Approximate root is :", N[x]];
Print["Estimated error is :", N[b - a]];
Plot[f[x], {x, - 5, 5}]
https://dunotes.wixsite.com/dunotes
16 rishab_8185.nb
1500
1000
500
Out[ ]=
-4 -2 2 4
-500
-1000
-1500
Ques 5: Find the cube root of 17 by using regular falsi method and the interval is (0,3),
In[ ]:=
f[x_] := x3 - 17;
a = 0;
b = 3;
ϵ = 0.01;
Nmax = 10; "Maximum number of iterations";
Iff[a] * f[b] > 0,
Print["These values do not satisfy the IVP, so change the initial value"],
Fori = 1, i ≤ Nmax, i ++,
a * f[b] - b * f[a]
x = N ;
f[b] - f[a]
If[f[x] * f[b] > 0, b = x, a = x];
If[Abs[b - a] < ϵ, Return[x]];
Print[i, "th iteration value is :", N[x]];
Print["Estimated error is :", N[b - a]];
Print["Approximate root is :", N[x]];
Print["Estimated error is :", N[b - a]];
Plot[f[x], {x, - 1, 3}]
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 17
10
-1 1 2 3
Out[ ]=
-5
-10
-15
Practical - 4
SECANT METHOD
https://dunotes.wixsite.com/dunotes
18 rishab_8185.nb
Question 1
In[ ]:=
f[x_] := x3 + 2 x2 - 3 x - 1;
p0 = 2;
p1 = 1;
ϵ = 0.000005;
Nmax = 10;
Forn = 2, n ≤ Nmax, n ++,
pn-1 - pn-2
pn = Npn-1 - f[pn-1 ] ;
f[pn-1 ] - f[pn-2 ]
If[Abs[pn - pn-1 ] < ϵ, Return[pn ]];
Print[n - 1, "th iteration value is ", pn ];
Print["Estimated error is :", Abs[pn - pn-1 ]];
Plot[f[x], {x, 1, 2}]
Out[ ]= 4
Question 2
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 19
In[ ]:=
f[x_] := Exp[- x] - x;
p0 = 1;
p1 = 0;
ϵ = 0.000005;
Nmax = 10;
Forn = 2, n ≤ Nmax, n ++,
pn-1 - pn-2
pn = Npn-1 - f[pn-1 ] ;
f[pn-1 ] - f[pn-2 ]
If[Abs[pn - pn-1 ] < ϵ, Return[pn ]];
Print[n - 1, "th iteration value is ", pn ];
Print["Estimated error is :", Abs[pn - pn-1 ]];
Plot[f[x], {x, 0, 1}]
1.0
0.5
Out[ ]=
-0.5
Question 3
https://dunotes.wixsite.com/dunotes
20 rishab_8185.nb
In[ ]:=
f[x_] := x4 - x - 10;
p0 = 2;
p1 = 1;
ϵ = 0.000005;
Nmax = 10;
Forn = 2, n ≤ Nmax, n ++,
pn-1 - pn-2
pn = Npn-1 - f[pn-1 ] ;
f[pn-1 ] - f[pn-2 ]
If[Abs[pn - pn-1 ] < ϵ, Return[pn ]];
Print[n - 1, "th iteration value is ", pn ];
Print["Estimated error is :", Abs[pn - pn-1 ]];
Plot[f[x], {x, 1, 2}]
-2
Out[ ]=
-4
-6
-8
-10
Question 4
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 21
In[ ]:=
1.0
0.5
Out[ ]=
-0.5
-1.0
-1.5
-2.0
Question 5
https://dunotes.wixsite.com/dunotes
22 rishab_8185.nb
In[ ]:=
f[x_] := x3 - 13;
p0 = 3;
p1 = 2;
ϵ = 0.000005;
Nmax = 10;
Forn = 2, n ≤ Nmax, n ++,
pn-1 - pn-2
pn = Npn-1 - f[pn-1 ] ;
f[pn-1 ] - f[pn-2 ]
If[Abs[pn - pn-1 ] < ϵ, Return[pn ]];
Print[n - 1, "th iteration value is ", pn ];
Print["Estimated error is :", Abs[pn - pn-1 ]];
Plot[f[x], {x, 2, 3}]
10
Out[ ]= 5
-5
Question 6
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 23
In[ ]:=
f[x_] := 3 x3 + 10 x2 + 10 x + 7;
p0 = - 2;
p1 = - 3;
ϵ = 0.000005;
Nmax = 10;
Forn = 2, n ≤ Nmax, n ++,
pn-1 - pn-2
pn = Npn-1 - f[pn-1 ] ;
f[pn-1 ] - f[pn-2 ]
If[Abs[pn - pn-1 ] < ϵ, Return[pn ]];
Print[n - 1, "th iteration value is ", pn ];
Print["Estimated error is :", Abs[pn - pn-1 ]];
Plot[f[x], {x, - 4, 1}]
20
-4 -3 -2 -1 1
Out[ ]=
-20
-40
-60
Practical - 5
https://dunotes.wixsite.com/dunotes
24 rishab_8185.nb
In[ ]:=
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 25
1000
800
600
Out[ ]=
400
200
Question 2
https://dunotes.wixsite.com/dunotes
26 rishab_8185.nb
In[ ]:=
f[x_] := Exp[- x] - x;
x0 = 1;
ϵ = 5 * 10-5 ;
Nmax = 20;
Forn = 1, n ≤ Nmax, n ++,
f[x0 ]
x1 = Nx0 - ;
f '[x0 ]
If[Abs[x1 - x0 ] < ϵ, Return[x], x2 = x0 ; x0 = x1 ];
Print[n, "th iteration value is ", x1 ];
Print["Estimated error is :", Abs[x1 - x2 ]];
Print["The final approximate root is ", x1 ];
Print["Estimated error is :", Abs[x1 - x0 ]]
Plot[f[x], {x, - 1, 3}]
1
Out[ ]=
-1 1 2 3
-1
-2
-3
Question 3
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 27
In[ ]:=
f[x_] := x3 - 17;
x0 = 2;
ϵ = 5 * 10-5 ;
Nmax = 10;
Forn = 1, n ≤ Nmax, n ++,
f[x0 ]
x1 = Nx0 - ;
f '[x0 ]
If[Abs[x1 - x0 ] < ϵ, Return[x], x2 = x0 ; x0 = x1 ];
Print[n, "th iteration value is ", x1 ];
Print["Estimated error is :", Abs[x1 - x2 ]];
Print["The final approximate root is ", x1 ];
Print["Estimated error is :", Abs[x1 - x0 ]]
Plot[f[x], {x, 1, 4}]
40
30
20
Out[ ]=
10
-10
Question 4
https://dunotes.wixsite.com/dunotes
28 rishab_8185.nb
In[ ]:=
-5
Out[ ]=
-10
-15
Practical - 6
LU DECOMPOSITION
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 29
Question 1
In[ ]:=
ClearAll[a, p, x, lu, c, l, u, d]
a = {{5, 2, 1}, {3, 7, 4}, {1, 1, 9}};
MatrixForm[a]
{lu, p, c} = LUDecomposition[a]
l = lu SparseArray[{i_, j_} /; j < i → 1, {3, 3}] + IdentityMatrix[3];
MatrixForm[l]
u = lu SparseArray[{i_, j_} /; j ≥ i → 1, {3, 3}];
MatrixForm[u]
d = MatrixForm[l.u]
Out[ ]//MatrixForm=
5 2 1
3 7 4
1 1 9
4 245
Out[ ]= {1, 1, 9}, {5, - 3, - 44}, 3, - , - , {3, 1, 2}, 0
3 3
Out[ ]//MatrixForm=
1 0 0
5 1 0
3 -4 1
3
Out[ ]//MatrixForm=
1 1 9
0 - 3 - 44
0 0 - 245
3
Out[ ]//MatrixForm=
1 1 9
5 2 1
3 7 4
Question 2
https://dunotes.wixsite.com/dunotes
30 rishab_8185.nb
In[ ]:=
ClearAll[a, p, x, lu, c, l, u, d]
a = {{1, 1, 1}, {4, 3, - 1}, {3, 5, 3}};
b = {{1}, {6}, {4}};
MatrixForm[a]
MatrixForm[b]
{lu, p, c} = LUDecomposition[a]
l = lu SparseArray[{i_, j_} /; j < i → 1, {3, 3}] + IdentityMatrix[3];
MatrixForm[l]
u = lu SparseArray[{i_, j_} /; j ≥ i → 1, {3, 3}];
MatrixForm[u]
d = MatrixForm[l.u]
z = Inverse[l].b;
MatrixForm[z]
x = Inverse[u].z;
MatrixForm[x]
Out[ ]//MatrixForm=
1 1 1
4 3 -1
3 5 3
Out[ ]//MatrixForm=
1
6
4
Question 3
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 31
In[ ]:=
ClearAll[a, p, x, lu, c, l, u, d]
a = {{3, 2, 1}, {2, 3, 2}, {1, 2, 2}};
b = {{1}, {2}, {3}};
MatrixForm[a]
MatrixForm[b]
{lu, p, c} = LUDecomposition[a]
l = lu SparseArray[{i_, j_} /; j < i → 1, {3, 3}] + IdentityMatrix[3];
MatrixForm[l]
u = lu SparseArray[{i_, j_} /; j ≥ i → 1, {3, 3}];
MatrixForm[u]
d = MatrixForm[l.u]
z = Inverse[l].b;
MatrixForm[z]
x = Inverse[u].z;
MatrixForm[x]
Out[ ]//MatrixForm=
3 2 1
2 3 2
1 2 2
Out[ ]//MatrixForm=
1
2
3
Question 4
https://dunotes.wixsite.com/dunotes
32 rishab_8185.nb
In[ ]:=
ClearAll[a, p, x, lu, c, l, u, d]
a = {{1, 1, - 1}, {2, 2, 5}, {3, 2, - 3}};
b = {{2}, {- 3}, {6}};
MatrixForm[a]
MatrixForm[b]
{lu, p, c} = LUDecomposition[a]
l = lu SparseArray[{i_, j_} /; j < i → 1, {3, 3}] + IdentityMatrix[3];
MatrixForm[l]
u = lu SparseArray[{i_, j_} /; j ≥ i → 1, {3, 3}];
MatrixForm[u]
d = MatrixForm[l.u]
z = Inverse[l].b;
MatrixForm[z]
x = Inverse[u].z;
MatrixForm[x]
Out[ ]//MatrixForm=
1 1 -1
2 2 5
3 2 -3
Out[ ]//MatrixForm=
2
-3
6
- 47
7
9
2
7
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 33
Practical - 7
In[ ]:=
n = 3;
a = {{5, 2, 1}, {3, 7, 4}, {1, 1, 9}};
MatrixForm[a]
x = {0, 0, 0}
y = {0, 0, 0}
b = {10, 21, 12}
Fork = 1, k ≤ 25, k ++,
Fori = 1, i ≤ n, i ++,
y[[i]] = b[[i]] - Sum[a[[i, j]] * x[[j]], {j, 1, i - 1}] -
Sum[a[[i, j]] * x[[j]], {j, i + 1, n}] a[[i, i]];
For[m = 1, m ≤ n, m ++, x[[m]] = N[y[[m]]]]
For[p = 1, p ≤ n, p ++, Print["x[", p, "]=", x[[p]]]]
Out[ ]//MatrixForm=
5 2 1
3 7 4
1 1 9
Out[ ]= {0, 0, 0}
Out[ ]= {0, 0, 0}
x[1]=1.
x[2]=2.
x[3]=1.
Question 2
https://dunotes.wixsite.com/dunotes
34 rishab_8185.nb
In[ ]:=
n = 3;
a = {{4, 1, 1}, {1, 5, 2}, {1, 2, 3}};
MatrixForm[a]
x = {0.5, - 0.5, - 0.5}
y = {0, 0, 0}
b = {2, - 6, - 4}
Fork = 1, k ≤ 25, k ++,
Fori = 1, i ≤ n, i ++,
y[[i]] = b[[i]] - Sum[a[[i, j]] * x[[j]], {j, 1, i - 1}] -
Sum[a[[i, j]] * x[[j]], {j, i + 1, n}] a[[i, i]];
For[m = 1, m ≤ n, m ++, x[[m]] = N[y[[m]]]];
For[p = 1, p ≤ n, p ++, Print["x[", p, "]=", x[[p]]]]
Out[ ]//MatrixForm=
4 1 1
1 5 2
1 2 3
Out[ ]= {0, 0, 0}
Out[ ]= {2, - 6, - 4}
x[1]=0.75
x[2]=-1.1
x[3]=-1.16667
x[1]=1.06667
x[2]=-0.883333
x[3]=-0.85
x[1]=0.933333
x[2]=-1.07333
x[3]=-1.1
x[1]=1.04333
x[2]=-0.946667
x[3]=-0.928889
x[1]=0.968889
x[2]=-1.03711
x[3]=-1.05
x[1]=1.02178
x[2]=-0.973778
x[3]=-0.964889
x[1]=0.984667
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 35
x[2]=-1.0184
x[3]=-1.02474
x[1]=1.01079
x[2]=-0.987037
x[3]=-0.982622
x[1]=0.992415
x[2]=-1.00911
x[3]=-1.01224
x[1]=1.00534
x[2]=-0.993588
x[3]=-0.9914
x[1]=0.996247
x[2]=-1.00451
x[3]=-1.00605
x[1]=1.00264
x[2]=-0.996828
x[3]=-0.995744
x[1]=0.998143
x[2]=-1.00223
x[3]=-1.00299
x[1]=1.00131
x[2]=-0.998431
x[3]=-0.997894
x[1]=0.999081
x[2]=-1.0011
x[3]=-1.00148
x[1]=1.00065
x[2]=-0.999224
x[3]=-0.998958
x[1]=0.999545
x[2]=-1.00055
x[3]=-1.00073
x[1]=1.00032
x[2]=-0.999616
x[3]=-0.999484
x[1]=0.999775
https://dunotes.wixsite.com/dunotes
36 rishab_8185.nb
x[2]=-1.00027
x[3]=-1.00036
x[1]=1.00016
x[2]=-0.99981
x[3]=-0.999745
x[1]=0.999889
x[2]=-1.00013
x[3]=-1.00018
x[1]=1.00008
x[2]=-0.999906
x[3]=-0.999874
x[1]=0.999945
x[2]=-1.00007
x[3]=-1.00009
x[1]=1.00004
x[2]=-0.999953
x[3]=-0.999938
x[1]=0.999973
x[2]=-1.00003
x[3]=-1.00004
Question 3
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 37
In[ ]:=
n = 3;
a = {{2, - 1, 0}, {- 1, 2, - 1}, {0, - 1, 2}};
MatrixForm[a]
x = {0, 0, 0}
y = {0, 0, 0}
b = {7, 1, 1}
Fork = 1, k ≤ 25, k ++,
Fori = 1, i ≤ n, i ++,
y[[i]] = b[[i]] - Sum[a[[i, j]] * x[[j]], {j, 1, i - 1}] -
Sum[a[[i, j]] * x[[j]], {j, i + 1, n}] a[[i, i]];
For[m = 1, m ≤ n, m ++, x[[m]] = N[y[[m]]]]
For[p = 1, p ≤ n, p ++, Print["x[", p, "]=", x[[p]]]]
Out[ ]//MatrixForm=
2 -1 0
-1 2 -1
0 -1 2
Out[ ]= {0, 0, 0}
Out[ ]= {0, 0, 0}
Out[ ]= {7, 1, 1}
x[1]=5.99939
x[2]=4.9989
x[3]=2.99939
Question 4
https://dunotes.wixsite.com/dunotes
38 rishab_8185.nb
In[ ]:=
n = 3;
a = {{5, 1, 2}, {- 3, 9, 4}, {1, 2, - 7}};
MatrixForm[a]
x = {0, 0, 0}
y = {0, 0, 0}
b = {10, - 14, - 33}
Fork = 1, k ≤ 25, k ++,
Fori = 1, i ≤ n, i ++,
y[[i]] = b[[i]] - Sum[a[[i, j]] * x[[j]], {j, 1, i - 1}] -
Sum[a[[i, j]] * x[[j]], {j, i + 1, n}] a[[i, i]];
For[m = 1, m ≤ n, m ++, x[[m]] = N[y[[m]]]]
For[p = 1, p ≤ n, p ++, Print["x[", p, "]=", x[[p]]]]
Out[ ]//MatrixForm=
5 1 2
-3 9 4
1 2 -7
Out[ ]= {0, 0, 0}
Out[ ]= {0, 0, 0}
x[1]=1.
x[2]=-3.
x[3]=4.
Practical - 8
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 39
In[ ]:=
n = 3;
a = {{5, 2, 1}, {3, 7, 4}, {1, 1, 9}};
MatrixForm[a]
x = {0, 0, 0}
y = {0, 0, 0}
b = {10, 21, 12}
Fork = 1, k ≤ 25, k ++,
Fori = 1, i ≤ n, i ++,
y[[i]] = b[[i]] - Sum[a[[i, j]] * y[[j]], {j, 1, i - 1}] -
Sum[a[[i, j]] * x[[j]], {j, i + 1, n}] a[[i, i]];
For[m = 1, m ≤ n, m ++, x[[m]] = N[y[[m]]]]
For[p = 1, p ≤ n, p ++, Print["x[", p, "]=", x[[p]]]]
Out[ ]//MatrixForm=
5 2 1
3 7 4
1 1 9
Out[ ]= {0, 0, 0}
Out[ ]= {0, 0, 0}
x[1]=1.
x[2]=2.
x[3]=1.
Question 2
In[ ]:=
n = 3;
a = {{4, 1, 1}, {1, 5, 2}, {1, 2, 3}};
MatrixForm[a]
x = {0.5, - 0.5, - 0.5}
y = {0, 0, 0}
b = {2, - 6, - 4}
Fork = 1, k ≤ 10, k ++,
Fori = 1, i ≤ n, i ++,
y[[i]] = b[[i]] - Sum[a[[i, j]] * y[[j]], {j, 1, i - 1}] -
Sum[a[[i, j]] * x[[j]], {j, i + 1, n}] a[[i, i]];
For[m = 1, m ≤ n, m ++, x[[m]] = N[y[[m]]]];
For[p = 1, p ≤ n, p ++, Print["x[", p, "]=", x[[p]]]]
Out[ ]//MatrixForm=
4 1 1
1 5 2
1 2 3
https://dunotes.wixsite.com/dunotes
40 rishab_8185.nb
Out[ ]= {0, 0, 0}
Out[ ]= {2, - 6, - 4}
x[1]=0.75
x[2]=-1.15
x[3]=-0.816667
x[1]=0.991667
x[2]=-1.07167
x[3]=-0.949444
x[1]=1.00528
x[2]=-1.02128
x[3]=-0.987574
x[1]=1.00221
x[2]=-1.00541
x[3]=-0.997129
x[1]=1.00064
x[2]=-1.00128
x[3]=-0.999362
x[1]=1.00016
x[2]=-1.00029
x[3]=-0.999862
x[1]=1.00004
x[2]=-1.00006
x[3]=-0.999971
x[1]=1.00001
x[2]=-1.00001
x[3]=-0.999994
x[1]=1.
x[2]=-1.
x[3]=-0.999999
x[1]=1.
x[2]=-1.
x[3]=-1.
Question 3
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 41
In[ ]:=
n = 3;
a = {{2, - 1, 0}, {- 1, 2, - 1}, {0, - 1, 2}};
MatrixForm[a]
x = {0, 0, 0}
y = {0, 0, 0}
b = {7, 1, 1}
Fork = 1, k ≤ 25, k ++,
Fori = 1, i ≤ n, i ++,
y[[i]] = b[[i]] - Sum[a[[i, j]] * y[[j]], {j, 1, i - 1}] -
Sum[a[[i, j]] * x[[j]], {j, i + 1, n}] a[[i, i]];
For[m = 1, m ≤ n, m ++, x[[m]] = N[y[[m]]]]
For[p = 1, p ≤ n, p ++, Print["x[", p, "]=", x[[p]]]]
Out[ ]//MatrixForm=
2 -1 0
-1 2 -1
0 -1 2
Out[ ]= {0, 0, 0}
Out[ ]= {0, 0, 0}
Out[ ]= {7, 1, 1}
x[1]=6.
x[2]=5.
x[3]=3.
Question 4
https://dunotes.wixsite.com/dunotes
42 rishab_8185.nb
In[ ]:=
n = 3;
a = {{5, 1, 2}, {- 3, 9, 4}, {1, 2, - 7}};
MatrixForm[a]
x = {0, 0, 0}
y = {0, 0, 0}
b = {10, - 14, - 33}
Fork = 1, k ≤ 25, k ++,
Fori = 1, i ≤ n, i ++,
y[[i]] = b[[i]] - Sum[a[[i, j]] * y[[j]], {j, 1, i - 1}] -
Sum[a[[i, j]] * x[[j]], {j, i + 1, n}] a[[i, i]];
For[m = 1, m ≤ n, m ++, x[[m]] = N[y[[m]]]]
For[p = 1, p ≤ n, p ++, Print["x[", p, "]=", x[[p]]]]
Out[ ]//MatrixForm=
5 1 2
-3 9 4
1 2 -7
Out[ ]= {0, 0, 0}
Out[ ]= {0, 0, 0}
x[1]=1.
x[2]=-3.
x[3]=4.
Practical -9
SOR METHOD
Question 1
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 43
In[ ]:=
n = 3;
a = {{5, 1, 2}, {- 3, 9, 4}, {1, 2, - 7}};
ω = 0.9;
MatrixForm[a]
x = {0, 0, 0}
y = {0, 0, 0}
b = {10, - 14, - 33}
Fork = 1, k ≤ 6, k ++,
Fori = 1, i ≤ n, i ++,
y[[i]] = 1 - ω * x[[i]] + ω * b[[i]] - Sum[a[[i, j]] * y[[j]], {j, 1, i - 1}] -
Sum[a[[i, j]] * x[[j]], {j, i + 1, n}] a[[i, i]];
For[m = 1, m ≤ n, m ++, x[[m]] = N[y[[m]]]]
For[p = 1, p ≤ n, p ++, Print["x[", p, "]=", x[[p]]]]
Out[ ]//MatrixForm=
5 1 2
-3 9 4
1 2 -7
Out[ ]= {0, 0, 0}
Out[ ]= {0, 0, 0}
x[1]=0.99994
x[2]=-2.99999
x[3]=3.99999
Question 2
https://dunotes.wixsite.com/dunotes
44 rishab_8185.nb
In[ ]:=
n = 3;
a = {{2, - 1, 0}, {- 1, 2, - 1}, {0, - 1, 2}};
ω = 1.1716;
MatrixForm[a]
x = {0, 0, 0}
y = {0, 0, 0}
b = {7, 1, 1}
Fork = 1, k ≤ 5, k ++,
Fori = 1, i ≤ n, i ++,
y[[i]] = 1 - ω * x[[i]] + ω * b[[i]] - Sum[a[[i, j]] * y[[j]], {j, 1, i - 1}] -
Sum[a[[i, j]] * x[[j]], {j, i + 1, n}] a[[i, i]];
For[m = 1, m ≤ n, m ++, x[[m]] = N[y[[m]]]] ×
For[p = 1, p ≤ n, p ++, Print["x[", p, "]=", x[[p]]]]
Out[ ]//MatrixForm=
2 -1 0
-1 2 -1
0 -1 2
Out[ ]= {0, 0, 0}
Out[ ]= {0, 0, 0}
Out[ ]= {7, 1, 1}
x[1]=4.1006
x[2]=2.98793
x[3]=2.33613
x[1]=5.14727
x[2]=4.45685
x[3]=2.79574
x[1]=5.82815
x[2]=4.87288
x[3]=2.96058
x[1]=5.95502
x[2]=4.97238
x[3]=2.99058
x[1]=5.99154
x[2]=4.99426
x[3]=2.99826
Question 3
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 45
In[ ]:=
n = 3;
a = {{3, 2, 0}, {2, 3, - 1}, {0, - 1, 2}};
ω = 1.1;
MatrixForm[a]
x = {0, 0, 0}
y = {0, 0, 0}
b = {5, 4, 1}
Fork = 1, k ≤ 10, k ++,
Fori = 1, i ≤ n, i ++,
y[[i]] = 1 - ω * x[[i]] + ω * b[[i]] - Sum[a[[i, j]] * y[[j]], {j, 1, i - 1}] -
Sum[a[[i, j]] * x[[j]], {j, i + 1, n}] a[[i, i]];
For[m = 1, m ≤ n, m ++, x[[m]] = N[y[[m]]]]
For[p = 1, p ≤ n, p ++, Print["x[", p, "]=", x[[p]]]]
Out[ ]//MatrixForm=
3 2 0
2 3 -1
0 -1 2
Out[ ]= {0, 0, 0}
Out[ ]= {0, 0, 0}
Out[ ]= {5, 4, 1}
x[1]=1.00294
x[2]=0.997512
x[3]=0.998852
Question 4
https://dunotes.wixsite.com/dunotes
46 rishab_8185.nb
In[ ]:=
n = 3;
a = {{4, - 1, 0}, {- 1, 4, - 1}, {0, - 1, 4}};
ω = 1.16;
MatrixForm[a]
x = {0, 0, 0}
y = {0, 0, 0}
b = {3, 2, 3}
Fork = 1, k ≤ 9, k ++,
Fori = 1, i ≤ n, i ++,
y[[i]] = 1 - ω * x[[i]] + ω * b[[i]] - Sum[a[[i, j]] * y[[j]], {j, 1, i - 1}] -
Sum[a[[i, j]] * x[[j]], {j, i + 1, n}] a[[i, i]];
For[m = 1, m ≤ n, m ++, x[[m]] = N[y[[m]]]]
For[p = 1, p ≤ n, p ++, Print["x[", p, "]=", x[[p]]]]
Out[ ]//MatrixForm=
4 -1 0
-1 4 -1
0 -1 4
Out[ ]= {0, 0, 0}
Out[ ]= {0, 0, 0}
Out[ ]= {3, 2, 3}
x[1]=1.
x[2]=1.
x[3]=1.
Question 5
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 47
In[ ]:=
n = 3;
a = {{4, 1, 2}, {3, 5, 1}, {1, 1, 3}};
ω = 0.9;
MatrixForm[a]
x = {0, 0, 0}
y = {0, 0, 0}
b = {4, 7, 3}
Fork = 1, k ≤ 10, k ++,
Fori = 1, i ≤ n, i ++,
y[[i]] = 1 - ω * x[[i]] + ω * b[[i]] - Sum[a[[i, j]] * y[[j]], {j, 1, i - 1}] -
Sum[a[[i, j]] * x[[j]], {j, i + 1, n}] a[[i, i]];
For[m = 1, m ≤ n, m ++, x[[m]] = N[y[[m]]]]
For[p = 1, p ≤ n, p ++, Print["x[", p, "]=", x[[p]]]]
Out[ ]//MatrixForm=
4 1 2
3 5 1
1 1 3
Out[ ]= {0, 0, 0}
Out[ ]= {0, 0, 0}
Out[ ]= {4, 7, 3}
x[1]=0.500014
x[2]=0.999991
x[3]=0.499998
Practical - 10
Question 1
https://dunotes.wixsite.com/dunotes
48 rishab_8185.nb
Question 2
In[ ]:=
Clear[x];
sum = 0;
points = {{- 2, 9}, {- 1, 16}, {0, 17}, {1, 18}, {3, 44}, {4, 81}};
No = Length[points];
Print["Given values of x[i] are as follows:", y = points[[All, 1]]]
Print["Given values of f[x[i]] are as follows:", f = points[[All, 2]]]
Lagrange[No_, n_] :=
ProductIfEqual[k, n], 1, x - y[[k]] y[[n]] - y[[k]], {k, 1, No}
Fori = 1, i ≤ No, i ++,
sum += f[[i]] * Lagrange[No, i]
Print[sum]
Print["The polynomial function will be :", Expand[sum]]
Print["Polynomial at x=0.5 :", sum /. x → 0.5]
Print["Polynomial at x=3.1 :", sum /. x → 3.1]
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 49
Given values of f[x[i]] are as follows:{9, 16, 17, 18, 44, 81}
1 2
- (-1 - x) (1 - x) (3 - x) (4 - x) x - (1 - x) (3 - x) (4 - x) x (2 + x) +
20 5
17 1
(1 - x) (3 - x) (4 - x) (1 + x) (2 + x) + (3 - x) (4 - x) x (1 + x) (2 + x) +
24 2
11 9
(4 - x) (-1 + x) x (1 + x) (2 + x) + (-3 + x) (-1 + x) x (1 + x) (2 + x)
30 40
The polynomial function will be :17 + x3
Question 3
In[ ]:=
Clear[x];
sum = 0;
points = {{1, 3}, {3, 31}, {4, 69}, {5, 131}, {7, 351}, {10, 1011}};
No = Length[points];
Print["Given values of x[i] are as follows:", y = points[[All, 1]]]
Print["Given values of f[x[i]] are as follows:", f = points[[All, 2]]]
Lagrange[No_, n_] :=
ProductIfEqual[k, n], 1, x - y[[k]] y[[n]] - y[[k]], {k, 1, No}
Fori = 1, i ≤ No, i ++,
sum += f[[i]] * Lagrange[No, i]
Print[sum]
Print["The polynomial function will be :", Expand[sum]]
Print["Polynomial at x=3.5 :", sum /. x → 3.5]
Print["Polynomial at x=8.0 :", sum /. x → 8.0]
Given values of f[x[i]] are as follows:{3, 31, 69, 131, 351, 1011}
1 31
(3 - x) (4 - x) (5 - x) (7 - x) (10 - x) + (4 - x) (5 - x) (7 - x) (10 - x) (-1 + x) +
432 112
23 131
(5 - x) (7 - x) (10 - x) (-3 + x) (-1 + x) + (7 - x) (10 - x) (-4 + x) (-3 + x) (-1 + x) +
18 80
13 337 (-7 + x) (-5 + x) (-4 + x) (-3 + x) (-1 + x)
(10 - x) (-5 + x) (-4 + x) (-3 + x) (-1 + x) +
16 1890
The polynomial function will be :1 + x + x3
Question 4
https://dunotes.wixsite.com/dunotes
50 rishab_8185.nb
In[ ]:=
Clear[x];
sum = 0;
points = {{0, 1}, {1, 14}, {2, 15}, {4, 5}, {5, 6}, {6, 19}};
No = Length[points];
Print["Given values of x[i] are as follows:", y = points[[All, 1]]]
Print["Given values of f[x[i]] are as follows:", f = points[[All, 2]]]
Lagrange[No_, n_] :=
ProductIfEqual[k, n], 1, x - y[[k]] y[[n]] - y[[k]], {k, 1, No}
Fori = 1, i ≤ No, i ++,
sum += f[[i]] * Lagrange[No, i]
Print[sum]
Print["The polynomial function will be :", Expand[sum]]
Print["Polynomial at x=3.0 :", sum /. x → 3.0]
Print["Polynomial at x=5.5 :", sum /. x → 5.5]
Question 5
In[ ]:=
Clear[x];
sum = 0;
points = {{- 1, - 2}, {1, 0}, {4, 63}, {7, 342}};
No = Length[points];
Print["Given values of x[i] are as follows:", y = points[[All, 1]]]
Print["Given values of f[x[i]] are as follows:", f = points[[All, 2]]]
Lagrange[No_, n_] :=
ProductIfEqual[k, n], 1, x - y[[k]] y[[n]] - y[[k]], {k, 1, No}
Fori = 1, i ≤ No, i ++,
sum += f[[i]] * Lagrange[No, i]
Print[sum]
Print["The polynomial function will be :", Expand[sum]]
Print["Polynomial at x=5.0 :", sum /. x → 5.0]
rishab_8185.nb 51
Question 6
In[ ]:=
Clear[x];
sum = 0;
points = {{- 1, - 5}, {2, 13}, {4, 255}, {5, 625}};
No = Length[points];
Print["Given values of x[i] are as follows:", y = points[[All, 1]]]
Print["Given values of f[x[i]] are as follows:", f = points[[All, 2]]]
Lagrange[No_, n_] :=
ProductIfEqual[k, n], 1, x - y[[k]] y[[n]] - y[[k]], {k, 1, No}
Fori = 1, i ≤ No, i ++,
sum += f[[i]] * Lagrange[No, i]
Print[sum]
Print["The polynomial function will be :", Expand[sum]]
Print["Polynomial at x=3.0 :", sum /. x → 3.0]
Practical - 11
NEWTON INTERPOLATION
Question 1
https://dunotes.wixsite.com/dunotes
52 rishab_8185.nb
In[ ]:=
Clear[p]
sum = 0;
points = {{3, 293}, {5, 508}, {6, 585}, {9, 764}};
n = Length[points]
y = points[[All, 1]]
f = points[[All, 2]]
dd[k_] :=
Sumf[[i]] ProductIfEqual[j, i], 1, y[[i]] - y[[j]], {j, 1, k}, {i, 1, k}
p[x_] = Sumdd[i] * Product[If[i ≤ j, 1, x - y[[j]]], {j, 1, i - 1}], {i, 1, n}
Simplify[p[x]]
Evaluate[p[2.5]]
Out[ ]= 4
Out[ ]= {3, 5, 6, 9}
215 61 35
Out[ ]= 293 + - 3 + x - (- 5 + x) - 3 + x + (- 6 + x) (- 5 + x) - 3 + x
2 6 36
1
Out[ ]= - 9702 + 9003 x - 856 x2 + 35 x3
36
Out[ ]= 222.288
Question 2
In[ ]:=
Clear[p]
sum = 0;
points = {{0, 1}, {1, 3}, {3, 55}};
n = Length[points]
y = points[[All, 1]]
f = points[[All, 2]]
dd[k_] :=
Sumf[[i]] ProductIfEqual[j, i], 1, y[[i]] - y[[j]], {j, 1, k}, {i, 1, k}
p[x_] = Sumdd[i] * Product[If[i ≤ j, 1, x - y[[j]]], {j, 1, i - 1}], {i, 1, n}
Simplify[p[x]]
Out[ ]= 3
Out[ ]= {0, 1, 3}
Out[ ]= 1 + 2 x + 8 - 1 + x x
Out[ ]= 1 - 6 x + 8 x2
Question 3
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 53
In[ ]:=
Clear[p]
sum = 0;
points =
{{0.5, 1.625}, {1.5, 5.875}, {3.0, 31.0}, {5.0, 131.0}, {6.5, 282.125}, {8.0, 521.0}};
n = Length[points]
y = points[[All, 1]]
f = points[[All, 2]]
dd[k_] :=
Sumf[[i]] ProductIfEqual[j, i], 1, y[[i]] - y[[j]], {j, 1, k}, {i, 1, k}
p[x_] = Sumdd[i] * Product[If[i ≤ j, 1, x - y[[j]]], {j, 1, i - 1}], {i, 1, n}
Simplify[p[x]]
Out[ ]= 6
Question 4
https://dunotes.wixsite.com/dunotes
54 rishab_8185.nb
Out[ ]= {1, 2, 3, 4, 5, 6, 7}
1 1
Out[ ]= 1+ - 2 + x - 1 + x - - 3 + x - 2 + x - 1 + x +
2 6
1 3
(- 4 + x) - 3 + x - 2 + x - 1 + x - (- 5 + x) (- 4 + x) - 3 + x - 2 + x - 1 + x +
8 40
1
(- 6 + x) (- 5 + x) (- 4 + x) - 3 + x - 2 + x - 1 + x + x
45
1
Out[ ]= 11 160 - 24 600 x + 21 182 x2 - 8685 x3 + 1850 x4 - 195 x5 + 8 x6
360
Out[ ]= 11.7539
Practical - 12
Trapezoidal rule
In[ ]:= a = 0;
b = 2 π;
n = 50;
sum = 0;
b - a
h= ;
n
g[x_] := Sin[x];
For[i = 1, i ≤ n - 1, i ++, sum += N[g[a + i * h]]]
h
sum = N2 * sum + g[a] + g[b] *
2
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 55
In[ ]:= a = 0;
b = 2;
n = 8;
sum = 0;
b - a
h= ;
n
1
g[x_] := ;
5+3x
For[i = 1, i ≤ n - 1, i ++, sum += N[g[a + i * h]]]
h
sum = N2 * sum + g[a] + g[b] *
2
Out[ ]= 0.263314
In[ ]:= a = 1;
b = 2;
n = 2;
sum = 0;
b - a
h= ;
n
x
g[x_] := ;
Sin[x]
For[i = 1, i ≤ n - 1, i ++, sum += N[g[a + i * h]]]
h
sum = N2 * sum + g[a] + g[b] *
2
Out[ ]= 1.59886
In[ ]:= a = 1;
b = 2;
n = 4;
sum = 0;
b - a
h= ;
n
x
g[x_] := ;
Sin[x]
For[i = 1, i ≤ n - 1, i ++, sum += N[g[a + i * h]]]
h
sum = N2 * sum + g[a] + g[b] *
2
Out[ ]= 1.57335
https://dunotes.wixsite.com/dunotes
56 rishab_8185.nb
In[ ]:= a = 1;
b = 2;
n = 3;
sum = 0;
b - a
h= ;
n
x
g[x_] := ;
Sin[x]
For[i = 1, i ≤ n - 1, i ++, sum += N[g[a + i * h]]]
h
sum = N2 * sum + g[a] + g[b] *
2
Out[ ]= 1.58004
Practical - 13
Simpson’s rule
Question 1
76
Simpson estimate is :
45
Question 2
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 57
In[ ]:= Simp[a0_, b0_, m0_] := Module{a = N[a0], b = N[b0], k, m = m0, X},
b - a
h= ;
m
Xk_ = a + k h;
h m-1 m
Return f[a] + f[b] + 2 f[X2 k ] + 4 f[X2 k-1 ] ;
3 k=1 k=1
1
f[x_] := ;
1+x
N[Simp[0, 1, 2]]
Out[ ]= 1.12778
Question 3
In[1]:= ClearAll
a = Input["Enter the left hand point of the interval"];
b = Input["Enter the right hand point of the interval"];
b - a
h= ;
8
a + b
c= ;
2
1
l[x_] := ;
5+3x
h
sn = * l[x] /. x → a + 4 l[x] /. x → c + l[x] /. x → b;
3
Print["Simpson estimate is : ", sn]
Out[1]= ClearAll
8495
Simpson estimate is :
139 664
Question 4
Out[11]= 2.97049
https://dunotes.wixsite.com/dunotes
58 rishab_8185.nb
Question 5
In[12]:=
Out[14]= 2.93654
Question 6
In[15]:=
Out[17]= 2.91955
Practical - 14
Euler method
Question 1
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 59
In[18]:= EulerMethod[a0_, b0_, n0_, f_, alpha_] := Module{a = a0, b = b0, n = n0, h, ti},
b - a
h= ;
n
ti = Tablea + j - 1 h, {j, 1, n + 1};
wi = Table[0, {n + 1}];
wi[[1]] = alpha;
OutputDetails = {{0, ti[[1]], alpha}};
For[i = 1, i ≤ n, i ++,
wi[[i + 1]] = wi[[i]] + h * f[ti[[i]], wi[[i]]];
OutputDetails = Append[OutputDetails, {i, N[ti[[i + 1]]], N[wi[[i + 1]]]}];];
Print[NumberForm[
TableForm[OutputDetails, TableHeadings → {None, {"i", "ti", "wi"}}], 6]];;
t
f[t_, x_] := ;
x
a = 0; b = 5;
n = 10; alpha = 1;
EulerMethod[a, b, n, f, alpha];
i ti wi
0 0 1
1 0.5 1.
2 1. 1.25
3 1.5 1.65
4 2. 2.10455
5 2.5 2.57971
6 3. 3.06426
7 3.5 3.55377
8 4. 4.04621
9 4.5 4.5405
10 5. 5.03604
Question 2
https://dunotes.wixsite.com/dunotes
60 rishab_8185.nb
In[23]:=
EulerMethod[a0_, b0_, n0_, f_, alpha_] := Module{a = a0, b = b0, n = n0, h, ti},
b - a
h= ;
n
ti = Tablea + j - 1 h, {j, 1, n + 1};
wi = Table[0, {n + 1}];
wi[[1]] = alpha;
OutputDetails = {{0, ti[[1]], alpha}};
For[i = 1, i ≤ n, i ++,
wi[[i + 1]] = wi[[i]] + h * f[ti[[i]], wi[[i]]];
OutputDetails = Append[OutputDetails, {i, N[ti[[i + 1]]], N[wi[[i + 1]]]}];];
Print[NumberForm[
TableForm[OutputDetails, TableHeadings → {None, {"i", "ti", "wi"}}], 6]];;
x
f[t_, x_] := 1 + ;
t
a = 1; b = 10;
n = 10; alpha = 2;
EulerMethod[a, b, n, f, alpha];
i ti wi
0 1 2
1 1.9 4.7
2 2.8 7.82632
3 3.7 11.2419
4 4.6 14.8764
5 5.5 18.687
6 6.4 22.6449
7 7.3 26.7294
8 8.2 30.9248
9 9.1 35.219
10 10. 39.6021
Question 3
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 61
In[28]:=
EulerMethod[a0_, b0_, n0_, f_, alpha_] := Module{a = a0, b = b0, n = n0, h, ti},
b - a
h= ;
n
ti = Tablea + j - 1 h, {j, 1, n + 1};
wi = Table[0, {n + 1}];
wi[[1]] = alpha;
OutputDetails = {{0, ti[[1]], alpha}};
For[i = 1, i ≤ n, i ++,
wi[[i + 1]] = wi[[i]] + h * f[ti[[i]], wi[[i]]];
OutputDetails = Append[OutputDetails, {i, N[ti[[i + 1]]], N[wi[[i + 1]]]}];];
Print[NumberForm[
TableForm[OutputDetails, TableHeadings → {None, {"i", "ti", "wi"}}], 6]];;
f[x_, y_] := x + y;
a = 0; b = 4;
n = 4; alpha = 1;
EulerMethod[a, b, n, f, alpha];
i ti wi
0 0 1
1 1. 2.
2
3
2.
3.
5.
12. https://dunotes.wixsite.com/dunotes
4 4. 27.
Practical - 15
RUNGE-KUTTA METHOD
Ques 1: Using Runge kutta method of second order, find approximate solution for the initial value
problem x’(t) = 1 + xt , 1 ≤ t ≤ 6 , x(1) = 1. Use n=5 discrete points at equal space. Compare the approxi-
mate solution with the exact solution x(t) = t[1+ln(t)]
https://dunotes.wixsite.com/dunotes
62 rishab_8185.nb
In[33]:=
i ti wi actsol(ti) Abs(wi-actsol(ti))
0 1 1 1 0
1 2. 3.16667 3.38629 0.219628
2 3. 5.85 6.29584 0.445837
3 4. 8.87143 9.54518 0.673749
4 5. 12.1448 13.0472 0.902348
5 6. 15.6193 16.7506 1.13129
Ques 2: Using Runge kutta method of second order, find approximate solution for the initial value
problem x’(t) = t2 - x , 0 ≤ t ≤ 0.8 , x(0) = 1. Use n=8 discrete points at equal space. Compare the
approximate solution with the exact solution x(t) = 2-Exp[-t] - 2t + t2 .
In[ ]:=
Clear[f]
https://dunotes.wixsite.com/dunotes
rishab_8185.nb 63
i ti wi actsol(ti) Abs(wi-actsol(ti))
0 0. 1 1. 0.
1 0.1 0.902625 0.905163 0.00253758
2 0.2 0.816219 0.821269 0.00505018
3 0.3 0.741663 0.749182 0.00751908
4 0.4 0.679751 0.68968 0.00992936
5 0.5 0.6312 0.643469 0.0122694
6 0.6 0.596658 0.611188 0.0145304
7 0.7 0.576709 0.593415 0.0167059
8 0.8 0.57188 0.590671 0.0187914
DU Notes
A Family of Learning
https://dunotes.wixsite.com/dunotes