Numerical Approximation

advertisement
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
1
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: the
oscillating spring-mass system
Numerical Approximation
2
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
3
Here is a complete Python program
that 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
4
The VPython environment
Here is a VPython window ready to run our first
program
To run the program, click on Run and choose
Run Module
Numerical Approximation
5
A second window will appear:
Numerical Approximation
6
Another complete Python program
that prints hello, world
A variable is given the
named what
value
world
what = ”world”
print ”hello,”, what
First Python executes the first line of the program
Next Python executes the second line of the
program: it prints hello, followed by the value of the
variable what
Numerical Approximation
7
Loops
Often we wish to have a program execute the
same lines over and over
Loops do this
Assign variable x a value of 0
Example:
Is x less than 3?
If so, execute the
x = 0
following lines of
while x < 3:
program. If not, stop
print x
Increase the value of
x = x + 1
x by 1. Go back to the
while statement
Numerical Approximation
8
The Spring-Mass System
The force exerted on the mass by the spring:
F = -k x
(Hooke’s Law)
F=ma
(Newton’s Second Law)
Combine to form a
differential equation:
2
d x
ma  m 2  kx
dt
Numerical Approximation
9
Solving Differential Equations
1. Learn the math, or
2. Find a mathematician, or
2
d x
m 2  kx
dt
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 do
with Physics!
Numerical Approximation
10
The Mathematical Solution
x  A sin( t )
2
d x
m 2  kx
dt
k

m
You will be learning about this soon in class
Numerical Approximation
11
Avoiding all that mathematics
Recall: ma = -kx
At some time t we know the position x of the
mass and its speed v
1. Calculate the acceleration a = - (k/m) x
2. Calculate its speed a small time Dt later:
vnew = v + a Dt
3. Calculate its position a small time Dt later:
xnew = x + vnew Dt
Go back to #1 and repeat over and over
Numerical Approximation
12
Avoiding all that mathematics continued
1. Calculate the acceleration a = - (k/m) x
2. Calculate its speed a small time Dt later:
vnew = v + a Dt
3. Calculate its position a small time Dt later:
xnew = x + vnew Dt
Go back to #1 and repeat over and over.
This method is “numerical approximation”
This can be made as close to correct as we
desire by making the “time step” Dt
sufficiently small
Numerical Approximation
13
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
Numerical Approximation
14
Download