This document provides an introduction to using Matlab and Matlab-like packages such as Freemat for numerical computing. It discusses downloading and installing Freemat, the basic GUI interface, creating and manipulating variables, common operations on variables like addition and multiplication, and defining functions to encapsulate commands.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
48 views
Getting Started With Matlab: The GUI Window
This document provides an introduction to using Matlab and Matlab-like packages such as Freemat for numerical computing. It discusses downloading and installing Freemat, the basic GUI interface, creating and manipulating variables, common operations on variables like addition and multiplication, and defining functions to encapsulate commands.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4
Getting Started with Matlab (and Matlab-like packages)
Getting Started with Matlab
The numerical examples and exercises of this course can be run on a numerical package such as Matlab. Although numerical toolboxes are not ideal from the programming point of view, they do provide an extremely convenient sandbox for quick testing of ideas and for basic exercises where performance is not at a premium. Matlab is a commercial product and, although it can be obtained for free or at a discount if you are a student, it may still be expensive to license in the long run. Free alternatives to Matlab exist; in the following, in the interest of generality, we will use Freemat but Octave is another good candidate with extensive Matlab compatibility, although it does not come with a GUI by default. Freemat can be downloaded here.
The GUI window Upon launching Freemat, the following window appears.
The main window, called Command Window , is the place where user inputs command following the "-->" sign. This window contains as well several other embedded windows on the left. In particular, the File Browser window display the content of the working directory, the History window an history of all past commands and the Variables window the list of variables in the workspace (so far none).
To change the working directory, click on the folder symbol, browse and select the desired directory. The drop down menu on the left of this symbol contains a list of the previously used working directories.
Variables In numerical packages there is no need to declare a variable (or its type) ahead of use. It is up to the user to ensure the operations between two or more variables are possible and/or meaningful depending on their types. Note also that the name of the variables is case sensitive. To create and instantiate a 13 vector x=(1,2,3), write in the command line
x = [1 2 3];
A variable named x appears in the Variables window. To display its value, enter in the command line its name without a trailing semi-colon,
x
or double click on the variable name in the Variable window. When instantiating a variable, its value is displayed if the terminal semi-colon is omitted, for example,
x = [1 2 3]
To create a 31 vector y=(1,2,3)T, write
y = [1; 2; 3];
Note the use of the semi-colon to separate the elements on each row. Following the same idea, a matrix X=[1 2 3 4 5 6 7 8 9]
X = [1 2 3; 4 5 6; 7 8 9];
Suppose we want to create a large vector containing only 0s. Of course we could write in the command line
x = [0 0 0 0 0 0 0 0 0 0 0];
A more elegant and practical way of obtaining this is to use the zeros(...) function. It takes as input the desired size of the resulting matrix, in our case,
x = zeros(1, 11);
Likewise, the function ones(...) creates a matrix of all 1s and rand(...) a matrix containing random values drawn uniformly in the unit interval. Finally, the function
length(x)
display the length of a vector x and
size(x)
its size. To remove a variable from the workspace, enter
clear x
and
clear all
to remove all variables from the workspace. Operations Once we have created and instantiated variables, we can perform operations on them, for example, an addition
a = ones(10, 1); b = ones(10, 1); a+b
The compute the transpose of a vector xT, use the symbol "'"
a = rand(10, 1) size(a) a = a' size(a)
Commonly used operations are summarized in the following table
+ Addition - Substraction * Matrix multiplication .* Element-wise multiplication / Division ./ Element-wise division ^ Power .^ Element-wise power
Functions It is very useful to encapsulate a set of commands into a function. To do so, create a new .m file (File > New File) starting with the following declaration
function [list_of_outputs] = my_function(list_of_inputs)
where list_of_outputs (resp. list_of_inputs) is a (possibly empty) list of output variables, separated by a comma. Then save the file (File > Save as) under the name my_function.m. For example, the following functions computes the two roots of a polynomial of degree 2 whose coefficients are stored in decreasing power in a vector $x$.
function [root1, root2] = compute_roots_2(x) a = x(1); b = x(2); c = x(3); delta = b^2 - 4*a*c; root1 = (-b + sqrt(delta))/2*a; root2 = (-b - sqrt(delta))/2*a; return
To call the function, simply call it from the command line.
x = [1 2 1]; [root1, root2] = compute_roots_2(x)
Attention! In order for your function to be available on the command line, ensure that (a) the function my_function is saved in the file my_function.m (case sensitive) and (b) your working directory corresponds to the directory where the function is saved.