0% found this document useful (0 votes)
5 views

Practical

This document contains examples of using different root finding methods like bisection, Newton-Raphson, secant and regula falsi methods to find the roots of various functions. It shows the code implementation and output for each method on sample functions to find their roots iteratively.

Uploaded by

saloni99577
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)
5 views

Practical

This document contains examples of using different root finding methods like bisection, Newton-Raphson, secant and regula falsi methods to find the roots of various functions. It shows the code implementation and output for each method on sample functions to find their roots iteratively.

Uploaded by

saloni99577
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/ 31

practical.

wxmx 1 / 31

Practical-1 : Bisection
Method
kill(all)$
f(x):=cos(x);
'a=a:0$
'b=b:2.0$ n:10;
if(float(f(a)·f(b)>0)) then
print(" change values")
else
for i:1 thru n do
(c:(a+b)/2, if(f(c)·f(b))>0 then b:c
else a:c,print(i,"iteration gives ",c))$
print( "after iteration",n,"root is ",c)$
wxplot2d(f(x),[x,0,2.0],[grid,1,1],[ylabel,"f(x)"]);

f ( x ) := cos ( x ) 10
1 iteration gives 1.0
2 iteration gives 1.5
3 iteration gives 1.75
4 iteration gives 1.625
5 iteration gives 1.5625
6 iteration gives 1.59375
7 iteration gives 1.578125
8 iteration gives 1.5703125
9 iteration gives 1.57421875
10 iteration gives 1.572265625
after iteration 10 root is 1.572265625
practical.wxmx 2 / 31

kill(all)$
f(x):=cos(x);
'a=a:0$
'b=b:2.0$ n:10;
if(float(f(a)·f(b)>0)) then
print(" change values")
else
for i:1 thru n do
(c:(a+b)/2, if(f(c)·f(b))>0 then b:c
else a:c,print(i,"iteration gives ",c))$
print( "after iteration",n,"root is ",c)$
wxplot2d(sin(x)−x^2+5,[x,−1,3],[y,−2,2]);
f ( x ) := cos ( x ) 10
1 iteration gives 1.0
2 iteration gives 1.5
3 iteration gives 1.75
4 iteration gives 1.625
5 iteration gives 1.5625
6 iteration gives 1.59375
7 iteration gives 1.578125
practical.wxmx 3 / 31

8 iteration gives
1.5703125
9 iteration gives
1.57421875
10 iteration gives
1.572265625
after iteration
10 root is 1.572265625
plot2d: some values will be clipped.

kill(all)$
'a=a:2$
'b=b:3$
'err=err:.000001;
'n=n:100;
f(x):=sin(x)−x^2+5;
if(float(f(a)·f(b)>0))
then print("Change Values")
else (for i:1 thru n do(c:(a+b)/2,print(c),
if(abs(b−a)<err)then (print("root cgs in",i−1,"iteration"),return())
else if(f(c)·f(a)<0)then b:c else a:c))$

−6 2
err = 1.0 10 n = 100 f ( x ) := sin ( x ) − x + 5
practical.wxmx 4 / 31

5
2
9
4
19
8
39
16
77
32
153
64
305
128
611
256
1221
512
2441
1024
4883
2048
9767
4096
19535
8192
39071
16384
78141
32768
156283
65536
312565
131072
625129
262144
practical.wxmx 5 / 31

1250257
524288
2500515
1048576
5001029
2097152
root cgs in 20 iteration
Practical-2

Practical-2 NEWTON
RAPHSON METHOD
kill(all)$f(x):=x^3−2·x+3;
define(df(x),diff(f(x),x));
'x0=x0:0.5;
for n:1 thru 5 do
(x1:float(x0−f(x0)/df(x0)),
x0:x1,print(n,"iteration gives",x0))$
3 2
f ( x ) := x − 2 x + 3 df ( x ) := 3 x − 2 x0 = 0.5
1 iteration gives 2.2
2 iteration gives 1.4613418530351439
3 iteration gives 0.7355963645500034
4 iteration gives 5.850729922589881
5 iteration gives 3.9481658719984534
kill(all)$f(x):=x^5−90·x^2−x−1;
define(df(x),diff(f(x),x));
'x0=x0:4.0;'n=n:50;
'err=err:0.0000001;
print("itr","","root")$
for i:1 thru n do (
x1:float(x0−f(x0)/df(x0)),print(i,"",x1),
if(abs(x1−x0)<err)then
(print("root converges in",i,"iteration"),
return(""))
else
x0:x1)$
practical.wxmx 6 / 31

