numerical approximation1 you have some physics equation or equations which need to be solved but:...

14
Numerical Approximation 1 Numerical Approximation You have some Physics equation or equations which need to be solved But: • You can’t or don’t want to do all that mathematics, or • The equations can not be solved What to do? Numerical Approximation

Upload: rebecca-campbell

Post on 29-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Numerical Approximation 1

Numerical Approximation

You have some Physics equation or equations which need to be solved

But:• You can’t or don’t want to do all that

mathematics, or• The equations can not be solved

What to do?

Numerical Approximation

Numerical Approximation 2

Numerical Approximation Module

Based on the Python programming language and the Visual Python package VPython

We will investigate a system that you will soon be exploring in some detail in the course: theoscillating spring-mass system

Numerical Approximation 3

About Python and VPython

An ideal 1st programming language Not a toy: used for production programs by

Google, YouTube, etc. Open source Traditionally for all languages, for beginners the

first “program” only prints:

hello, world

Numerical Approximation 4

Here is a complete Python programthat prints: hello, world

print ”hello, world”

Note the quotes

Totally intolerant of typing mistakes: this will not work

prind ”hello, world”

Case sensitive: this won’t work either

Print ”hello, world”

Numerical Approximation 5

The VPython environment

To run the program, click on Run and chooseRun Module

Here is a VPython window ready to run our first program

Numerical Approximation 6

A second window will appear:

Numerical Approximation 7

Another complete Python programthat prints hello, world

what = ”world”print ”hello,”, what

First Python executes the first line of the program

A variable named what

is given thevalue

world

Next Python executes the second line of the program: it prints hello, followed by the value of the variable what

Numerical Approximation 8

Loops

Often we wish to have a program execute the same lines over and over

Loops do this

Example:

x = 0while x < 3:

print xx = x + 1

Assign variable x a value of 0

Is x less than 3? If so, execute the following lines of program. If not, stop

Increase the value ofx by 1. Go back to thewhile statement

Numerical Approximation 9

The Spring-Mass System

The force exerted on the mass by the spring: F = -k x (Hooke’s Law) F = m a (Newton’s Second Law)

kxdt

xdmma

2

2Combine to form a

differential equation:

Numerical Approximation 10

Solving Differential Equations

kxdt

xdm

2

2 1. Learn the math, or

2. Find a mathematician, or

3. Get hold of software that can solve differential equations, such as Maple or Mathematica

If you choose #2, note that you don’t need to tell them what, if anything, the equation is about

Solving differential equations has nothing to dowith Physics!

Numerical Approximation 11

The Mathematical Solution

kxdt

xdm

2

2

)sin( tAx

m

k

You will be learning about this soon in class

Numerical Approximation 12

Avoiding all that mathematics

1. Calculate the acceleration a = - (k/m) x

2. Calculate its speed a small time t later:vnew = v + a t

3. Calculate its position a small time t later:xnew = x + vnew t

Recall: ma = -kx

At some time t we know the position x of the mass and its speed v

Go back to #1 and repeat over and over

Numerical Approximation 13

Avoiding all that mathematics continued

1. Calculate the acceleration a = - (k/m) x

2. Calculate its speed a small time t later:vnew = v + a t

3. Calculate its position a small time t later:xnew = x + vnew t

Go back to #1 and repeat over and over.

This can be made as close to correct as we desire by making the “time step” t sufficiently small

This method is “numerical approximation”

Numerical Approximation 14

What you will do today

We have prepared a VPython program that animates the mass of a spring-mass system two different ways:

1. By coding the solution to the differential equation

2. By numerical approximation• You will examine the code and identify which

parts do what