0% found this document useful (0 votes)
16 views40 pages

Shivam File

shiv

Uploaded by

shivamjics584
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views40 pages

Shivam File

shiv

Uploaded by

shivamjics584
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

ACHARYA NARENDRA DEV COLLEGE

(University Of Delhi) 2024-25

Numerical Method Practical File

Submitted By: Submitted To:

Shivam Yadav Jyoti Ma’am


Roll No. = AE-1247
B.Sc (H) Computer Science
Semester - V
INDEX
S.No. Title

1. BISECTION METHOD

2. NEWTON-RAPHSON METHOD

3. SECANT METHOD

4. GAUSS-JACOBI METHOD

5. GAUSS-SEIDEL METHOD

7. LAGRANGE INTERPOLATION

8. NEWTON POLYNOMINAL

9. TRAPEZOIDAL RULE

REGULA FALSI
10.
Practical1(Bisection Method)

Question 1
In[1]:= f[x_] := x ^ 3 - 5 * x + 1;
Plotf[x], {x, - 1, 3} , PlotRange → {- 5, 5} ,
PlotStyle → Red, Thick, PlotLabel → "f[x]=" f[x], AxesLabel → {x, f[x]}
f[x]= x3 - 5 x + 1
3
1-5x +x

Out[2]=
x
-1 1 2 3

-2

-4

In[13]:= x0 = 0;
x1 = 1;
Nmax = 20;
eps = 0.0001;
IfN[f[x0] * f[x1]] > 0,
Print"your value do not satisfy the IVP , so change the values" ,
For i = 1 , i ≤ Nmax , i ++ , m = (x0 + x1) / 2;
IfAbs[(x1 - x0) / 2] < eps , Return[N[m]], Print i, "th iteration value is :", N[m];
Print"estimated error in" , i, "the iteration is:", N[(x1 - x0) / 2];
Iff[m] * f[x1] > 0 , x1 = m, x0 = m;
Print"Root is :", m *
Print"estimated error in" , "the iteration is:", (x1 - x0) / 2
2

1th iteration value is :0.5


estimated error in1the iteration is:0.5
2th iteration value is :0.25
estimated error in2the iteration is:0.25
3th iteration value is :0.125
estimated error in3the iteration is:0.125
4th iteration value is :0.1875
estimated error in4the iteration is:0.0625
5th iteration value is :0.21875
estimated error in5the iteration is:0.03125
6th iteration value is :0.203125
estimated error in6the iteration is:0.015625
7th iteration value is :0.195313
estimated error in7the iteration is:0.0078125
8th iteration value is :0.199219
estimated error in8the iteration is:0.00390625
9th iteration value is :0.201172
estimated error in9the iteration is:0.00195313
10th iteration value is :0.202148
estimated error in10the iteration is:0.000976563
11th iteration value is :0.20166
estimated error in11the iteration is:0.000488281
12th iteration value is :0.201416
estimated error in12the iteration is:0.000244141
13th iteration value is :0.201538
estimated error in13the iteration is:0.00012207
Out[17]=
Return[0.201599]
3

Question 2
f[x_] := Cos[x];
Plotf[x], {x, - 1, 3} , PlotRange → {- 5, 5} ,
PlotStyle → Red, Thick, PlotLabel → "f[x]=" f[x], AxesLabel → {x, f[x]}

Out[19]=
f[x]= cos(x)
cos(x)

x
-1 1 2 3

-2

-4

In[25]:= x0 = 1;
x1 = 2;
Nmax = 20;
eps = 0.0001;
IfN[f[x0] * f[x1]] > 0,
Print"your value do not satisfy the IVP , so change the values" ,
For i = 1 , i ≤ Nmax , i ++ , m = (x0 + x1) / 2;
IfAbs[(x1 - x0) / 2] < eps , Return[N[m]], Print i, "th iteration value is :", N[m];
Print"estimated error in" , i, "the iteration is:", N[(x1 - x0) / 2];
Iff[m] * f[x1] > 0 , x1 = m, x0 = m;
Print"Root is :", m *
Print"estimated error in" , "the iteration is:", (x1 - x0) / 2
4

1th iteration value is :1.5


estimated error in1the iteration is:0.5
2th iteration value is :1.75
estimated error in2the iteration is:0.25
3th iteration value is :1.625
estimated error in3the iteration is:0.125
4th iteration value is :1.5625
estimated error in4the iteration is:0.0625
5th iteration value is :1.59375
estimated error in5the iteration is:0.03125
6th iteration value is :1.57813
estimated error in6the iteration is:0.015625
7th iteration value is :1.57031
estimated error in7the iteration is:0.0078125
8th iteration value is :1.57422
estimated error in8the iteration is:0.00390625
9th iteration value is :1.57227
estimated error in9the iteration is:0.00195313
10th iteration value is :1.57129
estimated error in10the iteration is:0.000976563
11th iteration value is :1.5708
estimated error in11the iteration is:0.000488281
12th iteration value is :1.57056
estimated error in12the iteration is:0.000244141
13th iteration value is :1.57068
estimated error in13the iteration is:0.00012207
Out[29]=
Return[1.57074]
5

