Scientific Programming



Similar documents
Euler s Method and Functions

Analog and Digital Signals, Time and Frequency Representation of Signals

MATH 425, PRACTICE FINAL EXAM SOLUTIONS.

Readings this week. 1 Parametric Equations Supplement. 2 Section Sections Professor Christopher Hoffman Math 124

TMA4213/4215 Matematikk 4M/N Vår 2013

Solutions to Practice Problems for Test 4

The Heat Equation. Lectures INF2320 p. 1/88

The continuous and discrete Fourier transforms

f x a 0 n 1 a 0 a 1 cos x a 2 cos 2x a 3 cos 3x b 1 sin x b 2 sin 2x b 3 sin 3x a n cos nx b n sin nx n 1 f x dx y

Second Order Linear Partial Differential Equations. Part I

An Introduction to Partial Differential Equations

EDEXCEL NATIONAL CERTIFICATE/DIPLOMA UNIT 5 - ELECTRICAL AND ELECTRONIC PRINCIPLES NQF LEVEL 3 OUTCOME 4 - ALTERNATING CURRENT

Acceleration levels of dropped objects

N 1. (q k+1 q k ) 2 + α 3. k=0

Maximizing volume given a surface area constraint

Notice numbers may change randomly in your assignments and you may have to recalculate solutions for your specific case.

x 2 + y 2 = 1 y 1 = x 2 + 2x y = x 2 + 2x + 1

Auto-Tuning Using Fourier Coefficients

Positive Feedback and Oscillators

14.1. Basic Concepts of Integration. Introduction. Prerequisites. Learning Outcomes. Learning Style

Investigating Parametric Curves with MATLAB

Answer Key for California State Standards: Algebra I

Parabolic Trough Solar Collector Analysis

(a) We have x = 3 + 2t, y = 2 t, z = 6 so solving for t we get the symmetric equations. x 3 2. = 2 y, z = 6. t 2 2t + 1 = 0,

Chapter 8 - Power Density Spectrum

Waves Trans-X. Software Audio Processor. User s Guide

Section 4.4. Using the Fundamental Theorem. Difference Equations to Differential Equations

Unified Lecture # 4 Vectors

The Technical Archer. Austin Wargo

FIELDS-MITACS Conference. on the Mathematics of Medical Imaging. Photoacoustic and Thermoacoustic Tomography with a variable sound speed

Solving Quadratic Equations

ALGEBRA 2: 4.1 Graph Quadratic Functions in Standard Form

Chapter 15, example problems:

6.4 Logarithmic Equations and Inequalities

Section 1.1 Linear Equations: Slope and Equations of Lines

y cos 3 x dx y cos 2 x cos x dx y 1 sin 2 x cos x dx

16.2 Periodic Waves Example:

10 Polar Coordinates, Parametric Equations

MATHEMATICS FOR ENGINEERING BASIC ALGEBRA

Solutions for Review Problems

Signal Processing First Lab 01: Introduction to MATLAB. 3. Learn a little about advanced programming techniques for MATLAB, i.e., vectorization.

The one dimensional heat equation: Neumann and Robin boundary conditions

ANALYTICAL METHODS FOR ENGINEERS

Solutions to old Exam 1 problems

Appendix D Digital Modulation and GMSK

Review of Fundamental Mathematics

Practice with Proofs

Fraunhofer Diffraction

3 Contour integrals and Cauchy s Theorem

AP Physics 1 and 2 Lab Investigations

Waves. Wave Parameters. Krauss Chapter Nine

Beginner s Matlab Tutorial

MA4001 Engineering Mathematics 1 Lecture 10 Limits and Continuity

correct-choice plot f(x) and draw an approximate tangent line at x = a and use geometry to estimate its slope comment The choices were:

Structural Axial, Shear and Bending Moments

Copyright 2008 Pearson Education, Inc., publishing as Pearson Addison-Wesley.

