 Details
 Parent Category: Engineering Assignments' Solutions
We Helped With This Engineering Assignment: Have A Similar One?
Category  Engineering 

Subject  Other 
Difficulty  Undergraduate 
Status  Solved 
More Info  Computer Engineering Homework Help 
Assignment Description
ECOR 1010 – Introduction to Engineering (Fall 2016)
Laboratory 8 – Programming With MATLAB
Assigned Week of November 28, 2016
Due Week of December 5, 2016
I — Introduction:
In the last laboratory exercise you learned how to handle general vector and matrix problems with MATLAB. It also turns out that MATLAB can be used as a very convenient software system for general programming. For largescale programming (e.g., commercial software applications), languages such as C++ are more often used, however, MATLAB is a convenient program that can readily be used for managing the smaller problems most engineers need to solve routinely. MATLAB is very flexible: it also contains statistics and unstructured data analysis functions, and much more functionality, which has made it an important software tool for engineering students and practicing engineers.
II — Problem Statement:
Part 1: Section 30.6 in the 7^{th} Edition of the textbook (page 836) shows how to write a simple function that takes two numbers as inputs and then outputs their product (i.e., a x y ). Reproduce this function in MATLAB. (In Part 2, you will modify the output to return x times y^{2}.)
Part 2: Modify the code generated in Part 1 so that the new code takes input numbers, written as vectors, and multiplies one by the square of the other, elementbyelement (i.e., a x y^{2 }), and then prints the input and output vectors as vertical columns in a table with titles, like this:
x y xy^2
1 3 9
2 2 8
3 1 3
Write your code so that it produces the same output for horizontal and vertical input vectors: in other words, the output should be vertical column vectors regardless of whether the input vectors are vertical or horizontal. The code should check if the lengths of the input vectors are the same and terminate the calculation with an explanatory message to the user if they are not the same. Provide demonstrations with vectors of x and y to show your program meets the requirements listed above. Use the last three digits of your student number for the first three digits of your x vector (e.g. if your student number is 100231467, then x_{1}=4, x_{2}=6 and x_{3}=7).
(You may find the ‘size’ and ‘length’ commands helpful for deciding whether a vector is vertical or horizontal, and for finding how many elements it contains.)
Part 3: Write a MATLAB script file to plot the following piecewise function
1. 𝑥
𝑥
^{ }^{2 }𝑥<0
{+𝑥sin𝑥
for −10 ≤ x ≤ 15. Your code should make use of a “for” loop, a counter (index) and “if” statements. In an appendix of your report, include your code, and the plot created by the program. The plot should have the axes labelled and a title that includes your name, i.e., your name should appear in the title of the plot.
Part 4: For your automobile, you wish to determine the fuel efficiency, in miles per gallon (MPG), and fuel consumption in L/100 km. Over six months you record the distance travelled and fuel consumed each month. These records are listed below in the table.
• Write a MATLAB function that returns the fuel consumption in units of L/100 km from input values of distance travelled and the fuel used each month.
• When run, the MATLAB function shall automatically reproduce the table below with the fuel efficiency and fuel consumption calculated for each month.
• The MATLAB function shall automatically produce a separate output for the calculated average values for the fuel efficiency and fuel consumption over the sixmonth period. (i.e., replace the
‘???’ in the sentences after the table shown below with the calculated values.) All calculated output should be reported to the correct number of significant figures.
• Use the plot button in ‘Workspace’ to produce a graph of fuel consumption versus month. The axes should be labelled and your name should appear in the title (Click on “Insert” in the Figure window and find ‘X Label’ etc., in the dropdown box). Include the plot into an appendix to your laboratory report.
(1 US gal = 3.785L, 1 mile = 1.61 km)
Month  Distance (km)  Fuel (L)  MPG  L/100 km 
1 2 3 4 5 6  2545 2305 1951 2853 1984 2553  216 204 172 230 185 209 


The average fuel efficiency is ??? MPG.
The average fuel consumption is ??? L/100 km.
Part 5: Write a MATLAB program that asks for the input of a number, which will be your student number, and then follows this procedure:
• if the number is even, divide it by 2;
• if the number is odd, multiply the number by 3 and then add 1.
Iterate this procedure until 1 is reached; the program should display the number of iterations that were required to reach one (i.e., ‘The number of iterations = ‘). Your program should use a ‘While’ loop. This procedure will generate a series of numbers starting with the input number and ending with the number one. The program will then reverse this series and plot its logarithm in base 10 versus sequence number. Axes should be labelled and your name and student number should appear in the plot title.
Run your program with your student number as input. In the plot window, use the ‘Tools > Basic Fitting’ option to produce a cubic line (i.e., thirdorder polynomial) that represents the trend in the plotted data. In addition, include the “Plot residuals” in a second plot.
The commands rem(x,y) and mod(x,y)might be helpful in determining if a number is even.
III — Steps and Calculations:
Use MATLAB’s programming language to perform all the necessary steps to obtain the solutions to each of the questions and problems presented above in Parts 1 to 5. All programs and functions should include comments to explain the codes.
IV — Including Source Code in Your Report:
When including MATLAB code in your reports, please use the following method:
1. At the place in your report where you wish to display the block of code, create an empty table with a single cell:

2. To preserve font and syntax colour, copy and paste your code directly from the MATLAB script editor or command line into the empty table cell:
function [C] = matrix_mult(A, B) % Multiplies matrices A and B together
C = A*B;
3. Move your mouse cursor over the table cell and a small icon should appear near the upper lefthand corner of the box. Right click on that icon and select ‘Insert Caption…’ from the menu. Change the label to ‘Figure’ and append a title or description. Set position to ‘Below selected item’. Click OK. Finally, while the text cursor is somewhere in the caption, change the alignment to ‘center’. So you should have something like this:
function [C] = matrix_mult(A, B) % Multiplies matrices A and B together
C = A*B;
Figure 1: MATLAB code for matrix_mult function.
The function or script output can be included in a similar manner:
>> A = [3 2; 4 5]; >> B = A'; >> matrix_mult(A,B) ans =
13 22 22 41 
Figure 2: Example usage of matrix_mult function.
In fact, if the code and output are not too wide, you can create a two cell table and show the code and the output side by side for even better results:
function [C] = matrix_mult(A, B) % Multiplies matrices A and B % together
C = A*B;  >> A = [3 2; 4 5]; >> B = A'; >> matrix_mult(A,B) ans = 13 22 22 41 
Figure 3: MATLAB code for matrix_mult function and example usage
Try to make your code as readable as possible. For example, do not include mistakes you have made in the command line, like this:
>> A = [3 2; 4r 5]; A = [3 2; 4r 5];  Error: Unexpected MATLAB expression.
>> A = [3 2; 4 5]; >> B = A'; >> matrix_mult(A,B) ans =
13 22 22 41 
Figure 4: DO NOT DO THIS.
Finally, MATLAB can produce output with a lot of whitespace around the text by double spacing everything (after you copy and paste it to MS Word). If this is the case, remove the extra empty line to make the output more compact.
V — Report Requirements and Deliverables:
• Using the guidelines presented in Laboratory 1, produce a formal laboratory report that summarizes your findings.
• Like laboratory 7, this assignment is moreorless a series of programming exercises and not a good imitation of a reallife situation. Nonetheless, you should be able to identify a central theme (central objective) to use as a guide for writing your report.
• State briefly the results to all of the problems posed. Discuss the significance of the results in each case.
• Include your MATLAB code and plots for each problem in an appendix(ces).
• In general terms, discuss the usefulness of MATLAB for performing programming tasks. Discuss the difference between MATLAB script files and MATLAB functions. When is it less/more advantageous to use one or the other?
Deliverables Summary The lab assignment includes the following:  
1. Title page 2. Onepage report 3. A MATLAB program for Part 2 with some input and output demonstrating it works 4. A MATLAB program and the corresponding plot for Part 3 5. A MATLAB program, a Table followed by the average values, and the corresponding plot for Part 4 6. A MATLAB program and the corresponding plot for Part 5
IMPORTANT NOTES: A. Deliverables 36 above should be included in the laboratory report as an appendix. The maximum number of plots per page in the appendix is two. B. All the plots should have labelled axes and a title that includes your name. All programs and functions should include comment statements to explain the code.  
 Upload your completed lab assignment to cuLearn with file name: “Lab Section_Student 
 
 number.docx” (e.g. “C3_100812345.docx” is for C3 Lab section) 
 
VI — Submission and Timing:
Your report is to be uploaded to cuLearn within the first 30 minutes of your next laboratory period. LATE SUBMISSIONS WILL NOT BE ACCEPTED.
VII — Marking:
Laboratory submissions will be marked on a 10point scale: 910 (excellent); 78 (good); 56 (marginal); less than 5 (poor); if the mark is less than 1, the lab will not count as one of the 7 labs required to pass the course. Be sure that you are familiar with the University’s policy on plagiarism and academic integrity. Your instructors are obligated to report all suspected violations to the Associate Dean’s office for investigation (see also chapter 14 at http://calendar.carleton.ca/undergrad/regulations/academicregulationsoftheuniversity/).