Question 3
In[30]:= f[x_] := Cos[x] - x * Exp[x];
Plotf[x], {x, - 1, 3} , PlotRange → {- 5, 5} ,
PlotStyle → Red, Thick, PlotLabel → "f[x]=" f[x], AxesLabel → {x, f[x]}

Out[31]=
f[x]= cos(x) - ⅇx x
x
-ⅇ x + cos(x)

x
-1 1 2 3

-2

-4

In[32]:= x0 = 0;
x1 = 1;
Nmax = 20;
eps = 0.0001;
IfN[f[x0] * f[x1]] > 0,
Print"your value do not satisfy the IVP , so change the values" ,
For i = 1 , i ≤ Nmax , i ++ , m = (x0 + x1) / 2;
IfAbs[(x1 - x0) / 2] < eps , Return[N[m]], Print i, "th iteration value is :", N[m];
Print"estimated error in" , i, "the iteration is:", N[(x1 - x0) / 2];
Iff[m] * f[x1] > 0 , x1 = m, x0 = m;
Print"Root is :", m *
Print"estimated error in" , "the iteration is:", (x1 - x0) / 2
6

1th iteration value is :0.5


estimated error in1the iteration is:0.5
2th iteration value is :0.75
estimated error in2the iteration is:0.25
3th iteration value is :0.625
estimated error in3the iteration is:0.125
4th iteration value is :0.5625
estimated error in4the iteration is:0.0625
5th iteration value is :0.53125
estimated error in5the iteration is:0.03125
6th iteration value is :0.515625
estimated error in6the iteration is:0.015625
7th iteration value is :0.523438
estimated error in7the iteration is:0.0078125
8th iteration value is :0.519531
estimated error in8the iteration is:0.00390625
9th iteration value is :0.517578
estimated error in9the iteration is:0.00195313
10th iteration value is :0.518555
estimated error in10the iteration is:0.000976563
11th iteration value is :0.518066
estimated error in11the iteration is:0.000488281
12th iteration value is :0.517822
estimated error in12the iteration is:0.000244141
13th iteration value is :0.5177
estimated error in13the iteration is:0.00012207
Out[36]=
Return[0.517761]
Practical 2(N-R Method)
Question1
In[25]:= f[x_] = Cos[x];
Plotf[x] , {x, - 1, 3} , PlotStyle → Red, Thick ,
AxesLabel → {x, f[x]} , PlotLabel → "f[x]=" f[x]
Out[26]=
f[x]= cos(x)
cos(x)

1.0

0.5

x
-1 1 2 3

-0.5

-1.0

In[27]:= x0 = 1;
Nmax = 20;
eps = 10 ^ (- 6);
Print"f[x]=" , f[x];
Print"f[x]=" , D[f[x], x];
Fori = 1, i ≤ Nmax, i ++ , x1 = Nx0 - f[x] /. x → x0   D[f[x], x] /. x → x0;
If Absx1 - x0  < eps, Return[x1] , x0p = x0; x0 = x1;
Print "In", i , "th Number of iteration the approximation to root is:", x1;
Print  "Estimated error is :", Abs[x1 - x0p];
Print"the final approximation of root is:" , x1;
Print  "Estimated error is :", Abs[x1 - x0]
2

f[x]=Cos[x]
f[x]=- Sin[x]
In1th Number of iteration the approximation to root is:1.64209
Estimated error is :0.642093
In2th Number of iteration the approximation to root is:1.57068
Estimated error is :0.0714173
In3th Number of iteration the approximation to root is:1.5708
Estimated error is :0.00012105
Out[32]=
Return[1.5708]

the final approximation of root is:1.5708


Estimated error is :5.91305 × 10-13

Question2
In[35]:= f[x_] = x ^ 3 - 5 * x + 1;
Plotf[x] , {x, - 1, 3} , PlotStyle → Red, Thick ,
AxesLabel → {x, f[x]} , PlotLabel → "f[x]=" f[x]
Out[36]=
f[x]= x3 - 5 x + 1
3
1-5x +x

10

x
-1 1 2 3
3

In[37]:= x0 = 0.5;
Nmax = 20;
eps = 10 ^ (- 6);
Print"f[x]=" , f[x];
Print"f[x]=" , D[f[x], x];
Fori = 1, i ≤ Nmax, i ++ , x1 = Nx0 - f[x] /. x → x0   D[f[x], x] /. x → x0;
If Absx1 - x0  < eps, Return[x1] , x0p = x0; x0 = x1;
Print "In", i , "th Number of iteration the approximation to root is:", x1;
Print  "Estimated error is :", Abs[x1 - x0p];
Print"the final approximation of root is:" , x1;
Print  "Estimated error is :", Abs[x1 - x0]