ε: Voltage output of Signal Generator (also called the Source voltage or Applied

INTERFERENCE OF SOUND WAVES

DIODE CIRCUITS LABORATORY. Fig. 8.1a Fig 8.1b

chapter Introduction to Digital Signal Processing and Digital Filtering 1.1 Introduction 1.2 Historical Perspective

Derive 5: The Easiest... Just Got Better!

BASIC ELECTRONICS AC CIRCUIT ANALYSIS. December 2011

Introduction to Complex Numbers in Physics/Engineering

The Fourth International DERIVE-TI92/89 Conference Liverpool, U.K., July Derive 5: The Easiest... Just Got Better!

Standing Waves on a String

Lesson 11. Luis Anchordoqui. Physics 168. Tuesday, December 8, 15

AP1 Waves. (A) frequency (B) wavelength (C) speed (D) intensity. Answer: (A) and (D) frequency and intensity.

Math 120 Final Exam Practice Problems, Form: A

Oscillations. Vern Lindberg. June 10, 2010

Parallel and Perpendicular. We show a small box in one of the angles to show that the lines are perpendicular.

AP PHYSICS 2012 SCORING GUIDELINES

Find all of the real numbers x that satisfy the algebraic equation:

Waves-Wave Characteristics

Introduction to Complex Fourier Series

AC CIRCUITS - CAPACITORS AND INDUCTORS

3.2 Sources, Sinks, Saddles, and Spirals

Problem Set V Solutions

Exam 1 Practice Problems Solutions

Network Traffic Modelling

Rolle s Theorem. q( x) = 1

The Operational Amplfier Lab Guide

The Graphical Method: An Example

5.3 SOLVING TRIGONOMETRIC EQUATIONS. Copyright Cengage Learning. All rights reserved.

Absorption Coefficients and Impedance Daniel A. Russell Science and Mathematics Department, Kettering University, Flint, MI, 48504

Lab Exercise 1: Acoustic Waves

Clock Recovery in Serial-Data Systems Ransom Stephens, Ph.D.

How To Draw A Circle Of Radius 1 On A Computer (For A Computer)

Introduction to COMSOL. The Navier-Stokes Equations

AP CALCULUS AB 2008 SCORING GUIDELINES

y cos 3 x dx y cos 2 x cos x dx y 1 sin 2 x cos x dx y 1 u 2 du u 1 3u 3 C

Algebra I Vocabulary Cards

Step Response of RC Circuits

Probability and Statistics Prof. Dr. Somesh Kumar Department of Mathematics Indian Institute of Technology, Kharagpur

Frequency Response of Filters

Exercises on Oscillations and Waves

Rational Exponents. Squaring both sides of the equation yields. and to be consistent, we must have

Finding Equations of Sinusoidal Functions From Real-World Data

Transcription:

1 The wave equation Scientific Programming Wave Equation The wave equation describes how waves propagate: light waves, sound waves, oscillating strings, wave in a pond,... Suppose that the function h(x,t) gives the the height of the wave at position x and time t. Then h satisfies the differential equation: t 2 = c2 2 h x 2 (1) where c is the speed that the wave propagates. y y = h(x, t) L x Finite difference update rules Recall that the second derivative of a function can be approximated by the central difference f f(x + x) 2f(x) + f(x x) (x) The same technique can be used to approximate the second derivatives: t 2 x 2 h(x,t + t) 2h(x,t) + h(x,t t) t 2 h(x + x,t) 2h(x,t) + h(x x,t) Using these approximations, an approximation for the wave equation can be found replacing the second derivatives by these approximation. h(x,t + t) 2h(x,t) + h(x,t t) t 2 2 h(x + x,t) 2h(x,t) + h(x x,t) = c Note that one term is at time t + t, which we will refer to a future time. The rest involve time t (current time) or t t (past time). If we solve for this future time term we get: h(x,t + t) = r 2 (h(x + x,t) + h(x x,t)) + 2(1 r 2 )h(x,t) h(x,t t) (2) where r = c t x. It turns out that as long as t is chosen small enough so that r < 1/2 the simulation will be stable. Otherwise, the waves will continue to grow larger and larger. 1

Matlab code for update The update rule involves past, current and future times. Suppose each is represented by an array of length n. Each spot in the array represents the height of the array at coordinates x units apart. Index n corresponds to x = 0 and index n to x = L. for i =2:n 1 future ( i ) = r ˆ2 ( current ( i 1) + current ( i +1)) + 2 (1 r ˆ2) current ( i ) past ( i ) end This is just rewriting the mathematical update rule in Matlab. Notice that shifting x left or right x units correspond to adding or subtracting one from the index. Also, the for loop starts at 2 and ends at n 1. The reference to index i 1 would cause an error for i less than 2. Similarly, the reference to index i+1 is invalid for i greater than n 1. It is important to realize that this update rule cannot be used at the endpoints. Using array operations the for loop above can be replaced by future (2:n 1) = r ˆ2 ( current (1:n 2)+current (: n)) + 2 (1 r ˆ2) current (2:n 1) past (2:n 1) The advantage of using these array operations is that Matlab does the calculation much faster. 2 Example: Plucked string Consider a guitar string. Both ends are fixed at a height of 0. So the update rules for future(1) and future(n) can set the values to 0. This addresses the issues of the previous update rules for dealing with the boundary. Below is a complete Matlab program that simulates a plucked string. 1 dx =. 0 1 ; % Spacing of points on string 4 c = 5; % Speed of wave propagation 5 L = ; % Length of string 6 stoptime = 0; % Time to run the simulation 11 % Set current and past to the graph of a plucked string 12 current =.5.5 cos (2 pi/l [ 0 : dx :L ] ) ; 1 past = current ; 14 15 for t=0:dt : stoptime 16 % Calculate the future position of the string 1 future (1) = 0; 18 future (2:n 1) = r ˆ2 ( current (1:n 2)+current (: n)) + 2 (1 r ˆ2) current (2:n 1) past (2:n 1); 19 future (n) = 0; 20 21 % Set things up for the next time step 22 past = current ; 2 current = future ; 24 25 % Plot the graph after every th frame 26 if mod( t/dt, ) == 0 2 plot ( [ 0 : dx :L], current ) 28 axis ([0 L 2 2]) 29 pause (.001) 0 end 1 end 2

A couple of things to point out in the Matlab code. First, in lines 22-2 the current string heights are copied to the past and that future to the current. This sets things up for the next time step. In the next time step the current heights are what were the future and the past is what was the current. Also, in lines 26-1 a plot of the current string is displayed. The if statement ensures that it is only printed out every steps. This value can be changed and will affect the speed of the animation. The pause command inserts a small time delay to ensure the graph is displayed to the screen. Example: Jump rope Instead of fixing the ends of a rope, we can have them follow some function. For example, a two meter long jump rope with the left end going up and down using the function sin(5t) and the right end using the function sin(12t). Other than the starting conditions and how the left and right endpoints are handled, the code is essentially identical. 1 dx =. 0 1 ; % Spacing of points on rope 4 c = 1; % Speed of wave propagation 5 L = 2; % Length of rope 6 stoptime = 0; % Time to run the simulation 11 current = zeros (1,n ) ; 12 past = zeros (1,n ) ; 1 14 for t=0:dt : stoptime 15 % Calculate the future position of the rope 16 future (1) = sin (2 t ) ; 1 future (2:n 1) = r ˆ2 ( current (1:n 2)+current (: n)) + 2 (1 r ˆ2) current (2:n 1) past (2:n 1); 18 future (n) = sin ( t ) ; 19 20 % Set things up for the next time step 21 past = current ; 22 current = future ; 2 24 % Plot the graph after every th frame 25 if mod( t/dt, ) == 0 26 plot ( [ 0 : dx :L], current ) 2 axis ([0 L 8 8]) 28 pause (.001) 29 end 0 end

4 Example: Reflected wave In the previous two examples we specifically identified what was happening at the boundaries. This avoided the issue that equation 2 cannot be used at the boundary. We can also deal with this issue by having other types of constraints on the boundary. For example, have the wave reflect perfectly off of the boundary. It turns out a reflected wave is perpendicular to the boundary at all times. This adds the restrictions that h = 0 x x=0 h = 0 x x=l Combining these two equations with equation 1 when x = 0 gives use the following: t 2 = c 2 2 h x 2 h = 0 x x=0 Replacing the derivatives with their central difference approximations and substituting in x = 0 yields the following: h(0,t + t) 2h(0,t) + h(0,t t) 2 h( x,t) 2h(0,t) + h( x,t) t 2 = c h( x,t) h( x,t) = 0 2 x The term h( x,t) does not make sense since the x coordinate is negative. However, we can solve for this in the second equation yielding that h( x, t) = h( x, t). Plugging this into the first and solving for h(0,t + t) yields h(0,t + t) = 2r 2 h( x,t) + (1 r 2 )h(0,t) h(0,t t) This gives us an update rule to use on the left hand side when the wave is reflected. A similar argument can be used when x = L to yield h(l,t + t) = 2r 2 h(l x,t) + (1 r 2 )h(l,t) h(l,t t) where r = c t x. Below is the implementation with reflection on both sides with a pebble dropped in a meter wide pond. 1 dx =. 0 1 ; % Spacing of points on the pond 4 c = 1; % Speed of wave propagation 5 L = ; % Width of the pond 6 stoptime = 0; % Time to run the simulation 11 current = zeros (1,n ) ; 12 % Put a depression in the middle of the pond 1 current (/dx :4/ dx) =.5+.5 cos ( linspace (0,2 pi,4/dx /dx+1)); 14 past = current ; 15 16 for t=0:dt : stoptime 1 % Calculate the future position of ponds surface 4

18 future (1) = 2 r ˆ2 current (2) + 2 (1 r ˆ2) current (1) past ( 1 ) ; 19 future (2:n 1) = r ˆ2 ( current (1:n 2)+current (: n)) + 2 (1 r ˆ2) current (2:n 1) past (2:n 1); 20 future (n) = 2 r ˆ2 current (n 1) + 2 (1 r ˆ2) current (n) past (n ) ; 21 22 % Set things up for the next time step 2 past = current ; 24 current = future ; 25 26 % Plot the graph after every th frame 2 if mod( t/dt, ) == 0 28 plot ( [ 0 : dx :L], current ) 29 axis ([0 L 1 1]) 0 pause (.001) 1 end 2 end 5 Example: Wave continuing on forever In the previous example, we have perfect reflection of a wave. What if we want a wave to continue on forever? Obviously we cannot deal with an infinitely long wave; however, we can have a boundary condition that will not affect the wave whatsoever. The derivation of this is somewhat complicated, so we will not include it. But if we want to have the right end not affect the wave at all then we want h(l,t + t) = 2h(L,t) + (r 1)h(L,t t) + 2r2 (h(l x,t) h(l,t)) 1 + r where r = c t X. This update rule can be used to have a transparent boundary. Assuming that the left hand side has the value sin(t) and right side is transparent we get the following Matlab implementation: the right hand side is transparent 1 dx =. 0 1 ; % Spacing of points along the x axis 4 c = 1; % Speed of wave propagation 5 L = ; % Length of region considered 6 stoptime = 0; % Time to run the simulation 11 current = zeros (1,n ) ; 12 past = current ; 1 14 for t=0:dt : stoptime 15 % Calculate the future position of the wave 16 future (1) = sin ( t ) ; 1 future (2:n 1) = r ˆ2 ( current (1:n 2)+current (: n)) + 2 (1 r ˆ2) current (2:n 1) past (2:n 1); 18 future (n) = (2 current (n) + ( r 1) past (n) + 2 r ˆ2 ( current (n 1) current (n)))/(1+ r ) ; 19 20 % Set things up for the next time step 21 past = current ; 22 current = future ; 2 24 % Plot the graph after every th frame 25 if mod( t/dt, ) == 0 5

26 plot ( [ 0 : dx :L], current ) 2 axis ([0 L 2 2]) 28 pause (.001) 29 end 0 end Notice that when this is run, the wave moves from left to right and seems to continue past the right boundary. 6 Example: Sound wave going from a small tube into a large one Our final example demonstrates what happens when a sound traveling down a small tube enters a larger one. As the pressure wave hits the larger tube, some gets reflected and some gets transmitted. The transmitted sound is of lower amplitude since it is spread out over a wider area. This approximation only works well for narrow tubes and low frequencies. In larger tubes you need to keep track of sound reflecting off of the sides of the tubes, which we will ignore. S R T We will use three separate waves: S the source (or input wave), R the wave that is reflected back and T, the transmitted wave. Assume that the smaller tube has area, the larger A 2 and both tubes have length L. Each of the three waves are governed by the wave equation. And each will have a transparent end. The start of the source will be a 1 khz signal. And the starting point of the reflected and transmitted wave can be determined by the following formulas R(0,t + t) = T(0,t + t) = A2 (1 )S(L,t + t) S(L,t t) + R(0,t t) + A2 T(0,t t)) 1 + A2 A2 2S(L,t + t) S(L,t t) + R(0,t t) + T(0,t t) 1 + A2 This formulas are derived using acoustically laws that we will not go into. The following Matlab script implements these equations. Note that each of the three waveforms has a past, current and future version. 1 dx =. 0 1 ; % Spacing of points along the tube 2 dt =.00001; % Size of time step 4 c = 40; % Speed of sound 5 L = 1; % Length of each tube 6 stoptime =. 1 ; % Time to run the simulation 8 A1 = 5; 9 A2 = 15; 11 r = c dt/dx ; 6

