54
Adventures in 3D Modeling Using VPython ICTCM 2009 Russell Herman Gabriel Lugo UNC Wilmington Wilmington, NC http://www.enzim.hu/~szia/emanim/emanim.htm

Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Adventures in 3D Modeling Using VPython ICTCM 2009

Russell HermanGabriel Lugo

UNC WilmingtonWilmington, NC

http://www.enzim.hu/~szia/emanim/emanim.htm

Page 2: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

3D – Fast and Furious!Visual Python

Open SourcePython BasedMultiple OS

Summer Ventures in Science and Mathematics

State-wide Program14 Talented HS StudentsMath Modeling CourseFour Week Culminating in

Presentation

http://www.phy.syr.edu/~salgado/vpython/dillard/

http://bio.physics.drexel.edu/VPython.html

Page 3: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models
Page 4: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Course Web Site

http://people.uncw.edu/hermanr/SVSM.htm

First Two weeks of course:1. Modeling2. Examples3. Software Excel, Maple, Vpython, and more

Page 5: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

the process of representing a real-world phenomenon as a set of mathematical equations

FieldsNatural sciences, engineering, social sciencesMathematics, economics, and more

ClassificationsLinear vs nonlinearDeterministic vs stochasticContinuous vs discrete

What is Mathematical Modeling?

Page 6: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

TypesObservationExperimentComputationTheory

Processing SimulationVisualizationData Analysis

End ProductsPredictionVerificationNew Models

Using Computers to do science … Shodor

Methods in Science

Page 7: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

What is Needed?SoftwareNumerical Computation 3*5+16 =Symbolic Analysis (x+y)^2 = Visualization

Mathematics Algebra Trigonometry Graphing Functions Calculus Linear Algebra Differential Equations … and more

Page 8: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Examples of Models

Page 9: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Given a roll of paper find (without unrolling it) the total length of the paper on the roll. A single sheet may be provided for measuring.

Page 10: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Projectile Motion

http://ffden-2.phys.uaf.edu/211_fall2004.web.dir/Brendan_Karchere/Body%20Pages%20(2-5)/Page%203.html

Gravitation and Planetary Motion

http://www.bnsc.gov.uk/lzcontent.aspx?nid=4709 OscillationsMass-SpringPendulaNonlinear Systems

gravity.py

springmass.py

incline.py

Page 11: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

http://www.andrewclem.com/Chaos.html

http://en.wikipedia.org/wiki/Asteroid_belt

Page 12: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Fractal CoastlinesFractal MusicIterated Function SystemsXaos ExplorationMandelbrot and Julia Sets

http://home.pacbell.net/bulens/SelfSimi.htm

http://www.evl.uic.edu/aej/488/diagrams/fractal.gif

Page 13: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

http://www.sustainablescale.org/AreasofConcern/Population/PopulationandScale/QuickFacts.aspx

http://council.wisconsinforestry.org/deer/deerpop.php

transmission dynamics of communicable diseaseshttp://www.scholarpedia.org/article/Image:Animated_SIR_Epidemic.gif

sir.py

Page 14: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Rate of Change of a Quantity = Rate in – Rate outRate of Change = (Population Change)/TimeRate in = Birth rate = bPRate out = Mortality rate = dP

Page 15: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Rate of Change of a Quantity = Rate in – Rate out

P bP dP kPt

∆ = − ≡∆

1 00 1 0 0/

P P ktkP P P Pt N N

− = ⇒ = +

Let Time = [0, t] in N steps: ∆t = t/N Pn=n∆t , n=0,1,2,…,N

2

2 1 01 1kt ktP P PN N

= + = +

01N

NktP PN

= +

1 01 1N

j j Nkt ktP P P PN N−

= + ⇒ = +

As N gets large, PN is what?

0( ) ktP t P e=

Page 16: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Plot Data -MS ExcelFit given function – Regression CurveDetermine Parameters (like k)Modifying Model

LOGISTIC GROWTH

2 2P bP dP mP kP mPt

∆ = − − ≡ −∆

Page 17: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Spinning Tennis BallVibrations of Hollow Core BatsDriving Cars with High Center of GravityProjectiles with Air DragSky DivingAnd more

pinpongtable.py

pool.py

bungee.pybasketball.py

Page 18: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Separate out harmonic frequencies from signals