f[x]=1 - 5 x + x3
f[x]=- 5 + 3 x2
In1th Number of iteration the approximation to root is:0.176471
Estimated error is :0.323529
In2th Number of iteration the approximation to root is:0.201568
Estimated error is :0.0250975
In3th Number of iteration the approximation to root is:0.20164
Estimated error is :0.0000716007
Out[42]=
Return[0.20164]

the final approximation of root is:0.20164


Estimated error is :6.35602 × 10-10
4

Question 3
In[45]:= f[x_] = Cos[x] - x * Exp[x];
Plotf[x] , {x, - 1, 3} , PlotStyle → Red, Thick ,
AxesLabel → {x, f[x]} , PlotLabel → "f[x]=" f[x]
Out[46]=
f[x]= cos(x) - ⅇx x
x
-ⅇ x + cos(x)

x
-1 1 2 3

-10

-20

-30

-40

-50

-60

In[55]:= x0 = 1.5;
Nmax = 20;
eps = 10 ^ (- 6);
Print"f[x]=" , f[x];
Print"f[x]=" , D[f[x], x];
Fori = 1, i ≤ Nmax, i ++ , x1 = Nx0 - f[x] /. x → x0   D[f[x], x] /. x → x0;
If Absx1 - x0  < eps, Return[x1] , x0p = x0; x0 = x1;
Print "In", i , "th Number of iteration the approximation to root is:", x1;
Print  "Estimated error is :", Abs[x1 - x0p];
Print"the final approximation of root is:" , x1;
Print  "Estimated error is :", Abs[x1 - x0]
5

f[x]=-ⅇx x + Cos[x]
f[x]=-ⅇx - ⅇx x - Sin[x]
In1th Number of iteration the approximation to root is:0.954848
Estimated error is :0.545152
In2th Number of iteration the approximation to root is:0.632019
Estimated error is :0.322829
In3th Number of iteration the approximation to root is:0.527616
Estimated error is :0.104403
In4th Number of iteration the approximation to root is:0.517838
Estimated error is :0.00977784
In5th Number of iteration the approximation to root is:0.517757
Estimated error is :0.0000806043
Out[60]=
Return[0.517757]

the final approximation of root is:0.517757


Estimated error is :5.44033 × 10-9
Practical 3(Secant Method)
Question 1
In[63]:= f[x_] = Cos[x];
Plot f[x], x , - 1, 3
Out[64]=

1.0

0.5

-1 1 2 3

-0.5

-1.0

In[65]:= x0 = 1;
x1 = 2;
Nmax = 20;
eps = 10 ^ (- 6);
Print["f[x]=", f[x]];
Fori = 1, i ≤ Nmax, i ++,
x2 = Nx1 - f[x] /. x → x1  * (x1 - x0)  f[x] /. x → x1 - f[x] /. x → x0;
IfAbsx1 - x2 < eps , Return[x2] , x0 = x1; x1 = x2;
Print"In", i, "th Number of iterations the root is :", x2;
Print"estimated error is :", Abs[x1 - x0];
Print"root is :" , x2;
Print"estimated error is:", Abs[x2 - x1];
2

f[x]=Cos[x]
In1th Number of iterations the root is :1.5649
estimated error is :0.435096
In2th Number of iterations the root is :1.57098
estimated error is :0.0060742
In3th Number of iterations the root is :1.5708
estimated error is :0.000182249
Out[70]=
Return[1.5708]

root is :1.5708
estimated error is:1.02185 × 10-9

Question 2
In[73]:= f[x_] = x^3 - 5*x + 1;
Plot f[x], x , - 1, 3
Out[74]=

10

-1 1 2 3

In[75]:= x0 = 0;
x1 = 1;
Nmax = 20;
eps = 10 ^ (- 6);
Print["f[x]=", f[x]];
Fori = 1, i ≤ Nmax, i ++,
x2 = Nx1 - f[x] /. x → x1  * (x1 - x0)  f[x] /. x → x1 - f[x] /. x → x0;
IfAbsx1 - x2 < eps , Return[x2] , x0 = x1; x1 = x2;
Print"In", i, "th Number of iterations the root is :", x2;
Print"estimated error is :", Abs[x1 - x0];
Print"root is :" , x2;
Print"estimated error is:", Abs[x2 - x1];
3

f[x]=1 - 5 x + x3
In1th Number of iterations the root is :0.25
estimated error is :0.75
In2th Number of iterations the root is :0.186441
estimated error is :0.0635593
In3th Number of iterations the root is :0.201736
estimated error is :0.0152956
In4th Number of iterations the root is :0.20164
estimated error is :0.0000964033
Out[80]=
Return[0.20164]