12 n = L/dx + 1; 1 14 currents = zeros (1,n ) ; 15 pasts = zeros (1,n ) ; 16 currentr = zeros (1,n ) ; 1 pastr = zeros (1,n ) ; 18 currentt = zeros (1,n ) ; 19 pastt = zeros (1,n ) ; 20 21 for t=0:dt : stoptime 22 % Update the interiors and end of each using the wave equation 2 futures (2: n 1) = r ˆ2 ( currents (1:n 2)+currentS (: n)) + 2 (1 r ˆ2) currents (2: n 1) pasts (2:n 24 futures (n) = (2 currents (n) + ( r 1) pasts (n) + 2 r ˆ2 ( currents (n 1) currents (n)))/(1+ r ) ; 25 26 futurer (2: n 1) = r ˆ2 ( currentr (1:n 2)+currentR (: n)) + 2 (1 r ˆ2) currentr (2: n 1) pastr (2:n 2 futurer (n) = (2 currentr (n) + ( r 1) pastr (n) + 2 r ˆ2 ( currentr (n 1) currentr (n)))/(1+ r ) ; 28 29 futuret (2: n 1) = r ˆ2 ( currentt (1:n 2)+currentT (: n)) + 2 (1 r ˆ2) currentt (2: n 1) pastt (2:n 0 futuret (n) = (2 currentt (n) + ( r 1) pastt (n) + 2 r ˆ2 ( currentt (n 1) currentt (n)))/(1+ r ) ; 1 2 % Set the values for the start of each wave futures (1) = sin (2 pi 00 t ) ; 4 5 futurer (1) = ( (1 A2/A1) futures (n) pasts (n) + pastr (1) + A2/A1 pastt (1) )/(1+A2/A1) ; 6 futuret (1) = ( 2 futures (n) pasts (n) + pastr (1) + A2/A1 pastt (1) )/(1+A2/A1) ; 8 % Set things up for the next time step 9 pasts = currents ; 40 currents = futures ; 41 pastr = currentr ; 42 currentr = futurer ; 4 pastt = currentt ; 44 currentt = futuret ; 45 46 % Plot the graph after every 0 th frame 4 if mod( t/dt, 0) == 0 48 plot ( [ 0 : dx :L], currents, [L: dx : 0 ], currentr, [L: dx :2 L], currentt ) 49 axis ([0 2 L 2 2]) 50 legend( Source signal, Reflected signal, Transmitted signal ) 51 pause (.001) 52 end 5 end In this program c is the speed of sound (about 40 m/s) and the values for dt and dx are chosen small enough to represent a 1 kilohertz signal well.