You are on page 1of 63

Name

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

FIND THE HARMONIC SUM OF FINITE SERIES

In[ ]:= n = Input["Give an integer"];


sum = 0;
Fori = 1, i ≤ n, i ++,
1
sum += ;
i
Print["The harmonic sum of the series is ", sum]

The harmonic sum of the series is 1

In[ ]:= n = Input["Give an integer"];


sum = 0;
Fori = 1, i ≤ n, i ++,
1
sum += ;
i
Print["The harmonic sum of first ", "", i, " number is : ", sum]

The harmonic sum of first 1 number is : 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

In[ ]:= f[x_] := x3 + 4 x2 - 10;


a = 1;
b = 2;
ϵ = 0.01;
Nmax = 5; "Maximum number of iterations";
Iff[a] * f[b] > 0,
Print["These values do not satisfy the IVP, so change the initial value"],
a+b
Fori = 1, i ≤ Nmax, i ++, c = ;
2
b-a
IfAbs  < ϵ, 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, 1, 2}]

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

1.2 1.4 1.6 1.8 2.0

-5

Ques2

In[ ]:= f[x_] := Cos[x];


a = 0;
b = 2;
ϵ = 0.001;
Nmax = 20; "Maximum number of iterations";
Iff[a] * f[b] > 0,
Print["These values do not satisfy the IVP, so change the initial value"],
a+b
Fori = 1, i ≤ Nmax, i ++, c = ;
2
b-a
IfAbs  < ϵ, 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, 2}]

https://dunotes.wixsite.com/dunotes
rishab_8185.nb 5

1th iteration value is :1

Estimated error in 1th iteration is :1


3
2th iteration value is :
2
1
Estimated error in 2th iteration is :
2
7
3th iteration value is :
4
1
Estimated error in 3th iteration is :
4
13
4th iteration value is :
8
1
Estimated error in 4th iteration is :
8
25
5th iteration value is :
16
1
Estimated error in 5th iteration is :
16
51
6th iteration value is :
32
1
Estimated error in 6th iteration is :
32
101
7th iteration value is :
64
1
Estimated error in 7th iteration is :
64
201
8th iteration value is :
128
1
Estimated error in 8th iteration is :
128
403
9th iteration value is :
256
1
Estimated error in 9th iteration is :
256
805
10th iteration value is :
512
1
Estimated error in 10th iteration is :
512
1609
Out[ ]= Return 
1024
The approximate root is :1.57129

https://dunotes.wixsite.com/dunotes
6 rishab_8185.nb

1.0

0.8

0.6

0.4
Out[ ]=
0.2

0.5 1.0 1.5 2.0

-0.2

-0.4

Ques3

In[ ]:=

f[x_] := Cos[x] - x ⅇx ;
a = 0;
b = 1;
ϵ = 0.001;
Nmax = 20; "Maximum number of iterations";
Iff[a] * f[b] > 0,
Print["These values do not satisfy the IVP, so change the initial value"],
a+b
Fori = 1, i ≤ Nmax, i ++, c = ;
2
b-a
IfAbs  < ϵ, 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

0.2 0.4 0.6 0.8 1.0

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";
Iff[a] * f[b] > 0,
Print["These values do not satisfy the IVP, so change the initial value"],
a+b
Fori = 1, i ≤ Nmax, i ++, c = ;
2
b-a
IfAbs  < ϵ, 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

0.2 0.4 0.6 0.8 1.0

Out[ ]= -1

-2

-3

Practical - 3

REGULAR FALSI METHOD OR FALSE POSITION METHOD

Que-1

In[ ]:=

f[x_] := Cos[x];
a = 0;
b = 2;
ϵ = 0.00000001;
Nmax = 10; "Maximum number of iterations";
Iff[a] * f[b] > 0,
Print["These values do not satisfy the IVP, so change the initial value"],
Fori = 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

1th iteration value is :1.41228

Estimated error is :0.587717

2th iteration value is :1.57391

Estimated error is :0.161623

3th iteration value is :1.57078

