You are on page 1of 9

Second Year Engineering Mathematics Laboratory Exercise 2: Eigen systems and PDEs Hilary Term

A: Eigen systems
The purpose of this rst exercise is to explore eigenvalues and eigenvectors. We begin by implementing the Rayleigh method for computing eigenvalues (and eigenvectors), then look at a couple of applications (out of the many which can be encountered in engineering). Points at which you are expected to compute and work out answers and write them in your log book are marked with a pencil... .

Preparation
Modes of vibration
Consider the four unit masses mutually connected by springs in a square as shown in gure 1. The horizontal and vertical springs have unit sti ness and the diagonals are four times as sti . Assume that zero potential is when the springs have zero extension, and that the spring extensions are small compared with their lengths. Let us assume small motions. This then permits us to assume that the angles of the springs remain approximately constant relative to one another, and that the extension of the diagonal members is given to rst order by:
x+ y p 2

where x and y are the (positive) extensions in the x and y directions (this can be readily proved by Taylor expansion). Hence show that show that the motion can be described by the di erential equation  x + Kx = 0 1

k=1

k=4 k=1 k=4 k=1

k=1

Figure 1: where
K=

3 2 1 0 2 2 0 0

2 3 0 0 2 2 0 1

1 0 3 2 0 0 2 2

0 0 2 3 0 1 2 2

2 2 0 0 3 2 1 0

2 2 0 1 2 3 0 0

0 0 2 2 1 0 3 2

0 1 2 2 0 0 2 3

and

x1 y1 x2 y2 x= x3 y3 x4 y4

with xi and yi the (small) horizontal and vertical displacements of mass i from zero potential.