5 2 4
f(x ):=x − 90 x − x − 1 df ( x ) := 5 x − 180 x − 1 x0
−7
= 4.0 n = 50 err = 1.0 10
itr root
1 4.753130590339893
2 4.524884055347666
3 4.486906959082079
4 4.485925596920266
5 4.4859249530951875
6 4.48592495309491
root converges in 6 iteration
wxplot2d(f(x),[x,−5,5]);

Practical-3- Secant
method
practical.wxmx 7 / 31

kill(all)$f(x):=cos(x);
'x0=x0:0;
'x1=x1:2;
for n:1 thru 65 do
(x2:float(x1−f(x1)·(x1−x0)/(f(x1)−f(x0))),x1:x2,
print(x1))$

f(x ):=cos(x ) x0 = 0 x1 = 2

1.4122829274373918
1.6769977190363248
1.5162702403284438
1.6036686959769508
1.5526389662234332
1.5813505404639085
1.5648352415818874
1.5742192410454752
1.5688492151959628
1.5719098972554217
1.5701614122498488
1.571158963861565
1.570589409937488
1.5709144586182036
1.5707289055491016
1.5708348131890648
1.5707743597479211
1.5708088657800625
1.5707891696779657
1.57080041208019
1.5707939949385836
1.5707976578130185
1.570795567055653
1.5707967604515956
1.570796079265353
1.570796468083886
1.5707962461476719
1.57079637282804
1.5707963005193486
1.5707963417928834
practical.wxmx 8 / 31

f(x):=cos(x);
'x0=x0:0;
'x1=x1:2;
for n:1 thru 10 do
(x2:float(x1−f(x1)·(x1−x0)/(f(x1)−f(x0))),x1:x2,
print(x1))$

f(x ):=cos(x ) x0 = 0 x1 = 2

1.4122829274373918
1.6769977190363248
1.5162702403284438
1.6036686959769508
1.5526389662234332
1.5813505404639085
1.5648352415818874
1.5742192410454752
1.5688492151959628
1.5719098972554217

kill(all)$f(x):=x^3+2·x^2−3·x−1;
wxplot2d(f(x),[x,0,2.0],[grid,1,1],[ylabel,"f(x)"]);
'x0=x0:1.0;'x1=x1:2.0;
'n=n:8;
print("itr","","","","root")$
'j=j:0$
for i:1 thru 8 do
(x2:(x1−((x1−x0)/(f(x1)−f(x0)))·f(x1)),j:j+1,
print(i,"","","",x2),
if(abs(x2−x1)<.0000001)then
(print("root converges in",i,"iteration"),return())
else(x0:x1,x1:x2))$
print("after iteration",j,"root is",x2)$

3 2
f ( x ) := x + 2 x + − 3 x − 1
practical.wxmx 9 / 31

x0 = 1.0 x1 = 2.0 n=8


itr root
1 1.1
2 1.1517436380772856
3 1.203449860925807
4 1.1984799140986921
5 1.1986903248478271
6 1.1986912436939725
7 1.1986912435159969
root converges in 7 iteration
after iteration 7 root is 1.1986912435159969

Practical-4

wxplot2d(cos(x),[x,−2,2]);
practical.wxmx 10 / 31

/* regula falsi */
kill(all)$
'x0=x0:0;
'x1=x1:2.0;
f(x):=cos(x);'j=j:0;
if(float(f(x0)·f(x1)<0)) then
(
for i:1 thru 10 do (
a:float(x1−((x1−x0)/(f(x1)−f(x0)))·f(x1)),j:j+1, if(f(a)·f(x1))>0 then x1
else
x0:a,
print(i," iteration gives ",a)),
print( "after iteration", j,"root is " , a))
else
print("enter values")$
x0 = 0 x1 = 2.0 f ( x ) := cos ( x ) j=0
1 iteration gives 1.4122829274373918
2 iteration gives 1.5739063237228792
3 iteration gives 1.5739063237228792
practical.wxmx 11 / 31

4 iteration gives
1.5739063237228792
5 iteration gives
1.5739063237228792
6 iteration gives
1.5739063237228792
7 iteration gives
1.5739063237228792
8 iteration gives
1.5739063237228792
9 iteration gives
1.5739063237228792
10 iteration gives
1.5739063237228792
after iteration
10 root is 1.5739063237228792

wxplot2d(f(x),[x,0,2.0],[grid,1,1],[ylabel,"f(x)"]);
practical.wxmx 12 / 31

/* regulafalsi Method with error */