Estimated error is :0.0031228

4th iteration value is :1.5708

Estimated error is :0.0000128049


Out[ ]= Return[1.5708]

Approximate root is :1.5708

Estimated error is :2.05567 × 10-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";
Iff[a] * f[b] > 0,
Print["These values do not satisfy the IVP, so change the initial value"],
Fori = 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 :", Nb - 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

1th iteration value is :0.698162

Estimated error is :0.698162

2th iteration value is :0.58148

Estimated error is :0.58148

3th iteration value is :0.568735

Estimated error is :0.568735

4th iteration value is :0.56732

Estimated error is :0.56732

5th iteration value is :0.567163

Estimated error is :0.567163

6th iteration value is :0.567145

Estimated error is :0.567145

7th iteration value is :0.567144

Estimated error is :0.567144

8th iteration value is :0.567143

Estimated error is :0.567143

9th iteration value is :0.567143

Estimated error is :0.567143

10th iteration value is :0.567143

Estimated error is :0.567143

11th iteration value is :0.567143

Estimated error is :0.567143

12th iteration value is :0.567143

Estimated error is :0.567143

13th iteration value is :0.567143

Estimated error is :0.567143

14th iteration value is :0.567143

Estimated error is :0.567143

15th iteration value is :0.567143

Estimated error is :0.567143

16th iteration value is :0.567143

Estimated error is :0.567143


Out[ ]= Return[0.567143]

Approximate root is :0.567143

Estimated error is :7.77156 × 10-16

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%.

In[ ]:= f[x_] := Logx2  - 0.7;


a = 0.5;
b = 2;
ϵ = 0.01;
Nmax = 10; "Maximum number of iterations";
Iff[a] * f[b] > 0,
Print["These values do not satisfy the IVP, so change the initial value"],
Fori = 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
14 rishab_8185.nb

1th iteration value is :1.62871

Estimated error is :1.12871

2th iteration value is :1.49701

Estimated error is :0.997014

3th iteration value is :1.4484

Estimated error is :0.948399

4th iteration value is :1.43016

Estimated error is :0.930156

5th iteration value is :1.42327

Estimated error is :0.923267

6th iteration value is :1.42066

Estimated error is :0.920659

7th iteration value is :1.41967

Estimated error is :0.919671

8th iteration value is :1.4193

Estimated error is :0.919296

9th iteration value is :1.41915

Estimated error is :0.919154

10th iteration value is :1.4191

Estimated error is :0.9191

Approximate root is :1.4191

Estimated error is :0.9191


2

-1 1 2 3

-2

Out[ ]=
-4

-6

-8

Ques 4: Find the root of x 5 +2 x-1 , a=0 , b=1.

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";
Iff[a] * f[b] > 0,
Print["These values do not satisfy the IVP, so change the initial value"],
Fori = 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}]

1th iteration value is :0.333333

Estimated error is :0.666667

2th iteration value is :0.427562

Estimated error is :0.572438

3th iteration value is :0.462648

Estimated error is :0.537352

4th iteration value is :0.47665

Estimated error is :0.52335

5th iteration value is :0.482369

Estimated error is :0.517631

6th iteration value is :0.484725

Estimated error is :0.515275

7th iteration value is :0.4857

Estimated error is :0.5143

8th iteration value is :0.486103

Estimated error is :0.513897

9th iteration value is :0.486271

Estimated error is :0.513729

10th iteration value is :0.48634

Estimated error is :0.51366

Approximate root is :0.48634

Estimated error is :0.51366

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";
Iff[a] * f[b] > 0,
Print["These values do not satisfy the IVP, so change the initial value"],
Fori = 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

1th iteration value is :1.88889

Estimated error is :1.11111

2th iteration value is :2.45159

Estimated error is :0.548409

3th iteration value is :2.55287

Estimated error is :0.447126

4th iteration value is :2.56852

Estimated error is :0.431485

5th iteration value is :2.57087

Estimated error is :0.429133

6th iteration value is :2.57122

Estimated error is :0.42878

7th iteration value is :2.57127

