MATLAB
Introduction to Computer Programming with MATLAB CEE/MAE M20
EXTRA CREDIT 1 Due: Friday, August 13th 2021, 11:59 PM
Formatting Reminder: The submitted work MUST follow the naming convention listed below.
• “LastName UID EC code.zip”
• “LastName UID EC main.m”
Submit one file to the CCLE course website a .zip file containing all of the MATLAB files written for the assignment. Remember to use good coding practices by keeping your code organized, choosing suitable variable names, and commenting where applicable. Any of your MATLAB .m files should contain a few comment lines at the top to provide the name of the script, a brief description of the function of the script, and your name and UID. Any submission that does not follow the above-mentioned format will receive severe point deductions!
1. The Ranked-Choice Vote. In class, we talked about an alternative voting system in which voters rank all the candidates in order of preference instead of supporting only a single candidate. During each round of voting, only a voter’s highest-ranked eligible candidate is con- sidered. If no candidate wins a majority, the lowest performer is eliminated from all ballots and the totals are recalculated. This process continues until cone candidate achieves a majority, i.e. > 50% of the votes. Consider the following election with 9 voters deciding between 4 candidates.
1
where voter 1’s favorite candidate is 3 and least favorite candidate is 4. After the first round of voting, candidate 2 leads with 4 votes but does not secure > 50% of the total vote. Thus, the least-popular option, candidate 1, is eliminated from all ballots and the votes are recounted. Write a script that performs this vote counting process on a large set of election data until a winner has been found.
(a) Begin by writing a function to remove a specified candidate from all ballots. This function must use exactly the specification shown below, where the function name, inputs, and outputs must be followed exactly. function votes = removeCandidate(votes,losingCandidate)
This function takes the total votes array, searches for all instances of losingCandidate (losingCandidate = 1 in the above example), and removes them from the array. Since we are assuming voters will list every candidate only once, the dimension of the votes array is updated from [N × c] to [N × c − 1].
(b) Using your removeCandidate function, write a script to determine each win- ning candidate given the mock election results posted to CCLE (we will use the load function to access the raw data.) Your code only needs to process one set of votes per run, simply swapping the load filename between runs to generate both results. Print your results to the command window using exactly the format shown below:
1 2 3 4
Round 1 Totals: 1 4 2 2
Round 2 Totals: 0 4 3 2
Round 3 Totals: 0 4 5 0
Winning Candidate: 3
Where the candidates’ round-by-round totals are listed in addition to the win- ning candidate. In this example (which gives the correct solution to the voting table presented above) although candidate 2 leads after round 1, the elimi- nation of candidates 1 and 4 redirects enough votes to propel candidate 3 to victory after the 3rd round.
2
(c) In votes2.mat you should observe a tie between the two lowest-scoring can- didates in Round 4. Temporarily modify your script so that you can observe the consequences of removing either one or the other candidate (i.e. run your script first breaking the tie in favor of candidate A, then run the script again breaking the tie in favor of candidate B). Are there significant differences be- tween these two trials?
(d) Compare this ranked-choice voting system to a simple, non-iterative weighted sum. For example, suppose our “Round 1” table above awards 4 points to a voter’s first choice, 3 points to a second choice, 2 points to a third choice, and 1 point to a fourth choice then immediately declares the winner based on the largest total number of points accumulated by each candidate (for a general case, first choices receive numCandidates points, second choices numCandidate - 1, and so on). Perform this alternative calculation for both sets of election data. Which candidates win using this weighted voting system? Based on your results and intuition, is either ranked choice voting or a weighted sum a better approach to deciding elections?
Note: If you find it helpful, you are welcome to use the built-in MATLAB function max, min, and sort in your solution. Do not use the built-in MATLAB functions find or reshape in your solution. As with all thing programming, it is much easier to start with a smaller test case where you know the answer first! Try solving the example above and confirm that you recovered the correct answer before using the full array saved in votes1.mat and votes2.mat. You may submit your script using either of the two tie-breaking “directions” with either of the two .mat files active.
3