root is :0.20164
estimated error is:1.7717 × 10-7

Question 3
In[83]:= f[x_] = Cos[x] - x * Exp[x];
Plot f[x], x , - 1, 3
Out[84]=

-1 1 2 3

-10

-20

-30

-40

-50

-60
4

In[85]:= x0 = 0;
x1 = 1;
Nmax = 20;
eps = 10 ^ (- 6);
Print["f[x]=", f[x]];
Fori = 1, i ≤ Nmax, i ++,
x2 = Nx1 - f[x] /. x → x1  * (x1 - x0)  f[x] /. x → x1 - f[x] /. x → x0;
IfAbsx1 - x2 < eps , Return[x2] , x0 = x1; x1 = x2;
Print"In", i, "th Number of iterations the root is :", x2;
Print"estimated error is :", Abs[x1 - x0];
Print"root is :" , x2;
Print"estimated error is:", Abs[x2 - x1];

f[x]=-ⅇx x + Cos[x]
In1th Number of iterations the root is :0.314665
estimated error is :0.685335
In2th Number of iterations the root is :0.446728
estimated error is :0.132063
In3th Number of iterations the root is :0.531706
estimated error is :0.0849777
In4th Number of iterations the root is :0.516904
estimated error is :0.0148014
In5th Number of iterations the root is :0.517747
estimated error is :0.000842998
In6th Number of iterations the root is :0.517757
estimated error is :9.90548 × 10-6
Out[90]=
Return[0.517757]

root is :0.517757
estimated error is:7.07182 × 10-9
Practical 5(Gauss-Jacobi Method)
Question 1
GaussJacobi[A0_ , b0_ , X0_ , maxiter_] :=
ModuleA = N[A0], b = N[b0], xk = X0, xk1, i, j, k = 0, n, m, OutputDetails,
size = Dimensions[A];
n = size〚1〛;
m = size〚2〛;
Ifn ≠ m,
Print "Not a sqaure matrix , cannot proceed with gauss jacobi method ";
Return[];
OutputDeatils = {xk};

xk1 = Table[0, {n}];


Whilek < maxiter,
Fori = 1, i ≤ n, i ++, xk1i = 1  Ai, i *
bi - SumAi, j * xkj, j, 1, i - 1 - SumAi, j * xkj, j, i + 1, n;;
k ++;
OutputDeatils = Append[OutputDeatils, xk1];
xk = xk1;;
colHeading = Table[X[s], {s, 1, n}];
Print
NumberFormTableFormOutputDeatils, TableHeadings → None, colHeading, 6;
Print"no. of iterations performed=", maxiter;
;

A = {{5, 1, 2}, {- 3, 9, 4}, {1, 2, - 7}};


b = {10, - 14, - 33};
X0 = {0, 0, 0};
GaussJacobi[A, b, X0, 15]
2

X[1] X[2] X[3]


0 0 0
2. -1.55556 4.71429
0.425397 -2.98413 4.55556
0.774603 -3.43845 3.92245
1.11871 -3.04067 3.84253
1.07112 -2.89044 4.00534
0.975953 -2.97867 4.04146
0.979148 -3.02644 4.00266
1.00422 -3.00813 3.98947
1.00584 -2.99391 3.99828
0.99947 -2.99729 4.00257
0.998428 -3.00132 4.0007
0.999985 -3.00083 3.9994
1.00041 -2.99974 3.99976
1.00004 -2.99976 4.00013
0.999898 -3.00004 4.00008
no. of iterations performed=15
3

Question 2
In[6]:= GaussJacobi[A0_ , b0_ , X0_ , maxiter_] :=
ModuleA = N[A0], b = N[b0], xk = X0, xk1, i, j, k = 0, n, m, OutputDetails,
size = Dimensions[A];
n = size〚1〛;
m = size〚2〛;
Ifn ≠ m,
Print "Not a sqaure matrix , cannot proceed with gauss jacobi method ";
Return[];
OutputDeatils = {xk};

xk1 = Table[0, {n}];


Whilek < maxiter,
Fori = 1, i ≤ n, i ++, xk1i = 1  Ai, i *
bi - SumAi, j * xkj, j, 1, i - 1 - SumAi, j * xkj, j, i + 1, n;;
k ++;
OutputDeatils = Append[OutputDeatils, xk1];
xk = xk1;;
colHeading = Table[X[s], {s, 1, n}];
Print
NumberFormTableFormOutputDeatils, TableHeadings → None, colHeading, 6;
Print"no. of iterations performed=", maxiter;
;

A = {{4, 1, 1}, {1, 5, 2}, {1, 2, 3}};


b = {2, - 6, - 4};
X0 = {0.5, - 0.5, - 0.5};
GaussJacobi[A, b, X0, 15]
4

X[1] X[2] X[3]