Estimated error is :0.428728

8th iteration value is :2.57128

Estimated error is :0.42872

9th iteration value is :2.57128

Estimated error is :0.428719

10th iteration value is :2.57128

Estimated error is :0.428718

Approximate root is :2.57128

Estimated error is :0.428718

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;
Forn = 2, n ≤ Nmax, n ++,
pn-1 - pn-2
pn = Npn-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}]

1th iteration value is 1.1

Estimated error is :0.1

2th iteration value is 1.22173

Estimated error is :0.121729

3th iteration value is 1.19649

Estimated error is :0.0252442

4th iteration value is 1.19865

Estimated error is :0.00216004

5th iteration value is 1.19869

Estimated error is :0.000045968


Out[ ]= Return[1.19869]

Out[ ]= 4

1.2 1.4 1.6 1.8 2.0

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;
Forn = 2, n ≤ Nmax, n ++,
pn-1 - pn-2
pn = Npn-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}]

1th iteration value is 0.6127

Estimated error is :0.6127

2th iteration value is 0.572181

Estimated error is :0.0405184

3th iteration value is 0.567102

Estimated error is :0.00507933

4th iteration value is 0.567143

Estimated error is :0.0000412478


Out[ ]= Return[0.567143]

1.0

0.5

Out[ ]=

0.2 0.4 0.6 0.8 1.0

-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;
Forn = 2, n ≤ Nmax, n ++,
pn-1 - pn-2
pn = Npn-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}]

1th iteration value is 1.71429

Estimated error is :0.714286

2th iteration value is 2.03189

Estimated error is :0.317604

3th iteration value is 1.8351

Estimated error is :0.196786

4th iteration value is 1.85277

Estimated error is :0.0176632

5th iteration value is 1.85563

Estimated error is :0.00286739

6th iteration value is 1.85558

Estimated error is :0.0000492283


Out[ ]= Return[1.85558]

1.2 1.4 1.6 1.8 2.0

-2
Out[ ]=
-4

-6

-8

-10

Question 4

https://dunotes.wixsite.com/dunotes
rishab_8185.nb 21

In[ ]:=

f[x_] := Cos[x] - x Exp[x];


p0 = 1;
p1 = 0;
ϵ = 0.000005;
Nmax = 10;
Forn = 2, n ≤ Nmax, n ++,
pn-1 - pn-2
pn = Npn-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}]

1th iteration value is 0.314665

Estimated error is :0.314665

2th iteration value is 0.655377

Estimated error is :0.340712

3th iteration value is 0.493721

Estimated error is :0.161656

4th iteration value is 0.515134

Estimated error is :0.0214126

5th iteration value is 0.517811

Estimated error is :0.00267697

6th iteration value is 0.517757

Estimated error is :0.0000536062


Out[ ]= Return[0.517757]

1.0

0.5

0.2 0.4 0.6 0.8 1.0

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;
Forn = 2, n ≤ Nmax, n ++,
pn-1 - pn-2
pn = Npn-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}]

1th iteration value is 2.26316

Estimated error is :0.263158

2th iteration value is 2.36635

Estimated error is :0.103191

3th iteration value is 2.35076

Estimated error is :0.0155889

4th iteration value is 2.35133

Estimated error is :0.000571196


Out[ ]= Return[2.35133]
15

10

Out[ ]= 5

2.2 2.4 2.6 2.8 3.0

-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;
Forn = 2, n ≤ Nmax, n ++,
pn-1 - pn-2
pn = Npn-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}]

1th iteration value is -2.17647

Estimated error is :0.823529

2th iteration value is -2.2645

Estimated error is :0.0880268

3th iteration value is -2.34451

Estimated error is :0.0800148

4th iteration value is -2.33262

Estimated error is :0.0118902

5th iteration value is -2.33333

Estimated error is :0.000704349

6th iteration value is -2.33333

Estimated error is :7.05155 × 10-6


Out[ ]= Return[- 2.33333]

20

-4 -3 -2 -1 1

Out[ ]=
-20

