언어/Python

일반일을 위한 물리 코딩 과제 모음

이게될까 2024. 4. 14. 09:09
728x90
728x90

과제 1

Web VPython 3.2

myBox = box(pos = vec(-5,0,0), color = color.yellow, size = vec(0.5,0.25,0.25), make_trail=True, retain = 30)
myBox.v = vec(0,0,0)
myBox.a= vec(1,0,0)
t = 0 
dt = 0.01

while t <= 5:
    rate(1/dt)
    myBox.v = myBox.v +myBox.a * dt 
    myBox.pos =myBox.pos + myBox.v*dt 
    t = t + dt

 

과제 2

Web VPython 3.2
Earth = sphere(pos=vec(0,1.5e11,0), radius=6.4e9, texture = textures.earth, make_trail=True)
Earth.m = 5.97e24
Earth.v= vec(-29783,0,0)
Sun = sphere(pos=vec(0,0,0), radius=3.5e10,  color=color.yellow)
Sun.m = 1.99e30
Sun.v= vec(0,0,0)

G = 6.67e-11
t = 0
dt = 60*24
while t < 10*365*24*60*60:
    rate(60)
    r = Sun.pos - Earth.pos
    Sun.f = -G*Earth.m*Sun.m/mag(r)**2*norm(r)
    Earth.f = -Sun.f 
    Sun.v = Sun.v + Sun.f/Sun.m*dt
    Earth.v = Earth.v + Earth.f/Earth.m*dt
    Sun.pos = Sun.pos + Sun.v*dt
    Earth.pos = Earth.pos + Earth.v*dt
    t = t + dt

 

과제3

GlowScript 2.9 VPython

#ground
ground = box(pos=vec(0,0,0),size=vec(100,0.10,70), color = color.green)
myBox = box(pos = vec(15,1.5,0), color = color.white, size = vec(0.5,3,8))
#init. positon & velocity of ball
init_pos = vec(-30,0.11,0)
ball = sphere(pos=init_pos,radius=0.11, color = color.orange, make_trail = False)
#m
#
ball.m = 0.45 #kg
ball.speed = 25 #m/s
ball.angle = radians(35) #c.f) degrees
ball.v = ball.speed*vec(cos(ball.angle),sin(ball.angle),0)

attach_arrow(ball, "v", shaftwidth = 0.1, scale = 0.3, color=color.yellow)
scene.range = 30

#const.
g = -9.8 #m/s**2
rho = 1.204 #kg/m**3
Cd = 0.3#0.3#0.3#1 #laminar
Cm = 1 #0.5
w = 10*2*pi # 10 rev. per sec

##UI
scene.append_to_caption(' \nInitial Values \n \n')

#slider
velocitySlider = slider(min = 0, max = 45, value = 25, bind = setVelocity)
scene.append_to_caption(' \nVelocity:',velocitySlider.min, 'to' ,velocitySlider.max, '\n \n')

def setVelocity():
    global ball
    ball.speed = velocitySlider.value
    ball.v = ball.speed*vec(cos(ball.angle),sin(ball.angle),0)
    
angleSlider = slider(min = 0, max = 90, value = 35, bind = setAngle)
scene.append_to_caption(' \nAngle:',angleSlider.min, 'to' ,angleSlider.max, ' \n \n')

def setAngle():
    global ball
    ball.angle = radians(angleSlider.value)
    ball.v = ball.speed*vec(cos(ball.angle),sin(ball.angle),0)
    
angularSlider = slider(min = -10, max = 10, value = 10, bind = setAngualr)
scene.append_to_caption(' \nAngular velocity:',angularSlider.min, 'to',angularSlider.max, ' \n \n')

def setAngualr():
    global w
    w = angularSlider.value*2*pi
    #btn
    
btnStart = button(text = 'Shoot',bind = startbtn)

def startbtn(b):
    b.disabled = True
    return b.disabled
    
#time setting
t = 0
dt = 0.01

while t<20:
    rate(1/dt)
    
    if btnStart.disabled == True:
        ball.make_trail = True
        
        #Gravity Force
        grav = ball.m * vec(0,g,0) #gravity
        
        #Drag Force
        drag = -0.5*rho*Cd*(pi*ball.radius**2)*mag(ball.v)**2*norm(ball.v)
        
        #Magnus Force
        magnus = 0.5*rho*Cm*(pi*ball.radius**2)*ball.radius*w*mag(ball.v)*cross(vec(0,1,0),norm(ball.v))
        
        #Sum of Forces
        ball.f = grav + drag + magnus
        
        #Time stepping
        ball.v = ball.v + ball.f/ball.m*dt
        ball.pos = ball.pos + ball.v*dt
        
    #collision
    if ball.pos.y - ball.radius < 0:
        scene.waitfor('click')
        
        ##reset
        btnStart.disabled = False
        ball.pos = init_pos
        ball.v = ball.speed*vec(cos(ball.angle),sin(ball.angle),0)
        ball.make_trail = False
        t = 0
    if ball.pos.x - myBox.pos.x>0 and ball.pos.y - (myBox.pos.y+ myBox.size.y/2)<0 and ball.pos.z<(myBox.pos.z+ myBox.size.z/2) and ball.pos.z>(myBox.pos.z- myBox.size.z/2) :
        scene.append_to_caption('\n\n goal !!!!!!!!!!!!!!!!1')

        scene.waitfor('click')

        ##reset
        btnStart.disabled = False
        ball.pos = init_pos
        ball.v = ball.speed*vec(cos(ball.angle),sin(ball.angle),0)
        ball.make_trail = False
        t = 0
    t = t + dt

 

