Learning outcomes assessed

This coursework will test some concepts of Matlab programming for data analysis, including writing scripts, function, plotting, manipulating strings and using cell arrays and structure variables.

Instructions

Put your marking ID on your solutions. You can obtain your marking ID by running the command getmarkingid while logged into linux.cim.rhul.ac.uk. Please do not put your name on your solutions. Please make a copy of your solutions before submitting. Your solutions will not be returned together with the feedback.

You will submit your work electronically by means of the submission script submitCoursework which can be found on linux.cim.rhul.ac.uk. You may resubmit your script any number of times, though only the last submission will be kept. 

The submission will occur on linux.cim.rhul.ac.uk and the protocol is:

submitCoursework <your directory>
For example, assuming the directory with my solutions is assignment1 then I would submit by typing in the following command:

submitCoursework assignment1

and then follow the instructions accordingly.
The files you submit cannot be overwritten by anyone else, and they cannot be read by any other student. You can, however, overwrite your submission as often as you like, by resubmitting, though only the last version submitted will be kept. Submission after the deadline will be accepted but it will automatically be recorded as being late and is subject to College Regulations on late submissions. Please note that all your submissions will be graded anonymously.
IMPORTANT: Every exercise in this assignment will require you to write functions. You will have to submit a total of 10 files:

One file containing function outpatientreport required for exercise 1 

One file containing function caloriesaverage required for exercise 2

One file containing function displaycarmodels required for exercise 3

Two files containing functions encrypt and decrypt required for exercise 4

Three files containing function myevalueA, myevalueB and ecompare required for exercise 5

One file containing function plotfun required for exercise 6

One file containing function wordscount required for exercise 7 

Datasets myhospital.mat, cardata.mat, TheTortoiseAndTheHare.txt are provided on the course page on Moodle. On the same page you will also find templates for the functions -- you need to insert your code where indicated.
Please do not change the above file names in your submission.

All the work you submit should be solely your own work. Coursework submissions are routinely checked for this.

EXERCISE 1 (4 marks)

An outpatient hospital service stores, for every visiting patient, last name, sex, age, weight and whether the patient is a smoker or not. Create a function called outpatientreport, that will:

Load the myhospital.mat dataset containing the data (note that the data regarding patient i is found in the ith component in the arrays of myhospital.mat).

Build an array of structs containing, for each patient, his/her last name, sex, age, weight and whether the patient is a smoker or not. If displayed the array of structs would look something like this:

Provide the array of struct as an output parameter to the calling script or function.

Print the data for 10 patients taken at random, one patient per line. Each line, would contain orderly, last name, sex, age, weight and a binary value indicating whether the patient is a smoker or not. The first 2 lines of your printout would look something like this:

THOMAS Female 42 137 0
COLLINS Male 42 179 1

Draw a boxplot relating weight and smoking habit, as shown below. (you should use the Matlab function boxplot).

EXERCISE 2 (4 marks)

Write a function called caloriesaverage that will output the value of the average calorie counter. At every call, the function receives as input the amount of calories contained in an item of food. The function then returns the average amount of calories for the items entered until that moment. If the total amount calories goes above 2000, the function prints an alert message to let the user know that he/she has reached the recommended calories consumption for one day. Entering zero (0), will cause the function to reset its calories counter and start from zero again. Here is an example of calling the function for the first 3 times: 

>> caloriesaverage(0)

ans = 0

>> caloriesaverage(235)

ans = 235

>> caloriesaverage(478)

ans = 356.5


EXERCISE 3 (5 marks)

Write a function called displaycarmodels that allows the user to select a car based on simple menus. The file cardata.mat contains the following features for 406 cars: number of cylinders, model, horsepower, weight and origin. Your function will:

Load the cardata.mat dataset containing the data (note that the data regarding car i is found in the ith component in the arrays of cardata.mat).

Present the user with simple menus where he/she can select the number of cylinders for the car. The menu should look something the left hand side figure below.

Once the user has selected the number of cylinders, a second menu should pop up, displaying the models available with that number of cylinders – the menu obtained when selecting 3 cylinders is shown in the right hand side figure below.

Once the user has selected the model, the function will print the horsepower, the weight and the origin of the car(s) selected.

For example, the following call to the function:
>> displaycarmodels()
in which the user chooses 3 cylinders and the model ‘mazda rx2 coupe’, will print something like this:
Horsepower: 97
Weight (Kg): 2330
Origin: Japan

EXERCISE 4 (10 marks)

In cryptography, the intended message sometimes consists of the letters hidden in a seemingly random string.
In this exercise you will write 2 functions, one to encrypt and one to decrypt messages.
PART A – encryption Write a function encrypt(e,L,message)that will receive three input arguments: an integer e in the set {1,2}, the number of steps L and a string constituting the message to be encrypted. The function outputs the encrypted message.
• If e is 1. Create the encryption by interleaving L random characters between each pair of letters in the string to be encrypted. This is the result of a call to the function:
>> encrypted_message = encrypt( 1, 3, 'hi')
encrypted_message =
'hrywi'
If e is 2. Create the encryption by replacing each letter by the letter found L positions down the alphabet – the alphabet will cycle, going back to letter ‘a’ after letter ‘z’. For example, calling the function with L = 5 should return:
>> encrypted_message = encrypt( 2, 5, 'why')
encrypted_message =
'bmd'
PART B – decryption Write a function decrypt that receives as input arguments: the value e, the number of steps L and the encrypted message. The function will then output the original message.

If e is 1. The function should return the original message that was encrypted using the encryption algorithm with e equal to 1. This is the result of a call to the function:

>> original_message = decrypt( 1, 3, 'hrywi' )

original_message =

'hi'
If e is 2. The function should return the original message that was encrypted using the encryption algorithm with e equal to 2. This is the result of a call to the function:
>> original_message = decrypt( 2, 5, 'bmd' )
original_message =
'why'

EXERCISE 5 (7 marks)

In this exercise you will write 3 functions. The first and the second will calculate 2 different types of approximation to the number e (Euler’s number). The third function will provide a comparison between the 2 approximations. [Hint: In matlab the value of e can be obtained using exp(1)]
Part A The inverse of e can be approximated as follows:

Write a function myevalueA that receives as input a value err. The function should calculate an approximation to e (using the formula above) such that the difference between the approximation and the actual value of e is smaller than err. The function will then return the corresponding value of n. 

Part B The constant e can also be approximated by the following expression:

where