$25
Assignment 2 –
Objectives: understand the difference between Matlab scripts and functions ▪ Understand the idea of modular programming ▪ Understand the terminology regarding functions and their use: calling a function, passing arguments, returning values ▪ Convert a part of a script into a function ▪ Test the function ▪ Call a function from inside a script All your files should include at the top your name, course no., assignment no., and instructor name.
Calculator+ (cont.) The following problem was introduced in Lab 1: we want to write a program that inputs two numbers as floating point, then prints out the results of o adding them together o subtracting the second from the first o multiplying them together o dividing the first by the second o dividing the first into the second A possible sample run is given: First number: 23.0 Second number: 10 23.0 + 10.0 = 33.0 23.0 - 10.0 = 13.0 23.0 * 10.0 = 230.0 23.0 / 10.0 = 2.3 23.0 10.0 = 4.1427e+13 In Task 4 of Lab2, you wrote a script so achieve the functionality of the Calculator program, matching the sample run. Task 1 (30 points) Add a fifth operation to the program you wrote in Lab 1. The specifications are: Dividing the first (x) by the second (y) results in a value (let’s call i z). z = x/y If z is greater than x but less than y, the result is a logical TRUE. If the result of the division does not fall between x and y, the output is a logical FALSE. i.e. If x < z < y, result is TRUE. Otherwise, result is FALSE. Give pseudocode in a text format, then write the MATLAB code by appending to the m-file you created for Lab 1 (CalcScriptA.m). Task 2 (24 points) Write six functions addressing the functionality needed by the Calculator program. Below are the function headers for your reference. function [ out] = addFunc( input1, input2) function [ out] = subtractFunc( input1, input2) function [ out] = multFunc( input1, input2) function [ out] = divbyFunc( input1, input2) function [ out] = divintoFunc( input1, input2) function [ out] = quotientInBoundsFunc( input1, input2) Task 3 (16 points) Create a new script. This script should be a modified version of the original script such that the each of the 6 operations is replaced by the appropriate function call. Name your script CalcScriptB.m. Run some sample values through your program to verify correct outputs. (At your grading interview your grader might try different values to verify correctness.) Scripts vs. Functions Task 4 (15 points) In thermodynamics, the Carnot efficiency is the maximum possible efficiency of a heat engine operating between two reservoirs at different temperatures. The Carnot efficiency is given as , where and are the absolute temperatures at the cold and hot reservoirs, respectively. Write a script carnot.m that will: a) prompt the user for the two reservoir temperatures in Kelvin, b) call a function to calculate the Carnot efficiency, and then c) print the corresponding Carnot efficiency to 3 decimal places. Note: You will need to write the function as well. Task 5 (15 points) In CS it is often necessary to design logic that has a specific result based on some set of inputs. Let’s say we have 3 logical variables, X, Y, and Z that store the values of 3 inputs. Design a logic that would output true only if X and Y are true, or if Z is false. A. How many different combinations can we have given the 3 logical variables? B. Write a logical statement for the given logic (use MATLAB syntax). C. Create a truth table showing all of the possible variable combinations and the result for each combination. Submitting the assignment: Copy the code from all the scripts and function files into a text document, indicating the task number. Include the call statements from Task 2. Convert your text file to pdf and save it as ._Hwk2.pdf. Zip this file with all your .m files and submit the resulting file on Moodle by due date.