kill(all)$
'x0=x0:0;
'x1=x1:2.0;
f(x):=cos(x);
'j=j:0;
'n=n:100;
'err=err:0.00000001;
if(float(f(x0)·f(x1)>0)) then
print("change values")
else
(b:x1−((x0−x1)/(f(x0)−f(x1)))·f(x1),
j:j+1,
print(j,"iteration gives ",b),
for i:1 thru n−1 do (
if ((f(x0)·f(b))<0) then
x1:b
else
x0:b,
a:x1−((x0−x1)/(f(x0)−f(x1)))·f(x1),j:j+1,print(j," iteration gives ",a),
if(abs(b−a)<err) then
(print("root converges in",j," iteration and root is",b),return())
else
b:a),
if(j>n−1)then
print( "Root does not converges and after iteration", j,"root is ",b))$
x0 = 0 x1 = 2.0 f(x ):=cos (x ) j= 0 n

−8
= 100 err= 1.0 10

1 iteration gives 1.4122829274373918


2 iteration gives 1.5739063237228792
3 iteration gives 1.570783521943903
4 iteration gives 1.5707963268154532
5 iteration gives 1.5707963267948966
root converges in 5 iteration and root is 1.5707963268154532
practical.wxmx 13 / 31

kill(all)$f(x):= sin(x);
wxplot2d(f(x),[x,0,4.0],[ylabel,"f(x)"]);
'x0=x0:3.0;
'x1=x1:3.5;
'n=n:8;
print("itr","","root")$
'j=j:0$
for i:1 thru n do
(x2:(x1−((x1−x0)/(f(x1)−f(x0)))·f(x1)),j:j+1,
print(i,"",x2),
if(abs(x2−x1)<.00001)then
(print("root converges in",i,"iteration"),return())
else
(x0:x1,
x1:x2))$
print("after iteration",j,"root is",x2)$
f(x ):=sin(x )

x0 = 3.0 x1 = 3.5 n= 8
itr root
1 3.1434428540044568
2 3.141552228155302
3 3.1415926536123537
practical.wxmx 14 / 31

Practical-6 LU-Decomposition
kill(all);
A:matrix([1,2,3],[3,5,6],[7,8,9]);
n:3;
for i:1 thru n do(
for j:1 thru n do(
if(i=j) then l[i,j]:1,if (j>i) then l[i,j]:0));
for i:1 thru n do(
for j:1 thru n do(
if (i>j) then u[i,j]:0));
L:genmatrix(l,n,n);
U:genmatrix(u,n,n);
S:L.U−A;
1 2 3

done 3 5 6 3 done done


7 8 9

1 0 0 u u u
1,1 1,2 1,3

l 1 0 0 u u
2,1 2,2 2,3

l l 1 0 0 u
3,1 3,2 3,3

u −1 u −2 u −3
1,1 1,2 1,3

u l −3 u +u l −5 u +u l −6
1,1 2,1 2,2 1,2 2,1 2,3 1,3 2,1

u l −7 u l +u l −8 u +u l +u l −9
1,1 3,1 2,2 3,2 1,2 3,1 3,3 2,3 3,2 1,3 3,1

u[1,1]:1;
u[1,2]:2;
u[1,3]:3;
L:genmatrix(l,n,n);
U:genmatrix(u,n,n);
S:L.U−A;
1 0 0 1 2 3

l 1 0 0 u u
1 2 3 2,1 2,2 2,3

l l 1 0 0 u
3,1 3,2 3,3
practical.wxmx 15 / 31

0 0 0

l −3 u +2 l −5 u +3 l −6
2,1 2,2 2,1 2,3 2,1

l −7 u l +2 l −8 u +u l +3 l −9
3,1 2,2 3,2 3,1 3,3 2,3 3,2 3,1

l[2,1]:3;
l[3,1]:7;
L:genmatrix(l,n,n);
U:genmatrix(u,n,n);
S:L.U−A;

1 0 0 1 2 3

3 1 0 0 u u
3 7 2,2 2,3

7 l 1 0 0 u
3,2 3,3

0 0 0

0 u +1 u +3
2,2 2,3

0 u l +6 u +u l + 12
2,2 3,2 3,3 2,3 3,2

u[2,2]:−1;
u[2,3]:−3;
L:genmatrix(l,n,n);
U:genmatrix(u,n,n);
S:L.U−A;

1 0 0 1 2 3

−1 −3 3 1 0 0 −1 −3

7 l 1 0 0 u
3,2 3,3

0 0 0

0 0 0

0 6−l u −3 l + 12
3,2 3,3 3,2

