ALGORITHMIZATION AND PROGRAMMING
winter semester 2021/2022
Tutorials - Course Schedule (for Lectures, see here)
Week 1 - introduction to Matlab, elementary computations
themes:
- basic information about the course
- introduction to Matlab:
examples: dialog window, arrays
exercises:
- compute the sum of elements of a given vector
- find indices of negative numbers in a given vector and how many of them are there
- find the largest value in a given table of numbers (an array)
- perform the computation of a gross interest from the first lecture
HW: Exercises - prepare the formulas for computation of the problems from the second paragraph
Week 2 - Matlab: scripts, plots
themes:
- Matlab scripts (m-files), how to create a script: an example
- plots
examples: input and output (I/O), plots
exercises (do not use if
, for
or while
commands; use vectorization instead):
- write a sript, which
- reads two values and prints their sum
- reads a radius and prints the area of the circle
- reads lengths of edges and prints the volume of the cuboid
- write a sript, which reads a vector of numbers and prints
- sum of its elements
- an average of its elements
- how many positive, how many negative numbers it contains
- how many nonzero numbers it contains
- how many zeros it contains and their indices
- how many 3's it contains
- minimal and maximal number
- indices of maximal and minimal numbers
- write a sript, which reads a matrix (with at least 3 rows and columns) and prints
- its transpose
- number of rows and columns
- the number at position [2, 3]
- the number at upper right corner (first row and last column)
- the number at lower right corner (last row and column)
- the first column
- the second row
- the submatrix 2x2 in the left upper corner
- the submatrix 2x2 in the left lower corner
HW:
Exercises - the first paragraph and any 2 problems from the second paragraph
Week 3 - Matlab: flow control
sources:
a short list of commands, relational and logical operators,
conditional control,
loop control
exercises:
- conditional control:
write a Matlab script, which- reads a number and writes if it belongs to the open interval from 4 to 11 (use just one - compound - condition)
- reads a number and writes if it belongs to the union of closed intervals from 3 to 7 and from 15 to 17 (again, using only one compound condition)
-
for
loop - predefined number of repetitions:
write a Matlab script, which- successively (one after one) asks for 5 numbers and after reading every number, immediately prints its value
- successively asks for 7 numbers and will be printing their running sum
while
loop - number of repetitions not known in advance: write a Matlab script, which
- successively (one after one) reads a sequence of nonzero numbers terminated by zero, and continually prints their values
- prints all terms of Fibonacci sequence less than 1000
successively program these
Exercises - paragraphs 3 (branching) and 4 (loops)
- learn both ways of programming the loops: either explicit (using command for
or while
), or vectorized (using Matlab functions on vectors)
HW:
Exercises - one problem (of your choice) from the third paragraph (preferably chosen from the last three problems of the third paragraph)
and one or two problems from the fourth paragraph (loops: if possible, compare vectorized solution of the problem with explicit loop - an example)
Week 4 - Matlab: recapitulation
You should be able to program any of these Exercises - all paragraphs.
Week 5 22. 10. 2021
the first assessment test:
You will be given two exercises similar to these and you will write down the solution on a piece of paper, without use of a computer or any other tool (calculator, textbook, ...). However, you can print and use this list of commands and this list of operators and functions as a quick help.
For some suitable problems, you will be asked to solve them both with and without using Matlab vector functions like max, mean, sum or find (i.e. you should be able to write a program for performing these vector operations element by element) - an example.
Week 6 - Tutorial cancelled (Dean's day)
Week 7 - Matlab: functions
exercises:
- Script for computation of BMI:
- write a function for computation of BMI (BMI = (weight in kg)/(height in m)2):
input: height in cm, weight in kg
output: BMI index
you can check the correctness of your function for instance here
- change your function BMI so that it has 2 outputs: BMI index and the corresponding category, numbered from 1 to 3 (bellow 18.5 underweight, 18.5-25 ideal weight, over 25 overweight)
- use your function BMI in a script which asks for weight and height, call the function BMI and prints BMI index (as a number) and the corresponding category (by words)
- write a function for computation of BMI (BMI = (weight in kg)/(height in m)2):
- Write (matlab) function for numerical computation of a definite integral of the given function (which will be fixed in a source code of your script). The file with the source code can have approximately this structure:
function int = my_int(a, b, n) % function for computation of the definite integral of f(x) % input: % a, b ... end points of the interval % n ... number of sub-intervals for numerical integration % output: % int ... definite integral from a to b of the function f(x), defined bellow ... ... here you can use f(x) ... end function y = f(x) % definition of the integrated function ... ... end
Week 8 - functions
using functions: exercises
advanced exercise: write a script for the maze route algorithm described at the first lecture.
Use function for computing possible moves of a knight from a given position,
for example:
function [nr, nc] = NewPositions(r,c, N) % % input: % r, c ... row, column of a chess position % N ... size of the chessboard % output: % nr, nc ... column vectors of possible knight moves % nr(k), nc(k) ... a possible new position ... end
Then use this function in the main script, that asks for input (i.e. starting and target positions) and prints the output: minimum number of knight moves to reach the target.
Then you can change the script so that for given starting point, for every position of a chessboard of a given size N, it computes the minimal number of knight moves to reach the target.
Week 9 - recapitulation
- exercise algorithms using functions
Week 10 26. 11. 2021
The second assessment test, or the first assessment test, repeated.
the second assessment test: you will be given several problems similar to these to show that you can program your own functions and that you can write numerical algorithms using them.
You can use Matlab (Octave) to debug and check your scripts, you can use tutorials on internet to recall some operator, command or function. However, you are not supposed to look up SOLUTIONS of the given problems elsewhere - your scripts are expected to be YOUR WORK, not a copy-pasted scrips made by someone else.
Weeks 11 - 13 - Consultations
An opportunity to repeat an assessment test.
References
- Matlab Tutorial for Engineering
- Matlab Tutorial - Clarkson University
- Matlab Tutorial - University of Utah
- Matlab Tutorial - Boston University
- Matlab Tutorial - Mathworks
SW
Matlab
- student version can be installed from
download.cvut.cz
(it can be used from the CTU network only)
GNU OCTAVE - free, similar to Matlab