Exercises Lecture 1: Exercise
Exercises Lecture 1: Exercise
1 Exercises Lecture 1
Exercise 1.1: Entering commands
You are supposed to perform the following exercises in the specified order. Before entering
each of the commands, think about the output on the screen and the result of each exercise.
a) >> a = 1+2+3+4+5+6
b) >> b = 1+2+3+4+5+6;
c) >> b+3
d) >> 1+2+3+4+5+6
e) >> c = 10*ans
Enter the array 0:900000 and make sure the output appears in the MATLAB Command
Window.
Perform the exercise again and and use the key combination ‘ctrl-c’ quickly. What is the
effect?
a) Enter >> a = [2,3,4 and use the ‘return’ key. Finish the exercise.
b) Enter >> a = [2,3,4 and use the ‘return’ key. Interrupt the input of the exercise.
Define the variables u = [2 3 4] and v = [1 5] and then delete them. Check if the variables
are actually deleted.
>> f = 2
and
>> g = 3 + f
84
Exercise 1.6: Calculation of mathematical expressions
3+22
a) 16+52
b) 42/3
c) sin( π4 )
d) log(e)
Give the variable x the value 2. In MATLAB enter the following expressions and calculate
them.
x3
a) 6
2
b) e1+x
c) √ x
1+x2
d) x3 sin(x2 )
e) 21/3
arctan(x)
f) 1+x2
- Initial value: -1
- Final value: 9
- Initial value: 9
- Final value: 0
- Step size: -1
85
Exercise 1.10: Error messages with array operations
x3
Consider the function: f (x) = 1+x2
We want to declare the row vector [f (0), f (0.2), . . . , f (1.8), f (2)] to the variable f . To perform
the exercises below you first have to enter the array x with arguments (0, 0.2, . . . , 1.8, 2.0).
Thus; >> x = 0:0.2:2
REMARK:
IF YOU GET A RESULT, ALWAYS CHECK THAT RESULT!
Make for the following functions f the array [f (0), f (0.1), . . . , f (2)].
a) f (x) = x2 + 2x + 1
3x
b) f (x) = 1+3x
x√
c) f (x) = 1+ x
Perform the exercise: >> a = rand(5,5). This command generates an array with 5 rows and
5 columns where each element is chosen arbitrary between 0 and 1. Make an array with all
elements of a which are smaller than 0.3.
t√
f (t) = 1+ t
Make the row vector [f (0), f (0.1), . . . , f (1)] by using array operations and the function sqrt.
86
Exercise 1.14: Array operations
Give an one line command that, for an array a, determines the number of elements in a which
are greater than 5.
a) a = [1 2 3; 4 5 6; 7 8 9]
b) a = 1:10
c) a = 10*rand(6,6)
PAY ATTENTION: The same command must work in all three cases!
Give an one line command that, for an array a, calculates the mean value of the elements of a.
a) a = [1 2 3; 4 5 6; 7 8 9]
b) a = 1:10
c) a = 10*rand(6,6)
PAY ATTENTION: The same command must work in all three cases!
Plot the functions sin(t) and cos(t) on the interval [0, 2π] in one figure.
Plot the function f below on the interval [0, π]. Add names to the axes in the figure.
f (t) = (t) sin(2t)
f (t) = e−t
Why can one better save the commands to plot a figure than to save the figure itself?
87
Exercise 1.20: Script file [Lecture: Basics 2]
x = 0:0.1:2
y = abs(x.*sin(2*pi*x))
figure
plot(x,y)
title(’f(x) = |x*sin(2*pi*x)| on the interval [0,2]’)
axis([0 2 0 2])
shg
Open the ”MATLAB Editor/Debugger” via the menu ”File\New\M-file” and enter the fol-
lowing function f :
function y=f(x)
y = x.^2+exp(x);
function y=f(x)
y = x.^2+exp(x);
In the definition of the function f , with respect to the previous exercise, the semicolon (;) is
left out. What is the effect?
0, x ≤ 0
g(x) = (A.1)
x, x ≥ 0
Make your own function g in MATLAB. For this, use the MATLAB functions max and zeros.
88
Check your own function with the commands:
>> x = -1:0.2:1;
>> g(x)
WARNING: The names of the function and the variables may NOT BE the SAME!
Make your own function with the name h for the function h(x) = x2 that is capable for
row-operations.
a) >> h([1,2,3,4])
b) >> h = 2.5
c) >> h([1,2,3,4])
d) >> h(1)
e) >> h(1.4)
f) >> clear h
Questions:
Ask for information about the sine function and the function load. For this, use the command
>> help.
Search for information about the sine function by means of the MATLAB Help Browser.
HINT: You can call the MATLAB Help Browser by clicking on the yellow question mark
in the MATLAB menu bar.
Exercise 1.28
Make a row vector that consists of the fifth power of the first 40 natural numbers.
89
Exercise 1.29
Exercise 1.30
1 + 22 + 32 + 42 + · · · + 1992 + 2002
Draw a circle with radius 1 and center (0,0) in a square with −2 < x < 2 and −2 < y < 2.
Make sure that the circle looks round!
HINTS:
First make a list of x-coordinates and a list of y-coordinates. Draw the circle. Change the
shape of the figure by using the command >> axis in different combinations.
⎧ 2
⎨ t ,0≤t<2
f (t) = f (t − 2) ,2 ≤ t (A.2)
⎩
f (t + 2) ,t < 0
Define this function in MATLAB. Make use of the command >> mod. For a number r the
command >> mod(r,2) returns a number s in such a way that 0 < s < 2 and s and r differ
are a multiple of 2 from each other. Thus f (r) = f (s) = s2 .
Make sure that the function is also capable to work with arrays.
90