Sunteți pe pagina 1din 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 eﬀort.

Questions 1–5 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.

Question 2:

Interpolating polynomials

(8 marks)

(a) Write a MATLAB function that uses the Lagrange polynomials

L k (x) =

j

=k (x x j )

j

=k (x k x j )

(see slide 3.6 in the lecture notes) to ﬁnd the polynomial that interpolates datapoints (x k , y k ).

(4 marks)

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

L = C ds =

a

b

dr

dt

dt

given a suitable parametrisation of the curve r = r(t), limits for the parameter t, and a stepsize h.

(b)

Use your program to ﬁnd the length of the helix

x = a sin cos 1 Nπ 1 cos φ

φ

y = a sin cos 1 Nπ 1 sin φ

φ

z = a

1

φ

(4 marks)

which wraps N times around the surface of a sphere of radius a, between the south (φ = 0) and north (φ = 2) poles, for a = 1 and N = 1, 2, 3, 4, 5.

What is the length of the helix for general N ?

(4 marks)

Question 4:

Numerical diﬀerentiation

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

(a)

(b)

f (x) f(x + 2h) 2f(x + h) + f(x)

h

2

Plot a graph of the error in applying this formula to calculate f (1), where

with µ = 0.1 and σ = 0.5.

f(x) =

2π exp (ln x µ) 2 2σ 2

1

Hence ﬁnd the order of the truncation and rounding errors

(4 marks)

(4 marks)

Question 5:

Solving ordinary diﬀerential equations

(a)

Write a MATLAB function that can solve the ordinary diﬀerential equation

dx

dt

= f(x(t), t),

x(t i ) = x i

for t [t i , t f ], implementing the following algorithm:

where

x k+1 = x k + h

8

t k+1 = t k + h

(m 1 + 3m 2 + 3m 3 + m 4 )

m 1 =

f(x k , t k )

m 2 = f x k + h m 1 , t k + h

3

3

m 3 = f x k h m 1 + hm 2 , t k + 2h

3

3

m 4 = f(x k + hm 1 hm 2 + hm 3 , t k + h)

(b)

(4 marks)

By varying h, ﬁnd the order of the (global) truncation error in calculating x(1) for the ordinary diﬀerential equation

dx

dt using the method in part (a) above.

+ 3x = t e 2t ,

x(0) =

1

10

(4 marks)

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 inﬁnity, 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, Ridder’s or Brent’s 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 deﬁnition 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 signiﬁcant ﬁ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, 000x 4 1, 111, 000, 000x 3 + 11, 211, 000x 2 11, 110x + e |x|/1000 = 0,

(ii)

all roots 0 < x < 25 of 10x e 10x 2 cot(x) 11 = 0,

(iii) all roots of

(x 1) 2 x + 3

|x| 2 + sin(2x) 3 = 0,

(iv) the value of λ > 0 for which g(λ) =

1

λ 5 e 1/λ 1 is a maximum.

Question 7:

Nonsmooth ODEs

(8 marks)

(a) The following ODE models the position y(t) (measured downward) of a mass m that is mounted on a spring (with stiﬀness k) and damper (with damping coeﬃcient c), and subject to an external force input F (t)

my¨ + cy˙ + ky = F (t).

We shall consider a mass m 1 of 75g, a spring with stiﬀness k of 23.6kg/s 2 and damping c of 5.1kg/s. After 1 second, a mass m 2 of 45gis dropped onto the stationary mass m 1 , so the force input is

(i)

(ii)

F(t) =

t < 1

1

  t

(m 1 + m 2 )g

m 1 g

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

and velocity dy

dt

against t, and describe & explain what you see.

Experiment with changing the damping, and describe any qualitative diﬀerences you ﬁnd.

(b) The ordinary diﬀerential equation

d 2 y

dt 2

+ δ dy dt + 2κB(y) = 4πδ + cos(2πt),

(8 marks)

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

B(y) =

  y β

0

y + β

if y > β (forward contact)

if β y β (freeplay)

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, stiﬀness κ = 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.

at

t = 0. Plot graphs of position y and velocity dy

and interpret the results.

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

against t for each of the initial conditions,

(ii) Solve the ODE, using ode45, for t [0, 10], with initial conditions y = ±0.11, dy

dt

dt

(iii)

=

0

velocity dy

dt

against t for t > 240.

(iv)

Experiment with changing the initial conditions and/or the forcing , and describe any qual- itative diﬀerences 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)