Sunteți pe pagina 1din 7

Scientic Programming

Wave Equation

The 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 satises the dierential equation: 2h 2h = c2 2 (1) 2 t x where c is the speed that the wave propagates.

y y = h(x, t)

x L
Finite dierence update rules the central dierence Recall that the second derivative of a function can be approximated by

f (x + x) 2f (x) + f (x x) x2 The same technique can be used to approximate the second derivatives: f (x) 2h t2 2h x2 h(x, t + t) 2h(x, t) + h(x, t t) t2 h(x + x, t) 2h(x, t) + h(x x, t) x2

Using these approximations, an approximation for the wave equation can be found replacing the second derivatives by these approximation. h(x + x, t) 2h(x, t) + h(x x, t) h(x, t + t) 2h(x, t) + h(x, t t) = c2 2 t x2 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) = r2 (h(x + x, t) + h(x x, t)) + 2(1 r2 )h(x, t) h(x, t t) (2)

t where r = c 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.

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.
f o r i =2:n1 f u t u r e ( i ) = r 2 ( c u r r e n t ( i 1) + c u r r e n t ( i +1)) + 2 (1 r 2 ) c u r r e n t ( i ) p a s t ( 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 n1. 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 n1. 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
f u t u r e ( 2 : n 1) = r 2 ( c u r r e n t ( 1 : n2)+ c u r r e n t ( 3 : n ) ) + 2 (1 r 2 ) c u r r e n t ( 2 : n 1) p a s t ( 2 : n 1)

The advantage of using these array operations is that Matlab does the calculation much faster.

Example: Plucked string

Consider a guitar string. Both ends are xed 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 ; % S p a c i n g o f p o i n t s on s t r i n g 2 dt = . 0 0 1 ; % S i z e o f time s t e p 3 4 c = 5; % Speed o f wave p r o p a g a t i o n 5 L = 10; % Length o f s t r i n g 6 stopTime = 3 0 ; % Time t o run t h e s i m u l a t i o n 7 8 r = c dt /dx ; 9 n = L/dx + 1 ; 10 11 % S e t c u r r e n t and p a s t t o t h e gr a p h o f a p l u c k e d s t r i n g 12 c u r r e n t = .5 .5 cos ( 2 pi /L [ 0 : dx : L ] ) ; 13 p a s t = c u r r e n t ; 14 15 f o r t =0: dt : stopTime 16 % Calculate the future position of the string 17 future (1) = 0; 18 f u t u r e ( 2 : n 1) = r 2 ( c u r r e n t ( 1 : n2)+ c u r r e n t ( 3 : n ) ) + 2 (1 r 2 ) c u r r e n t ( 2 : n 1) p a s t ( 2 : n 1); 19 future (n) = 0; 20 21 % S e t t h i n g s up f o r t h e n e x t time s t e p 22 past = current ; 23 current = future ; 24 25 % P l o t t h e gr a p h a f t e r e v e r y 10 t h frame 26 i f mod( t / dt , 1 0 ) == 0 27 plot ( [ 0 : dx : L ] , c u r r e n t ) 28 axis ( [ 0 L 2 2 ] ) 29 pause ( . 0 0 1 ) 30 end 31 end

A couple of things to point out in the Matlab code. First, in lines 22-23 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-31 a plot of the current string is displayed. The if statement ensures that it is only printed out every 10 steps. This value can be changed and will aect 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 xing 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 dx = . 0 1 ; dt = . 0 0 1 ; c = 1; L = 2; stopTime = 3 0 ; r = c dt /dx ; n = L/dx + 1 ; c u r r e n t = zeros ( 1 , n ) ; p a s t = zeros ( 1 , n ) ; f o r t =0: dt : stopTime % C a l c u l a t e the f u t u r e p o s i t i o n of the rope f u t u r e ( 1 ) = sin ( 2 t ) ; f u t u r e ( 2 : n 1) = r 2 ( c u r r e n t ( 1 : n2)+ c u r r e n t ( 3 : n ) ) + 2 (1 r 2 ) c u r r e n t ( 2 : n 1) p a s t ( 2 : n 1); f u t u r e ( n ) = sin ( 3 t ) ; % S e t t h i n g s up f o r t h e n e x t time s t e p past = current ; current = future ; % P l o t t h e gr a p h a f t e r e v e r y 10 t h frame i f mod( t / dt , 1 0 ) == 0 plot ( [ 0 : dx : L ] , c u r r e n t ) axis ( [ 0 L 8 8 ] ) pause ( . 0 0 1 ) end end % S p a c i n g o f p o i n t s on r o p e % S i z e o f time s t e p % Speed o f wave p r o p a g a t i o n % Length o f r o p e % Time t o run t h e s i m u l a t i o n

Example: Reected wave

In the previous two examples we specically identied 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 reect perfectly o of the boundary. It turns out a reected wave is perpendicular to the boundary at all times. This adds the restrictions that h x h x = = 0 0

x=0

x=L

Combining these two equations with equation 1 when x = 0 gives use the following: 2h t2 h x
x=0

= c2 = 0

2h x2

Replacing the derivatives with their central dierence approximations and substituting in x = 0 yields the following: h(0, t + t) 2h(0, t) + h(0, t t) t2 h(x, t) h(x, t) 2x = c2 = 0 h(x, t) 2h(0, t) + h(x, t) x2

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 rst and solving for h(0, t + t) yields h(0, t + t) = 2r2 h(x, t) + (1 r2 )h(0, t) h(0, t t) This gives us an update rule to use on the left hand side when the wave is reected. A similar argument can be used when x = L to yield h(L, t + t) = 2r2 h(L x, t) + (1 r2 )h(L, t) h(L, t t)
t where r = c x . Below is the implementation with reection on both sides with a pebble dropped in a 10 meter wide pond.

1 dx = . 0 1 ; % S p a c i n g o f p o i n t s on t h e pond 2 dt = . 0 0 1 ; % S i z e o f time s t e p 3 4 c = 1; % Speed o f wave p r o p a g a t i o n 5 L = 10; % Width o f t h e pond 6 stopTime = 3 0 ; % Time t o run t h e s i m u l a t i o n 7 8 r = c dt /dx ; 9 n = L/dx + 1 ; 10 11 c u r r e n t = zeros ( 1 , n ) ; 12 % Put a d e p r e s s i o n i n t h e m i d d l e o f t h e pond 13 c u r r e n t ( 3 / dx : 4 / dx ) = .5+.5 cos ( linspace ( 0 , 2 pi , 4 / dx 3/dx + 1 ) ) ; 14 p a s t = c u r r e n t ; 15 16 f o r t =0: dt : stopTime 17 % C a l c u l a t e t h e f u t u r e p o s i t i o n o f ponds s u r f a c e

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

f u t u r e ( 1 ) = 2 r 2 c u r r e n t ( 2 ) + 2 (1 r 2 ) c u r r e n t ( 1 ) p a s t ( 1 ) ; f u t u r e ( 2 : n 1) = r 2 ( c u r r e n t ( 1 : n2)+ c u r r e n t ( 3 : n ) ) + 2 (1 r 2 ) c u r r e n t ( 2 : n 1) p a s t ( 2 : n 1); f u t u r e ( n ) = 2 r 2 c u r r e n t ( n 1) + 2 (1 r 2 ) c u r r e n t ( n ) p a s t ( n ) ; % S e t t h i n g s up f o r t h e n e x t time s t e p past = current ; current = future ; % P l o t t h e gr a p h a f t e r e v e r y 10 t h frame i f mod( t / dt , 1 0 ) == 0 plot ( [ 0 : dx : L ] , c u r r e n t ) axis ( [ 0 L 1 1 ] ) pause ( . 0 0 1 ) end end

Example: Wave continuing on forever

In the previous example, we have perfect reection of a wave. What if we want a wave to continue on forever? Obviously we cannot deal with an innitely long wave; however, we can have a boundary condition that will not aect 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 aect 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

c t where r = 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

dx = . 0 1 ; dt = . 0 0 1 ; c = 1; L = 10; stopTime = 3 0 ; r = c dt /dx ; n = L/dx + 1 ;

% S p a c i n g o f p o i n t s a l o n g t h e x a x i s % S i z e o f time s t e p % Speed o f wave p r o p a g a t i o n % Length o f r e g i o n c o n s i d e r e d % Time t o run t h e s i m u l a t i o n

c u r r e n t = zeros ( 1 , n ) ; past = current ; f o r t =0: dt : stopTime % C a l c u l a t e t h e f u t u r e p o s i t i o n o f t h e wave f u t u r e ( 1 ) = sin ( t ) ; f u t u r e ( 2 : n 1) = r 2 ( c u r r e n t ( 1 : n2)+ c u r r e n t ( 3 : n ) ) + 2 (1 r 2 ) c u r r e n t ( 2 : n 1) p a s t ( 2 : n 1); f u t u r e ( n ) = ( 2 c u r r e n t ( n ) + ( r 1) p a s t ( n ) + 2 r 2 ( c u r r e n t ( n1) c u r r e n t ( n ) ) ) / ( 1 + r ) ; % S e t t h i n g s up f o r t h e n e x t time s t e p past = current ; current = future ; % P l o t t h e gr a p h a f t e r e v e r y 10 t h frame i f mod( t / dt , 1 0 ) == 0

26 27 28 29 30

plot ( [ 0 : dx : L ] , c u r r e n t ) axis ( [ 0 L 2 2 ] ) pause ( . 0 0 1 ) end end

Notice that when this is run, the wave moves from left to right and seems to continue past the right boundary.

Example: Sound wave going from a small tube into a large one

Our nal 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 reected 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 reecting o of the sides of the tubes, which we will ignore.

S R

We will use three separate waves: S the source (or input wave), R the wave that is reected back and T , the transmitted wave. Assume that the smaller tube has area A1 , the larger A2 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 reected and transmitted wave can be determined by the following formulas R(0, t + t) = T (0, t + t) = (1
A2 A1 )S (L, t

+ t) S (L, t t) + R(0, t t) + 1+ 1+
A2 A1 A2 A1

A2 A1 T (0, t

t))