l[3,2]:6;
L:genmatrix(l,n,n);
U:genmatrix(u,n,n);
S:L.U−A;
practical.wxmx 16 / 31

1 0 0 1 2 3 0 0 0

6 3 1 0 0 −1 −3 0 0 0

7 6 1 0 0 u 0 0 u −6
3,3 3,3

u[3,3]:6;
L:genmatrix(l,n,n);
U:genmatrix(u,n,n);
S:L.U−A;
1 0 0 1 2 3 0 0 0

6 3 1 0 0 −1 −3 0 0 0

7 6 1 0 0 6 0 0 0

is(A=L.U);
true
b:matrix([2],[4],[7]);
X:invert(U).invert(L).b;

1
2
2
1
4 −
2
7
5
6

kill(all);
A:matrix([1,3,1,−2],[2,4,−1,2],[3,1,1,5],[4,2,−1,6]);
n:4;
for i:1 thru n do(
for j:1 thru n do(
if(i=j) then l[i,j]:1,if (j>i) then l[i,j]:0));
for i:1 thru n do(
for j:1 thru n do(
if (i>j) then u[i,j]:0));
L:genmatrix(l,n,n);
U:genmatrix(u,n,n);
S:L.U−A;
practical.wxmx 17 / 31

1 3 1 −2

2 4 −1 2
done 4 done done
3 1 1 5

4 2 −1 6

1 0 0 0 u u u u
1,1 1,2 1,3 1,4

l 1 0 0 0 u u u
2,1 2,2 2,3 2,4

l l 1 0 0 0 u u
3,1 3,2 3,3 3,4

l l l 1 0 0 0 u
4,1 4,2 4,3 4,4

u −1 u −3 u −1 u +2
1,1 1,2 1,3 1,4

u l −2 u +u l −4 u +u l +1 u +u l −2
1,1 2,1 2,2 1,2 2,1 2,3 1,3 2,1 2,4 1,4 2,1

u l −3 u l +u l −1 u +u l +u l −1 u +u l +u l −5
1,1 3,1 2,2 3,2 1,2 3,1 3,3 2,3 3,2 1,3 3,1 3,4 2,4 3,2 1,4 3,1

u l −4 u l +u l −2 u l +u l +u l +1 u +u l +u l +u l
1,1 4,1 2,2 4,2 1,2 4,1 3,3 4,3 2,3 4,2 1,3 4,1 4,4 3,4 4,3 2,4 4,2 1,4 4,1

u[1,1]:1;
u[1,2]:3;
u[1,3]:1;
u[1,4]:−2;
L:genmatrix(l,n,n);
U:genmatrix(u,n,n);
S:L.U−A;
1 0 0 0

l 1 0 0
2,1
1 3 1 −2
l l 1 0
3,1 3,2

l l l 1
4,1 4,2 4,3

1 3 1 −2

0 u u u
2,2 2,3 2,4

0 0 u u
3,3 3,4

0 0 0 u
4,4
practical.wxmx 18 / 31

0 0 0 0

l −2 u +3 l −4 u +l +1 u −2 l −2
2,1 2,2 2,1 2,3 2,1 2,4 2,1

l −3 u l +3 l −1 u +u l +l −1 u +u l −2 l −5
3,1 2,2 3,2 3,1 3,3 2,3 3,2 3,1 3,4 2,4 3,2 3,1

l −4 u l +3 l −2 u l +u l +l +1 u +u l +u l −2 l −6
4,1 2,2 4,2 4,1 3,3 4,3 2,3 4,2 4,1 4,4 3,4 4,3 2,4 4,2 4,1

l[2,1]:2;
l[3,1]:3;
l[4,1]:4;
L:genmatrix(l,n,n);
U:genmatrix(u,n,n);
S:L.U−A;
1 0 0 0

2 1 0 0
2 3 4 3 l 1 0
3,2

4 l l 1
4,2 4,3

1 3 1 −2

0 u u u
2,2 2,3 2,4

0 0 u u
3,3 3,4

0 0 0 u
4,4

0 0 0 0

0 u +2 u +3 u −6
2,2 2,3 2,4

0 u l +8 u +u l +2 u +u l − 11
2,2 3,2 3,3 2,3 3,2 3,4 2,4 3,2

0 u l + 10 u l +u l +5 u +u l +u l − 14
2,2 4,2 3,3 4,3 2,3 4,2 4,4 3,4 4,3 2,4 4,2

u[2,2]:−2;
u[2,3]:−3;
u[2,4]:6;
L:genmatrix(l,n,n);
U:genmatrix(u,n,n);
S:L.U−A;
practical.wxmx 19 / 31