http://www.thayerbirding.com/dnn/v39Features/Spectrogram/tabid/756/Default.aspx

http://physics.ucsc.edu/lecturedemonstrations/waves/sound.html

Page 19: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Brownian Motion is the random movement of particles suspended in a liquid or gas or the mathematical model used to describe such random movements

plotrand.py

Page 20: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

the behavior of connected clusters in a random graphExamples

Forest FiresGalaxy FormationOil FieldsElectrical Resistance

http://ccl.northwestern.edu/netlogo/models/Percolation

percolation.py

Page 21: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

BabyloniansClassic FormulaBinary vs HexadecimalMonte Carlo

http://zoonek2.free.fr/UNIX/48_R/16.html

montecarlo.py

Happy π Day: 3-14-2009

Page 22: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

A cellular automaton is an array of identically programmed automata, or “cells”, which interact with one another.

http://vlab.infotech.monash.edu.au/resources/tutorials/cellular-automata/

Page 23: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

http://people.uncw.edu/hermanr/mat367/FCABook/Book/ComplexI.pdf

Page 24: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Modeling in VPython

Page 25: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

“3D Programming for Ordinary Mortals”http://vpython.org/

- based on Python http://www.python.org/ - David Scherer, David Andersen, Jonathan

Brandmeyer, Ruth Chabay, Ari Heitner, Ian Peters, Bruce Sherwood.

- for physics instructionEasy to learn, 3D Interactive modelingRecently updated to Version 5

Page 26: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Install Python and then VPythonRun IDLEFirst line

from visual import *Next line

sphere()Save – CTRL-S

Save as test.pyRun – F5

Page 27: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Colorsphere(color=color.red)

Radius sphere(radius=0.5,color=color.red)

Nameball = sphere(radius=0.5,color=color.red)

Positionball = sphere(pos=(0,2,0),radius=0.5,color=color.red)

Change positionball.pos = (1,2,3)

... And navigate Zoom: Hold middle button and move mouse

Or, hold both left and right buttons Rotate: Hold right button

Page 28: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

from visual import *

#Create a sphereball = sphere(pos=(0,2,0),color=color.yellow,radius=1)

#Create a boxfloor = box(length=10, height=2, width=4,color=color.blue)

Page 29: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Cartesian CoordinatesLocate points with (x,y,z)

VectorsDraw arrow from originVectors have magnitude and

direction(1,2,3)

General VectorDraw from one point to another

ExamplesPosition, velocity, force

Page 30: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Draw arrow from origin to center of ballarrow(pos=(0,0,0), axis=ball.pos)Begins at posEnds at axis

Arbitrary arrowarrow(pos=(1,2,3), axis=(0,2,-1))

Arrow attributesarrow(pos=(1,2,3), axis=(0,2,-1),

shaftwidth=1,headwidth=2,headlength=3)

Page 31: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Moving Ball – Constant Velocity

Page 32: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Goal: Make ball move and bounce off wallCreate new program

File – New WindowSave as motion.py

Create scene

Run Example

Page 33: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

VelocityVelocity = displacement/elapsed timev = ∆x/∆t

Rewrite∆x=new position – old position∆x=v∆tnew position = old position + v∆t

In Vpythonball.pos = ball.pos + ball.velocity*dt

Page 34: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Need several time stepsUse while loop

Keep executing commands over and overAdditional Information

Specify dt and velocityNeed rate – while loop executed 100 times/sec

Run Example

Page 35: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Logical TestsIf the ball moves to far to right, reverse its direction.

If statementIf ball.x > wallR.x:Then what?

Reverse directionball.velocity = -ball.velocity

Run Example

Page 36: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Assignment 1Add wallL on left at (-6,00)Add test to have ball bounce off this wall too.

Assignment 2Change ball velocity to move ball at an angle

with no z-component

Assignment 3Add a ceiling and a floor that touch vertical wallsAdd back wallAdd invisible front wall (i.e., only use if statement)Run program with ball bouncing off all walls.

Run Example

Page 37: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Uniform Motion – Adding Acceleration

Page 38: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Goal: Drop a ball that bounces on the floor.Free fall

All objects near the surface of the Earth fall with an acceleration -9.8 m/s2, or -32 ft/s2 (or a = -g).

Accelerationa = ∆v/∆t

Rewrite for VPythonnew velocity = old velocity + a ∆tball.velocity = ball.velocity + ball.acceleration*dt

