Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Jacobi iteration

In [20]:

1 import numpy as np
2 a = np.array([[5,-2,3],[-3,9,1],[2,-1,-7]])
3 b = np.array([-1,2,3])
4
5 x = np.zeros(len(a[0]))
6 xnew = np.zeros(len(a[0]))
7 n = len(a)
8 lim = 10
9 e = 0.001
10
11
12 for k in range(lim):
13 for i in range(n):
14 sum = 0
15 for j in range(n):
16 if (j!=i):
17 sum = sum + a[i][j]*x[j]
18 xnew[i] = (1/a[i][i])*(b[i]-sum)
19 diff = abs(x-xnew)
20 if (max(diff) <e):
21 break
22 x = list(xnew)
23 print(xnew)

[-0.2 0.22222222 -0.42857143]

[ 0.14603175 0.2031746 -0.51746032]

[ 0.19174603 0.32839506 -0.41587302]

[ 0.18088183 0.33234568 -0.42070043]

[ 0.18535853 0.32926066 -0.42436886]

[ 0.18632558 0.33116049 -0.42264909]

Gauss Seidal Method


In [22]:

1 import numpy as np
2 a = np.array([[5,-2,3],[-3,9,1],[2,-1,-7]])
3 b = np.array([-1,2,3])
4 x = np.zeros(len(a))
5 lim = 10
6 n = len(a)
7 y = list(x)
8 e = 0.001
9 for k in range(lim):
10 for i in range(n):
11 sum = 0
12 for j in range(n):
13 if (j!=i):
14 sum = sum + a[i][j]*x[j]
15 x[i] = (1/a[i][i])*(b[i]-sum)
16
17 diff = abs(x-y)
18 if (max(diff) <e):
19 break
20 y = np.array(x)
21 print(k)
22 print(y)

[-0.2 0.15555556 -0.50793651]

[ 0.16698413 0.33432099 -0.42862182]

[ 0.19090149 0.3334807 -0.42166825]

[ 0.18639323 0.33120533 -0.42263127]

Bisection method
In [16]:

1 import numpy as np
2 def y(x):
3 return(np.cos(x)-x)
4
5 a=0
6 b=1
7
8 while(abs(a-b)>=0.001):
9 c = (a+b)/2
10 if y(a)*y(b)>0:
11 b=c
12 else:
13 a=c
14 print("the root of the equation is ", c)

the root of the equation is 0.7509765625

Newton Rhapson method


In [15]:

1 import numpy as np
2 fun = np.poly1d([1,-2,-2])
3
4 der = fun.deriv()
5 err = 123
6 x1 = 3
7 x2 = x1 - fun(x1)/der(x1)
8 while (abs(err)>=0.000001):
9 x3 = x2 - fun(x2)/der(x2)
10 x1 = x2
11 x2 = x3
12 err = (x2-x1)/x1
13 print(x1,x2)
14

2.75 2.732142857142857

2.732142857142857 2.7320508100147274

2.7320508100147274 2.732050807568877

trapezoidal rule
In [1]:

1
2 import numpy as np
3 def y(x):
4 y = 1/(1 + x*x)
5 return(y)
6
7 a = int(input("Enter the lower limit a = "))
8 b = int(input("Enter the upper limit b = "))
9 n = int(input("Number of intrevals, n = "))
10
11 x0 = a
12 y0 = y(x0)
13
14 xn = b
15 yn = y(b)
16 h = abs((b-a)/n)
17
18 Y1 = y0 + yn
19 Y = 0
20 for i in range(1,n):
21 Y = Y + y(i*h)
22 I = (h*0.5)*(Y1 + (2*Y))
23 print(I)
24

Enter the lower limit a = 0

Enter the upper limit b = 10

Number of intrevals, n = 10000

1.4711276741403547

1/3 Simpson's rule


In [2]:

1 import numpy as np
2 def y(x):
3 y = 1/(1 + x*x)
4 return(y)
5
6 a = int(input("Enter the lower limit a = "))
7 b = int(input("Enter the upper limit b = "))
8 n = int(input("Number of intrevals, n = "))
9
10 x0 = a
11 y0 = y(x0)
12
13 xn = b
14 yn = y(b)
15 h = abs((b-a)/n)
16
17 Y = y0 + yn
18 Y1 = 0
19 Y2 = 0
20 for i in range(1,n):
21 if i%2==0:
22 Y1 = Y1 + y(i*h)
23 elif i%2!=0:
24 Y2 = Y2 + y(i*h)
25 I = (h/3)*(Y + 4*Y2 +2*Y1)
26 print(I)

