Laboratory #7 Solution




PURPOSE: To practice plot and FOR loop in conjunction with the IF and BREAK statements; and switch statement.


For each problem, create a MATLAB script file and name it FIRSTNAME_LASTNAME_LAB7_ problemX.m. Put ALL the commands for the required steps in your script file:

  • Be sure to clear the display and the memory.
  • Display your name.
  • Separate and label different steps using comments.


%{Class: ENGR15100: Software Tools for EngineersInstructor: Xiaoli Yang    Author: [Student’s Name]Assignment: Lab [No.]File Name: LASTNAME_LAB[No.]_problem[No.].mDate: [MM]/[DD]/[YY]%}


%clear screen


%clear workspace



disp(‘Your Name Here’);


disp(‘starting code: ‘);


%Completing lab x

%your source code here%




problem 1 (30 points):

Using what you have learned in the last several labs it is desired to make a plot of the following equation:


The equation is derived from the circuit shown below:



The Y axis variable P is power of the resistor RL which is transferred from a battery modeled as a voltage source VTH  equal to 10 V also having and internal heating loss modeled by RTH=20 Ohms .


It is desired to plot the power P on the Y axis in terms of the load resistance RL on the X axis for the purpose of estimating the value of RL which results in a maximum value of P. Assume the input RL, changes from 0 to 40 Ohms with a step size of 0.1 Ohms. Create the plot described above and add a title, the appropriate x and y labels and lastly activate a grid to aid in reading the plot.


problem 2 (30 points):

Next, use a FOR loop starting at RL =0 having an ohmic step size of 0.001 ohms to find the value of RL which makes power P maximum. You will need to include an appropriate IF statement in your code to determine when the maximum value of P is reached and stop the FOR loop using the BREAK command. Lastly, use FPRINTF command to print out the solution found for the value of RL and the corresponding value of load power P.


problem 3(40 points):

The average monthly precipitation (in.) for Boston and Seattle during 2012 are given in the vectors below (data from the U.S. National Oceanic and Atmospheric Administration).

BOS=[2.67 1.00 1.21 3.09 3.43 4.71 3.88 3.08 4.10 2.62 1.01 5.93]

SEA=[6.83 3.63 7.20 2.68 2.05 2.96 1.04 0.00 0.03 6.71 8.28 6.85]

Where the elements in the vectors are in the order of the months (January, February, etc.) Write your program to answer the following:


  • Calculate the total precipitation for the year and monthly average precipitation in each city. Do NOT use MATLAB’s built-in functions sum and mean.
  • How many months was the precipitation above the average in each city?
  • How many months, and on which months, was the precipitation in Boston lower than the precipitation in Seattle?



problem 4(Bonus: 30 points):

Create a new script file called LASTNAME_LAB6_problem4.m. Again, begin by printing both your name and a title at the beginning of code using the DISP command.


A vector is given by x=[4.5 5 -16.12 21.8 10.1 -16.11 5 14 -3 3 2]. Using conditional statements and loops, write a program that implements “Selection Sort” to rearrange the elements of x in order from the smallest to the largest. Do NOT use MATLAB’s built-in function sort().


Selection Sort

Suppose x is an array of n values. We want to sort x in ascending order. That is, x[1] should be the smallest and x[n] should be the largest. The idea of Selection Sort is that we repeatedly find the smallest element in the unsorted part of the array and swap it with the first element in the unsorted part of the array.




Submit your lab by uploading .m file using the Blackboard Assignment feature no later than the date specified.

error: Content is protected !!