School of Sciences

MATH 2109 – Mathematical Computing Week 6 – Hand in Lab Class –

Due 5 : 00pm Friday 23rd April 2021.

- Write a function in an m-file for y = cos5

(x) sin3

(x) + 2 that will accept a row vector of x

values as input and output a row vector of y values. Now generate a row vector of x values in

the interval [0, 7] (use 100 points) and input this into your function to find the corresponding

y values. Plot the function using the plot() command. Also by looking up the xlabel()

and ylabel() functions label the x and y axes. - A sequence is given by a5 = 2, a6 = −4, and a7 = 1, and the recurrence relation

an = 5 an−1 − 3 an−2 + 6 an−3an−2

when n ≥ 8.

Write a function in an m-file that has n as its only input, where n > 5, and returns the value

an. Put in a check that n ≥ 5 and exit with a printed error if this is not the case, assigning

an the value NaN.

Call your function in a main program, so that it prints, using fprintf, a table of values for

n and f(n), for n = 5, 6, . . . 13. Your table should have headers. - The symbolic toolbox allows us to integrate symbolic expressions using the following commands

(as an example):

f = @(x) cos(x). ̂5.*sin(x). ̂3

syms x ;

int(f(x),x)

ans =

(cos(x) ̂5*(5*cos(x) ̂2 - 7))/35

Write a function call IntFunct in an m-file that takes as input a function of one variable

and values of a and b for x. As outputs it returns the value of the definite integral over the

interval [a, b] and (only if asked for) additionally a symbolic expression for its derivative. Also

include a check that not more than 2 outputs are requested and if they are, return a printed

error message (of your making) and cease execution before Matlab complains. Test this by

calculating the integral of y = cos5

(x) sin3

(x) over [0, 2]. You need to pass the function f

and the values a = 0 and b = 2 as parameters when you call the function from the command

window.

[Hint: Use double(subs(Sym_expression, x, a)) to get a number after substituting. ] - The intersection of a circular cylinder of unit radius, oriented around the xn axis, and the

sphere centred at the point e1 = (1, 0, . . . , 0) correspond to the points (x1, x2, . . . , xn) (in

n-dimensions) that satisfy the equations:

Write a function area_approx = Monte_Carlo_Test(n,N) that inputs the dimension n

and the number of random samples N in the following Monte-Carlo estimation of the volume

1

of the intersection. Generate N uniformly distributed random samples in the following n

dimensional box that contains the region defined in (1):

and count the number k, of the N, events that land inside the region defined in (1). Include this function as a sub-functions (place it at

the end of your work) and call it to print a table of the areas associated with the dimensions

n = 3, 4, 5, 6 using N = 106.

[Hint: Watch the week 5 Wednesday Lectorial Collaborate Ultra recording.]

NOTE : Note that M-File functions MUST NOT contain any fprintf() or disp() commands,

or echo print any values inside the function (other than for error messages). The Function MUST

return a value or values, NOT just print them with a disp() command.

You MUST include listings of your functions in your published output as follows. If your function

is contained in the M-file with name myfunction.m, just include the line

type myfunction.m

at the point in your code where you want the listing of the function to appear.