-40

-60

Practical - 5

https://dunotes.wixsite.com/dunotes
24 rishab_8185.nb

NEWTON RAPHSON’S METHOD


Question 1

In[ ]:=

f[x_] := 27 x4 + 162 x3 - 180 x2 + 62 x - 7;


x0 = 0;
ϵ = 5 * 10-5 ;
Nmax = 10;
Forn = 1, n ≤ Nmax, n ++,
f[x0 ]
x1 = Nx0 - ;
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, 2}]

https://dunotes.wixsite.com/dunotes
rishab_8185.nb 25

1th iteration value is 0.112903

Estimated error is :0.112903

2th iteration value is 0.187147

Estimated error is :0.0742436

3th iteration value is 0.236208

Estimated error is :0.0490615

4th iteration value is 0.268729

Estimated error is :0.0325205

5th iteration value is 0.290328

Estimated error is :0.0215988

6th iteration value is 0.304691

Estimated error is :0.0143635

7th iteration value is 0.314251

Estimated error is :0.0095599

8th iteration value is 0.320617

Estimated error is :0.00636631

9th iteration value is 0.324858

Estimated error is :0.00424112

10th iteration value is 0.327685

Estimated error is :0.00282605

The final approximate root is 0.327685

Estimated error is :0.

1000

800

600
Out[ ]=

400

200

1.2 1.4 1.6 1.8 2.0

Question 2

https://dunotes.wixsite.com/dunotes
26 rishab_8185.nb

In[ ]:=

f[x_] := Exp[- x] - x;
x0 = 1;
ϵ = 5 * 10-5 ;
Nmax = 20;
Forn = 1, n ≤ Nmax, n ++,
f[x0 ]
x1 = Nx0 - ;
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}]

1th iteration value is 0.537883

Estimated error is :0.462117

2th iteration value is 0.566987

Estimated error is :0.0291041

3th iteration value is 0.567143

Estimated error is :0.000156295


Out[ ]= Return[2.57128]

The final approximate root is 0.567143

Estimated error is :4.42066 × 10-9


4

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;
Forn = 1, n ≤ Nmax, n ++,
f[x0 ]
x1 = Nx0 - ;
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}]

1th iteration value is 2.75

Estimated error is :0.75

2th iteration value is 2.58264

Estimated error is :0.167355

3th iteration value is 2.57133

Estimated error is :0.0113131


Out[ ]= Return[2.57128]

The final approximate root is 2.57128

Estimated error is :0.0000499204


50

40

30

20
Out[ ]=

10

1.5 2.0 2.5 3.0 3.5 4.0

-10

Question 4

https://dunotes.wixsite.com/dunotes
28 rishab_8185.nb

In[ ]:=

f[x_] := Cos[x] - x * Exp[x];


x0 = 1;
ϵ = 10-8 ;
Nmax = 10;
Forn = 1, n ≤ Nmax, n ++,
f[x0 ]
x1 = Nx0 - ;
f '[x0 ]
If[Abs[f[x1 ]] < ϵ, 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, 2}]

1th iteration value is 0.653079

Estimated error is :0.346921

2th iteration value is 0.531343

Estimated error is :0.121736

3th iteration value is 0.51791

Estimated error is :0.0134335

4th iteration value is 0.517757

Estimated error is :0.00015253


Out[ ]= Return[2.57128]

The final approximate root is 0.517757

Estimated error is :1.94824 × 10-8

-1.0 -0.5 0.5 1.0 1.5 2.0

-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

Out[ ]= {{{1, 1, 1}, {4, - 1, - 5}, {3, - 2, - 10}}, {1, 2, 3}, 0}


Out[ ]//MatrixForm=
1 0 0
4 1 0
3 -2 1
Out[ ]//MatrixForm=
1 1 1
0 -1 -5
0 0 - 10
Out[ ]//MatrixForm=
1 1 1
4 3 -1
3 5 3
Out[ ]//MatrixForm=
1
2
5
Out[ ]//MatrixForm=
1
1
2
-1
2

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

