Description
Please read the file, “How to complete and upload the solution template” before you
begin. Download assign3.m and fill it in and then upload the completed file on D2L
drop box. Also download test_assign3.m and assign3_solutions.mat to test your
answers as you go. Make sure your name and UCID are correctly filled out in the
template file.
The questions are based on content from chapter 4 of the textbook “MATLAB, An
introduction with applications”. Assume that all the angles are in radians except
where specifically noted.
Note for this assignment in Questions 8-10 you will need to understand how to use
for, and end. Use
help for
to get information on how to use the for statement. You can also type
doc for
on the command line which opens the document file.
Do NOT use loops (e.g. for) for questions 1-7
1. Given a vector of samples X1 [ 200, 190,…,10, 20] = – – , determine the sum of all of the
elements of the vector X1.
2. Given a vector of samples X 2 [ 200, 190,…,10, 20] = – – , calculate a vector Y2 based on
the function 5 100 i i y x = + where i x is the ith element of X 2 and i y is the ith element of
Y 2. Make the function return 2 sin( )cos i i ( )
i
Q xy =å .
3. Given vectors of samples X3 [0,1,…,100] = andY3 [10,11,…,110] = , calculate vector
Z3 based on the function
2
sin 2cos
180 180
i
i ii
x
z xy
æ ö p æ ö æ ö p
= + ç ÷ ç ÷ ç ÷ è ø è ø è ø
. i x , i y and i z are the ith
elements of X3, Y3 and Z3 respectively.
4. Solve for X4 in the matrix equation � �4 = �4 given
43 1
56 2
2 5 4.5
A
é ù –
ê ú = – ê ú
ê ú ë û –
and
18.2
4 48.8
92.5
Y
é ù –
ê ú = –
ê ú
ê ú ë û
5. A rocket flying straight up measures the angle � with the horizon at different heights, ℎ.
The measured values of � and ℎ are stored in two binary data files named Q5h.mat
(contains a row vector of measured values of ℎ) and Q5theta.mat (contains a row
vector of measured values of �). All angles are in degrees and height is given in ��.
Write a Matlab function that does the following:
a. Reads the two binary data files and generates a row vector � whose
elements are the estimates the radius of the earth at each data point using
�, = ℎ, cos �,
1 − cos �,
b. Outputs the average of all of the values of �.
6. The Matlab function rand() generates a random number between 0 and 1. Write a
Matlab function that does the following:
a. Given � = 10,000 generate M random numbers using rand(), and store
them in a column vector of X.
b. Computes the standard deviation (std) of the elements of X using
��� = 1
�
�8 − 1
�
�9
:
9;<
: =
8;<
and outputs the result. Do NOT use Matlab’s built-in functions std() or var() for this
question.
7. A digital magnetometer is a small low-power integrated circuit (IC) that measures the
intensity of the earth’s magnetic field and produces digital outputs that correspond to
the magnetic field intensity along two or three axes of a local coordinate system. This
electronic component is ubiquitously used in smartphones, GPS devices, vehicles, airplanes,
etc. to produce accurate compass heading information.
The measured values of the earth’s magnetic field (in nT) along the x and y axes of a
magnetometer are provided in a binary data file named Q7xy.mat. The data file contains a
5 by 2 matrix named Mdata. Write a Matlab function that does the following:
a. Reads the binary data file.
b. Assigns the values of the 1st column of Mdata to row vector �7.
c. Assigns the values of the 2nd column of Mdata to row vector �7.
d. Outputs a row vector �7 whose elements are the estimates of the relative
compass heading from
�, = ������ �,
�,
×
180
�
Use for loops to answer questions 8-10.
8. Given N=10, determine the value of
� = sin
��
10
9
8;NO
P
9;Q
9. Given M=11, determine the value of
� = (� + � + �)
=U
V;9
<Q
9;Q
:
8;U
10. Given K=12, determine the value of
� = � � cos � + �
8
9;NO
V
8;Q
W
V