Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 1267
Kernel: Python 2 (SageMath)
from __future__ import division, print_function from vpython import * from math import *
## SETTING UP THE SCENE ## scene = canvas() scene.waitfor gray = (0.7,0.7,0.7) floor = box(pos=vector(0,-1.5,0),length=2, height=0.1, width=2) ball = sphere(pos=vector(0.55,0.25,0), radius = 0.075, color=color.green) ball.velocity = vector(0,0,0) torso = cylinder(pos=vector(-0.5,-0.5,0), axis=vector(0,1,0), radius=0.2, color=color.red) lleg = cylinder(pos=vector(-0.6,-1.5,0), axis=vector(0,1,0), radius=0.1, color=color.blue) rleg = cylinder(pos=vector(-0.4,-1.5,0), axis=vector(0,1,0), radius=0.1, color=color.blue) arm = cylinder(pos=vector(-0.5,0.25,0), axis=vector(1,0,0), radius=0.1, color=color.red) scene.autoscale = 0 ## THE PHYSICAL DYNAMICS ## dt = 0.001 while 1: rate(200) ball.pos = ball.pos + ball.velocity*dt ball.velocity.y = ball.velocity.y - 9.8*dt if (ball.pos.y < -1.5): ball.velocity.y = -ball.velocity.y
<IPython.core.display.Javascript object>
scene=canvas(title="Electric field due to uniformly charged ring") scene.background=color.white R=0.02 #radius of ring in m Q=1e-9 #charge of ring in C N=4 #number of unique pieces #draw the objects myring=ring(pos=vector(0,0,0), radius=R, axis=vector(0,0,1), color=color.blue, thickness=0.02*R) zaxis=cylinder(pos=-2*R*myring.axis, radius=0.015*R, axis=4*R*myring.axis, color=color.black) point=sphere(pos=R*myring.axis, color=color.red, radius=5*zaxis.radius) oofpez=9e9 #1/(4pi epsilon_0) in N m^2/C^2 dq=Q/N #charge of a piece dtheta=2*pi/N #theta increment for our loop theta=dtheta/2 #initial theta for first piece of loop Enet=vector(0,0,0) #net electric field of all pieces rpoint=point.pos #location of the point in space to calculate E field scale=1.2*mag(rpoint)/8000 #used to scale the arrows representing E-field while theta<2*pi: rpiece=R*vector(cos(theta),sin(theta),0) #location of piece r=rpoint-rpiece #vector from piece to point in space rmag=mag(r) #magnitude of r rhat=norm(r) #unit vector for r dE=oofpez*dq/rmag/rmag*rhat #Electric field due to piece at rpoint Enet=Enet+dE #net electric field of the first one up to this one particle=sphere(pos=rpiece, radius=point.radius, color=color.yellow) #draw a particle at center of piece dEvector=arrow(pos=rpoint, axis=scale*dE, color=color.magenta, shaftwidth=point.radius/2) theta=theta+dtheta print("The net electric field = ",Enet, "N/C") Evector=arrow(pos=rpoint, axis=scale*Enet, color=color.orange, shaftwidth=point.radius/2)
<IPython.core.display.Javascript object>
The net electric field = <-0.000000, -0.000000, 7954.951288> N/C
from vpython import * scene = canvas() print(""" Right button drag to rotate "camera" to view scene. On a one-button mouse, right is Command + mouse. Middle button to drag up or down to zoom in or out. On a two-button mouse, middle is left + right. On a one-button mouse, middle is Option + mouse. """) side = 4.0 thk = 0.3 s2 = 2*side - thk s3 = 2*side + thk wallR = box (pos=vector( side, 0, 0), size=vector(thk, s2, s3), color = color.red) wallL = box (pos=vector(-side, 0, 0), size=vector(thk, s2, s3), color = color.red) wallB = box (pos=vector(0, -side, 0), size=vector(s3, thk, s3), color = color.blue) wallT = box (pos=vector(0, side, 0), size=vector(s3, thk, s3), color = color.blue) wallBK = box(pos=vector(0, 0, -side), size=vector(s2, s2, thk), color = vector(0.7,0.7,0.7)) #ball = sphere (color = color.green, radius = 0.4) ball = sphere (color = color.green, radius = 0.4, make_trail=True, interval=5, retain=30) #ball = cone (color = color.green, radius = 0.4, make_trail=True, interval=5, retain=30) ball.mass = 1.0 ball.p = vector (-0.15, -0.23, +0.27) side = side - thk*0.5 - ball.radius display(scene) dt = 0.5 t=0.0 while t < 2000: rate(100) t = t + dt ball.pos = ball.pos + (ball.p/ball.mass)*dt if not (side > ball.pos.x > -side): ball.p.x = -ball.p.x if not (side > ball.pos.y > -side): ball.p.y = -ball.p.y if not (side > ball.pos.z > -side): ball.p.z = -ball.p.z
<IPython.core.display.Javascript object>
import ipywidgets as wd from vpython import * # For some reason, this program that uses widgets sometimes doesn't make the 3D display without a restart # This is an experiment in using Jupyter widgets (button, slider, menu). # Compare with the similar program in the Example programs at glowscript.org. # Ideally, we would like to use the same widget statements in both GlowScript and Jupyter VPython. scene.width = 350 scene.height = 300 scene.range = 1.5 scene.title = "Buttons, Sliders, and Drop-down Menus" box_object = box(visible=True) cone_object = cone(visible=False, radius=0.5) pyramid_object = pyramid(visible=False) cylinder_object = cylinder(visible=False, radius=0.5) objects = {'box':box_object, 'cone':cone_object, 'pyramid':pyramid_object, 'cylinder':cylinder_object} currentobject = box_object currentobject.color = color.cyan b = wd.Button(description='Pause') c = wd.Button(description='Red') m = wd.Dropdown(options=['box', 'cone', 'cylinder', 'pyramid'], value='box', description='Object:') container = wd.HBox(children=[b,c,m]) display(container) sl = wd.FloatSlider(description='Rotation speed:', min=20, max=500, step=1, value=250) display(sl) running = True def b_handler(s): global running running = not running if s.description == 'Run': s.description = 'Pause' else: s.description = 'Run' b.on_click(b_handler) def c_handler(s): if s.description == 'Red': s.description = 'Cyan' currentobject.color = color.red else: s.description = 'Red' currentobject.color = color.cyan c.on_click(c_handler) def m_handler(s): global currentobject col = currentobject.color currentobject.visible = False currentobject = objects[s['new']] currentobject.color = col currentobject.visible = True m.observe(m_handler, names='value') while True: rate(100) if running: currentobject.rotate(angle=1e-4*sl.value, axis=vec(0,1,0))
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>