Enter the lower limit a = 0

Enter the upper limit b = 10

Number of intrevals, n = 1000

1.4711276743037227

Simpson's 3/8 rule


In [3]:

1 import numpy as np
2 def y(x):
3 y = 1/(1 + x*x)
4 return(y)
5
6 a = int(input("Enter the lower limit a = "))
7 b = int(input("Enter the upper limit b = "))
8 n = int(input("Number of intrevals, n = "))
9
10 x0 = a
11 y0 = y(x0)
12
13 xn = b
14 yn = y(b)
15 h = abs((b-a)/n)
16
17 Y = y0 + yn
18 Y1 = 0
19 Y2 = 0
20
21 sum1 = 0
22 sum2 = 0
23
24 for i in range(1,n):
25 sum1 = sum1 + y(i*h) - y(3*i*h)
26 sum2 = sum2 + y(3*i*h)
27 I = (3*h/8)*(Y1 + Y2 + (3*sum1) + (2*sum2))
28 print(I)

Enter the lower limit a = 0

Enter the upper limit b = 10

Number of intrevals, n = 1000

1.4590305874912595

Euler's method
In [2]:

1 def f(x,y):
2 f = y**2-1
3 return (y**2-1)
4 x0 = int(input("enter x0 : "))
5 y0 = int(input("enter y0 : "))
6 xn = int(input("enter xn : "))
7 h = float(input("enter stepsize: "))
8
9 while x0 < xn:
10 y0 = y0 + h*f(x0,y0)
11 x0 = x0 + h
12 print(x0,y0)

enter x0 : 0

enter y0 : 0

enter xn : 1

enter stepsize 0.125

0.125 -0.125

0.25 -0.248046875

0.375 -0.3653559684753418

0.5 -0.47367034551277243

0.625 -0.5706248959854988

0.75 -0.6549232994956912

0.875 -0.7263077334679008

1.0 -0.7853673680059909

Runge-Kutta method
In [6]:

1 def f(x,y):
2 f = y-x
3 return (y-x)
4 x0 = int(input("enter x0 : "))
5 y0 = int(input("enter y0 : "))
6 xn = float(input("enter xn : "))
7 h = float(input("enter stepsize: "))
8
9 k1 = h*f(x0,y0)
10 k2 = h*f(x0 + h, y0 + k1)
11
12 while x0 < xn:
13 k1 = h*f(x0,y0)
14 k2 = h*f(x0 + h, y0 + k1)
15 y = y0 + (1/2)*(k1 + k2)
16 y0 = y
17 x0 = x0 + h
18 print(x0, y)

enter x0 : 0

enter y0 : 2

enter xn : 0.2

enter stepsize 0.01

0.01 2.02005

0.02 2.0402010025

0.03 2.060454022575125

0.04 2.080810085502005

0.05 2.1012702268613004

0.060000000000000005 2.1218354926412566

0.07 2.1425069393423013

0.08 2.1632856340826914

0.09 2.1841726547052223

0.09999999999999999 2.20516908988501

0.10999999999999999 2.2262760392383543

0.11999999999999998 2.2474946134327

0.12999999999999998 2.2688259342976984

0.13999999999999999 2.2902711349373903

0.15 2.311831359843511

0.16 2.3335077650099385

0.17 2.3553015180482886

0.18000000000000002 2.3772137983046737

0.19000000000000003 2.3992457969776355

0.20000000000000004 2.4213987172372606

Free fall
In [10]:

1 y0 =0
2 t0 = 0
3 tn = 20
4 v0 = 0
5 n = 10
6
7 g = 9.8
8
9 h = (tn-t0)/n
10 V=[0]
11 t=[0]
12
13 while t0<tn-h :
14 y0 = y0+(h*v0)
15 v0 = v0 + (h*g)
16 t0 = t0 + h
17 V.append(v0)
18 t.append(t0)
19 print(t0,y0,v0)

2.0 0.0 19.6

4.0 39.2 39.2

6.0 117.60000000000001 58.800000000000004

8.0 235.20000000000002 78.4

10.0 392.0 98.0

