Octave tasks
Create matrices octave-matrices
Create the following matrices. Create an octave function that returns a matrix. Name the function as proposed in bold at the beginning of each task.
Please, use only methods that were discussed at the lecture. That is, for example, don’t
use loops and functions such as repmat
. And try to find a short solution, that is, don’t
enter all the values manually.
-
task1
The \(10\times10\) matrix:
\( \begin{pmatrix} 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \end{pmatrix} \)
-
task2
The \(5\times5\) matrix:
\( \begin{pmatrix} 2 & 2 & 2 & 2 & 2 \\ 2 & 2 & 2 & 2 & 2 \\ 2 & 2 & 2 & 2 & 2 \\ 2 & 2 & 2 & 2 & 2 \\ 2 & 2 & 2 & 2 & 2 \end{pmatrix} \)
-
task3
The \(10\times10\) matrix:
\( \begin{pmatrix} 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 \\ 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 \\ 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 \\ 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 \\ 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 \\ 2 & 2 & 2 & 2 & 2 & 3 & 3 & 3 & 3 & 3 \\ 2 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matrix} \)
-
task6
The \(10\times10\) matrix with a range of numbers from 1 to 100:
\( \begin{pmatrix} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\ 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 \\ 21 & 22 & . & . & . & . & . & . & . & . \\ . & . & . & . & . & . & . & . & . & . \\ . & . & . & . & . & . & . & . & . & . \\ . & . & . & . & . & . & . & . & . & . \\ . & . & . & . & . & . & . & . & . & . \\ . & . & . & . & . & . & . & . & . & . \\ . & . & . & . & . & . & . & . & 99 & 100 \end{pmatrix} \)
-
task7
The \(9\times9\) matrix with a “multiplication table”. That is, the element
a(i, j) = i * j
:\( \begin{pmatrix} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9\\ 2 & 4 & 6 & 8 & 10 & 12 & 14 & 16 & 18 \\ 3 & 6 & . & . & . & . & . & . & . \\ . & . & . & . & . & . & . & . & . \\ . & . & . & . & . & . & . & . & . \\ . & . & . & . & . & . & . & . & . \\ . & . & . & . & . & . & . & 56 & . \\ . & . & . & . & . & . & . & . & . \\ . & . & . & . & . & . & . & 72 & 81 \end{pmatrix} \)
-
task8
You are given numbers
n
,a
,b
. Create a matrix of size $n\times n$, that hasa
on the main diagonal,b
strictly below and above the main diagonal, and 0 in all other places. Here is an example for arguments (4, 1, 2):1 2 0 0 2 1 2 0 0 2 1 2 0 0 2 1
-
task9
This task is similar to the chess-board task, but the size of matrix should be $20\times20$, and cells with ones or zero should have size $2\times2$:
0 0 1 1 0 0 ..... 0 0 1 1 0 0 ..... 1 1 0 0 1 1 ..... 1 1 0 0 1 1 ..... .................
-
task10.
You are given a number
n
. Create the matrix of size $n\times n$ of the following structure: Its main diagonal has numbers 1 and 2 that go. Дальше от каждой клетки главной диагонали направо и вниз расставляется то же числа, что и в самой этой клетке. Пример для аргумента (5):1 1 1 1 1 1 2 2 2 2 1 2 1 1 1 1 2 1 2 2 1 2 1 2 1
Try to avoid loops. Additional question. What is a determinant of this matrix? Can you explain, why the determinant is as it is?
Indexing octave-indexing
- task1 You are given a square matrix of an even size. (you are not supposed to test that the matrix is actually even and square, just use it). Imagine we split a matrix vertically and horizontally through the center. Return the matrix at the left-top.
- task2 You are given a matrix with an even number of rows. Return a new matrix where the top half is exchanged with the bottom half of the initial matrix.
- task3
You are given a vector with integer numbers. Return a vector that contains only that numbers of the initial vector, that are
divisible by 3. Use the
mod
function to test divisibilty. - task4 Given a vector of integer numbers, return a vector with the same numbers, but where even numbers are divided by 2. Will your solution work if the matrix is 2 dimensional?
-
Create two functions filter_multiples and seive. The first one is
filter_multiples(a, k)
, it removes all numbers from the vectora
that are divisible byk
and are strictly greater thank
. For example,filter_multiples([1 5 10], 5)
yields[1 5]
The second one is
sieve(n)
, and it implements the Sieve of Eratosthenes. You will probably need to use the while loop. - task6 You are given a matrix with two columns. Each column describes
an animal: its height in meters and weight in kg.
Return the following set of answers:
- sum of animals’ heights
- mean height of animals
- mean height of animals that are higher than 100 kg
- one column matrix with the body mass index (BMI) of animals. BMI is equal to weight divided by the square of height.
- two columns matrix with animals, that are higher than 10 meters, and heaver than 100 kg.
Vectorization octave-vectorization
- task1
You are given a row matrix, for example,
a = [1, 2, -2, 4]
and a numberx
, for example, 1. The rowa
specifies coefficients of a polinomial, starting from the lowest degree, that is, the row from the example specifies the polynomial \(1 + 2x - 2x^2 + 4x^3\). Compute the value of the polinomial at the pointx
. The answer in the example is 5. - task2 You are given a column matrix \(x\). Create a square matrix of the same size, its value in the row number \(i\) and the column number \(j\) should be equal to \(a_{i,j} = x_i – x_j\).
- task3 You are given a three columns matrix. Imagine, that each row specifies a line of the form \(ax + by + c = 0\),
whith the columns being, correspondingly,
a
,b
,c
. Return a matrix, that has two columns, vcorrespondingly,k
иb
, that define the same lines, but in the form \(y = kx + b\). For example, the row[1 1 1]
should be converted to the row[-1 -1]
. - all2dets(x). You are given a matrix
x
of two columns. Return a new square matrix with the size equal to the number of rows inx
. An element in the row number \(i\) and the column number \(j\) should be equal to \(a_{i,j} = \left|\begin{matrix}x_{i,1}&x_{i,2}\\x_{j,1} &x_{j,2}\end{matrix}\right|\). This is a determinant of a matrix composed from the i-th and the j-th rows ofx
. I remind that \(\left|\begin{matrix}a&b\\c&d\end{matrix}\right|=ad - bc\). -
all_lines_intersections(a). You are given a matrix
а
of three columns, it describes a set of lines (as previously). The short task statement is: intersect each line with each line. To be more specific: the intersection of the line from the i-th row and the line from j-th row has two coordinates \(x\) and \(y\). The function should return two matricesx
andy
. The first one contains the x-coordinate of this intersection in the row \(i\) and the column \(j\). The second one contains, correspondingly, the y-coordinate. Don’t consider the case of parallel lines.Here is the formula for intersection of lines \(a_1x+b_1y+c_1\) = 0 and \(a_2x+b_2y+c_2 = 0\):
Δ = det([a1 b1; a2 b2]) Δx = det([-c1 b1; -c2 b2]) Δy = det([a1 -c1; a2 -c2]) x = Δx / Δ y = Δy / Δ
So, you will need a previously implemented
all2dets
function.
Plotting octave-plotting
Don’t use loops, unless the opposite is explicitly stated. Your functions will have the side effect: they draw some plot. They should not return anything.
- house Draw a house. You should draw just a few lines, of the house body, its roof, may be a window or a door. Don’t draw a lot to not spend a lot of time for this task.
- task1 Plot the function \(\sin(x) + \sin(3x)\) from \(-2\pi\) to \(2\pi\).
- task2 Plot \(\sin(x) + \frac1{2}\sin(2x) +
\frac1{3}\sin(3x) + \cdots + \frac1{10}\sin(10x)\).
Think how to do it without loops. Remember, that
sin
accepts matrices and evaluates results for each element separately. - taskA You are given a matrix
A
of the size \(N\times2\), each row contains coordinates of one point on the plane.- Draw set
A
- Draw the center of mass for
A
. - Create and draw the set
B
that is a translation ofA
in such a way that its center of mass is in \((0, 0)\). Remind: translated by the vector \((dx, dy)\), point \((x, y)\) goes to \((x + dx, y + dy)\). - Create and draw the set
C
: a rotation of A around (0, 0) by \(5^\circ\). Remind: to rotate the point \((x, y)\) by \(\varphi\) around zero, one should left-multiply \(\begin{pmatrix}x\\y\end{pmatrix}\) by the matrix \(\begin{pmatrix}\cos(\varphi)&\sin(\varphi)\\-\sin(\varphi)&\cos(\varphi)\end{pmatrix}\). - Create and draw the set
D
: A rotation ofА
around its center of mass by \(5^\circ\).
Draw each set with a different color and a different marker.
- Draw set
- task10circles Draw 10 concentric circles with radiuses: 1, 2, …, 10. Use the fact, that points with coordinates \((\cos(\varphi), \sin(\varphi))\) lay on a circle of radius 1, when \(\varphi\) changes from 0 to \(2\pi\).
-
plot_line this is a hard task You are given a linear matrix
[a, b, c]
. You are also given matricesxrange=[xmin, xmax]
andyrange=[ymin, ymax]
. Plot a line \(ax + by + c = 0\), and draw only the part of the line, that is contained inside a rectangle \(x_{min} \leq x \leq x_{max}\), and \(y_{min} \leq y \leq y_{max}\). Consider all the cases, including vertical lines.One of the ways to solve the task is as follows: intersect the line with each of the rectangle sides. They are horizontal and vertical, so it is quite easy to find intersections. Usually, you are left with only two intersections, so you now can call
plot()
for the line between the two points. - plot_intersections. You are given a matrix with lines as in the task 3
from octave-vectorization. Plot all intersections of these lines (do not draw lines),
use the function
all_lines_intersections()
from octave-vectorization. - plot_lines. You are given a matrix
a
with lines (the same as matrices from the block octave-vectorization, you are also givenxrange
,yrange
with ranges to draw the lines by means of theplot_line
function from the previous task. Draw all of the lines froma
. Use a loop to call plotting for each line. - plot_lines_and_intersections You are given the same as in
the previous task. Draw all lines from the matrix
a
and draw all the intersections. Remember, that you have already implemented evaluating of intersections in the block octave-vectorization.