Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Last revision Both sides next revision
summer_2018:vpython_modeling [2018/06/20 17:04]
tallpaul created
summer_2018:vpython_modeling [2018/06/20 17:14]
tallpaul [Example of VPython Code]
Line 1: Line 1:
-~~NOTOC~~ 
- 
 [[184_notes:​three_principles|Previous Page: 3 Fundamental Principles of Mechanics]] [[184_notes:​three_principles|Previous Page: 3 Fundamental Principles of Mechanics]]
  
Line 12: Line 10:
   - **Objects** - Each program that you write is modeling some physical object(s). In setting up your objects, you are telling the computer what shape, size, and color to make the object as well as where to put the object. A big list of objects is [[http://​vpython.org/​contents/​docs/​primitives.html|available online]]. Simplicity is key for choosing your objects; for example, a river can be modeled as a large, blue rectangle.   - **Objects** - Each program that you write is modeling some physical object(s). In setting up your objects, you are telling the computer what shape, size, and color to make the object as well as where to put the object. A big list of objects is [[http://​vpython.org/​contents/​docs/​primitives.html|available online]]. Simplicity is key for choosing your objects; for example, a river can be modeled as a large, blue rectangle.
   - **Parameters & initial conditions** - You will then need your program to associate physical quantities with one or more of the objects in the scene. These might be the object'​s mass, momentum, charge, etc. These parameters and initial conditions depend on the problem you are trying to solve and are often informed by the problem statement or analytical calculations.   - **Parameters & initial conditions** - You will then need your program to associate physical quantities with one or more of the objects in the scene. These might be the object'​s mass, momentum, charge, etc. These parameters and initial conditions depend on the problem you are trying to solve and are often informed by the problem statement or analytical calculations.
-  - **Step variable and step size** - In every program you also need to answer two questions: What variable do I want to change? And how much do I want to change it by? These are the step variable and step size. In Mechanics, the step variable is often time because you want to calculate how the object'​s position and velocity change as time changes. Another common step variable is distance because sometimes how the force or energy changes as you get farther away from the object is more important (as you will see this semester). Typically, [[183_notes:​springmotion#​modeling_motion_with_spring_forces|the smaller the step size, the more accurate the program will be]], but there'​s a tradeoff: the computer has to do more calculations -- making the program run longer. +  - **Step variable and step size** - In every program you also need to answer two questions: What variable do I want to change? And how much do I want to change it by? These are the step variable and step size. In Mechanics, the step variable is often time because you want to calculate how the object'​s position and velocity change as time changes. Another common step variable is distance because sometimes how the force or energy changes as you get farther away from the object is more important (as you will see this semester). Typically, [[http://​p3server.pa.msu.edu/​coursewiki/​doku.php?​id=183_notes:​springmotion#​modeling_motion_with_spring_forces|the smaller the step size, the more accurate the program will be]], but there'​s a tradeoff: the computer has to do more calculations -- making the program run longer. 
-  - **Calculation loop** - A loop is a programming structure that will repeat certain lines of code multiple times. You get to choose the number of repetitions based on what step variable you chose. When you have a loop like this, particularly if the loop calculates new values based on old values and a step size, it is called an [[183_notes:​iterativepredict|iterative loop or iterative process]]. In general, a calculation loop needs to:+  - **Calculation loop** - A loop is a programming structure that will repeat certain lines of code multiple times. You get to choose the number of repetitions based on what step variable you chose. When you have a loop like this, particularly if the loop calculates new values based on old values and a step size, it is called an [[http://​p3server.pa.msu.edu/​coursewiki/​doku.php?​id=183_notes:​iterativepredict|iterative loop or iterative process]]. In general, a calculation loop needs to:
     *Have a limit on how many times to run (i.e., repeat this code as long as t < 10 or d < 1)     *Have a limit on how many times to run (i.e., repeat this code as long as t < 10 or d < 1)
-    *Calculate the fundamental physical parameters (i.e., [[183_notes:​momentum_principle#net_force|calculate all the forces acting on the system, and determine the net force]]) +    *Calculate the fundamental physical parameters (i.e., [[http://​p3server.pa.msu.edu/​coursewiki/​doku.php?​id=183_notes:​momentum_principle|calculate all the forces acting on the system, and determine the net force]]) 
-    *Update any physical parameters that depend on those (i.e., [[183_notes:​motionpredict|update the momentum using that net force]] and [[183_notes:​displacement_and_velocity#​predicting_the_motion_of_objects|update the position using this new momentum]])+    *Update any physical parameters that depend on those (i.e., [[http://​p3server.pa.msu.edu/​coursewiki/​doku.php?​id=183_notes:​motionpredict|update the momentum using that net force]] and [[http://​p3server.pa.msu.edu/​coursewiki/​doku.php?​id=183_notes:​displacement_and_velocity|update the position using this new momentum]])
     *Update the step variable by adding 1 step size (i.e., move a little forward in time or move a little farther away from the object)     *Update the step variable by adding 1 step size (i.e., move a little forward in time or move a little farther away from the object)
   - **Comments** - Comments are lines of code that the computer will ignore. In VPython this is done by starting a line with a # symbol. Comments serve as notes to yourself or others about what that piece of code is supposed to do.  These are particularly useful if someone else is going to read your code or if you look back on your code at some later date. Commenting your code is a good habit to get into.   - **Comments** - Comments are lines of code that the computer will ignore. In VPython this is done by starting a line with a # symbol. Comments serve as notes to yourself or others about what that piece of code is supposed to do.  These are particularly useful if someone else is going to read your code or if you look back on your code at some later date. Commenting your code is a good habit to get into.
Line 27: Line 25:
  
 === VPython Code === === VPython Code ===
-Below is the code that was written in the lecture video above.+Below is the code that was written in the lecture video above, modified to work in Glowscript.
  
-<​code ​python fancartModel.py>+<​code>​ 
 +get_library('​https://​rawgit.com/​perlatmsu/​physutil/​master/​js/​physutil.js'​)
 from visual import * from visual import *
-from physutil import * 
- 
-#Set up windows 
-scene.width = 1024 
-scene.height = 768 
  
 #Objects #Objects
  • summer_2018/vpython_modeling.txt
  • Last modified: 2018/06/20 17:14
  • by tallpaul