Course Code: M106
Course Title: Introduction to MATLAB programming
Tutor Marked Assignment
Cut-Off Date: TBA Total Marks: 40
Contents
Question 1: 12 marks
Question 2: 10 marks
Question 3: 8 marks
Question 4: 10 marks
This TMA covers chapters 1, 2, 3, 4 and 5. It consists of four questions. Please solve all questions. You should give the details of your solutions and not just the final results. You must submit your Matlab code well commented as .m Matlab files. Also you must submit all figures and relevant plots as part of your work as a single file.
Plagiarism Warning:
As per AOU rules and regulations, all students are required to submit their own TMA work and avoid plagiarism. The AOU has implemented sophisticated techniques for plagiarism detection. You must provide all references in case you use and quote another person's work in your TMA. You will be penalized for any act of plagiarism as per the AOU's rules and regulations.
Declaration of No Plagiarism by Student (to be signed and submitted by student with TMA work):
I hereby declare that this submitted TMA work is a result of my own efforts and I have not plagiarized any other person's work. I have provided all references of information that I have used and quoted in my TMA work.
Name of Student:
Signature:
Date:
Question 1: [12 marks]
a) Create an array for θ from 0 to 2π with 1000 elements and generate a graph showing sin(nθ) for n=1, 2, … 5, all on a polar plot using an M-file. Include a legend and for each graph use a different line specification (colour). (4 marks)
b) In a script M-file, plot the hyperbolic sine, cosine and tangent on a figure window of 3-by-1 graphs in the range -π < θ < π. Use a different line colour for each plot and include a legend, a title and an x-label. (8 marks)
Question 2: [10 marks]
Write a program that recognizes palindromes; i.e. strings that read the same backwards as forwards. (e.g. MADAM).
The code should be organised as follows:
a) Write a getString() function that prompts the user for a string and returns it. (1 mark)
b) Write a testPalindromes() function that accepts a string then returns true if the string reads the same backwards as forwards and false otherwise. (Do NOT use loops) (5 marks)
c) Write a main script to do the following: (4 marks)
• Call getString() function to get the string from the user.
• Call testPalindromes() to recognise Palindromes.
• Based on the test result, print either ‘Palindrome string’ or ’Not Palindrome’ message.
Question 3: [8 marks]
A little school building has 10 classrooms of various sizes. Each semester the school runs several classes that must be scheduled for the rooms in the building. Write a Matlab script that, given the capacity of each room in the building and the size of a class, will attempt to find satisfactory room assignment that will accommodate the class in the building. If the class cannot be satisfactory placed, the script should print a “Room is not available” message, otherwise, a message saying that “Room number XX which has YY seats, is best for the class of size ZZ”.
Hints:
• An array indexed from 1 to 10 should be convenient structure for representing the capacities of each room in the building. The indices of the array element represent the room number as pictured below.
1 2 3 4 5 6 7 8 9 10
Room
30 50 15 40 11 25 10 40 15 120
The capacity of room 7 is 10 seats
• To find a satisfactory room assignment (Best fit room number for a given class size), the number of leftover –remaining- seats for the room when the assignment is made should be minimum, where the (leftover seats) = (room capacity) - (class size).
• The class cannot be satisfactory placed when the leftover seats in a room is negative.
• Do not use loops.
• Sample run:
Run#1:
Please enter room capacities: [12 23 44 54 23 6 65 100 212 45]
Please enter a class size: 400
Room is not available
Run#2:
Please enter room capacities: [12 23 44 54 23 6 65 100 212 45]
Please enter a class size: 44
Room number 3 which has 44 seats, is best fit the class of size 44
Run#3:
Please enter room capacities: [12 23 44 54 23 6 65 100 212 45]
Please enter a class size: 45
Room number 10 which has 45 seats, is best fit the class of size 45
Run#4:
Please enter room capacities: [12 23 44 54 23 6 65 100 212 45]
Please enter a class size: 60
Room number 7 which has 65 seats, is best fit the class of size 60
Question 4: [10 marks]
a) Write a single for statement to print the letters ‘A’ to ‘Z’ each 10 letters on a separate line. (4 marks)
b) Write a function drawBox which draws boxes on the screen. The function is called with a character (ch) as the first argument and two positive integers which represent the width and the height of the box to be drawn. It automatically draws a box with the character (ch) by the specified width and height. (Include your M-file and show a snapshot of test call). (6 marks)
For example,
drawBox(‘A’,10,6);
Prints:
AAAAAAAAAA
A A
A A
A A
A A
AAAAAAAAAA
drawBox(‘*’,20,6);
Prints:
********************
* *
* *
* *
* *
********************
“End of Questions”