184_notes:using_python

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
184_notes:using_python [2017/05/26 17:51] dmcpadden184_notes:using_python [2020/08/24 19:31] (current) dmcpadden
Line 1: Line 1:
 +~~NOTOC~~
 +
 +/*[[184_notes:three_principles|Previous Page: 3 Fundamental Principles of Mechanics]]
 +
 +[[184_notes:python_syntax|Next Page: Common Commands in Python]]*/
 +
 ===== Making Models with VPython ===== ===== Making Models with VPython =====
 Computers and computational modeling provide a powerful (and almost necessary) tool in modern scientific research and engineering work. While analytic (i.e., paper and pencil) solutions do exist for some problems, computational modeling allows you to create 3D visualizations, incorporate more complex behavior, model millions of particles at the same time, analyze massive amounts of data, or simply solve repetitive calculations. In addition to solving analytic problems, this course will have you working with simple computational models in VPython (a Python-based programming language that specializes in visualization). You will develop these computational models in class with the help of your classmates and the guidance of instructors. In these notes, you will read about how to write your programs so that they follow a common structure, which will make it easier to write new programs in the future.  Computers and computational modeling provide a powerful (and almost necessary) tool in modern scientific research and engineering work. While analytic (i.e., paper and pencil) solutions do exist for some problems, computational modeling allows you to create 3D visualizations, incorporate more complex behavior, model millions of particles at the same time, analyze massive amounts of data, or simply solve repetitive calculations. In addition to solving analytic problems, this course will have you working with simple computational models in VPython (a Python-based programming language that specializes in visualization). You will develop these computational models in class with the help of your classmates and the guidance of instructors. In these notes, you will read about how to write your programs so that they follow a common structure, which will make it easier to write new programs in the future. 
Line 4: Line 10:
 ==== Structuring your programs ==== ==== Structuring your programs ====
  
-There are 5 major components to this code that you will repeat in each program that you write:+There are 5 major components that you will repeat in each program that you write:
  
-  - **Objects** - Each program that you write is modeling some sort of physical objects. 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 for 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 solutions 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, [[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 chunk of code for a certain amount of time or over a certain distance (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 [[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., 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., [[183_notes:momentum_principle#net_force|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., [[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]])
  • 184_notes/using_python.1495821098.txt.gz
  • Last modified: 2017/05/26 17:51
  • by dmcpadden