- QQ：99515681
- 郵箱：[email protected]
- 工作時間：8:00-23:00
- 微信：codinghelp

MA1MSP: Mathematical and Statistical Programming

Assignment 4

This assignment counts to 35% of the total marks of this module. Full marks of this

assignment can be gained from complete answers to ALL questions and submitting the

MATLAB files to me by email ([email protected]) before 12pm (noon) 5th

June, 2019 (Wednesday).

Note:

For Questions 1, 2, you need to write a function named ‘MSP Asgn4 Qx StudentID.m’, where

’x’ should be the question number (namely one out of 1, 2) and ’StudentID’ should be your

Student ID number.

For Questions 3, 4 and 5, you will need to develop two codes, i.e. one function named

‘MSP Asgn4 Qxa StudentID.m’ and one script named ‘MSP Asgn4 Qxb StudentID.m’, respectively,

where ‘x’ should be the question number (namely one out of 3, 4 and 5) and ‘StudentID’

should be your Student ID number.

At the beginning of each file, add a comment line to state ‘This is the script/function for Q.x

of Assignment 4’ where ’x’ is the question number.

For each script (not function), add two command lines “clear all” and “close all” right below

the first comment line.

If no script or function file is uploaded then a mark of zero will be recorded for that part of the

question.

Your code should not output anything to the command window unless explicitly stated, except for

error messages. If your code does output anything that is not asked for, marks will be deducted.

By submitting your work you are confirming that the code is your own, independent work.

1. Consider a bank deposit where the interest is added to the deposit at the end of each year. The

amount collected after n years can be computed as

P = D(1 + I/100)n

,

where P is the future value, D is the initial deposit amount and I is the interest rate in percent.

We consider three different banks A, B and C.

Bank A pays the client an interest rate of 1.5%;

Bank B pays the client as follows:

1% rate if the initial amount D is less than or equal to ￡20,000,

1.5% rate if D is more than ￡20,000, but less than or equal to ￡40,000,

2.ab% rate for D greater than ￡40,000, where ab are the last two digits of your student

number.

Bank C pays the client an interest rate of 2% for the first three years, and then the interest

rate decreases to 1%. Note that the interest is always computed from the full balance,

which includes the interest accumulated over the previous years.

1

Write a function ’MSP Asgn4 Q1 StudentID.m’ that requires three inputs: the initial amount

deposited D, the number of years n, and a single character that identifies which bank is being

considered, and returns one output as the future value of the deposit P.

[7.5 marks]

2. If A is an n × m matrix and B is an m × p matrix, the matrix product C = AB is the n × p

matrix defined by the equation

Write a function named ’MSP Asgn4 Q2 StudentID.m’ that takes two matrices, A of dimensions

n × m and B of dimension m × p, as inputs and returns their product matrix C, which

should fulfill the following tasks:

(a) Before performing the multiplication, the function should test if the number of columns in

A equals to the number of rows in B, and display an error message if this condition is not

satisfied;

(b) The calculation of C must be accomplished by using triple nested for loops, but NOT

using any built-in MATLAB matrix multiplication function or operator;

(c) After the calculation, the function should check if the generated matrix C is of dimensions

n × p. If yes, it displays a message ‘The dimensions of C are correct!’; Otherwise, outputs

an error message ‘The dimensions of C are wrong!’.

[7.5 marks]

3. (a) Write a function named ’MSP Asgn4 Q3a StudentID.m’ that takes as input a vector x

(including scalar) and returns the vector (including scalar)

y = aex/b cos(2x)

where the constants a, b are given by the last two digits of your student number ab. If any

of these digits are equal to zero, then replace them with 2.

[3 marks]

(b) Write a script named ’MSP Asgn4 Q3b StudentID.m’ that

i. plots the function y coded in Part (a) in the interval x ∈ [0, 5π] using a solid blue line

with linewidth 2.

ii. uses function handles to calculate the zeros of the function coded in Part (a) that are

close to the values nπ/2 with n = 0, 1, . . . , 10 and save them to a vector. Plot the

points corresponding to the zeros you have saved as red diamond symbols in the same

plot of Part (b)i.

iii. Add approximate title, legends and axis labels to the above plot.

[4.5 marks]

4. Let f(x) be defined on the interval [a, b] and let the interval be divided into n+ 1 equally spaced

points x0, x1, . . . , xn, where x0 = a and xn = b. Then the Trapezoidal rule for approximating

2

the definite integral of f over the interval is as follows:

[f(x0) + 2f(x1) + 2f(x2) + . . . + 2f(xn?1) + f(xn)] .

(a) Write a function named ’MSP Asgn4 Q4a StudentID.m’ which takes three inputs: the

lower limit a, the upper limit b and the number of sub-intervals n, and uses the Trapezoidal

rule to approximate the integral

where p is the last digit of your student number, and outputs the calculated approximate

integration value.

(Note that you must NOT use any of the MATLAB built-in functions for calculating integrals

or applying the Trapezoidal rule.)

[3 marks]

(b) Write a script ’MSP Asgn4 Q4b StudentID.m’ that uses the function coded in Part (a) to

approximate the integral values of y for a fixed interval [π/2, π] but different numbers of

sub-intervals n = 100, 200, 300, . . . , 1600. Plot the approximate results of y (y-axis) vs n

(x-axis) as cross symbols linked by dashed blue line. Add appropriate title and axis labels

to the plot.

[2 marks]

5. (a) We say two matrices A, B are similar if there exists an invertible matrix X such that

AX = XB. Write a function named ‘MSP Asgn4 Q5a StudentID.m’ which will take three

inputs: a matrix X and two matrices A, B. The function will then

– first test if X is invertible. If X is not invertible, the function will return the error

message, ‘Error: matrix must be invertible.’

– If X is invertible the function will test if A, B are similar using the given matrix X.

If A, B are similar, the function outputs the single number 1. If A, B are not similar,

it outputs the single number 0.

You must also include some help documentation for the user.

[5 marks]

(b) Write a script named ‘MSP Asgn4 Q5b StudentID.m’ which will test the function from

part (a) for the following matrices:

版權所有：編程輔導網 2018 All Rights Reserved 聯系方式：QQ:99515681 電子信箱：[email protected]

免責聲明：本站部分內容從網絡整理而來，只供參考！如有版權問題可聯系本站刪除。