You are on page 1of 5

Department of Engineering Mathematics

EMAT20920: NUMERICAL METHODS WITH MATLAB WORKSHEET 6 COURSEWORK ASSESSMENT Your answers, including any M-les that you create, should be submitted electronically on SAFE https://wwwa.fen.bris.ac.uk/EMAT20920/ by 23:59:59 on Friday 20 December 2013. You should report not only numerical answers, but the MATLAB commands/functions used to obtain them. You may discuss the problems with each other, but your submission must be an individual eort. Questions 15 are worth 8 marks each, and questions 6 and 7 are worth 20 marks each. The maximum mark for this worksheet is 80 marks.

Question 1:

Matrix operations

Given a matrix x of integers, state one-line MATLAB commands that will (a) list the elements of x that are negative or zero, (b) list the values of x that are both odd, and not prime, (c) delete from x any column with a negative entry, (d) return the elements of x, excluding those in the rst column, for which the element in the rst column of x is strictly greater than 10, (e) nd the distribution of elements in x, i.e. return a matrix containing the unique elements of x and the number of occurrences of each element, (f) sort the columns of x in order of increasing maximum element in each column, (g) replace the elements of x that are more than one standard deviation ( ) away from the mean () with the nearest of , (h) return a matrix y, the same size as x, with all the elements of x sorted in descending order, reading along the rows; i.e. so that each element is less than or equal to all previous elements in the same row, and all those in preceding rows. (8 marks)

Question 2:

Interpolating polynomials

(a) Write a MATLAB function that uses the Lagrange polynomials Lk (x) =
j =k (x j =k (xk

xj ) xj )

(see slide 3.6 in the lecture notes) to nd the polynomial that interpolates datapoints (xk , yk ). (4 marks)

EMAT20920 2013-14

(b) Use your function to plot a graph of the interpolating polynomial for the following data: xk 0.00 1.00 2.00 3.00 4.00 5.00 yk 0.000000 0.346574 0.219722 0.138629 0.094673 0.068914 xk 0.25 1.25 2.25 3.25 4.25 yk 0.210017 0.316461 0.194417 0.125139 0.086989 xk 0.50 1.50 2.50 3.50 4.50 yk 0.324372 0.281936 0.172795 0.113515 0.080223 xk 0.75 1.75 2.75 3.75 4.75 yk 0.358154 0.249009 0.154366 0.103445 0.074237

(2 marks) (c) Comment on the advantages of using Lagrange polynomials, rather than the Vandermonde matrix approach (as on slide 3.4 in the lecture notes). How do you know the two methods give the same interpolating polynomial? (2 marks)

Question 3:

Arclength

(a) Write a MATLAB function that uses one of the Newton-Cotes integration rules to calculate the arclength L of a curve C in 3D space
b

L=
C

ds =
a

dr dt dt

given a suitable parametrisation of the curve r = r (t), limits for the parameter t, and a stepsize h. (4 marks) (b) Use your program to nd the length of the helix x = a sin cos1 y = a sin cos1 z=a 1 N 1 N 1 N cos sin

which wraps N times around the surface of a sphere of radius a, between the south ( = 0) and north ( = 2N ) poles, for a = 1 and N = 1, 2, 3, 4, 5. What is the length of the helix for general N ? (4 marks)

EMAT20920 2013-14

Question 4:

Numerical dierentiation

The second derivative of a function f (x) can be approximated numerically with the formula f (x) f (x + 2h) 2f (x + h) + f (x) h2

(a) Plot a graph of the error in applying this formula to calculate f (1), where f (x) = with = 0.1 and = 0.5. (4 marks) (b) Hence nd the order of the truncation and rounding errors (4 marks) 1 exp (ln x )2 2 2

x 2

Question 5:

Solving ordinary dierential equations

(a) Write a MATLAB function that can solve the ordinary dierential equation dx = f (x(t), t), dt for t [ti , tf ], implementing the following algorithm: xk+1 = xk + tk+1 where m1 = f (xk , tk ) m2 = f m3 = f xk + h h m1 , tk + 3 3 2h h xk m1 + hm2 , tk + 3 3 h (m1 + 3m2 + 3m3 + m4 ) 8 = tk + h x(ti ) = xi

