You are on page 1of 3

EE 105: MATLAB as an Engineer’s Problem Solving Tool

Jay Farrell, College of Engineering, University of California, Riverside


January 7, 2011

Abstract and how to plot data with axes labels, grids, and
legends1 .
The objective of this laboratory is to familiarize the stu- Do not just read passively. Instead, while reading,
dent with the usage of MATLAB as an tool for engineering enter the examples and make sure that you can make
problem solving. MATLAB will be used in all labs related them work.
to this course.
3. Matrices and Arrays. A main goal is to make sure
that you know the basic methods for using Matlab as
1 Background a matrix manipulation tool; therefore, complete the
following.
Matlab is an engineering tool used for computations, in-
In the Matlab command window, use matrix opera-
cluding matrix algebra and simulation, which are two key
tions to find the matrix C:
topics of this course.
This lab will show you how to figure out command A = [π; sqrt(2); exp(1)] (1)
names for functions that you might want to compute and
B = [1; 5; 7] (2)
will familiarize you with Matlab through a few simple
programming exercises. C = A⊤ B (3)

where a superscript ⊤ denotes a matrix transpose.


2 Matlab Tutorial 4. Command line help. In addition to the help menu
GUI you can get help on specific functions by typing
Matlab is available in every EE lab that has a computer. ‘help fname’ where fname is the function of interest.
It is also available in many other College of Engineering Type ‘help for’ to get information about how to write
labs. Therefore, if you do not finish any Matlab based a ‘for loop.’
exercise in lab, you can finish it outside of the assigned
lab time. Matlab is platform independent meaning that A main challenge in using ‘help’ is finding the correct
the same program runs under Windows or Unix. name of the function. The help index and search are
useful for this purpose.
1. Start Matlab. On Windows machines, you should 5. Scripts. Usually in Matlab it is useful to write
find it in the list of programs under the start menu. scripts, rather than to work directly in the command
It may be slow to start as it has to communicate window. A script is just a list of variable definitions
with a license server, but once it is started it should and a command sequence. By writing a script, the
run very fast. If you get a message stating that no process can be debugged, run repeatedly, and saved
licenses are available, alert the TA who will contact for future use.
the Systems Administrator.
Write a script that
2. Run “Help: Product help”. You should see a
Document Set. All four items are useful. (a) clears the memory (‘help clear’);
(b) defines the matrices A and B given above;
Click on the Getting Started link.
(c) implements a ‘for loop’ to compute
(a) Work through What is Matlab?

3
(b) Work through Matrices and Arrays D= ai bi . (4)
(c) Work through Graphics i=1
1 In this course, and in your career, every figure in any report
(d) Work through Programming
should: (1) have labels on the horizontal and vertical axes that give
the name, symbol, and units of the variable plotted along that axis;
Make sure that you learn how to enter, add, trans- (2) a descriptive caption; (3) a label such as “Figure 3”; and, (4) a
pose, and multiply matrices and vectors; how to enter reference by label in the body of the report such as “Figure 3 shows
complex numbers; how to create and debug m-files; ....”
Your answer should be identical to C in the previous Start with a large value of N = 200, run the
step, since the summation used in computing D is function. Repeat this process with smaller
the definition of vector multiplication. values on N such as 5 or 10. Note that
Note: Matlab is designed to perform vector and ma- Matlab is not really plotting the function.
trix multiplications directly and efficiently. When- It is just drawing lines between the (xi , yi )
ever possible, use matrix vector operations (see eqn. points. For the figure to look accurate, the
(3)) instead of scalar operations with for loops (see value of N must be large relative to the cur-
eqn. (4)). In fact, whenever you are about to write vature of the function.
a ‘for loop’ you should consider whether it can be Comment: Any code that you turn in must
written in matrix notation. be well-commented and well-organized or it will
not be graded.
6. Run “Help: Product help”. Note that you can
search for help on particular topics by entering the 7. In this step, we will compare two approaches to nu-
first few letters of the topic. merically integrating the area under the graph of the
curve y = f (x) where you already have a m-file to
(a) Select the ‘Getting Started’ hyperlink.
compute f from the previous step. In the following,
(b) Remember this link as it includes many useful I will assume that the name of that m-file is myfun.m.
items for this class (e.g., matrices, graphics, pro-
gramming). (a) Use the Matlab help feature to learn about in-
(c) Follow the link titled ‘Programming: Scripts tegration routines and in particular the ‘quad’
and Functions.’ Read and understand the fol- function. Then ∫use the function ‘quad’ to ap-
5
lowing items: scripts, functions, global vari- proximate Q = 0 f (x)dx. Based on the help
ables, vectorization, and preallocation. information, you should know the accuracy of
this value of Q. In the following steps, you will
• Scripts or M-files contain a list of Mat-
form various estimates of Q and compare them
lab commands (written by you or someone
with this value.
else). When you want to perform a se-
quence of Matlab commands, it is almost (b) Based on your calculus experience, you know
always preferable to use an m-file than to that there are various methods to estimate the
type the commands directly into the com- value of an integral:
mand window.

N −1 ∑
N
• From the Matlab command window, you
Q1 = f (xi )dxi Q2 = f (xi )dxi−1
can open the Matlab editor by clicking on
i=1 i=2
the ‘new script’ button in the upper left cor-
ner. M-files contain ascii text, so you can and
edit them with any text editor. Using the

