0% found this document useful (0 votes)
19 views

Ass 1

This document provides instructions for a MATLAB assignment involving scalar, vector, and matrix variables as well as plotting functions. Students are asked to write scripts to perform calculations on various data structures and plot multiple lines on the same graph.

Uploaded by

Mina Nath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Ass 1

This document provides instructions for a MATLAB assignment involving scalar, vector, and matrix variables as well as plotting functions. Students are asked to write scripts to perform calculations on various data structures and plot multiple lines on the same graph.

Uploaded by

Mina Nath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

Submit either a *.doc or *.pdf file.

For problems 1-7, write a script called yourname rollnumber.m and put all the commands in it. Separate

and label different problems using comments.

1) Scalar Variables. Make the following variables.

a) a = 10

b) b = 2.5 × 1023

c) c = 2 + 3i, where i is the square root of −1


2π √
d) d = ej 3 , where j = −1

2) Vector Variables. Make the following variables.


 
a) avec = 3.14 15 9 26
 
2.71
 
 8 
 
b) bvec =  

 28 
 
 
182
 
c) cvec = 5 4.8 ... −4.8 −5

January 13, 2024 DRAFT


2

 
d) dvec = 100 100.01 ... 100.99 101 , Use logspace, make sure you get the length
right.

e) evec = Hello, evec is a string, which is a vector of characters

3) Matrix Variables. Make the following variables.


 
2 ... 2
 
a) aMat = .. ... ..  , all the entries are 2’s.

 
2 ... 2
 9×9

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

4) Scalar Equations Using the variables created in 1, calculate x, y, and z.


1
a) x = 1+e(−(a−15)/16)
√ √ π
b) y = a + 21 b
log(R[(c+d)(c−d)]sin( aπ3 ))
c) z = cc∗
, where c∗ is the complex conjugate of c, and log is the natural

logarithm.

5) Matrix Equations Use the variables created in 2, and 3, find the values of xMat, yMat, and zMat

below. Use matrix operations.

a) xMat = (avec.bvec) .aMat2

b) yMat = (bvec.avec)

c) zMat = |cMat| (aMat.bMat)T , where |aMat is the determinant of the matrix, and T indicates

the transpose.

6) Common functions and indexing

January 13, 2024 DRAFT


3

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

values to 0 (use find)

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

a black dotted line.

e) Now, we’ll add labels ot the plot

• Label the x-axis using xlabel

• Label the y-axis using ylabel

• Give the figure a title using title

• Create a legend to describe the two lines you have plotted by using legend and passing to

it the two strings ‘Sin’ and ‘Cos’

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.

January 13, 2024 DRAFT


4

g) Run the script to verify that everything runs right. You should see something like this.

Sin and Cos functions


1
Sin
Cos
0.8

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)

January 13, 2024 DRAFT

You might also like