0.5 -0.5 -0.5
0.75 -1.1 -1.16667
1.06667 -0.883333 -0.85
0.933333 -1.07333 -1.1
1.04333 -0.946667 -0.928889
0.968889 -1.03711 -1.05
1.02178 -0.973778 -0.964889
0.984667 -1.0184 -1.02474
1.01079 -0.987037 -0.982622
0.992415 -1.00911 -1.01224
1.00534 -0.993588 -0.9914
0.996247 -1.00451 -1.00605
1.00264 -0.996828 -0.995744
0.998143 -1.00223 -1.00299
1.00131 -0.998431 -0.997894
0.999081 -1.0011 -1.00148

no. of iterations performed=15


5

Question 3
In[11]:= GaussJacobi[A0_ , b0_ , X0_ , maxiter_] :=
ModuleA = N[A0], b = N[b0], xk = X0, xk1, i, j, k = 0, n, m, OutputDetails,
size = Dimensions[A];
n = size〚1〛;
m = size〚2〛;
Ifn ≠ m,
Print "Not a sqaure matrix , cannot proceed with gauss jacobi method ";
Return[];
OutputDeatils = {xk};

xk1 = Table[0, {n}];


Whilek < maxiter,
Fori = 1, i ≤ n, i ++, xk1i = 1  Ai, i *
bi - SumAi, j * xkj, j, 1, i - 1 - SumAi, j * xkj, j, i + 1, n;;
k ++;
OutputDeatils = Append[OutputDeatils, xk1];
xk = xk1;;
colHeading = Table[X[s], {s, 1, n}];
Print
NumberFormTableFormOutputDeatils, TableHeadings → None, colHeading, 6;
Print"no. of iterations performed=", maxiter;
;

A = {{- 3, 1, 0}, {2, - 3, 1}, {0, 2, - 3}};


b = {- 2, 0, - 1};
X0 = {0, 0, 0};
GaussJacobi[A, b, X0, 15]
6

X[1] X[2] X[3]


0 0 0
0.666667 0. 0.333333
0.666667 0.555556 0.333333
0.851852 0.555556 0.703704
0.851852 0.802469 0.703704
0.934156 0.802469 0.868313
0.934156 0.912209 0.868313
0.970736 0.912209 0.941472
0.970736 0.960982 0.941472
0.986994 0.960982 0.973988
0.986994 0.982658 0.973988
0.994219 0.982658 0.988439
0.994219 0.992293 0.988439
0.997431 0.992293 0.994862
0.997431 0.996575 0.994862
0.998858 0.996575 0.997716
no. of iterations performed=15
Practical 6(Gauss-Seidel Method)
Question1
In[26]:= GaussSeidel[A0_ , b0_ , X0_ , maxiter_] :=
ModuleA = N[A0], b = N[b0], xk = X0, xk1, i, j, k = 0, n, m, OutputDetails,
size = Dimensions[A];
n = size〚1〛;
m = size〚2〛;
Ifn ≠ m,
Print "Not a sqaure matrix , cannot proceed with gauss seidel method ";
Return[];
OutputDeatils = {xk};

xk1 = Table[0, {n}];


Whilek < maxiter,
Fori = 1, i ≤ n, i ++,
xk1i = 1  Ai, i *
bi - SumAi, j * xk1j, j, 1, i - 1 - SumAi, j * xkj, j, i + 1, n;;
k ++;
OutputDeatils = Append[OutputDeatils, xk1];
xk = xk1;;
colHeading = Table[X[s], {s, 1, n}];
Print
NumberFormTableFormOutputDeatils, TableHeadings → None, colHeading, 6;
Print"no. of iterations performed=", maxiter;
;

A = {{2, - 1, 0}, {- 1, 2, - 1}, {0, - 1, 2}};


b = {7, 1, 1};
X0 = {0, 0, 0};
GaussSeidel[A, b, X0, 15]
2

X[1] X[2] X[3]


0 0 0
3.5 2.25 1.625
4.625 3.625 2.3125
5.3125 4.3125 2.65625
5.65625 4.65625 2.82813
5.82813 4.82813 2.91406
5.91406 4.91406 2.95703
5.95703 4.95703 2.97852
5.97852 4.97852 2.98926
5.98926 4.98926 2.99463
5.99463 4.99463 2.99731
5.99731 4.99731 2.99866
5.99866 4.99866 2.99933
5.99933 4.99933 2.99966
5.99966 4.99966 2.99983
5.99983 4.99983 2.99992
no. of iterations performed=15
3

Question 2
In[36]:= GaussSeidel[A0_ , b0_ , X0_ , maxiter_] :=
ModuleA = N[A0], b = N[b0], xk = X0, xk1, i, j, k = 0, n, m, OutputDetails,
size = Dimensions[A];
n = size〚1〛;
m = size〚2〛;
Ifn ≠ m,
Print "Not a sqaure matrix , cannot proceed with gauss seidel method ";
Return[];
OutputDeatils = {xk};

