0% found this document useful (0 votes)
16 views7 pages

Exercises Lecture 1: Exercise

Uploaded by

Anass Aloulou
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)
16 views7 pages

Exercises Lecture 1: Exercise

Uploaded by

Anass Aloulou
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/ 7

A.

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

Exercise 1.2: Interruption of calculations or output

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?

Exercise 1.3: Incomplete commands

By entering a = [2, 3, 4] the array (2, 3, 4) is specified to the variable a.

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.

Exercise 1.4: Removal of variables

Define the variables u = [2 3 4] and v = [1 5] and then delete them. Check if the variables
are actually deleted.

Exercise 1.5: Use defined variables

Perform the following two exercises:

>> f = 2

and

>> g = 3 + f

84
Exercise 1.6: Calculation of mathematical expressions

Calculate the following expressions in MATLAB:

3+22
a) 16+52

b) 42/3

c) sin( π4 )

d) log(e)

Exercise 1.7: Calculation of mathematical expressions with one variable

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

Exercise 1.8: Making a row vector

Make a row vector with:

- Initial value: -1

- Final value: 9

- Step size: 0.5

Exercise 1.9: Making a row vector

Make a row vector with:

- 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

a) Perform the exercise: >> f = x.^3/(1+x.^2).


MATLAB gives a result! But why is it not an array with function values?

REMARK:
IF YOU GET A RESULT, ALWAYS CHECK THAT RESULT!

b) Perform the exercise: >> f = x^3./(1+x.^2).


What does the MATLAB error mean?

Exercise 1.11: Array operations

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

Exercise 1.12: Relational array operations

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.

HINT: use the MATLAB-function find in this exercise.

Exercise 1.13: Array operations

Consider the function:

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.

Test your command with the following arrays:

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!

Exercise 1.15: Array operations

Give an one line command that, for an array a, calculates the mean value of the elements of a.

Test you command with the following arrays:

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!

Exercise 1.16: Drawing a graph [Lecture: Basics 2]

Plot the functions sin(t) and cos(t) on the interval [0, 2π] in one figure.

Exercise 1.17: Drawing a graph [Lecture: Basics 2]

Plot the function f below on the interval [0, π]. Add names to the axes in the figure.

f (t) = (t) sin(2t)

Exercise 1.18: Drawing a graph [Lecture: Basics 2]

Plot the function f below on the interval [−2, 3].

f (t) = e−t

Exercise 1.19: Script file [Lecture: Basics 2]

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]

Open via the menu ”File\New\M-file” the ”MATLAB Editor/Debugger”.


Save the lines below under the name ‘plate.m’ and describe the meaning of the lines in this
file.

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

Exercise 1.21: User defined function [Lecture: Basics 2]

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);

Save this function under the name ‘f.m’.


Where is the file ‘f.m’ placed?

Exercise 1.22: User defined function [Lecture: Basics 2]

Enter the following function f :

function y=f(x)
y = x.^2+exp(x);

and calculate some values.

In the definition of the function f , with respect to the previous exercise, the semicolon (;) is
left out. What is the effect?

Exercise 1.23: User defined function [Lecture: Basics 2]

Consider the function:


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!

Exercise 1.24: User defined function [Lecture: Basics 2]

Make your own function with the name h for the function h(x) = x2 that is capable for
row-operations.

Perform the following exercises in the correct order:

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:

- Is h, during the parts c) till e), be seen as a function or a variable?


- Why does part d) not give an error message?

- Does MATLAB still know the function h?

Exercise 1.25: Help command

Ask for information about the sine function and the function load. For this, use the command
>> help.

Exercise 1.27: Help browser

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

In MATLAB calculate the product 1 · 2 · · · 24 · 25.

Exercise 1.30

In MATLAB calculate the exact sum of:

1 + 22 + 32 + 42 + · · · + 1992 + 2002

Exercise 1.31 [Lecture: Basics 2]

Draw the graph of the function f (x):


x
f (x) = 1+x

on the interval [0, 0.01, . . . , 3.99, 4]. Create an anonymous function!

Exercise 1.32 [Lecture: Basics 2]

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.

Exercise 1.33 [Lecture: Basics 2]

The periodic function f with period 2 is given by:

⎧ 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

You might also like