-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyankou_quiz1.py
114 lines (95 loc) · 2.57 KB
/
yankou_quiz1.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
106
107
108
109
110
111
112
113
114
#Robert Yankou quiz uncorrected
#Warning: do not run this program; it is in an eternal while loop
#All of the mistakes are at the bottom, the critical one being that t does not increase inside the loop
import pygame, time, math
from pygame.locals import *
def slope_x(vx):
sx = vx
return sx
def slope_y(vy):
sy = vy
return sy
def slope_z(vz):
sz = vz
return sz
def slope_vx(vy,vz,By,Bz):
svx = vy*Bz-vz*By
return svx
def slope_vy(vx,vz,Bx,Bz):
svy = vz*Bx-vx*Bz
return svy
def slope_vz(vx,vy,Bx,By):
svz = vx*By-vy*Bx
return svz
Bx = 0
By = 0
Bz = 10
x = 0
y = 0
z = 0
vx = 1
vy = 1
vz = 1
t = 0
tf = 10
h = 0.05
running = True
W,H = 500,500
pygame.init()
screen = pygame.display.set_mode((W,H))
pygame.draw.line(screen,(128,128,128),(0,H/2),(W,H/2),1)
pygame.draw.line(screen,(128,128,128),(W/2,0),(W/2,H),1)
pygame.display.flip()
xold = W/2 + x
yold = H/2 - y
pygame.draw.circle(screen,(255,0,0),(xold,yold),5,1)
print(x,y,z,vx,vy,vz)
print("KE= ", ((vx**2+vy**2+vz**2)/2))
while abs(t)<tf:
M1=slope_x(vx)
J1=slope_vx(vy,vz,By,Bz)
N1=slope_y(vy)
K1=slope_vy(vx,vz,Bx,Bz)
O1=slope_z(vz)
L1=slope_vz(vx,vy,Bx,By)
M2=slope_x(vx+J1*h/2)
J2=slope_vx(vy+K1*h/2,vz+L1*h/2,By,Bz)
N2=slope_y(vy+K1*h/2)
K2=slope_vy(vx+J1*h/2,vz+L1*h/2,Bx,Bz)
O2=slope_z(vz+L1*h/2)
L2=slope_vz(vx+J1*h/2,vy+K1*h/2,Bx,By)
M3=slope_x(vx+J2*h/2)
J3=slope_vx(vy+K2*h/2,vz+L2*h/2,By,Bz)
N3=slope_y(vy+K2*h/2)
K3=slope_vy(vx+J2*h/2,vz+L2*h/2,Bx,Bz)
O3=slope_z(vz+L2*h/2)
L3=slope_vz(vx+J2*h/2,vy+K2*h/2,Bx,By)
M4=slope_x(vx+J3*h)
J4=slope_vx(vy+K3*h,vz+L3*h,By,Bz)
N4=slope_y(vy+K3*h)
K4=slope_vy(vx+J3*h,vz+L3*h,Bx,Bz)
O4=slope_z(vz+L3*h)
L4=slope_vz(vx+J3*h,vy+K3*h,Bx,By)
x += (M1+2*M2+2*M3+M4)*h/6
y += (N1+2*N2+2*N3+N4)*h/6
z += (O1+2*O2+2*O3+O4)*h/6
vx += (J1+2*J2+2*J3+J4)*h/6
vy += (K1+2*K2+2*K3+K4)*h/6
vz += (L1+2*L2+2*L3+L4)*h/6
xnew = W/2 + x
ynew = H/2 - y
pygame.draw.aaline(screen,(0,0,255),(xold,yold),(xnew,ynew),1)
pygame.display.flip()
xold = xnew
yold = ynew
print(x,y,z,vx,vy,vz)
print("KE= ", ((vx**2+vy**2+vz**2)/2))
while running:
for event in pygame.event.get()==Quit:
running = False
pygame.display.quit()
#Left m&q out completely
#Assumed values for vx,vy,vz,Bz,tf&h
#Did not fit or scale graph, assumed values for W&H
#Could have used 2 functions instead of 6
#Did not have to set B's as inputs of functions since they are constant