Professional Documents
Culture Documents
Topics
Mathematics: converting higher-order ODEs to systems of rst-order ODEs; methods for solving systems of rst-order ODEs. MATLAB: adapting programs for a solving single ODE to be able to cope with systems of ODEs; plotting solutions to systems of ODEs. Mathematics: solution of boundary value problems; eect of changing step size. MATLAB: setting up tridiagonal matrices; solving systems of linear equations.
Systems of ODEs
RK2 method
In lab 6, you used Heuns method to solve a single rst-order ODE. In this lab, you will use the same basic method to solve systems (i.e. more than one) of ODEs. Consider a simple pendulum of length l metres which pivots about the point P . We assume that friction and air resistance can be neglected. If is the angle (in radians) which the rod of the pendulum makes with the vertical at time t, then the motion of the pendulum can be described as a second-order ODE: d2 g + sin = 0, (1) dt2 l where g = 9.81 m s2 is acceleration due to gravity. Notice that this equation is valid even if the pendulum is allowed to perform complete revolutions (as in some of the questions below). On paper, rewrite equation (1) as a system of two rst-order ODEs in two independent variables y1 = and : y2 = dy1 = ... dt dy2 = ... dt The independent ( ) variable is now the row vector y = (y1 , y2 ). Write a function M-le myodes.m which calculates dy1 dy2 y dy as a function of y = (y1 , y2 ) and t. The input variable y and the output variable d dt = dt , dt dt should both be column vectors. The function should begin: function f = myodes(t, y) In lab 7, you used the function M-le myrk2.m to solve a single rst-order ODE. Now, we want to solve a system of rst-order ODEs. The only real dierence is that some of the variables are now vectors instead of scalars, and the output variable y will be a matrix instead of a vector but this doesnt matter to MATLAB. Modify myrk2.m so that it will work for systems of rst-order ODEs (refer to your lecture notes if youre stuck). Call the new M-le myrk2sys.m From now on, assume the pendulum has length l = 0.1 m. Use myrk2sys.m to solve the system of ODEs with 3 s. Find the solution up to t = 5 s. initial conditions (0) = 3 , (0) = 0 using a step size of h = 10
Some physics
(0) > 0). Suppose that you start the pendulum in its resting position (0) = 0 and give it an initial push (so that (0) have to be for the pendulum to reach the upright position = ? Hint: think about kinetic How large does and potential energy. : one that is slightly smaller than your answer to the above question and one Choose two initial values of that is slightly larger. Solve the pendulum problem up to t = 5 s (using either your own function myrk2sys or MATLABs function ode23) for both these initial conditions. Checkpoint: 2 Plot a graph of against t.
Tridiagonal matrices
Boundary value problems often result in a system of linear equations Ax = b where A is a tridiagonal (N + 1) (N + 1) matrix. Suppose 1 1 A= 4 1 1 4 .. . 1 .. . 1 .. , . 4 1 1 4 1 1 b= 1 1 0 0 1 1 . . .
Write an M-le that sets up A and b. The easiest way to do this is to dene three column vectors containing the entries of the main diagonal (N + 1 entries), subdiagonal (N entries) and superdiagonal (N entries). The ones command is useful here, although the rst and/or last entries must be treated separately: main = [1;-4*ones(N-1,1);1]; sub = ... super = ... These vectors can be combined into a tridiagonal matrix using the diag command (type help diag for help with this part): A = diag(main) + ... 2
Ta
x=0
Ta
x=L
T (0) = a,
T (L) = b
where c is a heat transfer coecient for heat loss to the surroundings, and Ta is the temperature of those surroundings. As in the lectures, we divide the rod into intervals of length h. We replace the derivative term by a central dierence approximation, and evaluate the resulting equation at each grid point. This gives a series of linear equations Ti+1 2Ti + Ti1 + c(Ta Ti ) = 0 (2) h2 for i = 1, . . . , N 1 where N is the number of steps being used. We also have the two boundary conditions T0 = a and TN = b. This gives a total of N + 1 equations in the N + 1 unknowns T0 , . . . , TN . On paper, write down the system of N + 1 linear equations in matrixvector form. The coecient matrix A should be a tridiagonal (N + 1) (N + 1) matrix. The rst and last rows of the system should correspond to the boundary conditions; the other N 1 rows correspond to equation (2). For this lab session use the following values of the parameters: L = 10 m, Ta = 0 C , a = 200 C , b = 40 C , c = 0.1 m2 .
A function le
Write a function le that solves the problem to nd the temperature values along the rod: function [x, T] % INPUTS: x = % T = % N = = rod(N) column vector of x-values along interval 0 to L column vector of temperature values at x values number of steps to use between x=0 and x=L
Hints: It may help to think in terms of the following steps: (a) Including the two endpoints, your function will use N + 1 points. Set up column vectors x and h of the apporiate size. (b) Calculate the step size h in terms of L and N . (c) Set up the tridiagonal coecient matrix A (you should be able to adapt your code from earlier). (d) Set up the right-hand side vector b. (e) Solve for the temperatures at the grid points by using the backslash operation. 3
Now solve the problem when Ta = 100 sin(x/L). Repeat the process with double the number of steps, and keep doubling until you have the values of T for x = 0, 2, . . . , 10 correct to 3 signicant gures. For what value of N do you achieve this? Checkpoint: 4 Plot your nal T values and your initial approximations from the previous checkpoint against x on a single graph. What happens to the solution as the step size is decreased? Where is the hottest point on the rod?