xk1 = Table[0, {n}];


Whilek < maxiter,
Fori = 1, i ≤ n, i ++,
xk1i = 1  Ai, i *
bi - SumAi, j * xk11 j, j, 1, i - 1 - SumAi, j * xkj, j, i + 1, n;;
k ++;
OutputDeatils = Append[OutputDeatils, xk1];
xk = xk1;;
colHeading = Table[X[s], {s, 1, n}];
Print
NumberFormTableFormOutputDeatils, TableHeadings → None, colHeading, 6;
Print"no. of iterations performed=", maxiter;
;

A = {{4, 1, 1}, {1, 5, 2}, {1, 2, 3}};


b = {2, - 6, - 4};
X0 = {0.5, - 0.5, - 0.5};
GaussSeidel[A, b, X0, 15]
4

X[1] X[2] X[3]


0.5 -0.5 -0.5
0.75 -1.15 -0.816667
0.991667 -1.07167 -0.949444
1.00528 -1.02128 -0.987574
1.00221 -1.00541 -0.997129
1.00064 -1.00128 -0.999362
1.00016 -1.00029 -0.999862
1.00004 -1.00006 -0.999971
1.00001 -1.00001 -0.999994
1. -1. -0.999999
1. -1. -1.
1. -1. -1.
1. -1. -1.
1. -1. -1.
1. -1. -1.
1. -1. -1.

no. of iterations performed=15


5

Question 3
In[41]:= GaussSeidel[A0_ , b0_ , X0_ , maxiter_] :=
ModuleA = N[A0], b = N[b0], xk = X0, xk1, i, j, k = 0, n, m, OutputDetails,
size = Dimensions[A];
n = size〚1〛;
m = size〚2〛;
Ifn ≠ m,
Print "Not a sqaure matrix , cannot proceed with gauss seidel method ";
Return[];
OutputDeatils = {xk};

xk1 = Table[0, {n}];


Whilek < maxiter,
Fori = 1, i ≤ n, i ++,
xk1i = 1  Ai, i *
bi - SumAi, j * xk1j, j, 1, i - 1 - SumAi, j * xkj, j, i + 1, n;;
k ++;
OutputDeatils = Append[OutputDeatils, xk1];
xk = xk1;;
colHeading = Table[X[s], {s, 1, n}];
Print
NumberFormTableFormOutputDeatils, TableHeadings → None, colHeading, 6;
Print"no. of iterations performed=", maxiter;
;

A = {{5, 1, 2}, {- 3, 9, 4}, {1, 2, - 7}};


b = {10, - 14, - 33};
X0 = {0, 0, 0};
GaussSeidel[A, b, X0, 15]
6

X[1] X[2] X[3]


0 0 0
2. -0.888889 4.74603
0.279365 -3.57178 3.73369
1.22088 -2.80801 4.08641
0.927039 -3.06272 3.97166
1.02388 -2.97944 4.00929
0.992174 -3.00674 3.99696
1.00256 -2.99779 4.001
0.99916 -3.00072 3.99967
1.00028 -2.99976 4.00011
0.99991 -3.00008 3.99996
1.00003 -2.99997 4.00001
0.99999 -3.00001 4.
1. -3. 4.
0.999999 -3. 4.
1. -3. 4.
no. of iterations performed=15
Practical 7(Lagrange Interpolation)
P-1:
In[48]:= LagrangePolynomial[x0_ , f0_] :=
Modulexi = x0, fi = f0, n, m, polynomial,
n = Length[xi];
m = Length[fi];
Ifn ≠ m, Print"List of points and functions values are not of same size";
Return[];;
Fori = 1, i ≤ n, i ++, L[i, x_] =  Productx - xij  xii - xij, j, 1, i - 1 *
Productx - xij  xii - xij, j, i + 1, n;;
polynomial[x_] = Sum[L[k, x] * fi〚k〛, {k, 1, n}];
Return[polynomial[x]];

Question 1
In[49]:= nodes = {0, 1, 3};
values = {1, 3, 55};
lagrangePolynomial[x_] = LagrangePolynomial[nodes, values]

Out[51]=
1 3 55
(1 - x) (3 - x) + (3 - x) x + (- 1 + x) x
3 2 6

In[52]:= Expand[%]
Out[52]=
1 - 6 x + 8 x2

Question 2:
In[53]:= nodes = {0, 1, 3};
values = {1, 3};
lagrangePolynomial[x_] = LagrangePolynomial[nodes, values]

List of points and functions values are not of same size


2

Question3:
In[56]:= nodes = {1, 3, 5, 7, 9};
values = {N[Log[1]], N[Log[3]], N[Log[5]], N[Log[7]], N[Log[9]]};
lagrangePolynomial[x_] = LagrangePolynomial[nodes, values]

