Skip to content

Instantly share code, notes, and snippets.

@esemwy
Created November 15, 2018 14:59
Show Gist options
  • Save esemwy/45fad1e2c77572f1b5f1aa774dd5a336 to your computer and use it in GitHub Desktop.
Save esemwy/45fad1e2c77572f1b5f1aa774dd5a336 to your computer and use it in GitHub Desktop.
Ipython Test
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:70ea0bd4d8065522c89fdc0639f9c61a844c5f482b4cee6ab4de91fe5546a99a"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from ivisual import *\n",
"from math import *"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<div id=\"scene\"><div id=\"glowscript\" class=\"glowscript\"></div></div>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.HTML at 0x105c1fe50>"
]
},
{
"javascript": [
"require.undef(\"nbextensions/glow.1.0.min\");"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Javascript at 0x107679790>"
]
},
{
"javascript": [
"require.undef(\"nbextensions/glowcomm\");"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Javascript at 0x1076797d0>"
]
},
{
"javascript": [
"require([\"nbextensions/glowcomm\"], function(){console.log(\"glowcomm loaded\");})"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Javascript at 0x107679810>"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(\"\"\"\n",
"Converted everything we can to vectors.\n",
"Eliminated excess variables.\n",
"\n",
"Important points:\n",
"\n",
"A vector is something with an x,y, and z componenet.\n",
"\n",
"The following are always vectors:\n",
"position\n",
"velocity\n",
"acceleration\n",
"force\n",
"\n",
"The magnitude of a vector gives us it's total length.\n",
"Magnitude is not a vector and it is always positive.\n",
"\n",
"The norm of a vector gives us a vector in the same direction with a\n",
"magnitude of one.\n",
"\n",
"A vector is equal to it's magnitude multiplied by it's norm:\n",
"aVector == mag(aVector) * norm(aVector)\n",
"\n",
"A vector in the opposite direction of the same length would be the\n",
"same thing multiplied by negative one:\n",
"oppositeVector = -1 * aVector\n",
"or\n",
"oppositeVector = -1 * mag(aVector) * norm(bVector)\n",
"\"\"\")\n",
"\n",
"# SET UP THE WINDOW\n",
"scene = idisplay()\n",
"\n",
"scene.width = 800\n",
"scene.height = 600\n",
"\n",
"scene.autoscale = 0 # stop vpython from zooming in and out on its own\n",
"scene.range = (100,100,100) \n",
"scene.center = (50,40,0) # center in on this point\n",
"\n",
"# CREATE OUR PUCK OBJECT AND SET IT'S INITIAL VALUES\n",
"puck = cylinder(pos=(0,0,0),axis=(0,2,0), radius=2, color=color.green)\n",
"puck.velocity = vector(0,0,0)\n",
"puck.acceleration = vector(0,0,0)\n",
"puck.mass = 1 # kg\n",
"\n",
"# CREATE A GROUND FOR THE PUCK\n",
"ground = box(pos=(50,-1,1),size=(100,2,50))\n",
"\n",
"# CREATE A LABEL FOR US TO PUT INFO IN\n",
"mylabel = label(pos=(-50,90,0),height=10,box=0)\n",
"mylabel2 = label(pos=(-50,78,0),height=10,box=0)\n",
"mylabel3 = label(pos=(-50,66,0),height=10,box=0)\n",
"mylabel4 = label(pos=(-50,54,0),height=10,box=0)\n",
"mylabel5 = label(pos=(-50,42,0),height=10,box=0)\n",
"\n",
"# THESE ARE ALL THE ACCELERATION VECTORS THAT WILL BE\n",
"# ACTING ON OUR PUCK (IGNORE THE COLLISION FORCE AND ACCELERATION)\n",
"accelerationGravity = vector(0,-9.8,0) # m/s**2\n",
"accelerationNormal = vector(0,0,0) # m/s**2, none until hits the ground\n",
"accelerationKineticFriction = vector(0,0,0) # m/s**2, none until hits ground\n",
"\n",
"# SET UP SOME INITIAL CONDITIONS FOR THROWNING THE PUCK\n",
"velocityThrownMagnitude = 25 # m/s\n",
"angleThrown = 45 # degrees\n",
"angleThrown = angleThrown * (pi/180) # converted to radians\n",
"\n",
"# CONVERTING THE VELOCITY THROWN ANGLE AND MAGNITUDE\n",
"# INTO A VECTOR\n",
"velocityThrown = vector(0,0,0)\n",
"velocityThrown.y = velocityThrownMagnitude * sin(angleThrown)\n",
"velocityThrown.x = velocityThrownMagnitude * cos(angleThrown)\n",
"velocityThrown.z = 0\n",
"\n",
"# SET THE WIND VELOCITY AS A VECTOR\n",
"velocityWind = vector(0,0,-3)\n",
"\n",
"# SET THE PUCK'S INITAL VELOCITY AND ACCELERATION\n",
"puck.velocity = velocityThrown + velocityWind\n",
"puck.acceleration = accelerationGravity + accelerationNormal + accelerationKineticFriction\n",
"\n",
"seconds = 0 # total time starts out at zero\n",
"dt = .01 # .01 seconds (difference in time)\n",
"\n",
"display(scene)\n",
"\n",
"finished = False\n",
"while not finished:\n",
" rate(100) # go thru the loop no more than 100 times/s\n",
" seconds += dt # total time (we don't use it anymore)\n",
"\n",
" # we update the position and velocity incrementally\n",
" # based on the last calculated acceration and its previous\n",
" # velocity and accleration (.01 seconds ago).\n",
" \n",
" # velocity equation: vel = vel0 + accel * time\n",
" # position equation: pos = pos0 + vel0*time + .5 * accel * time**2\n",
" puck.velocity += puck.acceleration * dt\n",
" puck.pos += puck.velocity * dt + .5 * puck.acceleration * dt**2\n",
"\n",
" # if we've hit the ground we deal with friction\n",
" if puck.pos.y <= 0:\n",
" # We're ignoring in the force of the collision but we'll say\n",
" # the end result of the collision will make the velocity\n",
" # in the y direction equal to zero\n",
" puck.velocity.y = 0\n",
" puck.pos.y = 0\n",
" \n",
" # Force Magnitude Due To Kinetic Friction = Normal Force * CoefficientKinetic\n",
" CoefficientOfKineticFriction = .45\n",
"\n",
" # Newton's second law: Force = Mass * Acceleration\n",
" forceGravity = puck.mass * accelerationGravity\n",
"\n",
" # Newton's third law: forces come in pairs, and these two\n",
" # forces are equal in magnitude and opposite in direction\n",
" forceNormal = -forceGravity\n",
"\n",
" # Newton's second law written as Acceleration = Force / Mass\n",
" accelerationNormal = forceNormal / puck.mass\n",
"\n",
" # Figure out the magnitude of the frictional force\n",
" forceKineticFrictionMagnitude = mag(forceNormal * CoefficientOfKineticFriction)\n",
"\n",
" # Figure out the direction of the frictional force\n",
" forceKineticFrictionDirection = norm(puck.velocity) * -1\n",
"\n",
" # Multiply them to get the frictional force vector\n",
" forceKineticFriction = forceKineticFrictionMagnitude * forceKineticFrictionDirection\n",
"\n",
" # Use the Force to find Acceleration: Acceleration = Force / Mass \n",
" accelerationKineticFriction = forceKineticFriction / puck.mass\n",
"\n",
" # Update the pucks acceleration\n",
" puck.acceleration = accelerationNormal + accelerationGravity + accelerationKineticFriction\n",
"\n",
" # End the loop when the puck stops moving\n",
" # magnitude is always positive unless it's zero\n",
" # but due to imprecision in our program it will likely\n",
" # never be zero so stop it when the velocity is close to zero\n",
" if mag(puck.velocity) <= .02:\n",
" finished = True\n",
" puck.velocity = (0,0,0)\n",
"\n",
" # show some info as we go through the loop\n",
" message = \"Vectors (Force,Acceleration,Velocity,Position) & Kinetic Friction\\n\"\n",
" message2 = \"Puck Info:\\n\"\n",
" message2 += \"position is: \" + str(puck.pos) + \"\\n\"\n",
" message3 = \"velocity is: \" + str(puck.velocity) + \"\\n\"\n",
" message3 += \"acceleration is: \" + str(puck.acceleration) + \"\\n\"\n",
" message4 = \"magnitude of velocity vector is: \" + str(mag(puck.velocity)) + \" m/s\" + \"\\n\"\n",
" message5 = \"norm of velocity vector is: \" + str(norm(puck.velocity))\n",
" mylabel.text = message\n",
" mylabel2.text = message2\n",
" mylabel3.text = message3\n",
" mylabel4.text = message4\n",
" mylabel5.text = message5\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Converted everything we can to vectors.\n",
"Eliminated excess variables.\n",
"\n",
"Important points:\n",
"\n",
"A vector is something with an x,y, and z componenet.\n",
"\n",
"The following are always vectors:\n",
"position\n",
"velocity\n",
"acceleration\n",
"force\n",
"\n",
"The magnitude of a vector gives us it's total length.\n",
"Magnitude is not a vector and it is always positive.\n",
"\n",
"The norm of a vector gives us a vector in the same direction with a\n",
"magnitude of one.\n",
"\n",
"A vector is equal to it's magnitude multiplied by it's norm:\n",
"aVector == mag(aVector) * norm(aVector)\n",
"\n",
"A vector in the opposite direction of the same length would be the\n",
"same thing multiplied by negative one:\n",
"oppositeVector = -1 * aVector\n",
"or\n",
"oppositeVector = -1 * mag(aVector) * norm(bVector)\n",
"\n"
]
},
{
"html": [
"<div id=\"scene2\"><div id=\"glowscript\" class=\"glowscript\"></div></div>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.HTML at 0x108c1ea90>"
]
},
{
"javascript": [
"window.__context = { glowscript_container: $(\"#glowscript\").removeAttr(\"id\")}"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Javascript at 0x1076796d0>"
]
},
{
"html": [
"<div id=\"glowscript2\" ><div id=\"glowscript\" class=\"glowscript\"></div></div>"
],
"metadata": {},
"output_type": "display_data"
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment