Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
183_notes:modeling_with_vpython [2014/07/17 20:49] – [Structuring your programs] caballero | 183_notes:modeling_with_vpython [2022/12/01 19:39] (current) – valen176 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | Section 1.7 and 1.11 in Matter and Interactions (4th edition) | ||
+ | |||
===== Modeling Motion with VPython ===== | ===== Modeling Motion with VPython ===== | ||
- | There is a restricted class of motion that can be modeled or explained with analytical tools (i.e., algebra and calculus). Most modern scientific research (and, indeed, engineering work) uses computational modeling as a significant part of the scientific endeavor. VPython is a Python-based programming language that allows you to create short programs that model the motion of physical systems. 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. You will develop these computational | + | There is a restricted class of motion that can be modeled or explained with analytical tools (i.e., algebra and calculus). Most modern scientific research (and, indeed, engineering work) uses computational modeling as a significant part of the scientific endeavor. VPython is a Python-based programming language that allows you to create short programs that model the motion of physical systems.** 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.** You will develop these computational |
==== Lecture Video ==== | ==== Lecture Video ==== | ||
- | {{youtube> | + | {{youtube> |
Line 12: | Line 14: | ||
Below is the code that was written in the lecture video above. There are 4 major components to this code that you will repeat in each program that you write: | Below is the code that was written in the lecture video above. There are 4 major components to this code that you will repeat in each program that you write: | ||
- | - //Objects// - Each program that you write is modeling the motion of some physical objects. So you will need to set up and place those objects in the scene. A big list of objects is [[http:// | + | - **Objects** - Each program that you write is modeling the motion of some physical objects. So you will need to set up and place those objects in the scene. A big list of objects is [[http:// |
- | - //Parameters & Initial Conditions// - Each program will have associated physical quantities for one or more of the objects in the scene. These might be the object' | + | - **Parameters & Initial Conditions** - Each program will have associated physical quantities for one or more of the objects in the scene. These might be the object' |
- | - //Time conditions// - The initial time and time step are needed in each program. The time step is particularly important because it controls how often the calculations occur. Typically, the more frequent the calculations are, the more accurate the solutions will be. But there' | + | - **Time conditions** - The initial time and time step are needed in each program. The time step is particularly important because it controls how often the calculations occur. Typically, |
- | - //Calculation loop// - Your job in mechanics is to predict or explain the motion of systems and the calculation loop is where that happens. In the loop is where the [[183_notes: | + | - **Calculation loop** - Your job in mechanics is to predict or explain the motion of systems and the calculation loop is where that happens. In the loop is where the [[183_notes: |
- | | + | |
- | | + | |
- | | + | |
- | <code python fancartModel.py> | + | Note that in this example, the cart was moving at constant velocity, so we didn't need to do much step 4 above. In future weeks, there will be examples of how to use Glowscript to model motion when there is nonzero net force. |
- | from visual import * | + | |
- | from physutil import * | + | |
- | #Set up windows | + | <code python videoexample.py> |
- | scene.width = 1024 | + | Web VPython 3.2 |
- | scene.height = 768 | + | |
- | #Objects | + | # object setup |
- | track = box(pos=vector(0,0,0), size=vector(10,0.1,1), color=color.white) | + | road = box(pos = vec(0,0,0), size = vec(10,0.5,1)) |
- | fancart | + | cart = box(pos = vec(-4,0.5,0), size = vec(1,1,0.9), color = color.red, velocity |
- | fancartMotionMap | + | |
- | #Parameters | + | # parameters and initial conditions |
- | fancart.mass = 0.3 | + | cart.velocity |
- | fancart.p = vector(0.15,0,0) | + | |
- | #time and time step | + | # time setup |
- | dt = 0.5 | + | |
t = 0 | t = 0 | ||
+ | dt = 0.01 | ||
+ | tf = 2 | ||
- | #calculation | + | # loop to do physics |
- | while t < 10: | + | while cart.pos.x |
- | + | rate(100) | |
- | rate(25) | + | |
- | + | | |
- | | + | |
- | + | ||
- | fancart.p = fancart.p + Ffan*dt | + | |
- | fancart.pos = fancart.pos + (fancart.p/ | + | |
- | | + | |
t = t + dt | t = t + dt | ||
- | | + | print(' |
</ | </ |