Anda di halaman 1dari 7

MATLAB Exercise Set 1

FEB22012(X)

September 4, 2017

Making the assignment

You should make this assignment individually. You are allowed and encouraged to discuss ideas
necessary to solve the assignment with your peers, but you should write the code by yourself. We
will employ fraud checking software specifically created for programming classes such as this.
In case we suspect fraud, we can invite you to explain your code. Due to university policy cases of
(suspected) fraud must be reported to the exam committee. The exam committee will then decide
on possible disciplinary actions.
The deadline for this assignment is Wednesday, 13th of September 2017, 21:00, CEST.

Grade and Scores

The grade of your assignment will depend on two important criteria:


1. Correctness: does your code work according to the specification in the assignment?
2. Code Quality: is your code well written and understandable?
For this assignment you can earn 10 points in total and counts for 10% towards the final grade of
the course. Specific parts of the assignment count as follows:
Part of Assignment Points
Sum Divisors 1
Predator-Prey Simulation 1
Sieve of Sundaram 2
Optimal Buy and Sell Times 2
Credit Card Check 2
Code Quality 2
Total 10
When we ask you to implement a function using a specific algorithm, you will get no points if you
use a different algorithm. Your are also not allowed to use functions from MATLAB Toolboxes as
these are not available in Autolab.

1
Handing in your assignment

You need to hand in your assignments via Autolab. If you hand in an exercise more than 35
times, you wil l get a strike. After three strikes, you can only obtain 75% of the correctness
score for subsequent exercises you hand in more than 35 times.
When Autolab tells you that your assignment is not working correctly, i.e. you get less than
100 points, you will get 0 points for that assignment towards your grade. Your code should
produce the correct answer in all situations, not just in some.
The scores generated by Autolab only give an indication of your possible grade. Final grades
will only be given after your code has been checked manually.
Deliberate attempts to trick Autolab into accepting answers that are clearly not a solution to
the assignments will be considered fraud.

Code Quality

Part of your grade will also depend on the quality of your code.
Your code should be well structured with sensible methods and parameters.
Give clear, understandable names to methods and variables. Something like cost should be
preferred to c.
Use consistent layout, formatting and indent style for your code.
Write comments where appropriate. In MATLAB, functions should be documented. Directly
after the function signature you should write the name of the function in upper case, with a
short description of the function. The second line of the comment should contain the function
signature. Please look at the lecture slides for an example.
Try to keep code as clean and elegant as possible.
Avoid magic numbers, define constants.

General Remarks

For many operations, such as summing up the elements in a matrix or vector, you can either
write a loop or use a standard function in MATLAB that does it for you. Clever use of such
functions can save writing a lot of code.
The last two exercises this week are quite challenging. Please be aware that these are also the
final exercises of the MATLAB module of the course and resemble some of the data processing
problems you may run into when you need to program for a thesis project.
The Windows version of Octave can be very slow and unresponsive the first time you call a
plotting function. If you wait some time (up to a minute), the plot will become visible and
subsequent plotting calls will hapenn very fast.

2
Sum Divisors MATLAB Exercise Set 1 August 17, 2016

Sum Divisors Part 1

In this introductory exercise, we want to find the sum of all positive numbers below 500 that
are divisible by 3 or 7. Use for and if to find your answer.
You dont have to hand in this exercise.

Sum Divisors Part 2

In this exercise we will generalize the previous problem. Write a function with the following
signature:

function [res] = sum divisors(bound, divs)

This function takes a given bound and a vector of dividers. The goal is similar to Part 1 of this
exercise: write a function which sums up all positive numbers smaller than the bound that are
divisible by any of the numbers in divs. You can use part 1 to test your function.
Hint: Pay close attention to the precise wording of this exercise. Additionally, make sure that
your code works for edge cases as well.
This exercise must be handed in through Autolab. More information about handing in your
exercise through Autolab can be found on Blackboard.
Predator-Prey MATLAB Exercise Set 1 August 17, 2016

Predator-Prey Simulation

In a predator-prey simulation, we will compute the populations of predators and prey, using
the following equations:

Yn+1 = Yn (1 + a b Dn )
Dn+1 = Dn (1 c + d Yn )

