Anda di halaman 1dari 14

Numerical Approximation

You have some Physics equation or equations which need to be solved But: You cant or dont 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 or have already be exploring in some detail in the course: the oscillating spring-mass system

Numerical Approximation

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

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 wont 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

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 (Hookes Law) F=ma (Newtons Second Law) Combine to form a differential equation:

d x ma m 2 kx dt
Numerical Approximation 9

Solving Differential Equations


1. Learn the math, or
2. Find a mathematician, or

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 dont 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 )
d x m 2 kx dt
2

k m

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

Anda mungkin juga menyukai