Out[58]=
0. + 0.0114439 (5 - x) (7 - x) (9 - x) (- 1 + x) + 0.0251475 (7 - x) (9 - x) (- 3 + x) (- 1 + x) +
0.0202699 (9 - x) (- 5 + x) (- 3 + x) (- 1 + x) + 0.00572194 (- 7 + x) (- 5 + x) (- 3 + x) (- 1 + x)

In[59]:= Simplify[%]
Out[59]=
- 0.987583 + 1.18991 x - 0.223608 x2 + 0.0221231 x3 - 0.000844369 x4

In[60]:= PlotlagrangePolynomial[x], Log[x], {x, 1, 10},


Ticks → {Range[0, 10]}, PlotLegends → "Expressions"
Out[60]=

lagrangePolynomial(x)
1 log(x)

2 3 4 5 6 7 8 9 10

Question 4:
In[61]:= nodes = {- 1, 0, 1, 2};
values = {5, 1, 1, 11};
lagrangePolynomial[x_] = LagrangePolynomial[nodes, values]

Out[63]=
5 1 1 11
- (1 - x) (2 - x) x + (1 - x) (2 - x) (1 + x) + (2 - x) x (1 + x) + (- 1 + x) x (1 + x)
6 2 2 6

In[64]:= Simplify[%]
Out[64]=
1 - 3 x + 2 x2 + x3
3

In[65]:= lagrangePolynomial[1.5]
Out[65]=
4.375
In[1]:= 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[3]= 4

Out[4]= {3, 5, 6, 9}

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

215 61 35
Out[7]= 293 + (- 3 + x) - (- 5 + x) (- 3 + x) + (- 6 + x) (- 5 + x) (- 3 + x)
2 6 36
1 2 3
Out[8]= - 9702 + 9003 x - 856 x + 35 x 
36

222.288

In[11]:= ClearAll

Out[11]= ClearAll
2 newton polynomial.nb

In[12]:= sum = 0;
points = {{1, 1}, {2, 5}, {3, 2}};
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[9.2 ]]

Out[14]= 3

Out[15]= {1, 2, 3}

Out[16]= {1, 5, 2}

7
Out[18]= 1 + 4 (- 1 + x) - (- 2 + x) (- 1 + x)
2
1 2
Out[19]= - 20 + 29 x - 7 x 
2

Out[20]= - 172.84

ClearAll

In[21]:= sum = 0;
points = {{0, 1}, {1, 7}, {2, 23 }, {3, 55 }, {4, 109 }};
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[10 ]]

Out[23]= 5

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

Out[25]= {1, 7, 23, 55, 109 }

Out[27]= 1 + 6 x + 5 (- 1 + x) x + (- 2 + x) (- 1 + x) x

Out[28]= 1 + 3 x + 2 x2 + x3

Out[29]= 1231
newton polynomial.nb 3

In[32]:= ClearAll
sum = 0;
points = {{4, 48 }, {5, 100 }, {7, 294 }, {10, 900 }};
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[0]]

Out[32]= ClearAll

Out[35]= 4

Out[36]= {4, 5, 7, 10 }

Out[37]= {48, 100, 294, 900 }

Out[39]= 48 + 52 (- 4 + x) + 15 (- 5 + x) (- 4 + x) + (- 7 + x) (- 5 + x) (- 4 + x)
2
Out[40]= (- 1 + x) x

Out[41]= 0
Practical 9(Trapezoidal Method)
Question 1:
In[2]:= a = Input"Enter the left end point:";
b = Input"Enter the right end point:";
n = Input"Enter the number of sub-intervals to be formed:";
h = (b - a)  n;
y = Tablea + i * h, i, 1, n;
f[x] := Log[x];
sumodd = 0;
sumeven = 0;
Fori = 1, i < n, i += 2, sumodd += 2 * f[x] /. x → yi;
Fori = 2, i < n, i += 2, sumeven += 2 * f[x] /. x → yi;
Tn = h  2 * f[x] /. x → a + N[sumodd] + N[sumeven] + f[x] /. x → b;
Print"For n=", n, ",Trapezoidal estimate is: ", Tn
in = Integrate[Log[x], {x, 4, 5.2}];
Print"True value is ", in
Print"Absolute error is ", Abs[Tn - in]

For n=6,Trapezoidal estimate is: 1.82766


True value is 1.82785
Absolute error is 0.000192256
2

Question 2:
In[17]:= a = Input"Enter the left end point:";
b = Input"Enter the right end point:";
n = Input"Enter the number of sub-intervals to be formed:";
h = (b - a)  n;
y = Tablea + i * h, i, 1, n;
f[x] := Sin[x];
sumodd = 0;
sumeven = 0;
Fori = 1, i < n, i += 2, sumodd += 2 * f[x] /. x → yi;
Fori = 2, i < n, i += 2, sumeven += 2 * f[x] /. x → yi;
Tn = h  2 * f[x] /. x → a + N[sumodd] + N[sumeven] + f[x] /. x → b;
Print"For n=", n, ",Trapezoidal estimate is: ", Tn
in = IntegrateSin[x], x, 0, Pi  2;
Print"True value is ", in
Print"Absolute error is ", Abs[Tn - in]