과제4

Web VPython 3.2


## Objects

ceiling = box(size = vector(0.3, 0.01, 0.3))

ball1 = sphere(pos=vector(0.0,-0.3,0.0), radius=0.03, texture=textures.metal, make_trail=True, trail_color = color.blue,retain=50)

spring1 = helix(pos=ceiling.pos, axis=ball1.pos-ceiling.pos, color=color.black, thickness=.003, coils=30, radius=0.01)


ball2 = sphere(pos = spring1.axis + ball1.pos, radius=0.03, texture=textures.metal, make_trail=True, trail_color = color.red, retain=50)

spring2 = helix(pos = ball1.pos, axis = ball2.pos - ball1.pos, color=color.black, thickness=.003, coils=30, radius=0.01)

 

# constants and physical properties

# ball1

g = 9.8

ball1.m = 1

l01 = 0.3

ks1 = 100

kd1 = 1.0

# ball2

l02 = 0.3

ball2.m = 1

ks2 = 100

kd2 = 1.0

 

# initial values

ball1.v = vector(0,0,0)

ball2.v = vector(0,0,0)

 

 

Fgrav1 = (ball1.m+ball2.m) * vector(0,-g,0)

Fgrav2 = ball2.m * vector(0,-g,0)

# time setting

t = 0

dt = 0.01

 

# the display

scene.background = color.white

scene.autoscale = True

scene.center = vector(0,-l01,0)

scene.waitfor('click')

 

## Graph

motion_graph = graph(title = 'Motion graph', xtitle = 'time', ytitle = 'spring length')

traj = gcurve(color=color.blue)

traj2 = gcurve(color=color.red)

 

## Simulation loop

while True:

    rate(1/dt) # Real time

    # Spring1 force

    l1 = mag(ball1.pos - ceiling.pos)

    s1 = l1 - l01

    lhat1 = norm(ball1.pos)

    Fspr1 = -ks1 * s1 * lhat1

    # Spring2 focre
    l2 = mag(ball2.pos - ball1.pos)
    s2 = l2 - l02
    lhat2 = norm(ball2.pos)
    Fspr2 = -ks2 * s2 * lhat2
    

 

 

    #Damping force for ball1

    Fdamp1 = -kd1 * dot(ball1.v, lhat1) * lhat1

    #Damping force for ball2
    Fdamp2 = -kd2 * dot(ball2.v, lhat2) * lhat2

 

 

 

    # Total force for ball1 and ball2
    Fnet1 = Fspr1 + Fdamp1 + Fgrav1
    Fnet2 = Fspr2 + Fdamp2 + Fgrav2
 

 

 

    #time stepping

    ball1.v = ball1.v + (Fnet1)/ball1.m*dt

    ball1.pos = ball1.pos + ball1.v*dt

    spring1.axis = ball1.pos - ceiling.pos

    ball2.v = ball2.v + Fnet2/ball2.m*dt

    ball2.pos = ball2.pos + ball2.v*dt

    spring2.pos = ball1.pos

    spring2.axis = ball2.pos - ball1.pos

    t = t + dt

    #graph

    traj.plot(pos=(t,l1))

    traj2.plot(pos=(t,l2))

 

과제5

Web VPython 3.2


ground = box(size=vec(40,40,1), color= color.green)

ball = sphere(pos=vec(-18,0,0), radius = 0.5)

ground.pos.z = ground.pos.z - ground.width/2-ball.radius

hole=cylinder(pos=vec(15,0,ground.pos.z+ground.width/2),axis=vec(0,0,1), radius=3*ball.radius, color= vec(0.8,0.8,0.8))

hole.pos.z = hole.pos.z - mag(hole.axis)*0.9

 

#properties

ball.m = 0.045

g = 9.8

mu = 0.5

 

initialspeed = 18

 

scale = initialspeed

ball.v = initialspeed*vec(1,0,0)

 

#time

t = 0

dt = 0.01

 

while t < 100:

    rate(100)
    ball.f = -mu*ball.m*g*norm(ball.v)
    ball.v = ball.v + ball.f/ball.m*dt
    ball.pos = ball.pos + ball.v*dt

    
    
    t = t + dt
728x90