Out[ ]= {{{1, 2, 2}, {2, - 1, - 2}, {3, 4, 3}}, {3, 2, 1}, 0}


Out[ ]//MatrixForm=
1 0 0
2 1 0
3 4 1
Out[ ]//MatrixForm=
1 2 2
0 -1 -2
0 0 3
Out[ ]//MatrixForm=
1 2 2
2 3 2
3 2 1
Out[ ]//MatrixForm=
1
0
0
Out[ ]//MatrixForm=
1
0
0

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

Out[ ]= {{{1, 1, - 1}, {3, - 1, 0}, {2, 0, 7}}, {1, 3, 2}, 0}


Out[ ]//MatrixForm=
1 0 0
3 1 0
2 0 1
Out[ ]//MatrixForm=
1 1 -1
0 -1 0
0 0 7
Out[ ]//MatrixForm=
1 1 -1
3 2 -3
2 2 5
Out[ ]//MatrixForm=
2
-9
2
Out[ ]//MatrixForm=

- 47
7
9
2
7

https://dunotes.wixsite.com/dunotes
rishab_8185.nb 33

Practical - 7

GAUSS JACOBI METHOD


Question 1

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}
Fork = 1, k ≤ 25, k ++,
Fori = 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}

Out[ ]= {10, 21, 12}

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}
Fork = 1, k ≤ 25, k ++,
Fori = 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.5, - 0.5, - 0.5}

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}
Fork = 1, k ≤ 25, k ++,
Fori = 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}
Fork = 1, k ≤ 25, k ++,
Fori = 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}

Out[ ]= {10, - 14, - 33}

x[1]=1.

x[2]=-3.

x[3]=4.

Practical - 8

GAUSS SEIDEL METHOD


Question 1

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}
Fork = 1, k ≤ 25, k ++,
Fori = 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}

Out[ ]= {10, 21, 12}

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}
Fork = 1, k ≤ 10, k ++,
Fori = 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

Out[ ]= {0.5, - 0.5, - 0.5}

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}
Fork = 1, k ≤ 25, k ++,
Fori = 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}
Fork = 1, k ≤ 25, k ++,
Fori = 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}

Out[ ]= {10, - 14, - 33}

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}
Fork = 1, k ≤ 6, k ++,
Fori = 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}

Out[ ]= {10, - 14, - 33}

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}
Fork = 1, k ≤ 5, k ++,
Fori = 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}
Fork = 1, k ≤ 10, k ++,
Fori = 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}
Fork = 1, k ≤ 9, k ++,
Fori = 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}
Fork = 1, k ≤ 10, k ++,
Fori = 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

LAGRANGE FORM OF INTERPOLATING POLYNOMIAL

Question 1

https://dunotes.wixsite.com/dunotes
48 rishab_8185.nb

In[ ]:= Clear[x];


sum = 0;
points = {{1, 2}, {2, 5}, {3, 10}};
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_] :=
ProductIfEqual[k, n], 1, x - y[[k]]  y[[n]] - y[[k]], {k, 1, No}
Fori = 1, i ≤ No, i ++,
sum += f[[i]] * Lagrange[No, i]
Print[sum]
Print["The polynomial function will be :", Expand[sum]]
Print["Polynomial at x=2.5 :", sum /. x → 2.5]

Given values of x[i] are as follows:{1, 2, 3}

Given values of f[x[i]] are as follows:{2, 5, 10}

(2 - x) (3 - x) + 5 (3 - x) (-1 + x) + 5 (-2 + x) (-1 + x)

The polynomial function will be :1 + x2

Polynomial at x=2.5 :7.25

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_] :=
ProductIfEqual[k, n], 1, x - y[[k]]  y[[n]] - y[[k]], {k, 1, No}
Fori = 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 x[i] are as follows:{-2, -1, 0, 1, 3, 4}

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

Polynomial at x=0.5 :17.125

Polynomial at x=3.1 :46.791

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_] :=
ProductIfEqual[k, n], 1, x - y[[k]]  y[[n]] - y[[k]], {k, 1, No}
Fori = 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 x[i] are as follows:{1, 3, 4, 5, 7, 10}

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

