-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion_of_2_balls.py
105 lines (87 loc) · 2.44 KB
/
motion_of_2_balls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from visual import *
from numpy.linalg import solve
from visual.graph import *
scence = display(x = 0, uy = 0, widht = 500, height = 500, title =
'Strings and Masses configuration')
tempe = curve(x=range(0,500),color = color.black)
n = 9
eps = 1e-3
deriv = zeros((n,n),float)
f = zeros((n),float)
x = array([0.5,0.5,0.5,0.5,0.5,0.5,0.5,1.,1.,1.])
def plotconfig():
for obj in scence.objects:
obj.visible = 0
L1 = 3.0
L2 = 4.0
L3 = 4.0
xa = L1*x[3]
ya = L1*x[0]
xb = xa+L2*x[4]
yb = ya+L2*x[1]
xc = xb+L3*x[5]
yc = xc-L3*x[2]
mx = 100.0
bx = -500.0
my = -100.0
by = 400.0
xap = mx*xa+bx
yap = my*ya+by
ball1 = sphere(pos=(xap,yap),color = color.cyan,radius = 15)
xbp = mx*xb+bx
ybp = my*yb+by
ball2 = sphere(pos=(xbp,ybp),color = color.cyan, radius= 25)
xcp = mx*xc+bx
ycp = my*yc+by
x0 = mx*0+bx
y0 = my*0+by
line1 = curve(pos=[(x0,y0),(xap,yap)],color = color.yellow, radius = 4)
line2 = curve(pos=[(xap,yap),(xbp,ybp)],color = color.yellow, radius = 4)
line3 = curve(pos=[(xbp,ybp),(xcp,ycp)],color = color.yellow, radius = 4)
topline = curve(pos=[(x0,y0),(xcp,ycp)],color = color.red, radius = 4)
def F(x,f):
f[0] = 3*x[3] + 4*x[4] + 4*x[5] -8.0
f[1] = 3*x[0] + 4*x[1] - 4*x[2]
f[2] = x[6]*x[0] - x[7]*x[1] -10.0
f[3] = x[6]*x[3] - x[7]*x[4]
f[4] = x[7]*x[1] + x[8]*x[2] -20
f[5] = x[7]*x[4] - x[8]*x[5]
f[6] = pow(x[0],2) + pow(x[3],2) - 1.0
f[7] = pow(x[1],2) + pow(x[4],2) - 1.0
f[8] = pow(x[2],2) + pow(x[5],2) - 1.0
def dFi_dXj(x,deriv,n):
h = 1e-4
for j in range(0,n):
temp = x[j]
x[j] = x[j] - h/2
F(x,f)
for i in range(0,n): deriv[i,j] = f[i]
x[j] = temp
for j in range(0,n):
temp = x[j]
x[j] = x[j] - h/2
F(x,f)
for i in range(0,n): deriv[i,j] = (deriv[i,j]-f[i])/h
x[j] = temp
for it in range(1,100):
rate(1)
F(x,f)
dF_dXj(x, deriv, n)
B = array([[-f[0]],[-f[1]],[-f2],[-f[3]],[-f[4]],[-f[5]],[-f[6]],[-f[7]],
[-f[8]]])
sol = solve(deriv,B)
dx = take(sol,(0,),1)
for i in range(0,n):
x[i] = x[i] + dx[i]
plotconfig()
errX = errF = errXi = 0.0
for i in range(0,n):
if (x[i] != 0.):errXi = abs(dx[i]/x[i])
else:
errXi = abs(dx[i])
if(errXi > errX): errX = errXi
if(abs(f[i]>errF)):errF = abs(f[i])
if((errX <= eps) and (errF <= eps)): break
print('Number of iterations = ',it,"\n Final Solution : ")
for i in range(0,n):
print('x[',i,']',x[i])