--- tags: Matlab Workshop --- # Lesson 2: If-else Statement [TOC] ## Playing Hide and Seek ![children-playing-hide-seek-cartoon-vector_504403-460](https://hackmd.io/_uploads/ByhzQ6b0A.jpg) ``` IF I find someone: I will tag them ELSE: Continue searching ``` ## I. Example of Grading calculation :::warning You have 8 homeworks in total, we take the highest 5 to decide your overall score, which account for 80% of your score. The remaining 20% are Math quiz. Here is your grade based on your overall score. ![image](https://hackmd.io/_uploads/ryzH71jaC.png) Now we write a code to output one's grade. ::: ### 1. Example Scores Loci did very well on his homework with the following scores: | | HW1 | HW2 | HW3 | HW4 | HW5 | HW6 | HW7 | HW8 | MQ1 | MQ2 | |-----------|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----| | Score | 90 | 85 | 92 | 88 | 95 | 75 | 80 | 70 | 80 | 70 | $$ loci\_overScore = \frac {95 + 92 + 90 + 88 + 85}{5}*0.8 + \frac {80+70}{2}*0.2 = 87 \rightarrow A $$ Chris, on the opposite, didn't perform that well with the following scores: | | HW1 | HW2 | HW3 | HW4 | HW5 | HW6 | HW7 | HW8 | MQ1 | MQ2 | |-----------|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----| | Score | 60 | 65 | 58 | 55 | 70 | 72 | 67 | 66 | 75 | 80 | $$ chris\_overScore = \frac {72 + 70 + 67 + 66 + 65}{5}*0.8 + \frac {75 + 80}{2}*0.2 = 69.4 \rightarrow C+ $$ ### 2. Pseudocode for Grade Calculation ```pseudocode! START INPUT scores // list of 8 homework scores INPUT quizScores // list of 2 math quiz scores // Sort homework scores in descending order SORT(scores) // 80% of scores and 20% of quizScores overallScore = mean(scores)*0.8 + mean(quizScores)*0.2 // Output final grade based on overallScore IF overallScore >= 90 DISPLAY "Grade: A+" ELSEIF overallScore >= 85 DISPLAY "Grade: A" ELSEIF overallScore >= 80 DISPLAY "Grade: A-" ... ELSE DISPLAY "Grade: X" END END ``` ### 3. MATLAB scripts for Homework grading --- #### a. If-else Statements ```matlab= % Start loci_scores = [90, 85, 92, 88, 95, 75, 80, 70]; loci_quizScores = [80, 70]; chris_Scores = [60, 65, 58, 55, 70, 72, 67, 66]; chris_QuizScores = [75, 80]; % Sort homework scores in descending order sortedScores = sort(loci_scores, 'descend'); % Take the highest 5 scores highestScores = sortedScores(1:5); overallScore = mean(highestScores) * 0.8 + mean(loci_quizScores) * 0.2; % Output final grade based on overallScore if overallScore >= 90 disp('Grade: A+'); elseif overallScore >= 85 disp('Grade: A'); elseif overallScore >= 80 disp('Grade: A-'); elseif overallScore >= 75 disp('Grade: B+'); elseif overallScore >= 70 disp('Grade: B'); elseif overallScore >= 65 disp('Grade: B-'); elseif overallScore >= 60 disp('Grade: C+'); elseif overallScore >= 55 disp('Grade: C'); elseif overallScore >= 50 disp('Grade: C-'); elseif overallScore >= 45 disp('Grade: D+'); elseif overallScore >= 40 disp('Grade: D'); else disp('Grade: X'); end ``` ## II. Check any mismatch between two nucleotide sequences ```matlab! % Define nucleotide sequences using numbers seq1 = ['A', 'C', 'G', 'T', 'A', 'C']; % ACGTAC seq2 = ['A', 'C', 'G', 'A', 'A', 'T']; % ACGAAT % Check if the sequences are of the same length if length(seq1) ~= length(seq2) disp('Error: Sequences must be of the same length.'); else % Calculate mismatches % What is the result of the mismatch? mismatches = (seq1 ~= seq2); % Logical array (1 for mismatch, 0 for match) % Check if there are any mismatches if sum(mismatches) > 0 numMismatches = sum(mismatches); % Count the total mismatches disp('Nucleotide Mismatches (1=Mismatch, 0=Match):'); disp(mismatches); fprintf('Total number of mismatches: %d\n', numMismatches); else disp('The sequences are identical.'); end end ``` ## ## Homework :::warning The solutions of quadratic equation $ax^2 + bx + c = 0$ are $$ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} $$ Depending on the value of the discriminant $\Delta = b^2 - 4ac$ : - If $\Delta > 0$ , there are two distinct real roots. - If $\Delta = 0$ , there is one real root (a repeated root). - If $\Delta < 0$ , there are two complex roots. ::: ### 1. Example Let’s solve the quadratic equation $2x^2 - 4x + 1 = 0$. $$ \Delta = b^2 - 4ac = (-4)^2 - 4*2*1 = 16 - 8 = 8 $$ The solutions are: $x_1 = 1 + \frac{\sqrt{2}}{2} \quad \text{and} \quad x_2 = 1 - \frac{\sqrt{2}}{2}$ ### 2. Pseudocode of Quadratic Equation ``` START INPUT a INPUT b INPUT c IF a = 0 DISPLAY "This is not quadratic function" ELSE Δ = b^2 - 4ac IF Δ > 0 x1 = (-b + sqrt(Δ)) / (2a) x2 = (-b - sqrt(Δ)) / (2a) DISPLAY x1, x2 ELSEIF Δ = 0 x = -b / (2a) DISPLAY x ELSE DISPLAY "No real solution" END END END ``` ### 3. MATLAB scripts for Quadratic Equation ```matlab= % Input coefficients a = input('Enter coefficient a: '); b = input('Enter coefficient b: '); c = input('Enter coefficient c: '); % Check if the equation is quadratic if a == 0 disp('This is not a quadratic function.'); else Delta = b^2 - 4*a*c; if Delta > 0 x1 = (-b + sqrt(Delta)) / (2*a); x2 = (-b - sqrt(Delta)) / (2*a); disp(x1, x2); elseif Delta == 0 x = -b / (2*a); disp(x); else disp('No real solution') end end ``` :::info ### Take-Home exercise Given the temperature (in Celsius) per day, can you tell me whether the weather on that day is hot, warm, or cold. Use `if-else` statement to do this. **Cold**: Temperature below 15°C **Warm**: Temperature between 15°C and 25°C **Hot**: Temperature above 25°C | Day | Temperature (°C) | |:---------:|:----------------:| | Sunday | 10 | | Monday | 18 | | Tuesday | 30 | | Wednesday | 22 | | Thursday | 14 | | Friday | 26 | | Saturday | 19 | [Please put in this GitHub Classroom your matlab code for this take-home exercise.](https://classroom.github.com/a/jK8tyj3E) ::: :::warning ### How to submit the code? **Step 1**: After clicking the link above, you need to accept the assignment first by looking for your name then click. **Step 2**: Once you accepted the assignment, you will be redirected to another page just like the screenshot below. Click the link `https://` ![Screenshot from 2024-10-03 10-52-52](https://hackmd.io/_uploads/HkofwFjRR.png) **Step3**: Click "uploading an existing file" link to upload your matlab code. ![Screenshot from 2024-10-03 10-45-02](https://hackmd.io/_uploads/BJmrHtiCC.png) ![Screenshot from 2024-10-03 10-58-12](https://hackmd.io/_uploads/BypIuFiRC.png) :::