Polynomial at x=3.5 :47.375

Polynomial at x=8.0 :521.

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_] :=
ProductIfEqual[k, n], 1, x - y[[k]]  y[[n]] - y[[k]], {k, 1, No}
Fori = 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]

Given values of x[i] are as follows:{0, 1, 2, 4, 5, 6}

Given values of f[x[i]] are as follows:{1, 14, 15, 5, 6, 19}


1 7
(1 - x) (2 - x) (4 - x) (5 - x) (6 - x) + (2 - x) (4 - x) (5 - x) (6 - x) x +
240 30
5 5
(4 - x) (5 - x) (6 - x) (-1 + x) x + (5 - x) (6 - x) (-2 + x) (-1 + x) x +
16 48
1 19
(6 - x) (-4 + x) (-2 + x) (-1 + x) x + (-5 + x) (-4 + x) (-2 + x) (-1 + x) x
10 240
The polynomial function will be :1 + 21 x - 9 x2 + x3

Polynomial at x=3.0 :10.

Polynomial at x=5.5 :10.625

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_] :=
ProductIfEqual[k, n], 1, x - y[[k]]  y[[n]] - y[[k]], {k, 1, No}
Fori = 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

Given values of x[i] are as follows:{-1, 1, 4, 7}

Given values of f[x[i]] are as follows:{-2, 0, 63, 342}


1 7 19
- (1 - x) (4 - x) (7 - x) + (7 - x) (-1 + x) (1 + x) + (-4 + x) (-1 + x) (1 + x)
40 5 8
The polynomial function will be :-1 + x3

Polynomial at x=5.0 :124.

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_] :=
ProductIfEqual[k, n], 1, x - y[[k]]  y[[n]] - y[[k]], {k, 1, No}
Fori = 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]

Given values of x[i] are as follows:{-1, 2, 4, 5}

Given values of f[x[i]] are as follows:{-5, 13, 255, 625}


1 13
- (2 - x) (4 - x) (5 - x) + (4 - x) (5 - x) (1 + x) +
18 18
51 625
(5 - x) (-2 + x) (1 + x) + (-4 + x) (-2 + x) (1 + x)
2 18
The polynomial function will be :35 + 3 x - 27 x2 + 10 x3

Polynomial at x=3.0 :71.

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_] :=
Sumf[[i]]  ProductIfEqual[j, i], 1, y[[i]] - y[[j]], {j, 1, k}, {i, 1, k}
p[x_] = Sumdd[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}

Out[ ]= {293, 508, 585, 764}

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_] :=
Sumf[[i]]  ProductIfEqual[j, i], 1, y[[i]] - y[[j]], {j, 1, k}, {i, 1, k}
p[x_] = Sumdd[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, 3, 55}

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_] :=
Sumf[[i]]  ProductIfEqual[j, i], 1, y[[i]] - y[[j]], {j, 1, k}, {i, 1, k}
p[x_] = Sumdd[i] * Product[If[i ≤ j, 1, x - y[[j]]], {j, 1, i - 1}], {i, 1, n}
Simplify[p[x]]

Out[ ]= 6

Out[ ]= {0.5, 1.5, 3., 5., 6.5, 8.}

Out[ ]= {1.625, 5.875, 31., 131., 282.125, 521.}

Out[ ]= 1.625 + 4.25 - 0.5 + x + 5. - 1.5 + x - 0.5 + x + 1. - 3. + x - 1.5 + x - 0.5 + x +


4.44089 × 10-16 (- 5. + x) - 3. + x - 1.5 + x - 0.5 + x -
1.66533 × 10-16 (- 6.5 + x) (- 5. + x) - 3. + x - 1.5 + x - 0.5 + x

Out[ ]= 1. + 1. x + 5.32907 × 10-14 x2 + 1. x3 + 3.19189 × 10-15 x4 - 1.66533 × 10-16 x5

Question 4