1 0 0 0

2 1 0 0
−2 −3 6
3 l 1 0
3,2

4 l l 1
4,2 4,3

1 3 1 −2

0 −2 −3 6

0 0 u u
3,3 3,4

0 0 0 u
4,4

0 0 0 0

0 0 0 0

0 8−2 l u −3 l +2 u +6 l − 11
3,2 3,3 3,2 3,4 3,2

0 10 − 2 l u l −3 l +5 u +u l +6 l − 14
4,2 3,3 4,3 4,2 4,4 3,4 4,3 4,2

l[3,2]:4;
l[4,2]:5;
L:genmatrix(l,n,n);
U:genmatrix(u,n,n);
S:L.U−A;

1 0 0 0 1 3 1 −2

2 1 0 0 0 −2 −3 6
4 5 0 0 u u
3 4 1 0 3,3 3,4

4 5 l 1 0 0 0 u
4,3 4,4

0 0 0 0

0 0 0 0

0 0 u − 10 u + 13
3,3 3,4

0 0 u l − 10 u +u l + 16
3,3 4,3 4,4 3,4 4,3

u[3,3]:10;
u[3,4]:−13;
L:genmatrix(l,n,n);
U:genmatrix(u,n,n);
S:L.U−A;
practical.wxmx 20 / 31

1 0 0 0 1 3 1 −2

2 1 0 0 0 −2 −3 6
10 − 13
3 4 1 0 0 0 10 − 13

4 5 l 1 0 0 0 u
4,3 4,4

0 0 0 0

0 0 0 0

0 0 0 0

0 0 10 l − 10 u − 13 l + 16
4,3 4,4 4,3

u[4,4]:−3;
L:genmatrix(l,n,n);
U:genmatrix(u,n,n);
S:L.U−A;
1 0 0 0 1 3 1 −2

2 1 0 0 0 −2 −3 6
−3
3 4 1 0 0 0 10 − 13

4 5 l 1 0 0 0 −3
4,3

0 0 0 0

0 0 0 0

0 0 0 0

0 0 10 l − 10 13 − 13 l
4,3 4,3

is(A=L.U);
false
b:matrix([3],[7],[10],[11]);
X:invert(U).invert(L).b;

10

11
practical.wxmx 21 / 31

49 3 − 2 ( 4 l −5) +3 l −4 +7 (4 l − 5 ) − 10 l + 11
4,3 4,3 4,3 4,3 69
+
60 20

7 3 − 2 (4 l −5) +3 l −4 +7 (4 l − 5 ) − 10 l + 11 1
− 4,3 4,3 4,3 4,3 −
20
20

13 3 − 2 ( 4 l −5) +3 l −4 +7 (4 l − 5 ) − 10 l + 11 3
− 4,3 4,3 4,3 4,3 −
10
30

3 − 2 (4 l −5) +3 l −4 +7 (4 l − 5 ) − 10 l + 11
− 4,3 4,3 4,3 4,3

Practical-7
Gauss−Jacobi method
/*Jacobi method */
kill(all)$
x10=x10:0.0;
x20=x20:0.0;
x30=x30:0.0;
for i:1 thru 10 do(
x1:(10−x20−2.0·x30)/5,
x2:(−14+3·x10−4·x30)/9,
x3:(33+x10+2·x20)/7,
print(i,"It gives ","x1=",x1," x2=",x2," x3=",x3),
x10:x1,
x20:x2,
x30:x3)$
print("x1=",x1)$
print("x2=",x2)$
print("x3=",x3)$
x10 = 0.0 x20 = 0.0 x30 = 0.0
1 It gives x1= 2.0 x2= − 1.5555555555555556 x3=
4.714285714285714
2 It gives x1= 0.42539682539682533 x2= − 2.984126984126984 x3=
practical.wxmx 22 / 31

4.555555555555555
3 It givesx1= 0.7746031746031747 x2= − 3.4384479717813052 x3=
3.9224489795918367
4 It givesx1= 1.1187100025195262 x2= − 3.040665154950869 x3=
3.842529604434366
5 It givesx1= 1.0711211892164274 x2= − 2.8904431566865423 x3=
4.005339956088256
6 It givesx1= 0.9759526489020061 x2= − 2.97866625074486 x3=
4.041462125120478
7 It givesx1= 0.979148400100781 x2= − 3.026443394863988 x3=
4.002660021058898
8 It givesx1= 1.0042246705492386 x2= − 3.008132764881472 x3=
3.989465944338972
9 It givesx1= 1.0058401752407056 x2= − 2.9939099739675745 x3=
3.9982798772551855
10 It givesx1= 0.9994700438914409 x2= − 2.9972887759220694
x3= 4.002574318186508
x1= 0.9994700438914409
x2= − 2.9972887759220694
x3= 4.002574318186508