For n=12,Trapezoidal estimate is: 0.998572


True value is 1
Absolute error is 0.0014283
3

Question 3:
In[32]:= a = Input"Enter the left end point:";
b = Input"Enter the right end point:";
n = Input"Enter the number of sub-intervals to be formed:";
h = (b - a)  n;
y = Tablea + i * h, i, 1, n;
f[x] := Sin[x] - Log[x] + Exp[x];
sumodd = 0;
sumeven = 0;
Fori = 1, i < n, i += 2, sumodd += 2 * f[x] /. x → yi;
Fori = 2, i < n, i += 2, sumeven += 2 * f[x] /. x → yi;
Tn = h  2 * f[x] /. x → a + N[sumodd] + N[sumeven] + f[x] /. x → b;
Print"For n=", n, ",Trapezoidal estimate is: ", Tn
in = Integrate[Sin[x] - Log[x] + Exp[x], {x, 0.2, 1.4}];
Print"True value is ", in
Print"Absolute error is ", Abs[Tn - in]

For n=12,Trapezoidal estimate is: 4.05617


True value is 4.05095
Absolute error is 0.00522485
4

Question 4:
In[47]:= a = Input"Enter the left end point:";
b = Input"Enter the right end point:";
n = Input"Enter the number of sub-intervals to be formed:";
h = (b - a)  n;
y = Tablea + i * h, i, 1, n;
f[x] := 1 / (1 + x ^ 2);
sumodd = 0;
sumeven = 0;
Fori = 1, i < n, i += 2, sumodd += 2 * f[x] /. x → yi;
Fori = 2, i < n, i += 2, sumeven += 2 * f[x] /. x → yi;
Tn = h  2 * f[x] /. x → a + N[sumodd] + N[sumeven] + f[x] /. x → b;
Print"For n=", n, ",Trapezoidal estimate is: ", Tn
in = Integrate[1 / (1 + x ^ 2), {x, 0, 1}];
Print"True value is ", in
Print"Absolute error is ", Abs[Tn - in]

For n=6,Trapezoidal estimate is: 0.784241


π
True value is
4
Absolute error is 0.0011574
4

In[4]:= f[x_] := x ^ 3 + 4 x ^ 2 - 10;


RegulaFalsi[a0_ , b0_ , m _] := Module{}, a = N[a0];
b = N[b0];
c = a * f[b] - b * f[a]  f[b] - f[a]; k = 0;
Whilek < m, If[Sign[f[b]] ⩵ Sign[f[c]], b = c, a = c;];
c = a * f[b] - b * f[a]  f[b] - f[a]; k = k + 1;;
Print" c =", NumberForm[c, 16]; Print" f[c] = ", NumberForm[f[c], 16];
RegulaFalsi[0, 1, 10]
c =1.365229589673846
-6
f[c] = -6.997390405771853 × 10

In[7]:= ClearAll
Out[7]= ClearAll

In[8]:= f[x_] := x ^ 3 + 8 x ^ 2 + 4 x - 10;


RegulaFalsi[a0_ , b0_ , m _,] := Module{}, a = N[a0]; b = N[b0];
c = a f[b] - b f[a]  f[b] - f[a]; k = 0;
Whilek < m, If[Sign[f[b]] ⩵ Sign[f[c]], b = c, a = c;];
c = a f[b] - b f[a]  f[b] - f[a]; k = k + 1;;
Print" c = ", NumberForm[c, 16]; Print" f[c] = ", NumberForm[f[c], 16];
RegulaFalsi[0, 1, 10]

c =0.860357511345688
-12
f[c] = -5.123901303250022 × 10

In[11]:= ClearAll
Out[11]=
ClearAll

In[12]:= f[x_] := x ^ 3 + 12 x ^ 2 - 4 x + 10;


RegulaFalsi[a0_ , b0_ , m _] := Module{}, a = N[a0]; b = N[b0];
c = a f[b] - b f[a]  f[b] - f[a]; k = 0;
Whilek < m, If[Sign[f[b]] ⩵ Sign[f[c]], b = c, a = c;];
c = a f[b] - b f[a]  f[b] - f[a]; k = k + 1;;
Print" c = ", NumberForm[c, 16]; Print" f[c] = ", NumberForm[f[c], 18];
RegulaFalsi[0, 1, 8]
c = 0.2726817760390718
f[c] = 9.82181245681386

In[15]:= ClearAll
Out[15]=
ClearAll

You might also like