Here Yn is the population of prey at time n, and Dn is the population of predators at time n.
Furthermore, a is the rate at which prey birth exceeds natural death, b is the rate of predation,
c is the rate at which predator deaths exceed births without food, and d represents predator
increase in the presence of food.
Write a MATLAB program using the following signature:

function [pred, prey] = predator prey(pred0, prey0, a, b, c, d, periods)

Here, pred0 and prey0 are the initial populations of predators and prey respectively, a, b, c,
and d are the rates described above, and periods is the number of periods in the simulation.
The output of the function should give the populations of predators and prey after the provided
number of periods has passed. In this exercise, populations can be considered to be continuous
quantities and you dont need to worry about them becoming negative.
This exercise must be handed in through Autolab. More information about handing in your
exercise through Autolab can be found on Blackboard.
Sundaram MATLAB Exercise Set 1 August 17, 2016

Sieve of Sundaram

Like the sieve of Eratosthenes, the sieve of Sundaram is a deterministic algorithm for finding a
list of prime numbers. The algorithm can be described as follows. Starting from a list of integers
from 1 to n, remove all the numbers of the form i + j + 2ij subject to the conditions:
1. i, j N
2. 1 i j
3. i + j + 2ij n
Finally, double each number and increment each by 1. The resulting list is a list of primes below
2n + 2, except the number 2. Write a MATLAB function with the following signature:

function [result] = sundaram(n)

This function should return a vector of all primes below 2n + 2.


Hint: Recall that row vectors can be concatenated in Matlab by using square brackets. For
instance, if a and b are vectors, then [a b] joins them together.
This exercise must be handed in through Autolab. More information about handing in your
exercise through Autolab can be found on Blackboard.
Buy and Sell MATLAB Exercise Set 1 August 17, 2016

Optimal Buy and Sell times

Imagine youre currently working on Wall Street and your boss asks you to analyze some historic
pricing data to figure out when would have been the best time to buy a certain stock and when
would have been the best time to sell the stock.
You are given a vector of data with historic prices and your job is to write a MATLAB function
which:
Gives the index of the price vector at which to buy the stock
Gives the index of the price vector at which to sell the stock
Gives the profit that could have been obtained
You may assume that there are no transaction costs, that you cant buy and sell the stock in the
same period, and that time is linear so you must buy a stock before selling it. Your MATLAB
function must have the following signature:

function [buy, sell, profit] = optimal buysell(prices)

Hint: Note that you must buy and sell once, even if this would result in a loss. In that case, the
goal is to minimize the loss that is made.
This exercise must be handed in through Autolab. More information about handing in your
exercise through Autolab can be found on Blackboard.
Credit Card Check MATLAB Exercise Set 1 August 17, 2016

Credit Card Check

In this exercise we will write a function which checks whether or not a given credit card number
is valid.
The last digit of a credit card number is the check digit, which protects against transcription
errors such as an error in a single digit or switching two adjacent digits. The following method
is used to verify actual credit card numbers but, for simplicity, we will describe it for numbers
with 8 digits instead of 16:
Starting from the rightmost digit, form the sum of every other digit. For example, if the
credit card number is 4358 9795, then you form the sum 5 + 7 + 8 + 3 = 23
Double each of the digits that were not included in the preceding step. Add all digits of
the resulting numbers. For example, with the number given above, doubling the digits,
starting with the next-to-last one, yields 18 18 10 8. Adding all digits in these values
yields 1 + 8 + 1 + 8 + 1 + 0 + 8 = 27.
Add the sum of the two preceding steps. If the last digit of the result is 0, the number is
valid. In our case, 23 + 27 = 50, so the number is valid.
Write a MATLAB program that implements the algorithm described above, using the signa-
ture:

1 function [result] = creditcard check(number)

This function should take a number as input and check if the credit card number is valid, if it is
the value of result should equal the last digit of the credit card number. If the number is not
valid, result should equal the last digit of the given number such that it becomes valid.
Hint 1: Your program may contain multiple functions, as long as the main function corresponds
to the name of the file. It can help to first create a function that converts a number to a vector
of its digits. Hint 2: Ensure that your code works with credit card numbers of any length, not
just those with 8 digits.
This exercise must be handed in through Autolab. More information about handing in your
exercise through Autolab can be found on Blackboard.

Anda mungkin juga menyukai