kill(all)$'n=n:3;'a=a:matrix([5,1,2],[3,−9,−4],[1,2,−7]);
'x=x:matrix([0],[0],[0]); 'b=b:matrix([10],[14],[−33]);
print("itr","","","","","x1","","","","","x2","","","","","x3")$
for k:1 thru 10 do(for i : 1 thru n do(y[i]:float
((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 i:1 thru n do(x[i]:y[i]),
print(k," "," "," ",x[1]," "," "," ",x[2]," "," "," ",x[3]))$
for p:1 thru n do print('x[p]=x[p]) $
5 1 2 0 10

n=3 a= 3 −9 −4 x= 0 b = 14
1 2 −7 0 − 33

itr x1 x2 x3
1 [ 2.0 ] [ − 1.5555555555555556 ] [ 4.714285714285714 ]
2 [ 0.42539682539682533 ] [ − 2.984126984126984 ]
[ 4.555555555555555 ]
3 [ 0.7746031746031747 ] [ − 3.438447971781305 ]
practical.wxmx 23 / 31

[ 3.9224489795918362 ]
4 [ 1.1187100025195265 ] [ − 3.040665154950869 ]
[ 3.842529604434366 ]
5 [ 1.0711211892164276 ] [ − 2.8904431566865423 ]
[ 4.005339956088256 ]
6 [ 0.9759526489020062 ] [ − 2.9786662507448596 ]
[ 4.041462125120478 ]
7 [ 0.979148400100781 ] [ − 3.026443394863988 ]
[ 4.002660021058897 ]
8 [ 1.0042246705492388 ] [ − 3.0081327648814717 ]
[ 3.989465944338972 ]
9 [ 1.0058401752407056 ] [ − 2.9939099739675745 ]
[ 3.9982798772551846 ]
10 [ 0.9994700438914411 ] [ − 2.9972887759220694 ]
[ 4.002574318186507 ]
x = [ 0.9994700438914411 ]
1
x = [ − 2.9972887759220694 ]
2
x = [ 4.002574318186507 ]
3

Practical-8
Gauss−Seidel method
kill(all)$x1:0.0;x2:0.0;x3:0.0;
print("itr","","","","","solution")$
for i:1 thru 10 do(
x1:(10−x2−2.0·x3)/5,
x2:(−14+3·x1−4·x3)/9,
x3:(33+x1+2·x2)/7,
print(i,"","","","x1=",x1,"x2=",x2,"x3=",x3))$
print("x1=",x1)$print("x2=",x2)$
print("x3=",x3)$
0.0 0.0 0.0
itr solution
practical.wxmx 24 / 31

1 x1= 2.0 x2= − 0.8888888888888888 x3= 4.746031746031746


2 x1= 0.2793650793650794 x2= − 3.5717813051146385 x3=
3.7336860670194
3 x1= 1.220881834215168 x2= − 2.8080109739369 x3=
4.086408555191624
4 x1= 0.9270387727107305 x2= − 3.0627242114038116 x3=
3.9716557642718726
5 x1= 1.0238825365720132 x2= − 2.979441716374606 x3=
4.0092855862603995
6 x1= 0.9921741087707613 x2= − 3.00673555763659 x3=
3.9969575704996543
7 x1= 1.0025640833274563 x2= − 2.997793114668472 x3=
4.000996836284359
8 x1= 0.9991598884199508 x2= − 3.000723075541954 x3=
3.9996733910480065
9 x1= 1.000275258689188 x2= − 2.9997630875693844 x3=
4.000107011935774
10 x1= 0.9999098127395672 x2= − 3.000077623280488 x3=
3.999964938025513
x1= 0.9999098127395672
x2= − 3.000077623280488
x3= 3.999964938025513

/* gauss-seidel method by Matrix */


kill(all)$
'n=n:3;
'a=a:matrix([5,1,2],[3,−9,−4],[1,2,−7]);
'x=x:matrix([0],[0],[0]);'b=b:matrix([10],[14],[−33]);
for k:1 thru 10 do(
for i:1 thru n do (
x[i]:float((b[i]−sum(a[i,j].x[j],j,1,i−1)−sum(a[i,j].x[j],j,i+1,n))/a[i]
print(k,"It gives:", 'x[1]=x[1],'x[2]=x[2],'x[3]=x[3]))$
for p:1 thru n do print('x[p]=x[p])$
Refusing to send cell to maxima: Un-closed parenthesis on encountering ; or $
practical.wxmx 25 / 31

kill(all);
'n=n:2;'a=a:matrix([1,−2],[1,1]);'x=x:matrix([0],[0]);'b=b:matrix([2],[5]
print( "iteration", ""," "," x1"," " ,"x2") $
for k:1 thru 5 do( for i:1 thru n do( x[i]:float((b[i]−sum(a[i,j]·x[j],j,
print(k,"","","",'x[1]=x[1],'x[2]=x[2]))$
for p:1 thru n do print ('x[p]=x[p])$

Refusing to send cell to maxima: Un-closed parenthesis on encountering ; or $

Practical-9 Lagrange
interpolation
/* lagrange interpolation*/
kill(all)$'N=N:3;
'sum=sum:0;
lagrange(N,n):=product(if equal(k,n)then 1
else(x−x[k])/(x[n]−x[k]),k,1,N);
for i:1 thru N do(sum:f(x[i])·lagrange(N,i)+sum);
display(sum)$
N=3 sum = 0 lagrange ( N , n ) :=
N

x−x
k
if equal ( k , n ) then 1 else done
x −x
n k
k=1

f(x ) (x−x ) (x−x ) f(x ) (x−x ) (x−x ) f(x ) (x−x ) (x−x )


1 2 3 2 1 3 3 1 2
sum = + +
(x −x ) (x −x ) (x −x ) (x −x ) (x −x ) (x −x )
1 2 1 3 2 1 2 3 3 1 3 2
kill(all)$'N=N:3;'sum=sum:0;
'x[1]=x[1]:1;
'x[2]=x[2]:2;
'x[3]=x[3]:3;
lagrange(N,n):=product(if equal(k,n)then 1
else(x−x[k])/(x[n]−x[k]),k,1,N);
for i:1 thru N do(sum:f(x[i])·lagrange(N,i)+sum);
subst([f(x[1])=1,f(x[2])=4,f(x[3])=9],sum);
ratsimp(%);ev(%,x=2.5);
practical.wxmx 26 / 31

N =3 sum = 0 x =1 x =2 x =3
1 2 3
N

x−x
k
lagrange ( N , n ) := if equal ( k , n ) then 1 else
x −x
n k
k=1

9 (x−2) (x−1) (2−x) (x−3)


done +4 (3−x) (x−1)−
2 2
2
x 6.25

Practical-10 Newton
interpolation

/* Newton's divided difference */


kill(all)$
'n=n:3;'sum=sum:0;
'x[0]=x[0]:3;
'x[1]=x[1]:5;
'x[2]=x[2]:6;
'x[3]=x[3]:9;
divdiff(n):=sum(f(x[i])/product(if equal(j,i)then 1
else (x[i]−x[j]),j,0,n),i,0,n);
p[n](x):=sum(divdiff(i)·product(if(i<=j) then 1
else(x−x[j]),j,0,n),i,0,n);
subst([f(x[0])=293,f(x[1])=508,f(x[2])=585,f(x[3])=764],
'p[3](x)=p[3](x));
ratsimp(%);
ev(%,x=7.0);

n=3 sum = 0 x =3 x =5 x =6
0 1 2
x =9 divdiff ( n ) :=
3
practical.wxmx 27 / 31

f(x )
i
,i,0,n
n
sum p ( x ) :=
n
( if equal ( j , i ) then 1 else x − x )
i j
j=0

sum divdiff ( i ) ( if i ≤ j then 1 else x − x ) , i , 0 , n p ( x )=


j 3
j=0

35 ( x − 6 ) ( x − 5 ) ( x − 3 ) 61 ( x − 5 ) ( x − 3 ) 215 ( x − 3 )
− + + 293
36 6 2
3 2
35 x − 856 x + 9003 x − 9702
p ( x )= p ( 7.0 ) =
3 36 3
649.4444444444445
kill(all)$'n=n:3;'sum=sum:0;
'x[0]=x[0]:0;
'x[1]=x[1]:5;
'x[2]=x[2]:10;
'x[3]=x[3]:15;
divdiff(n):=sum(f(x[i])/product(if equal (j,i)then 1
else(x[i]−x[j]),j,0,n),i,0,n)$
p[n](x):=sum(divdiff(i)·product(if(i<=j)then 1
else (x−x[j]),j,0,n),i,0,n)$
subst([f(x[0])=53,f(x[1])=127,f(x[2])=213,f(x[3])=378],
'p[3](x)=p[3](x));
ratsimp(%);ev(%,x=8.0);

n=3 sum = 0 x =0 x =5 x = 10
0 1 2
67 ( x − 10 ) ( x − 5 ) x 6 (x−5) x 74 x
x = 15 p ( x )= + + +
3 3 750 25 5
3 2
67 x − 825 x + 13550 x + 39750
53 p ( x )= p ( 8.0 ) =
3 750 3
172.87199999999999
practical.wxmx 28 / 31

Practical-11

Trapezoidal rule

kill(all)$a:1.0;
b:2.0;
f(x):=1/x;
h:(b−a)$
'integrate(1/x,x,1,2)=I:h/2·(f(a)+f(b));
'Exact−value=exact:(integrate(1/x,x,1,2));
exact:float(exact);
'err=err:abs(I−exact);
2

1 1
1.0 2.0 f ( x ) := dx =
x x
1

0.75 Exact − value = log ( 2 ) 0.6931471805599453


err = 0.056852819440054714
kill(all);
x0:1;
x1:5/4;
x2:3/2;
x3:7/4;
x4:2;
h:(x4−x0)/4;
f(x):=1/x;
'integrate(1/x,x,1,2)=I:h/2·(f(x0)+2·f(x1)+2·f(x2)+2·f(x3)+f(x4));
'Exact−value=exact:(integrate(1/x,x,1,2));
exact:float(exact);
'err=err:abs(I−exact);
5 3 7
done 1 2
4 2 4
practical.wxmx 29 / 31

1 1 1 1171
f ( x ) := dx =
4 x x 1680
1

Exact − value = log ( 2 ) 0.6931471805599453 err =


0.0038766289638642037

Practical-12 Simpson's
rule
kill(all)$
a:1.0;
b:2.0;
f(x):=1/x;
h:(b−a)/2$
'integrate(1/x,x,1,2)=I:h/3·(f(a)+4·f((a+b)/2)+f(b));
'Exact=I1:(integrate(1/x,x,1,2));
Exact:float(I1);
'err=err:abs(I−float(Exact));
2

1 1
1.0 2.0 f ( x ) := dx =
x x
1

0.6944444444444443 Exact = log ( 2 ) 0.6931471805599453


err = 0.0012972638844990225
kill(all)$
a:1.0;
b:2.0;
f(x):=1/x;
h:(b−a)/4$
'integrate(1/x,x,1,2)=I:h/3·(f(a)+4·f(a+h)+4·f(a+3·h)+2·f(a+2·h)+f(b));
'Exact=I1:(integrate(1/x,x,1,2));
Exact:float(I1);
'err=err:abs(I−float(Exact));
practical.wxmx 30 / 31

1.0 2.0 f(x ):= 1 1


dx =
x x
1

0.6932539682539682 Exact = log ( 2 ) 0.6931471805599453


−4
err = 1.067876940229473 10
kill(all)$
a:0.0;
b:1.0;
f(x):=1/(1+x^2);
h:(b−a)/2.0$
'integrate(1/(1+x^2),x,1,2)=I:h/3·(f(a)+4·f((a+b)/2)+f(b));
'I1=I1:(integrate(1/(1+x^2),x,0,1));
I1:float(I1);
'err=err:abs(I−float(I1));
1
0.0 1.0 f ( x ) :=
2
1+x
2

1 π
d x = 0.7833333333333333 I1 =
2 4
x +1
1

0.7853981633974483 err = 0.002064830064114953


kill(all)$
a:0.0;
b:1.0;
f(x):=x^2+1;
h:(b−a)/2.0$
'integrate((1+x^2),x,1,2)=I:h/3·(f(a)+4·f((a+b)/2)+f(b));
'I1=I1:(integrate((1+x^2),x,0,1));
I1:float(I1);
'err=err:abs(I−float(I1));
2
0.0 1.0 f ( x ) := x + 1
practical.wxmx 31 / 31

2 4
x + 1 d x = 1.3333333333333333 I1 =
3
1

1.3333333333333333 err = 0.0


kill(all)$
a:1.0;
b:2.0;
f(x):=1/x;
h:(b−a)/2$
'integrate(1/x,x,1,2)=I:3·h/8·(f(a)+3·f((a+b)/2)+f(b));
'Exact=I1:(integrate(1/x,x,1,2));
Exact:float(I1);
'err=err:abs(I−float(Exact));

1 1
1.0 2.0 f ( x ) := dx =
x x
1

0.65625 Exact = log ( 2 ) 0.6931471805599453 err =


0.036897180559945286

You might also like