https://dunotes.wixsite.com/dunotes
54 rishab_8185.nb

In[ ]:= Clear[p]


sum = 0;
points = {{1, 2}, {2, 3}, {3, 5}, {4, 7}, {5, 11}, {6, 13}, {7, 10}};
n = Length[points]
y = points[[All, 1]]
f = points[[All, 2]]
dd[k_] :=
Sumf[[i]]  ProductIfEqual[j, i], 1, y[[i]] - y[[j]], {j, 1, k}, {i, 1, k}
p[x_] = Sumdd[i] * Product[If[i ≤ j, 1, x - y[[j]]], {j, 1, i - 1}], {i, 1, n}
Simplify[p[x]]
p[6.5]
Out[ ]= 7

Out[ ]= {1, 2, 3, 4, 5, 6, 7}

Out[ ]= {2, 3, 5, 7, 11, 13, 10}

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 = N2 * sum + g[a] + g[b] * 
2

Out[ ]= - 1.74393 × 10-16

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 = N2 * 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 = N2 * 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 = N2 * 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 = N2 * sum + g[a] + g[b] * 
2

Out[ ]= 1.58004

Practical - 13

Simpson’s rule
Question 1

In[ ]:= a = Input["Enter the left hand point of the interval"];


b = Input["Enter the right hand point of the interval"];
b - a
h= ;
2
a + b
c= ;
2
1
g[x_] := ;
x
h
sn = * (g[x] /. x → a) + 4 (g[x] /. x → c) + g[x] /. x → b;
3
Print["Simpson estimate is : ", sn]

76
Simpson estimate is :
45

Question 2

Simpson Rule Module

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

In[9]:= 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
Sin[x]
f[x_] := 1 + ;
x
N[Simp[1, 2, 2]]

Out[11]= 2.97049

https://dunotes.wixsite.com/dunotes
58 rishab_8185.nb

Question 5

In[12]:=

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
Sin[x]
f[x_] := 1 + ;
x
N[Simp[1, 2, 4]]

Out[14]= 2.93654

Question 6

In[15]:=

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
Sin[x]
f[x_] := 1 + ;
x
N[Simp[1, 2, 8]]

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 = Tablea + 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 = Tablea + 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 = Tablea + 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]:=

RungeKutta2ndOrder[a0_, b0_, n0_, f_, alpha_, actualsolution_] :=


b - a
Module{a = a0, b = b0, n = n0, h, ti, k1, k2}, h = ;
n
ti = Tablea + j - 1 h, {j, 1, n + 1};
wi = Table[0, {n + 1}]; wi[[1]] = alpha;
actualsol = actualsolution[ti[[1]]];
difference = Abs[actualsol - wi[[1]]];
OutputDetails = {{0, ti[[1]], alpha, actualsol, difference}};
Fori = 1, i ≤ n, i ++,
k1 = h f[ti[[i]], wi[[i]]];
h k1
k2 = h fti[[i]] + , wi[[i]] + ;
2 2
1
k= k1 + k2;
2
wi[[i + 1]] = wi[[i]] + k;
actualsol = actualsolution[ti[[i + 1]]];
difference = Abs[actualsol - wi[[i + 1]]];
OutputDetails = Append[OutputDetails,
{i, N[ti[[i + 1]]], N[wi[[i + 1]]], N[actualsol], N[difference]}];;
Print[NumberForm[TableForm[OutputDetails, TableHeadings →
{None, {"i", "ti", "wi", "actsol(ti)", "Abs(wi-actsol(ti))"}}], 6]];;
x
f[t_, x_] := 1 + ;
t
actualsolution[t_] := t 1 + Log[t];
a = 1; b = 6;
n = 5; alpha = 1;
RungeKutta2ndOrder[a, b, n, f, alpha, actualsolution];

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

In[39]:= f[t_, x_] := t2 - x;


actualsolution[t_] := 2 - Exp[- t] - 2 t + t2 ;
RungeKutta2ndOrder[0, 0.8, 8, f, 1, actualsolution];

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

You might also like