(Part B will appear in a separate document and will have a later deadline.)
You must work either on your own or with one partner. If you work with a partner you must first register as a group in CMS and then submit your work as a group. Adhere to the Code of Academic Integrity. For a group, “you” below refers to “your group.” You may discuss background issues and general strategies with others and seek help from the course staff, but the work that you submit must be your own. In particular, you may discuss general ideas with others but you may not work out the detailed solutions with others. It is not OK for you to see or hear another student’s code and it is certainly not OK to copy code from another person or from published/Internet sources. If you feel that you cannot complete the assignment on you own, seek help from the course staff.
Completing this project will solidify your understanding of 2-dimensional and 3-dimensional arrays. Problems 1 and 2 (Part A) use type double
matrices while Problem 3 (Part B) involves images and the type uint8
. In Problem 2, you will learn how to use Matlab as a (black box) tool to solve a system of linear equations.
As usual, use only the functions and constructs learned so far in the course.
Sudoku is a popular Japanese puzzle game that rose to international popularity in 2005. At the higher levels of difficulty, the puzzles are difficult to solve for both human and machine! In this project, you will write a “Sudoku checker” that determines whether a candidate solution is valid—we are not writing a program to solve the puzzle. If you are not familiar with the game, consult Wikipedia and try to solve a few puzzles online!
A Sudoku solution is valid if all of the following properties are true:
You will write a function isValidSudoku(), containing at least one subfunction, isValidPartition(), as specified below:
Subfunction isValidPartition() has one type double
input parameter called subarray which stores integer values. This function returns the type logical value true
if the following are true:
false
is returned.Function isValidSudoku() has one type double
input parameter called board which stores integer values. This function returns the type logical
value true
if board is a valid Sudoku solution (properties 1–5 above); otherwise it returns the logical value false
. This function should make effective use of isValidPartition().
You will write the function headers as well as concise and informative function comments for these functions. Note these additional considerations:
zeros()
, ones()
, length()
, size()
, and from Chapter 6 of our textbook, sum()
, any()
, and all()
. (You don’t need all of them.)To help you with testing, we provide two example 9-by-9 matrices below. Be sure to do additional testing! Tip: write your subfunctions in their own “.m” files first, so you can test them in the command window, and then copy them into isValidSudoku.m after you’re confident they work.
validSol = [1 7 2 5 4 9 6 8 3; ... 6 4 5 8 7 3 2 1 9; ... 3 8 9 2 6 1 7 4 5; ... 4 9 6 3 2 7 8 5 1; ... 8 1 3 4 5 6 9 7 2; ... 2 5 7 1 9 8 4 3 6; ... 9 6 4 7 1 5 3 2 8; ... 7 3 1 6 8 2 5 9 4; ... 5 2 8 9 3 4 1 6 7]; invalidSol = [1 7 5 8 3 9 4 2 6; ... 6 3 8 2 7 4 9 1 5; ... 4 2 9 6 5 1 3 7 8; ... 8 1 6 3 9 6 7 4 2; ... 5 4 7 1 6 2 8 3 9; ... 2 6 3 4 2 7 6 5 1; ... 7 5 4 9 2 6 1 8 3; ... 9 8 1 5 4 3 2 6 7; ... 3 6 2 7 1 8 5 9 4];
Systems of linear equations often arise when trying to balance the effects of multiple interacting processes. Their interaction couples the equations governing any one of them, so the total set of equations must be solved simultaneously. Fortunately, regardless of whether such equations originate from mechanics, chemistry, or biology, Matlab makes solving them a straightforward affair.
[Our objective for this problem is to show how you can use Matlab as a solver of systems of linear equations. We do not expect you to know linear algebra, nor are we trying to teach linear algebra. We will use the solver as a “black box,” so your only real task is to turn a set of given equations into a matrix and a vector and then use the solver, which is just an operator. So there's no need to be afraid of the chemistry or math!]
Consider a set of n linear algebraic equations of the general form (1) where the a's are known constant coefficients, the b's are known constants, and the n unknowns, x1, x2, …, xn, are raised to the first power. This system of equations can be expressed in matrix notation as or (2) To solve this linear system of equations is to find the values x1, x2, …, xn such that . In Matlab, the solution can be found using the backslash, called the matrix left divide operator (3):
x = A\b
where A
is the n-by-n matrix of coefficients, b
is the length-n column vector of constants, and the result x
is the length-n column vector of values such that
.
Your job: Write a script chemBalance.m to find the amount of ingredients (methane and oxygen, in moles) to produce 5 mol (roughly 90 mL) of water via the following reaction:
xm CH4 + xo O2 → xc CO2 + xw H2O
Here, xm is the amount of methane provided, xo is the amount of oxygen provide provided, xc is the amount of carbon dioxide produced, and xw is the amount of water produced. In order to balance this reaction, we write down equations conserving the number of atoms of each element. Conservation of oxygen (O), for example, leads to the following equation:
2 xo = 2 xc + xw
(each oxygen molecule provides two oxygen atoms, while each carbon dioxide molecule requires two oxygen atoms and each water molecule requires one).
You need to do the following:
2 xo | = 2 xc + xw | Original |
2 xo - 2 xc - xw | = 0 | All unknowns moved to left side; all constants moved to right side |
0 xm + 2 xo - 2 xc - 1 xw | = 0 | Re-order left side and make coefficients explicit |
Note that in this problem, most of the work is done by hand and you will write very little code. However, make sure that your script is cleanly commented and that the print output is clean and descriptive.
Submit your files on CMS (after registering your group). They should include isValidSudoku.m and chemBalance.m.