You are on page 1of 5

# Matlab

Homologation
Extra exercises (2015)
By: Bouke Haarsma

This document contains extra exercises for the Matlab-basics homologation module. These exercises
will be harder compared to the module offered on oncourse. (http://oncourse.tue.nl).
Please use these questions as confirmation you understand the material, so solve these exercises
individually! It is also recommended to use the mathworks documentation
(http://mathworks.com/help) and matlab help command, before asking for help or copying code
from Google. By doing so, you will be better prepared when help is not easily available.

Good luck!
Bouke

Exercises
1. Starting
a)

25
25 1
5 1
1
b. 3
( 5 + 1) 2

a.

b)

## Calculate the following quantities:

a. e 3 , ln(e 3 ),10 log(e 3 ),10 log(10 5 )
b. e 163
c. Solve 3 x = 17

c)

a.

d)
e)
f)



 

b. 3



 

## Plot the graphs for the functions at c) on the interval 1  5.

Give an array of 10 elements in which each element has the value of the square of the
index.
Give an array of 10 elements in which the elements of odd index contain the square of
the index, while the even elements contain the third power of their index.

## 2. To the finish and back

Theres a popular story that Gauss, mathematician extraordinaire, had a lazy teacher. The so-called
educator wanted to keep the kids busy so he could take a nap; he asked the class to add the
numbers 1 to 100.
a) Create a vector X which has the values from 1 to 100.
b) Take the sum of all elements to get the solution
Since Gauss did not have Matlab at the time, he used a different solution. He added the first and last
number in the series and multiplied it with the numbers of pairs in the series.
c) Add the first and the last index together in the variable P. Use X(end) to get the last number
in the vector.
d) Since the vector has an even number of numbers, use the length of the vector to determine
the number of pairs in the series.
e) Calculate the solution using the method from Gaus.
f) The solution given in c) d) and e) should work for all vectors. Create a script with the
commands from these questions, and test it with a vector from 1 to 300.

3. Going diagonal
Create the following matrices using one line in the command window without defining the matrix
element wise. (Use the Matlab help!):
0 0 0
a)  = 0 0 0
0 0 0

1 1 1
b)  = 1 1 1
1 1 1
1 0 0
c)  = 0 1 0
0 0 1

1 0 0
d)  = 0 2 0
0 0 3
2 0 0
e)  = 2 2 0
2 2 2

f)

0 4 4
 =  2 1 4
2 2 2

## 4. Round and Round

This exercise will create a function that will be used to plot circles. First a script will be created,
commands in the script will be later used to create a function.

a) Create a script called Ex1. Since a full circle will be drawn, this means angle  is between 0
and 2 radians. So in the script, create a vector called theta containing all numbers between
0 and 2 with a step size of 1 .
b) Run the script. Verify if a) has 6 elements. Why should it have 6 elements? Why does the
vector end at 6 and not at 2?
c) The circle will have a certain radius. Call this radius R. Edit the script. Using a) and R how can
x and y vectors for this circle be obtained? For now, use R=5.
Hint: what are the equations for x and y?

d) Edit the script so the circle is plotted, is the result as expected? What can be done to
improve the results?
e) Create a new function called circ. The input for this function is R and the outputs of the
function the X and Y (these variables will be vectors). Use a) and c) for the proper equations.

f)

In the command window, use the circ function. (e.g. circ(5) ). By using only the command
window and the circ function, create X and Y values in the workspace and plot the results.
Hint: this will take 2 commands.

g) Edit the script created previously so it uses the function circ instead of manually creating the
X and Y vectors.
h) Using the Mathworks help page: what is the command for retaining multiple plots in a single
window? Also see the examples given on the page to get an idea on how to use the
command.
i)

Using the answer from g) and the circ function created. Change the script created that plots
2 circles with different radius in a single window.

5. Got it!
Matlab is very fast in many equations at once due to the usage of matrices and vectors. In this
exercise this set of equations will be solved using Matlab:
2 + 10 + 11 = 327
6 +  + 11 = 286

14 + 13 + 6 = 229
12 + 14 + 10 = 332

 14 + 10 + 3 = 139

## This set of equations can be rewritten in the form:

&' = (


With & as matrix and ( and ' as vectors. Whereas the vector ' is equal to )*.

a) Insert the matrix & and vector ( in Matlab. What should the sizes of these variables be?

One way to solve this is retrieving the row reduced echelon form of the matrix .
b) What is the command for retrieving this form in Matlab? Use the mathworks help.
c) Use the command on the matrix [& (]. What are the answers for ,  and ?
d) Verify these results by constructing the vector ' and calculating the vector (. Should the *
or the .* command be used? Why?
A other way to solve this set of equations is by using the mldivide command
e) The mldivide has a shorter notation. What is this notation?
f) Solve the equations using the mldivide command.
Matlab has a lot of commands to solve this set of equations.
g) Search the help for mldivide for another command to solve this system.