The Zeller Congruence
This is a simple task that you should solve carefully following the script below using your knowledge of MATLAB. You should use the desktop version of MATLAB on the lab computers or your own laptop. You will then be able to test your code to see if it works.
Background
A formula called Zeller’s congruence may be used to compute the day of the week for the Gregorian calendar, given the date:
\[\begin{equation} \label{eq:exampleEquation} f(m,k,c,y) = \left( \left\lfloor \frac{13(m+1)}{5} \right\rfloor + k + y + \left\lfloor \frac{y}{4} \right\rfloor + \left\lfloor \frac{c}{4} \right\rfloor - 2c \right) ( \text{modulo } 7) \end{equation}\]where:
- the half square brackets ($\left\lfloor \right\rfloor$) denote the integer part (i.e. integer part of 5.6 is 5, $\left\lfloor 5.6 \right\rfloor$ = 5,
- $\text{modulo } 7$ means the remainder when divided by 7 (i.e. 23 modulo 7 = 2),
- m is the month number, with January and February taken as months 13 and 14 of the preceding year (March is still month 3 and December is month 12),
- k is the day of the month,
- c is the century (actually the number of 100 years already passed – see examples below),
- y is the year in the century,
- f = 0 is Saturday, f = 1 is Sunday, f = 2 is Monday, etc.
For example,
- Friday 23rd August 1963 is represented by m = 8, k = 23, c = 19, y = 63;
- Weds 1st January 1800 is represented by m = 13, k = 1, c = 17, y = 99.
Note: The formula will not work if you go too far into the past. Shakespeare and Cervantes both died on 23 April 1616: Shakespeare, on a Tuesday; Cervantes, on a Saturday!
This is because England had not yet adopted the Gregorian calendar and was consequently ten days behind the rest of the world. The formula will also not work if you go too far forward.
Task 1: Implementing the function
Write a function in the following format: f = zeller(m,k,c,y)
that outputs f, given values of m,k,c,y.
Hints:
- Find out what the mod function does and how to use it. Try it out on some different numbers, e.g. mod(101,10), mod(8,7).
- Find out what floor, ceil, round, fix do, decide which one is required for this formula, and work out how to use it.
- Make sure that you test that your function is working correctly.
Task 2: Making it user-friendly
The zeller formula in the previous exercise is effective but not very user-friendly.
Therefore, you should now write another function,
that takes the date in the form of a vector:
and outputs the day of the week in letters, as a string, e.g. day = “Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”.
This function should call your function, zeller
, that you created in the previous task, which should be included as a sub-function below your main function.
Note: A sub-function is a function within a function. It is placed below a normal function, after the function end command. A sub-function can only be called by the other functions in the m-file where it is located.
Hints:
Some possible pseudocode might be…
- Calculate formula inputs
- Call the function zeller using these inputs
- Select the correct text output using the result from the function
- Test your program on some known dates (you can use the Window’s calendar for this).
Checking if your code works
MATLAB Grader is an online browser-based tool from Mathworks that will give you automated feedback for your code and a determination as to what extent your code works. You submit your code and MATLAB Grader will run some pre-defined tests comparing your output with output from other code that is known to be correct.
You can access MATLAB Grader using your university credentials, as with MATLAB Fundamentals.
Instructions
- You should develop your code outside of MATLAB Grader so that you can run your own tests and be able to monitor the workspace.
- When ready to test, you should copy your function or script into the relevant code section.
- You can then run your script or function to see if any errors are thrown and also any ‘pretests’.
- When you are confident that your code satisfies the requirements, you can ‘submit’ your code. This will run all the automated tests of your code and give you an evaluation and possibly some more feedback.
Providing evidence that your code works
You can use screenshots from MATLAB grader to validate that your code is successful.