Lab Work
Computing eigenvalues/eigenvectors
In lectures you have been introduced to the power method for nding the eigenvalues and eigenvectors of a symmetric matrix. You will nd an implementation of this method in the directory /packages/demo/comp/newlab2 in a le called pow.m. Copy it across to the directory which you are working in and try it out:
>> A = [ 2 2 6 6; 2 16 9 2; 6 9 8 7; 6 2 7 0 ]; >> [lambda,x] = pow(A,[1 1 1 1]',20)

Verify that the code is correct by comparing the answer to the result from matlab's built in function eig (type help eig to see how to use this function). Modify the code so that it implements the Rayleigh method instead and verify that it works for the matrix above. 2

Now use your Rayleigh code to nd the largest eigenvalue of the matrix 1:8750 B = 0:3062 0:1250

0:3062 1:2500 0:3062

0:1250 0:3062 1:8750

Try it for various starting vectors and note down what you observe about the estimated eigenvalues and particularly the eigenvectors. Explain what is going on. Now write a simple matlab routine to de ate a matrix by a given eigenvalue/eigenvector pair using Hotelling de ation. Create a function
function Anew = deflate(A, lambda, v) Anew = ???????;

in a le deflate.m. tors). Try it out with the 4 4 matrix A above to obtain all its eigenvalues (and eigenvec-

Modes of vibration
Consider again the four connected masses shown in gure 1. Use your programs from the previous section to compute the three largest eigenvalues, and the corresponding eigenvectors of the matrix K. We will now use some of the power of matlab to create an animation of these modes of vibration. Copy the les ode.m and anim.m from the usual place into your working directory. Have a look at the code in ode.m and write down in your log-book the maths which this code is implementing to solve the ODE  x = Kx. Now run it, using one of the eigenvectors of K (just computed above) as a starting point. The solution will be an oscillation whose form is determined by the eigenvector, and whose frequency is given by the square-root of the eigenvalue. The third argument to ode is a time over which to solve the equation. From the eigenvalue, determine a time which will correspond to exactly one period of the oscillation.
>> T = ??????; % time >> v = ??????; % eigenvector >> [t,y] = ode(K,0.2*v,T);

You can view the solution using a plot command


>> plot(t,y)

but rather more compelling is to create an animation. Have a look at the code in anim.m and try to understand it. Now create a movie by typing the matlab command 3

>> m = anim(t,y);

(don't forget the \;" or you will get lots of nonsense spewed out on the screen). The movie is stored in the matrix m with one frame per column. The command
>> movie(m,10)

will play the movie 10 times. Try this for various eigenvectors, and for a more general starting vector { for example, try displacing just the bottom left mass { and record your observations in your log-book.

B: Partial Di erential Equations


The purpose of this section of the lab work is to have a look at solutions to partial di erential equations by computer. You have seen in lectures how second order PDEs can be classi ed as elliptic, parabolic and hyperbolic. The prototypical equations for these type are respectively, Poisson's equation (of which Laplace's equation is a special case), the di usion equation, and the wave equation. From a computational point of view a more meaningful classi cation divides the problems into initial value (or Cauchy) problems and boundary value problems. In the former, we are interested in the time evolution of a system (typically parabolic or hyperbolic), given a set of initial conditions. In the latter, typically arising from elliptic equations, we are usually interested in nding a \static" function which satis es the equation within a region of interest, given some desired behaviour on the region boundary. You have seen examples of all three in lectures. In this lab, we concentrate on initial value problems (arising from parabolic and hyperbolic equations), and extend these from the one-dimensional versions you have seen, into two dimensions.

Preparation
2D di usion equation
The two-dimensional (unsteady) heat equation governing the di usion of heat in a twodimensional slab is given by @u K 2 = 5 u @t c where K , c and  are (respectively) the thermal conductivity, the speci c heat capacity, and the density of the material. By writing the left-hand side as a forward di erence (in time) and the right hand side as two central di erences in x and y (assuming that x = y = h), show that an explicit 4

update scheme for the time evolution of the temperature in a slab is given by
ui;j (k + 1) = r (ui
1;j (k ) + ui +1;j (k ) + ui;j 1 (k ) + ui;j +1 (k )

4ui;j (k )) + ui;j (k )

where

K t ch2 The stability criterion for this explicit scheme is that r r=

 0:25.

If we are welding

a slab of mild steel, and model the spatial dimensions at intervals of 10cm, what is the largest time step over which we can observe the evolution while maintaining stability of the iteration? For mild steel you may take K = 50, c = 450,  = 7:85 103 .

2D wave equation
The two-dimensional wave equation is given by @ 2u = c 2 52 u @t 2 By approximating both sides of the equation with appropriate nite di erences, show that an explicit update for the time evolution of the wave is given by
ui;j (k + 1) = r (ui
1;j (k ) + ui +1;j (k ) + ui;j 1 (k ) + ui;j +1 (k )

4ui;j (k )) + 2ui;j (k ) ui;j (k

1)

where
r=

and h = x = y . The highest value of r for stability is simpli es considerably.

c2 t2 h2
1 2

Show that when r =

1 2

the equation above

Show that if gi;j (0) is a central di erence approximation to the initial velocity at node (i; j ), then the rst update is given by 1 ui;j (1) = (ui 1;j (0) + ui +1;j (0) + ui;j 1 (0) + ui;j +1 (0)) + tgi;j 4

Lab Work
2D heat equation
In this exercise we look at the di usion of temperature through a slab of mild steel immediately after welding. Begin by creating a function called heatstep (of course put it in a le called heatstep.m) which will evolve the system forward by one time step. Use the code below as a guide, and ll in the missing bits from your preparation: 5

function vnew = heatstep(v,alpha,t,h) % evolve the system forward one time step % alpha: = k/(cp) where k = thermal conductivity % p = density % c = specific heat capacity % t: size of time step % h: size of grid spacing [m,n]=size(v); vnew = v; for j=2:m-1 for i=2:n-1 vnew(i,j) = ?????????; end end

What boundary conditions are implicit in this code? Now copy the le heat2d.m from the directory /ecslab/demo/comp/newlab2 into your own working directory. You are now almost in a position to try it out. Start by K de ning a parameter alpha which will equal the thermal di usivity; i.e. c for mild steel.
>> alpha = ??????;

Now assume that a spot weld has been made in the centre of a sheet of steel 2m by 2m. We will model this by a 20 by 20 grid at 18o C with an array of elements at 1600o C in the centre:
>> v0 = 18*ones(20,20); >> v0(9:12,9:12) = 1600*ones(4,4);

Call the function heat2d and observe the temperature evolution over 10 time steps. You will need rst to look at the code in heat2d.m or to type help heat2d to determine the parameter meanings.
>> heat2d(v0,alpha,??,??,10)

We can turn this into an animation by replacing a few lines in the le heat2d.m. Add two new (identical) lines, one after each of the pause statements:
m(:,i) = getframe;

and change the function header so that it returns the movie m:


function m = heat2d(v0,alpha,t,h,n)

If you now run the function with the same parameters as before, it will return an animation of the time evolution of the temperature in the slab. You can view a movie using the movie command.
>> m = heat2d(??,??,??,??,20); >> movie(m,10);

Try out a value for t which is greater than the maximum allowable for stability. Note down what you observe. Consider di erent initial conditions such as (a) the left half of the slab at 100o C and the right half at 0o C ; (b) a weld along a line from top to bottom, and note down your observations.

Two dimensional wave equation


In this exercise we model the vibration of a membrane stretched across a rectangular frame. Write a function called wavestep which implements the explicit update scheme you derived in part B of the preparation. The code below gives a template.
function unew = wavestep(u1,u0) % u1, u0: previous two time steps [m,n] = size(u1); unew = u1; for i=2:m-1 for j=2:n-1 unew(i,j) = ????????; end end

Copy the le wave2d.m from the usual place and have a look at the code. What are the initial conditions, and how does this code implement them? Try out the function wave2d (which calls your function wavestep) for a membrane stretched over a square frame occupying the region 0  x  2; 0  y  2, with the starting position of the membrane given by u = x (2 x )y (2 y ):
>> >> >> >> x = [0:0.5:2]; y = [0:0.5:2]; u0 = x'*y; for i=1:length(y) for j=1:length(x) u0(i,j) = x(i)*(2-x(i))*y(j)*(2-y(j));

end end >> uexp = wave2d(u0,20);

You can view each time step using the surf function. For example, type
>> surf(uexp(:,:,1));

to view the rst time step. If c 2 = 3, what is the maximum value of t to ensure stability? An analytic solution to this problem is given by 1 1 m2 n2 ny mx u (x; y ; t ) = + Bmn sin sin cos ct a b a2 b 2 m=1 n=1 where Bmn = and the membrane is attached to a frame 0  x  a; 0  y  b (see Kreyszig 11.8 for details). The le membranesoln.m implements a function membranesoln which computes the rst 625 fourier components to provide an approximate analytic solution from the usual place. You can obtain an estimate of the solution by typing
>> t = [0:????:????*10]; >> utrue = membranesoln(2,2,sqrt(3),x,y,t);

16a2 b2 (1  6 m3 n3

cos m)(1

cos n )

Where the \????" should be replaced by your calculation above for maximum t for stability. You can view the analystic solution for the di erent times in the array t as you did for the numerical solution:
>> surf(utrue(:,:,1));

to view the solution at t = 0. Unfortunately, our forward di erencing scheme for this membrane problem, although stable, introduces errors. We can view these by nding the di erence between the numerical and analytic solutions.
>> diff = uexp(:,:,1:10) - utrue(:,:,1:10);

Plot the maximum errors for the rst ten time steps:
>> for i=1:10 r(i) = norm(diff(:,:,i),inf); end >> plot(r);

Note down your observations in your log. It turns out that to improve the numerical accuracy of our explicit scheme we must use a much smaller quantisation of the x and y directions (i.e. make h smaller). In order to maintain stability this also means that t must decrease in proportion. What value of t maintains stability? In practice this is not a great idea, and one would be better o with an implicit method. However, just to nish o , we will increase the spatial resolution and create a couple of movies. Code to do this is provided in the usual place in a le called wave2dmov.m. Copy this to your area, then create an array u0 as you did before, but this time with h = 0:1 (i.e. x=[0:0.1:2] and y=[0:0.1:2]). Now type
>> m = wave2dmov(u0,20); >> movie(m,20);

You might also like