Page 39: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models
Page 40: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Free fall with constant horizontal velocity.

Page 41: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

bounce.py

Page 42: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

floor = box(length=300, height=0.5, width=4, color=color.blue)ball = sphere(pos=(-90,100,0),radius=2, color=color.red)ball.velocity = vector(30,0,0)ball.trail = curve(color=color.yellow)

ball2 = sphere(pos=(-90,100,0),radius=2, color=color.red)ball2.velocity = vector(30,0,0)ball2.trail = curve(color=color.green)

ball3 = sphere(pos=(-90,100,0),radius=2, color=color.red)v = vector(30,0,0)ball3.trail = curve(color=color.blue)a = vector(0,-9.8,0)

dt = 0.01while 1: rate(100) ball.pos = ball.pos + ball.velocity*dt if ball.y < 1: ball.velocity.y = -ball.velocity.y else: ball.velocity.y = ball.velocity.y - 9.8*dt ball.trail.append(pos=ball.pos)

ball.pos = ball.pos + ball.velocity*dt vel0=ball2.velocity if ball2.y < 1: ball2.velocity.y = -ball2.velocity.y else: ball2.velocity.y = ball2.velocity.y - 9.8*dt ball2.pos = ball2.pos + (vel0+ball2.velocity)*dt/2 ball2.trail.append(pos=ball2.pos)

if ball3.y < 1: v.y= -v.y else: v += a * dt ball3.pos += v* dt + .5 * a * dt**2 ball3.trail.append(pos=ball3.pos)bounces.py

Page 43: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Mass-Spring System

Page 44: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Hooke’s Law for Springs F = -kx

Newton’s 2nd Law of Motion F = ma Or, a = F/m

Page 45: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

from visual import *

scene.range = (1,1,1)scene.center = (0,.1,0)scene.width = 800scene.height = 275scene.background = (1,1,1)

x0 = vector(.65,0,0)Surface = box(size=(2,.02,.5),pos=(0,-.1,0))wall = box(size=(.04,.5,.3),pos=(-.77,.15,0))spring = helix(pos=(-.75,0,0),axis=x0,

radius=.08,coils=6,thickness=.01,color=color.blue)block = box(pos=(0,0,0),size=(.2,.2,.2),color=color.red)

Page 46: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

k = 10m = 10b=2v = vector(0,0,0)a = vector(0,0,0)F = vector(0,0,0)

block.pos=(0.25,0,0) # Set Initial position of block and connect spring x = block.posspring.axis = x0 + x

Page 47: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

finished = False dt = .01 while not finished: rate(100) F = -k*x - b*v a = F/m v += a * dt x += v + .5 * a * dt**2

block.pos = x spring.axis = x0 + x

spring.py springmass.py

Page 48: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

What happens when the mass is changed?What happens when k is changed?Change the initial block position

i.e., block.pos=(0.25,0,0)Do you need to change other settings?

NOW – EXPLORE AND MODIFY

Page 49: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Adding More ...

LotkaVolterra.py

Page 50: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Rubik's Cube

Rubik5.exeStand Alone Application

Page 51: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Stochastic Spatial Dynamics of Epidemic Models

EpidemicLooperWallsQuad.py.py

Page 52: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

More Examples?

bounce.pygravity.py

springmass.pyspring.py

bounces.pymotion.py

motioninbox.pycouposc.py

balls.pypool.py

basket.pybasketball.py

Page 53: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

More Python

Scientific Computing with Python ...NumPySciPyMatplotlibSymPyhttp://www.pythonxy.com

...

Page 54: Adventures in 3D Modeling Using VPython ICTCM 2009russherman.com/Talks/VPython_ICTCM_2009.pdf · We demonstrate the use of Vpython for developing and analyzing mathematical models

Thank You!Further information can be found at

http://people.uncw.edu/hermanr/SVSM.htm or [email protected]

ABSTRACT

We demonstrate the use of Vpython for developing and analyzing mathematical models of real world continuous and discrete processes with minimal training and graphics programming. VPython lets students focus on computations without having to write explicit graphics statements while creating their own 3D visualizations. We will provide examples, taken mostly from a summer course for high school students, of how easy it is to simulate projectile motion, nonlinear dynamics, the spread of epidemics, percolation and cellular automata. Such simulations can be used well into applied courses in the undergraduate mathematics and science curriculum.