top of page

Introduction to Matlab

 

Introduction

In this course, you will learn how to use the MATLAB® command prompt for performing calculations and creating variables. Exercises include basic operations, and are designed to help you get familiar with the basics of the MATLAB interface. One of MATLAB's conveniences is its ability to work with lists of numbers. You will have the opportunity to practice constructing and manipulating lists, vectors, and matrices. Since the unit also serves as an introduction to programming, you will receive guidance on defining variables, storing values in variables, and changing the values of variables. 

 

  1. Arrays,matrices and operations

Arrays are the fundamental data type of MATLAB. As in many traditional languages, arrays in MATLAB are a collection of several values of the same type. They are indexed through the use of a single integer or, to get more than one value (also called element), an array of integers.

 

  • Declaring Arrays

Arrays can be represented in two forms: the row arrays where all the elements are on only one row and the column arrays with all itselements on the same column. They can also be considered as a matrice with respectively one row and several columns or vice versa. To create a row array, we can give the elements of the array inside the bracket separated with a comma or just a blank space. Just look at this example where we create an array called A by giving the elements on the command window:

>> A = [1,2,3,4] // or A = [1 2 3 4]

A =

  1 2 3 4

To create a column array we can use semicolons to separate the elements of the array inside the brackets:

>> A = [1;2;3;4]

A =

       1 2 3 4

Arrays can be multi-dimensional. To create a 2 dimensional array (a matrix in Linear Algebra terms), we have to give a list of comma separated values, and each row should be separated by a semi colon:

>> matrix = [1, 2, 3; 4, 5, 6]

matrix =

              1 2 3

    4 5 6

In MATLAB the term array is synonymous with matrix and will more often than not be referred to as a matrix. It should be noted that a matrix, as its mathematical equivalent, requires all its rows and all its columns to be the same size:

>> matrix = [1, 2, 3; 4, 5] ??? Error using ==> vertcat

All rows in the bracketed expression must have the same number of columns.

 

 

  • Properties of Matlab_arrays and matrices

Contrary to low level languages such as C, an array in MATLAB is a more high level type of data: it contains various information about its size, its data type, and so on.

>> array = [0,1,4,5];

>> length(array)

ans = 4

>> class(array)

ans = double

 

The number of rows and columns of the matrix can be known through the built-in size function. Following the standard mathematical convention, the first number is the number of rows and the second is the number of columns:

>> matrix = [1, 2, 3; 4, 5, 6];

>> size(matrix)

ans = 2 3

 

The goal of MATLAB arrays is to have a type similar to mathematical vectors and matrices. As such, row and column arrays are not equivalent. Mono-dimensional arrays are actually a special case of multi-dimensional arrays, and the 'size' function can be used for them as well.

>> size(array)

ans =

   1 4

 

Row and column do not have the same size, so they are not equivalent:

>> size(column)

ans =

   3 1

>> size(row)

ans =

   1 3

 

 

  • Operations

Matlab allows a very simple creation of matrices. With it we can easily manipulate matrices. In this part we will do simple operations on matrices like addition, multiplication, division and subtraction.Note that a vector is a matrice as well as a scalar can be considered as a matrice of size 1x1. With the command size we can get the size of the matrice, and length for the length.For example, let's take a simple matrice:

>> M = [0 1 2];

>> size(M)

ans =

   1 3

>> length(M)

ans =

          3

 

Now let's consider a scalar as matrice

>> s = 3;

>> size(3)

ans =

         1 1

>> length(a)

ans =

         1

To create automatically a vector of a certain length, we can write for example:

>> t = 0:1:10

t =

      0 1 2 3 4 5 6 7 8 9 10

 

Where t is a vector of length 11, going from 0 to 10 with an interval of 1. 

 

 

  • Addition and Subtraction

For the addition or subtraction of two vectors, they must be defined as column vectors.

>> a = [1;2;3];

b =[4;5;6];

>> a+b

ans =

           5

           7

           9

>> a-b

ans =

          -3

          -3

          -3

In the case we add or subtract a scalar to a vector, it will be added or subtracted to all elements of the vector.For the matrices, both matrices should have the same dimensions. Let's explain it in this example:

>> c = [1 2;3 4]  

c =

          1 2

   3 4

>> a+c

Error using +

Matrix dimensions must agree.

 

Let's correct it by creating a new matrice with the same dimension:

>> d = [5 4;3 4]

d =

    5 4

    3 4

>> c+d

ans =

    6 6

    6 8

 

 

 

  • Multiplication, Division

The multiplication of vectors or matrices is possible only if they are compatible. That means the number of lines of one should match to the number of columns of the other matrice. Let's try it with the matrices defined before:

>> a*b

Error using *

Inner matrix dimensions must agree.

>> c*d

ans =

    11 12 27 28

Sometimes we need to do a element-wise (pointwise) multiplication and for that we use a dot operator before the multiplication operator. In this the condition for the normal multiplication is not more valid, the matrices just need to have the same dimensions.

>> a.*b

ans =

    4 10 18

The division of two matrices is nothing else as the multiplication of the first one with the inverse of the other one.

 

 

2.  basic plot in Matlab

To represent functions in Matlab in a x-y coordinate, we use the function plot. To get more informations about that function, we can type on the command window the command: help plot. Let's start with a very simple example where we plot a common function y over the time x. Now we define our function:

>> x = 0:0.1:100;

>> y = x.^2 + 2*x +1;

>> plot(x,y)

grid on                                          % to activate the grid on the graph

title('y=f(x)')                                  % Title of the figure

xlabel('time x')                              % to show the x-coordinate

ylabel('Function values y')

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Let's plot a sine function. We define a new time vector t going from 0 to 10 and a sine function called y.

>> t = 0:0.1:10;

>> y = sin(t);

>> plot(t,y)

grid on                                    % to activate the grid on the graph

xlabel('time [s]')                       % to show the x-coordinate

ylabel('sin(t)')

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.  Introduction to Simulink

Simulink, developed by MathWorks, is a data flow graphical programming language tool for modeling, simulating and analyzing multidomain dynamic systems. Its primary interface is a graphical block diagramming tool and a customizable set of block libraries. It offers tight integration with the rest of the MATLAB environment and can either drive MATLAB or be scripted from it. Simulink is widely used in control theory and digital signal processing for multidomain simulation and Model-Based Design.

  • How to create a Model ?

Before creating a model, we need to start Matlab and then Simulink by entering the command Simulink in the Matlab command window. The Simulink Library Browser opens. From the Simulink Library Browser menu, select File > New > Model. A Simulink editor window opens with an empty model. - Select File > Save as. The Save As dialog box opens. - In the File name box, enter a name for your model, and then click Save. For example, enter simple_model. Your model is saved with the file name simple_model.mdl.

  • How to add Blocks ?

In the Simulink library browser we opened before, we can find the needed blocks to build our model. We will build as example a very simple model in the simple_model file. Our model should plot the sine function ploted in the previous chapter.In the Sources library we can choose the signal we need as input, in our case we choose the sine signal.In the Sinks library we choose the scope blöck to visualize the signal.

By double-clicking on the Sine Wave block we can configure the parameters of the Sine function like in the Matlab example from previous chapter by setting the amplitude and the frequency to 1.To connect the blocks together, click and drag a line from the output port of the Sine wave block to the input port of the scope block. By releasing the mouse button over the output port, Simulink connects the blocks with an arrow indicating the direction of signal flow.

 

 

© Eric Kamdem - 2014

bottom of page