Ass 1
Ass 1
Matlab Assignment: 1
This Assignment is designed to teach you to think in terms of matrices and vectors because this is
how MATLAB organizes data. You will find that complicated operations can often be done with one or
two lines of code if you use appropriate functions and have the data stored in an appropriate structure.
The other purpose of this homework is to make you comfortable with using help to learn about new
functions. The names of the functions you’ll need to look up are provided in bold where needed. Also,
in case, recall we cannot use space in the script’s name. Assignment must be submitted on the teams.
What to turn in: Copy the text from your scripts and paste it into a document. If a question asks you
to plot or display something to the screen, also include the plot and screen output your code generates.
For problems 1-7, write a script called yourname rollnumber.m and put all the commands in it. Separate
a) a = 10
b) b = 2.5 × 1023
d) dvec = 100 100.01 ... 100.99 101 , Use logspace, make sure you get the length
right.
1 0 .. 0
0 .. .. 0
b) bMat =
, however, diagonal entries are 1 2 3 4 5 4 3 2 1
.. .. .. 0
0 .. .. 1
9×9
1 11 .. 91
2 12 .. 92
c) cMat =
, a vector 1 : 100 runs down the column
.. .. .. ..
10 20 .. 100
10×10
d) dM at be 5 × 3 matrix of random integers with values on the range −3 to 3
logarithm.
5) Matrix Equations Use the variables created in 2, and 3, find the values of xMat, yMat, and zMat
b) yMat = (bvec.avec)
c) zMat = |cMat| (aMat.bMat)T , where |aMat is the determinant of the matrix, and T indicates
the transpose.
a) Make cSum the column-wise sum of cMat. The answer should be a row vector (use sum).
b) Make eMean the mean across the rows of eMat . The answer should be a column (use mean).
c) Replace the top row of eMat with 1 1 1
d) Make cSub the submatrix of cMat that only contains rows 2 through 9 and columns 2 through
9
e) Make the vector lin = 1 2 ... 20 , and then make every even value in it negative to get
lin = 1 −2 3 − 4 ... −20
f) Make r a 1 × 5 vector using rand. Find the elements that have values < 0.5 and set those
7) Plotting multiple lines and colors Write a script to plot two lines on the same axes.
a) Open a script and name it twoLinePlot.m Write the following commands in this script
b) Plot Sin(t)
c) Type hold on to turn on the ’hold’ property of the figure. This tells the figure not to discard
lines that are already plotted when plotting new ones. Similarly, you can use hold off the
hold property.
d) Plot cos(t) using a red dashed line. To specify line color, and style, simply add a third argument
to your plot command (see the third paragraph of the plot help). This argument is a string
specifying the line properties as described in the help file. For example, the string ‘k:’ specifies
• Create a legend to describe the two lines you have plotted by using legend and passing to
f) If you run the script now, you’ll see that the axis goes from 0 to 7 and y goes from -1 to 1.
To make this look nicer, we’ll manually specify the x and y limits. Use xlim to set the x-axis
to be from 0 to 2π and use ylim to set the y-axis to be from -1.4 to 1.4.
g) Run the script to verify that everything runs right. You should see something like this.
0.6
0.4
Function Values
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
Time (s)