12.0 588.0 117.6

14.0 823.2 137.2

16.0 1097.6 156.79999999999998

18.0 1411.1999999999998 176.39999999999998

In [11]:

1 import matplotlib.pyplot as plt


2 plt.scatter(t,V)
3 plt.show()

SHM
In [4]:

1 x0 = 1
2 v0 = 0
3 t0 = 0
4 tn = 25
5 n = 100
6
7 h = (tn-t0)/n
8 k=1
9 m=1
10 while(t0<tn-h):
11 a=(-k/m)*x0
12 v0 = v0+(h*a)
13 x0 = x0+(h*v0)
14 t0 = t0+h
15 print(t0,x0,v0,a)

0.25 0.9375 -0.25 -1.0

0.5 0.81640625 -0.484375 -0.9375

0.75 0.644287109375 -0.6884765625 -0.81640625

1.0 0.4319000244140625 -0.84954833984375 -0.644287109375

1.25 0.1925191879272461 -0.9575233459472656 -0.4319000244140625

1.5 -0.05889409780502319 -1.0056531429290771 -0.1925191879272461

1.75 -0.30662650242447853 -0.9909296184778214 0.05889409780502319

2.0 -0.535194750642404 -0.9142729928717017 0.30662650242447853

2.25 -0.7303133269451791 -0.7804743052111007 0.535194750642404

2.5 -0.8797873203138806 -0.5978959734748059 0.7303133269451791

2.75 -0.9742746061629646 -0.3779491433963358 0.8797873203138806

3.0 -1.0078697291268632 -0.13438049185559464 0.9742746061629646

3.25 -0.9784729940203329 0.11758694042612117 1.0078697291268632

3.5 -0.8879216967875319 0.3622051889312044 0.9784729940203329

3.75 -0.74187529350551 0.5841856131280874 0.8879216967875319

4.0 -0.5494616843793938 0.7696544365044649 0.74187529350551

4.25 -0.32270671997956546 0.9070198575993134 0.5494616843793938

4.5 -0.07578258558101428 0.9876965375942047 0.32270671997956546

4.75 0.17587796041635032 1.0066421839894584 0.07578258558101428

5.0 0.416546133887693 0.9626726938853708 -0.17587796041635032

5.25 0.631180173991055 0.8585361604134476 -0.416546133887693

5.5 0.806365453219976 0.7007411169156839 -0.631180173991055

5.75 0.9311528916226485 0.49914975361068986 -0.806365453219976

6.0 0.9977432742989054 0.26636153070502777 -0.9311528916226485

6.25 1.0019747023314807 0.016925712130301418 -0.9977432742989054

6.5 0.9435827114683385 -0.23356796345256875 -1.0019747023314807

6.75 0.8262168011384252 -0.46946364131965335 -0.9435827114683385

7.0 0.6572123407373602 -0.6760178416042597 -0.8262168011384252

7.25 0.4471321090402103 -0.8403209267885998 -0.6572123407373602

7.5 0.20910612052804717 -0.9521039540486524 -0.4471321090402103

7.75 -0.0419890005171189 -1.0043804841806643 -0.20910612052804717

8.0 -0.29045980902996504 -0.9938832340513846 0.0419890005171189

8.25 -0.5207768794784384 -0.9212682817938933 0.29045980902996504

8.5 -0.7185453949595093 -0.7910740619242838 0.5207768794784384

8.75 -0.8714048232556109 -0.6114377131844064 0.7185453949595093

9.0 -0.9698014500982368 -0.3935865073705037 0.8714048232556109

9.25 -1.0075854863097229 -0.1511361448459445 0.9698014500982368

9.5 -0.9823954296268513 0.10076022673148621 1.0075854863097229

9.75 -0.8958056585923015 0.34635908413819905 0.9823954296268513

10.0 -0.7532280338957329 0.5703104987862744 0.8958056585923015

10.25 -0.563573657080681 0.7586175072602076 0.7532280338957329

10.5 -0.3386959266980865 0.8995109215303778 0.563573657080681

10.75 -0.09264970089686164 0.9841849032048995 0.3386959266980865

11.0 0.15918713121041705 1.0073473284291148 0.09264970089686164

11.25 0.4010747676170447 0.9675505456265105 -0.15918713121041705

11.5 0.617895231047607 0.8672818537222494 -0.4010747676170447

