Skills Mapping
Dr P. Shields - 19/02/2024
Schedule:
Tuesday, Week 19 .

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:

  1. 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,
  2. $\text{modulo } 7$ means the remainder when divided by 7 (i.e. 23 modulo 7 = 2),
  3. 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),
  4. k is the day of the month,
  5. c is the century (actually the number of 100 years already passed – see examples below),
  6. y is the year in the century,
  7. f = 0 is Saturday, f = 1 is Sunday, f = 2 is Monday, etc.

For example,

  1. Friday 23rd August 1963 is represented by m = 8, k = 23, c = 19, y = 63;
  2. 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:

  1. 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).
  2. Find out what floor, ceil, round, fix do, decide which one is required for this formula, and work out how to use it.
  3. 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,

function day=dayofweek(d)
Code snippet

that takes the date in the form of a vector:

d = [dd mm yyyy] % (e.g., [9 3 2001] for 9 March 2001)
Code snippet

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…

  1. Calculate formula inputs
  2. Call the function zeller using these inputs
  3. Select the correct text output using the result from the function
  4. 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

  1. You should develop your code outside of MATLAB Grader so that you can run your own tests and be able to monitor the workspace.
  2. When ready to test, you should copy your function or script into the relevant code section.
  3. You can then run your script or function to see if any errors are thrown and also any ‘pretests’.
  4. 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.

An example is shown in :

An example screenshot that could be used within your ePortfolio to demonstrate that your code functions correctly. Please note the visible name of the user logged into MATLAB Grader to verify its authenticity.
An example screenshot that could be used within your ePortfolio to demonstrate that your code functions correctly. Please note the visible name of the user logged into MATLAB Grader to verify its authenticity.