University of California, Berkeley
Department of Mechanical Engineering
Fall Semester 2008
Instructors: M. Frenklach, R.
. Horowitz
E7, Assignment 2
Assigned: Thursday, September 11 2008
Due: 12:00pm, Friday, September 19, 2008.
This assignment is an introduction to using arrays, which are the basic building blocks in
MATLAB
. The ability to represent and handle multiple items in single collections is one of
the main strengths of
MATLAB
and it is what makes code-writing very short. As in the
previous assignment, turn in the hard copy of your published file to the drop boxes in
Etcheverry 1109 and upload the soft copy of your script (the m-file) to Bspace. Do not
forget to name your m-file as lastname_firstname_SID_lab02.m
Useful
MATLAB
commands
*
in this assignment:
linspace, length, sort,
find, upper, lower, double, char, rem, size, max, min,
plot, polyder, polyval, roots, conv, poly
1. Using
linspace
, create the row vector
a
having 9 regularly spaced elements starting
with 16 and ending with 48. Then, using the colon notation and transpose, create the
column vector
b
having a regular spacing of 0.5, starting with 0.5 and ending with 4.5.
Do the following tasks by using the appropriate array operations.
i.
Divide each element of
a
by 2.
ii.
Add 1 to each element of
b
.
iii.
Take the third power of each element of
a
.
iv.
Divide all elements of
a
by the corresponding element in
b
(e.g. element by element
division).
v.
Find the number of elements of
a
using the
MATLAB
function
length.
vi.
Assign the 5
th
element of
a
to a new variable
c
.
vii.
Create a row vector
d
which is composed of the 6
th
, 4
th
, 2
nd
and 5
th
elements of
a
.
(e.g. the 1
st
element of
d
is the 6
th
element of
a
.)
viii.
Using the function
sort
, define the row vector
d_sorted
that contains the
*
Please refer to
MATLAB
help to learn how to use the functions introduced in this assignment.
Assignment 2
E7
1