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.

  1. 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} \)

  2. 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} \)

  3. 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 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9 & 9 & 9 \end{pmatrix} \)

  6. 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} \)

  7. 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} \)

  8. task8

    You are given numbers n, a, b. Create a matrix of size $n\times n$, that has a 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
    
  9. 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 .....
     .................
    
  10. 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?


type: tasks —

Indexing octave-indexing

  1. 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.
  2. 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.
  3. 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.
  4. 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?
  5. Create two functions filter_multiples and seive. The first one is filter_multiples(a, k), it removes all numbers from the vector a that are divisible by k and are strictly greater than k. 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.

  6. 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

  1. task1 You are given a row matrix, for example, a = [1, 2, -2, 4] and a number x, for example, 1. The row a 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 point x. The answer in the example is 5.
  2. 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\).
  3. 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].
  4. 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 in x. 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 of x. I remind that \(\left|\begin{matrix}a&b\\c&d\end{matrix}\right|=ad - bc\).
  5. 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 matrices x and y. 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.

Linear equations octave-linear-equations.

  1. task1 You are given n, find roots of the polynomial \(x^n + x^{n - 1} + \cdots + x + 1\), that is a polynomial of degree \(n\), that has all coefficints equal to 1. Return roots as one row-matrix.
    • Make the function task1 also plot the roots on a complex plane. Just call plot for the matrix with roots.
    • comment out the plot that you have just made. Extract real and imaginary parts of the roots and then plot them again by supplying real-valued x and y coordinates of the roots.
  2. task2 Solve the equation \(x + \sin(x)=1\).
  3. task3 solve the system of equations \(\begin{cases} x + \sin(y) = 1 \\ y + \cos(x) = 1 \end{cases}\)
  4. task4 You are given a real number a. Solve the equation \(e^x - e^{-x}=a\). Return for results from the function:
    • Solve with fsolve.
    • Solve with fzero, you will need to specify a segment where to search for the solution.
    • Solve with an explicit expression. Think, how to express x in terms of a.
    • The difference between solutions with the explicit expression and with fsolve.

    test your function on a = 1 000 000.

  5. task_circles The circle is specified with a matrix of three values: [x, y, r], that is its center and its radius. You are given two circles. Find their intersections. To do this, create a system of two equations, and use fsolve to solve it. Your function should return a matrix with two rows, where intersection points are defined by columns. The function should also draw both circles and use a special marker to mark intersections. Don’t use an explicit expression for evaluating intersections and don’t do any geometry. Just use fsolve.
    • To start, find at least one intersection point
    • Then find both points. Think, what initial points should be specified for fsolve.
  6. domino.m. Based on http://www.math.cornell.edu/~levine/18.312/alg-comb-lecture-18.pdf

    You are given natural numbers $m$ and $n$, they are the size of a rectangles made of cells. There are $m\times n$ cells, they are numbered from $1$ to $m\cdot n$.

    Create a matrix $A$ of size $m\cdot n\times m\cdot n$, in which the element at $i,j$ contains:

    • 1, if cells $i$ and $j$ are next to each other horizontally,
    • complex unit ($\texttt i$), if cells $i$ and $j$ are next to each other vertically,
    • 0 otherwise

    Evaluate the determinant of $A$ and take the square root of it. You will get the number of domino tilings of a $m\times n$ rectangle.

Plotting deadline: (not obligatory) Wednesday, 18 December

Don’t use loops, unless the opposite is explicitly stated.

  1. task1 Plot the function \(\sin(x) + \sin(3x)\) from \(-2\pi\) to \(2\pi\). The function task1 should both create a plot, and return two values: x and y with coordintes of points that were passed to plot(x,y).
  2. 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. Return the result the same way you did in the previous task.
  3. taskA You are given a matrix A of the size \(N\times2\), each row contains coordinates of one point on the plane. In this task the function should not return results, and it should only draw one picture consisting of the following:
    1. Draw set A
    2. Draw the center of mass for A.
    3. Create and draw the set B that is a translation of A 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)\).
    4. 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}\).
    5. 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.

  4. 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\).
  5. plot_line hard task You are given a linear matrix [a, b, c]. You are also given matrices xrange=[xmin, xmax] and yrange=[ymin, ymax]. Plot a line \(ax + by + c = 0\), and draw only that 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.

  6. 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.
  7. plot_lines. You are given a matrix a with lines (the same as matrices from the block octave-vectorization, you are also given xrange, yrange with ranges to draw the lines by means of the plot_line function from the previous task. Draw all of the lines from a. Use a loop to call plotting for each line.
  8. 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.