m4 = f (xk + hm1 hm2 + hm3 , tk + h) (4 marks) (b) By varying h, nd the order of the (global) truncation error in calculating x(1) for the ordinary dierential equation dx 1 + 3x = t e2t , x(0) = dt 10 using the method in part (a) above. (4 marks)

EMAT20920 2013-14

Question 6:

Root-nding

(a) Write a safe root-nding program in MATLAB, that will quickly and robustly nd solutions of the equation f (x) = 0 given an interval [a, b] that contains a root. As a minimum, your function should have as inputs the function f , a bracket [a, b] and a tolerance, and should return the approximation to the root, and the number of steps taken. The routine should avoid problems of iterates diverging to innity, by for example taking a step only if the next iterate lies within the bracketing interval, and otherwise warning the user that there is a convergence problem. Ideally, the iteration should converge in a maximum of ten steps or so. Some algorithms that you may wish to consider (in addition to the methods discussed in lectures) include false position, secant, Ridders or Brents methods. Other possible, optional, features that you might choose to add to the function include:
allowing choice of relative and/or absolute error tolerances, allowing choice of a maximum number of steps, display of iterations and absolute/relative errors during the progress of the algorithm, help text explaining usage (lines starting with the comment character % immediately after the function denition are printed when you type help function name), checks on the input parameters, e.g. whether the interval is really a bracket, whether the tolerance parameters are positive, etc.

(12 marks) (b) Use your function to nd, correct to 12 signicant gures (reporting the MATLAB command you use, as well as the approximation to the root, and the number of steps taken): (i) all roots of 10, 000, 000, 000x4 1, 111, 000, 000x3 + 11, 211, 000x2 11, 110x + e|x|/1000 = 0, (ii) all roots 0 < x < 25 of 10x e10x cot(x) 11 = 0, (x 1)2 x + 3 3 = 0, (iii) all roots of |x| 2 + sin(2x) 1 (iv) the value of > 0 for which g () = 5 1/ is a maximum. e 1 (8 marks)
2

Question 7:

Nonsmooth ODEs

(a) The following ODE models the position y (t) (measured downward) of a mass m that is mounted on a spring (with stiness k ) and damper (with damping coecient c), and subject to an external force input F (t) my + cy + ky = F (t).

EMAT20920 2013-14

We shall consider a mass m1 of 75g, a spring with stiness k of 23.6kg/s2 and damping c of 5.1kg/s. After 1 second, a mass m2 of 45gis dropped onto the stationary mass m1 , so the force input is m g t<1 1 F (t) = (m + m )g t 1
1 2

(i) Using the 4th order Runge Kutta method, solve the ODE for t [0, 5], with initial conditions such that the mass is at equilibrium, with zero velocity at t = 0. Plot graphs of position y y and velocity d dt against t, and describe & explain what you see. (ii) Experiment with changing the damping, and describe any qualitative dierences you nd. (8 marks) (b) The ordinary dierential equation dy d2 y + + 2B (y ) = 4 + cos(2t), dt2 dt is a simple nondimensional model of the displacement y (t) of a system with backlash, which can occur wherever there is play between two mechanical components (e.g. in wing aps, gears, rotors, and even electrical relays). The function B (the backlash) is given by y if y > (forward contact) B (y ) = 0 . if y (freeplay) y + if y < (backward contact) The desired state of the system is y > ; any excursion into y < (rattling ) is undesirable, and can be the root cause of noise and vibration problems in the physical application. We shall consider a system with damping = 0.1, stiness = 100, half backlash width = 0.1 and forcing = 0.5. (i) Write a right-hand side function that can be used to simulate the behaviour of the system above, using ode45.
y (ii) Solve the ODE, using ode45, for t [0, 10], with initial conditions y = 0.11, d dt = 0 at y t = 0. Plot graphs of position y and velocity d dt against t for each of the initial conditions, and interpret the results.

(iii) Run the simulation for longer, to let transients die away, by changing the integration time to (say) t [0, 250], and view the nal state of the system by plotting graphs of position y and y velocity d dt against t for t > 240. (iv) Experiment with changing the initial conditions and/or the forcing , and describe any qualitative dierences in the behaviour of the system. Is there a value of for which the desired solution ceases to exist altogether? Is it possible to remove the undesirable rattling solutions? (12 marks)

EMAT20920 2013-14