11.75 0.796097242537694 0.7128080459603476 -0.617895231047607

12.0 0.924543176369175 0.5137837353259241 -0.796097242537694

12.25 0.9952051616775827 0.28264794123363035 -0.924543176369175

12.5 1.0036668243811413 0.033846650814234686 -0.9952051616775827

12.75 0.9493993105608787 -0.21707005528105064 -1.0036668243811413

13.0 0.8357943398305611 -0.45441988292127033 -0.9493993105608787

13.25 0.6699522228608334 -0.6633684678789106 -0.8357943398305611

13.5 0.4622380919623037 -0.830856523594119 -0.6699522228608334

13.75 0.22563408031612997 -0.9464160465846949 -0.4622380919623037

14.0 -0.02507206134980189 -1.0028245666637274 -0.22563408031612997

14.25 -0.27421119918137116 -0.996556551326277 0.02507206134980189

14.5 -0.5062121370641047 -0.9280037515309342 0.27421119918137116

14.75 -0.7065748163803317 -0.801450717264908 0.5062121370641047

15.0 -0.862776569672788 -0.6248070131698251 0.7065748163803317

15.25 -0.965054787360695 -0.4091128707516281 0.862776569672788

15.5 -1.0070170808385586 -0.16784917391145435 0.965054787360695

15.75 -0.9860408067640123 0.0839050962981853 1.0070170808385586

16.0 -0.9034369822667152 0.33041529798918834 0.9860408067640123

16.25 -0.7643683463777484 0.5562745435558671 0.9034369822667152

16.5 -0.5775266888401723 0.7473666301503042 0.7643683463777484

16.75 -0.35458961325008553 0.8917483023603473 0.5775266888401723

17.0 -0.10949068683186836 0.9803957056728687 0.35458961325008553

17.25 0.14245140751334062 1.007768377380836 0.10949068683186836

17.5 0.3854902888889658 0.9721555255025007 -0.14245140751334062

17.75 0.6044360272090306 0.8757829532802592 -0.3854902888889658

18.0 0.785604513828531 0.7246739464780017 -0.6044360272090306

18.25 0.9176727183337483 0.5282728180208689 -0.785604513828531

18.5 0.9923863779431062 0.2988546384374318 -0.9176727183337483

18.75 1.00507588893102 0.05075804395165526 -0.9923863779431062

19.0 0.9549481568607451 -0.20051092828109976 -1.00507588893102

19.25 0.8451361649866735 -0.43924796749628603 -0.9549481568607451

19.5 0.6825031628009349 -0.6505320087429545 -0.8451361649866735

19.75 0.47721371294013787 -0.8211577994431882 -0.6825031628009349

20.0 0.2420984060205822 -0.9404612276782227 -0.47721371294013787

20.25 -0.00814805127525986 -1.0009858291833682 -0.2420984060205822

20.5 -0.25788525536639817 -0.9989488163645532 0.00814805127525986

20.75 -0.4915046309971366 -0.9344775025229537 0.25788525536639817

21.0 -0.694404967190554 -0.8116013447736695 0.4915046309971366

21.25 -0.8539049929345618 -0.638000102976031 0.694404967190554

21.5 -0.9600359566201595 -0.4245238547423906 0.8539049929345618

21.75 -1.0061646730169973 -0.18451486558735075 0.9600359566201595

22.0 -0.9894080973502727 0.06702630266689857 1.0061646730169973

22.25 -0.910813515599156 0.3143783270044668 0.9894080973502727

22.5 -0.775293089123092 0.5420817059042558 0.910813515599156

22.75 -0.5913168445768349 0.7359049781850288 0.775293089123092

23.0 -0.3703832972445255 0.8837341893292374 0.5913168445768349

23.25 -0.12630079383443332 0.9763300136403688 0.3703832972445255

23.5 0.12567550919031095 1.007905212098977 0.12630079383443332

23.75 0.36979709289066076 0.9764863348013993 -0.12567550919031095

24.0 0.5908063582853443 0.8840370615787342 -0.36979709289066076

24.25 0.7748902262871938 0.7363354720073981 -0.5908063582853443

24.5 0.9105434551460938 0.5426129154355996 -0.7748902262871938

24.75 0.9892877180583628 0.3149770516490762 -0.9105434551460938

In [ ]:

You might also like