2S (L, t + t) S (L, t t) + R(0, t t) +

A2 A1 T (0, t

t)

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 2 3 4 5 6 7 8 9 10 11 dx = . 0 1 ; dt = . 0 0 0 0 1 ; c = 340; L = 1; stopTime = . 1 ; A1 = 5 ; A2 = 1 5 ; r = c dt /dx ; % Spacing of p o i n t s along the tube % S i z e o f time s t e p % Speed o f sound % Length o f each t u b e % Time t o run t h e s i m u l a t i o n

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

n = L/dx + 1 ; c u r r e n t S = zeros ( 1 , n ) ; p a s t S = zeros ( 1 , n ) ; c u r r e n t R = zeros ( 1 , n ) ; pastR = zeros ( 1 , n ) ; c u r r e n t T = zeros ( 1 , n ) ; pastT = zeros ( 1 , n ) ;

f o r t =0: dt : stopTime % Update t h e i n t e r i o r s and end o f each u s i n g t h e wave e q u a t i o n f u t u r e S ( 2 : n 1) = r 2 ( c u r r e n t S ( 1 : n2)+ c u r r e n t S ( 3 : n ) ) + 2 (1 r 2 ) c u r r e n t S ( 2 : n 1) p a s t S ( 2 : n f u t u r e S ( n ) = ( 2 c u r r e n t S ( n ) + ( r 1) p a s t S ( n ) + 2 r 2 ( c u r r e n t S ( n1) c u r r e n t S ( n ) ) ) / ( 1 + r ) ;

f u t u r e R ( 2 : n 1) = r 2 ( c u r r e n t R ( 1 : n2)+c u r r e n t R ( 3 : n ) ) + 2 (1 r 2 ) c u r r e n t R ( 2 : n 1) pastR ( 2 : n f u t u r e R ( n ) = ( 2 c u r r e n t R ( n ) + ( r 1) pastR ( n ) + 2 r 2 ( c u r r e n t R ( n1) c u r r e n t R ( n ) ) ) / ( 1 + r ) ;

f u t u r e T ( 2 : n 1) = r 2 ( c u r r e n t T ( 1 : n2)+c u r r e n t T ( 3 : n ) ) + 2 (1 r 2 ) c u r r e n t T ( 2 : n 1) pastT ( 2 : n f u t u r e T ( n ) = ( 2 c u r r e n t T ( n ) + ( r 1) pastT ( n ) + 2 r 2 ( c u r r e n t T ( n1) c u r r e n t T ( n ) ) ) / ( 1 + r ) ; % S e t t h e v a l u e s f o r t h e s t a r t o f each wave f u t u r e S ( 1 ) = sin ( 2 pi 1000 t ) ; f u t u r e R ( 1 ) = ( (1 A2/A1) f u t u r e S ( n ) p a s t S ( n ) + pastR ( 1 ) + A2/A1 pastT ( 1 ) )/(1+A2/A1 ) ; f u t u r e T ( 1 ) = ( 2 f u t u r e S ( n ) p a s t S ( n ) + pastR ( 1 ) + A2/A1 pastT ( 1 ) )/(1+A2/A1 ) ; % S e t t h i n g s up f o r t h e n e x t time s t e p pastS = currentS ; currentS = futureS ; pastR = c u r r e n t R ; currentR = futureR ; pastT = c u r r e n t T ; currentT = futureT ; % P l o t t h e gr a p h a f t e r e v e r y 100 t h frame i f mod( t / dt , 1 0 0 ) == 0 plot ( [ 0 : dx : L ] , c u r r e n t S , [ L: dx : 0 ] , currentR , [ L : dx : 2 L ] , c u r r e n t T ) axis ( [ 0 2 L 2 2 ] ) legend ( S o u r c e s i g n a l , R e f l e c t e d s i g n a l , Transmitted s i g n a l ) pause ( . 0 0 1 ) end end

In this program c is the speed of sound (about 340 m/s) and the values for dt and dx are chosen small enough to represent a 1 kilohertz signal well.

S-ar putea să vă placă și