N −1
Matlab editor gives you some useful debug- dx1 dxN −1
ging features. Q3 = f (x1 ) + f (xi )dxi + f (xN )
2 i=2
2
(d) Create two m-files. Note that for Matlab to
find your functions, you must use ‘cd’ in the where dxi = xi+1 − xi for i = 1, . . . , N − 1 and
command window to change the present work- 0 = x1 < x2 < . . . < xN −1 < xN = 5.
ing directory (‘pwd’) to the directory in which i. Draw pictures and explain why each of
you have saved the function. these equations is valid.
i. The first m-file will have an input column ii. Show that Q3 = (Q1 + Q2 )/2.
vector x and an output column vector y
where the i-th element of the output vec- (c) The summation in the calculation of Q1 can be
tor is yi = f (xi ) and f (xi ) = √2π exp(−x2i ). written as the product of two vectors and cal-
culated using vector arithmetic.
Your m-file should use vector math, not
scalar processing (i.e., no “for” loops) and 5−0
it must work for any dimension of the vec- dx = ;
N −1
tor x. Use the help feature to learn about x = 0 : dx : 5;
exp and “.∗”.
y = myf un(x);
ii. The second m-file will have an input integer [ ]
N and no outputs. The m-file should de- ones(N − 1, 1)
A =
fine the vector x so that it contains (N + 1) 0
equally spaced points on the interval [0, 5], Q1 = y A dx;
call the previous m-file to calculate the vec-
tor y = f (x), and plot y as a function of where the semicolon is placed at the end in Mat-
x. lab to prevent the result from printing to the
screen (i.e., the code will be faster since screen 4. The report must include all theoretical derivations
printing is slow). The above should be working with each step of the derivation clearly explained.
Matlab code.
5. All variable must be completely defined (i.e., mean-
Note that Q1 is a function of N . Implement the
ing, units, symbol, sign convention).
above code. Then implement another function
which computes and plots Q1 for all integer val-
ues of N from 2 to 200. Plot Q1 (N ) versus N . 5 Prelab Rules
Also plot the constant value of Q determined by
the ‘quad’ function. Compare the result with In general, for each week, upon entering the lab you must
the result that you found using ‘quad’ by plot- show your prelab to the TA. The TA will keep a record of
ting the difference between the two. who has been signed off. Anyone not signed of in the first
(d) Write you own code similar to the above to 15 minutes of lab will not receive credit for the prelab.
compute Q3 directly (i.e., do not compute Q2 ); This week, there is no prelab.
clearly state your definition of the matrix A.
Compute Q3 for all integer values of N from
2 to 200. Plot Q3 (N ) versus N on the same 6 Figure Guidelines
graph as Q1 versus N . Also plot the error in Q3
The following guidelines apply to every figure that you
versus N on the same graph as the error in Q1 .
use this quarter. The guidelines will not be repeated in
Compare the rates of convergence for Q1 and each lab description.
Q3 . Which is the better algorithm?
1. Every figure that you turn in must include the fol-
8. Run “Help: Demos”. This will open a new win- lowing items:
dow that allows you to select among various demos.
Several of these may be useful over the duration of (a) Each figure must be numbered.
this course and the remainder of your time at UCR. (b) Each figure must have a caption describing it.
Matlab will be used in the majority of your future
courses, so learning it well now will help you in the (c) Each figure must be referenced (by figure num-
future. Note that the ‘Simulink’ demo will be useful ber) and discussed in your lab report.
in the later part of this quarter. (d) Each axis must be labeled (i.e., name, symbol,
units).

3 Lab Report If any of the above are ignored, the figure may be
ignored at the grader’s discretion
This weeks lab report should only be a few pages long.
Answer the questions from the lab. Include and discuss 2. The extent of the axis should be optimized to com-
the graphs that are requested. There is very little code municate the maximum amount of information.
that actually needs to be turned in. 3. The type of graph (e.g., loglog, semilog, rectan-
gular, hist) used should be carefully considered.
3.1 Grading of this lab report For example, the following sequence of data:
t- 1 2 3 4 5 6 7
The TA will describe the method that will be used to x - 1e5 3 .5 .05 .005 .0005 .000005
grade the report. Points will be given for clarity and conveys much more information using the ‘semil-
correctness of the figures, code, and report discussion. ogy(t,x)’ graph than the ‘plot(t,x)’ graph. Try both
and see for yourself.
4 Lab Report Guidelines 4. Use ‘subplot’ to fit various related graphs on the same
page to make comparison easy and to save trees.
The following guidelines apply to every lab report that
you will turn in this quarter. The guidelines will not be
repeated in each lab description. 7 Code Guidelines
1. Each student (not each group) must turn in his or
Avoid componentwise operations on vectors and matrices
her own lab report.
to the greatest extent possible.
2. Every lab report should be as short as possible while Any code printouts that you turn in,
still communicating to the grader that you under-
1. must be well organized
stand all aspects of the lab. Excess length or print-
outs will lower your grade. 2. must be well commented
3. Proper use of English grammar will increase your 3. must be discussed by name in the lab report.
grade.

You might also like