Wednesday, September 30, 2015

Sudoku App for Android

Today some offtopic-theme: Next to my Sudoku page I now wrote an Android app, with which the Sudokus can be solved easily and conveniently on the smartphone. I would be happy, if some checked it out, or even got interested themselves in app programming. I wrote this app in Java, about the creation of Android apps with C# I wrote an introduction on this blog.
The app can be downloaded for free via Google Play Store.

Saturday, September 19, 2015

Matlab Tutorial Part 5 - Plots

In today's post I want to give a short introduction into the creation of plots with Matlab. It will only be a short summary, for more details I refer to the documentation of Matlab.  There all kinds of plots are listed with parameters, it is explained for example how to label axis, change colors etc.
Here I want to describe 2 kinds of 2 - dimensional plots, namely line and bar plots, as well as the 3 -dimensional surface plot.
We create a line plot with the function plot(). For this we first create some test data via x = rand(8, 1), a new vector with 8 random values between 0 and 1.
Then plot(x) gives us the following result:

bar() creates a bar chart from the given data. bar(x) looks as follows:

Now to a 3 - dimensional plot, the surface plot. This displays (for example) an inputted 2 -dimensional matrix in the third dimension (through height and color). On the point with the coordinates (i, j) thus the (i, j)th - entry of the matrix is displayed, meaning its value is represented by the z - coordinate and additionally colored corrrespondingly.
As an example via A = rand(32) we create a random 32 x 32 matrix. surf(A) then creates the following diagram:

Friday, September 18, 2015

Matlab Tutorial Part 4 - Scripts and Functions

In Matlab we have the possibility to create scripts and functions containing often used code, helping us to not having to type it frequently.
The scripts and functions are saved in files with the ending .m. Scripts are simply collections of lines of code, whereas functions have some return value.
We can edit these files conveniently out of Matlab: For that we edit the command edit Name.m and confirm. Then an editor opens with this file.
As an example we create the script MyScript.m with the following content:

A = rand(100);
d = det(A)

So a random 100 x 100 matrix A is created and the determinant d calculated. In Matlab we can call this script by entering the name MyScript. Then the script is executed and the variable d outputted. What is outputted we can control via the character ";". If we end one line with a semicolon, it is executed without output. If we omit it, the result of the operation is outputted. So if we remove the semicolon in the first line of the script, the complete matrix is displayed as well.

As a second example we now create a function which calculates and returns the average over the given values. For this we enter edit MyFunction.m.
As content we input:

function [y] = MyFunction(x)
y = sum(x);
y = y / numel(x);

As one can see a function declaration starts with the keyword function and ends with end. In square brackets we first list the return values - here only y is returned, multiple parameters are simply separated by comma. After the equal sign we write the name of the function (which should coincide with the file name) as well as the input parameters in round brackets - here only x.
In the function we then sum over x (thus we expect a vector) and then divide this by the number of elements in x. The result is then returned by the function in y. In Matlab we then can call the function and use the return value as follows: test = MyFunction([1,2,3,])
(Note: In Matlab of course the average value can be calculated easier - for example via the predefined function mean(). I only chose this example for explanation.)

Thursday, September 17, 2015

Matlab Tutorial Part 3 - Solve a System of Linear Equations

In today's part I want to show how to solve a system of linear equations. A system of linear equations has the form A * x = b, where A is the matrix representing the coefficients and b the right side of the equation system. We want to solve for x.
For this we simpy have to input A and x in Matlab and then call the command

x = linsolve(A, b)

One example:
We want to solve

x1   + x2     + x3   = 5
2x1             + 3x3 = 2
          10x2 - x3    = 1

Thus in Matlab we enter the matrix of coefficients (A = [1, 1, 1; 2, 0, 3; 0, 10, -1]) as well as the vector b (b = [5;2;1]).
After entering x = linsolve(A, b) Matlab returns the correct answer

x =


Tuesday, September 15, 2015

Matlab Tutorial Part 2 - Matrices and Vectors

After I gave in the previous post a little introduction into the programming language Matlab, I want to continue with this post in that topic.
Here I want to write about the usage of matrices and vectors, for which the language is optimized and which can be used very conveniently.
A matrix is defined in square brackets, entries of one row are separated by comma, rows by semicolon, so for example: A = [1, 2; 3, 4]
Vectors of course are only special cases of matrices, namely one-dimensional ones. Thus they can be defined via V1 = [1, 2] (row vector) or V2 = [3;4] (column vector) - in the following I will only use the term matrix.
There are different predefined Matlab function for creating certain matrices: Here I want to mention ones(), zeros() and rand().
ones(a, b) creates a a x b matrix (a rows, b columns) with all entries a 1. This can also be done for higher dimensions, if only one parameter is passed over a 2-dimensional square matrix is created.
zeros() and rand() do the same, zeros() fills the resulting matrix with zeros, rand() with random entries between 0 and 1.
With matrices we can calculate as we are used to. Via +, -, * we can do matrix addition, subtraction and multiplication. * is overloaded and can also be used for a scalar multiplication between a scalar (a number) and a matrix.
In the Matlab documentation all possible functions regarding matrices are listed, like for example determining the rank (rank()) and and and.
Another intesting function when using matrices is the extraction of submatrices.
We can define a submatrix of the matrix A consisting of the rows a to b and columns c to d as follows: A(a:b, c:d)
When emitting the colon the dimension is taken completely: A(:, c:d)
Via the function sum() we thus can for example conveniently sum over the rows / colums of a matrix, for row i the command for this is: sum(A(1,:))
In the next post I will show another interesting application, there we describe the solving of equation systems.

Wednesday, September 2, 2015

Matlab Tutorial Part 1 - Introduction

After excursions into app programming for Android and internet programming with PHP I now want to extend the theme field of this blog a bit further and give an overview about the programming language Matlab. This is a commercial software by the company The MathWorks and is especially suitable for solving and visualizing mathematical problems. Matlab is very popular and used for example in the industry a lot. This is due to its simplicity, it was designed for mathematical use cases, quickly problems can be modelled and solutions tested. One tradeoff though is the performance, optimized C++ code can be faster by a factor of 50 - 100.
First Matlab has to be installed. As already mentioned the software is commercial, but luckily in many universities for example for students free. In this case Matlab can be installed from these sources. After a successfull installation and registration the program can be started, one should see roughly this picture:

In the middle window, the Command Window, we now can directly enter commands and this way execute code. Here one can grasp the simplicity of the scripting language Matlab quickly: Variables for example do not have to be defined, there are no types, numbers, matrices etc. can just be used.
Let us enter A = 1 and confirm with Enter, after that B = 2 and confirm too. In the Matlab environment now A and B are saved as the corresponding numbers, which we then can use, for example via A + B.
Just like that we can also define matrices (and thus vectors): Values in one row are separated by a comma, rows with semicolon, the whole matrix is cornered by brackets. C = [1, 2; 3, 4] defines the corresponding matrix, after pressing Enter we then also see this graphically in the output window.
With these numbers and matrices we can now operate as we are used to, try for example B * C + C to execute a scalar multiplication followed by matrix addition.
This should suffice for a first introduction, soon other posts will follow.