Electrical Engineering Tutorial

A blog to help electrical engineers and students to learn electrical engineering topics. Basic Electrical Engineering, Power Systems, Electrical Machines, tec.. Now special course on MATLAB for Beginners.

Class

Class

Thursday, January 23, 2020

Methods to Improve the Calculation Speed of MATLAB Programs - Part 2



Improving Performance of the Code

MATLAB programs are interpreted. Hence large MATLAB programs will take a long time to execute. The power of MATLAB is realized with its extensive set of libraries which are compiled or are carefully coded in MATLAB to utilize 'vectorization'.
The concept of vectorization will help to write efficient MATLAB code. The speed of a numerical algorithm in MATLAB is very sensitive to whether or not vectorized operations are used.

This section presents some basic considerations to writing efficient MATLAB routines. It is possible, of course, to become obsessed with performance and waste man-hours of programming time in order to save a few seconds of execution time. The ideas presented below require little extra effort, yet can yield significant speed improvements. Once you understand how to use these procedures they will become natural parts of your MATLAB programming style.


Using vector operations instead of loops


MATLAB is a matrix language, which means it is designed for vector and
matrix operations. You can often speed up your M-file code by using vectorizing
algorithms that take advantage of this design. Vectorization means converting
for and while loops to equivalent vector or matrix operations.
Simple Example of Vectorizing
Here is one way to compute the sine of 1001 values ranging from 0 to 10:
i = 0;
for t = 0:.01:10
i = i + 1;
y(i) = sin(t);

end
A vectorized version of the same code is
t = 0:.01:10;
y = sin(t);
The second example executes much faster than the first and is the way
MATLAB is meant to be used. Test this on your system by creating M-file
scripts that contain the code shown, and then using the tic and toc functions

to time the M-files.




Consider the following loop, translated directly from Fortran or C
         dx = pi/30;
         nx = 1 + 2*pi/dx;
         for i = 1:nx
            x(i) = (i-1)*dx;
            y(i) = sin(3*x(i));
         end
The preceding statements are perfectly legal MATLAB statements, but they are an inefficient way to create the x and y vectors. Recall that MATLAB allocates memory for variables on the fly. On the first time through the loop (i=1), MATLAB creates two row vectors x and y, each of length one. On each subsequent pass through the loop MATLAB appends new elements to x and y. Not only does this incur extra overhead in the memory allocation calls, the elements of x and y will not be stored in contiguous locations in RAM. Thus, any subsequent operations with x and y, even though these operations may be vectorized, will take a performance hit because of memory access overhead.
The preferred way to create the same two x and y vectors is with the following statements.
         x = 0:pi/30:2*pi
         y = sin(3*x);
The first statement creates the row vector, x, with 1 + pi/15 elements stored in contiguous locations in RAM. The second statement creates a new (matrix) variable, y, with the same number of rows and columns as x. Since x is a row vector, as determined by the preceding step, y is also a row vector. If x were, for example, a 5 by 3 matrix, then y = sin(3*x) would create a 5 by 3 matrix, y.
MATLAB is designed to perform vector and matrix operations efficiently. To take maximum advantage of the computer hardware at your disposal, therefore, you should use vectorized operations as much as possible.

Pre-allocating memory for vectors and matrices

for and while loops that incrementally increase, or grow, the size of a data
structure each time through the loop can adversely affect performance and
memory use. Repeatedly resizing arrays often requires that MATLAB spend
extra time looking for larger contiguous blocks of memory and then moving the
array into those blocks. You can often improve on code execution time by
preallocating the maximum amount of space that would be required for the

array ahead of time.
The following code creates a scalar variable x, and then gradually increases the
size of x in a for loop instead of preallocating the required amount of memory
at the start:
x = 0;
for k = 2:1000
x(k) = x(k-1) + 5;
end
Change the first line to preallocate a 1-by-1000 block of memory for x initialized
to zero. This time there is no need to repepatedly reallocate memory and move
data as more values are assigned to x in the loop:
x = zeros(1, 1000);
for k = 2:1000
x(k) = x(k-1) + 5;

end




Though MATLAB will automatically adjust the size of a matrix (or vector) it is usually a good idea to pre-allocate the matrix. Pre-allocation incurs the cost of memory allocation just once, and it guarantees that matrix elements will be stored in contiguous locations in RAM (by columns).
Consider the following (admitedly artificial) sequence of statements.
         dx = pi/30;
         nx = 1 + 2*pi/dx;
         nx2 = nx/2;
 
         for i = 1:nx2
            x(i) = (i-1)*dx;
            y(i) = sin(3*x(i));
         end
 
         for i = nx2+1:nx
            x(i) = (i-1)*dx;
            y(i) = sin(5*x(i));
         end
Since we know the size of x and y, a priori, we can pre-allocate memory for these vectors. Pre-allocation involves creating a matrix (or vector) with one vectorized statement before any of the matrix elements are referenced individually. The ones and zeros functions are typically used to pre-allocate memory.
Here is an improvement of the preceding statements with pre-allocation of memory for x and y.
         dx = pi/30;
         nx = 1 + 2*pi/dx;
         nx2 = nx/2;
 
         x = zeros(1,nx);      % pre-allocate row-vectors, x
         y = zeros(1,nx);      % and y
 
         for i = 1:nx2
            x(i) = (i-1)*dx;
            y(i) = sin(3*x(i));
         end
 
         for i = nx2+1:nx
            x(i) = (i-1)*dx;
            y(i) = sin(5*x(i));
         end
The statements x(i) = ..., and y(i) = ... still do not take advantage of possibilities for vectorization, but at least the elements of x and y are stored contiguously in RAM. We will improve the efficiency of the loops shortly. First, however, note that we could have written the pre-allocation statements as
         x = zeros(1,nx);      % pre-allocate row-vectors, x
         y = x;                % and y
The statement y = x does not mean that y will stay equal to x. It simply creates another matrix y with the same ``shape'' as x. Understanding that pre-allocation is important for efficiency will help you understand these apparently confusing twists of MATLAB programming logic.
We can further improve our loops by pulling the assignment of x out of the loops.
         x = 0:pi/30:2*pi;     % vectorized calculation of x
         nx = length(x);
         nx2 = nx/2;
 
         y = x;                % pre-allocate memory for y
 
         for i = 1:nx2
            y(i) = sin(3*x(i));
         end
 
         for i = nx2+1:nx
            y(i) = sin(5*x(i));
         end
Finally, if we're obsessed with performance, we observe that the calculation of y can also be vectorized. To do this we use the colon notation to refer to segments of the x and y vectors.
         x = 0:pi/30:2*pi;     % vectorized calculation of x
         nx = length(x);
         nx2 = nx/2;
 
         y = x;                             % pre-allocate memory for y
 
         y(1:nx2) = sin(3*x(1:nx2));        % compute first part of y
         y(nx2+1:nx) = sin(5*x(nx2+1:nx));  % and the second part
To those new to MATLAB programming, the preceding statements may appear unecessarily obfuscated. The comment statements, of course, help, but the logic behind the logic comes from a true understanding of vectorization. Once you get the hang of MATLAB's colon notation you too will come to write code like this. Whenever the speed of MATLAB code is important, there is no substitute for vectorization.


Coding Loops in a MEX-File
If there are instances where you cannot vectorize and must use a for or while
loop, consider coding the loop in a MEX-file. In this way, the loop executes
much more quickly since the instructions in the loop do not have to be

interpreted each time they execute.

Functions Are Generally Faster Than Scripts
Your code executes more quickly if it is implemented in a function rather than
a script.
Load and Save Are Faster Than File I/O Functions
If you have a choice of whether to use load and save instead of the low-level
MATLAB file I/O routines such as fread and fwrite, choose the former. load
and save have been optimized to run faster and reduce memory fragmentation.
Avoid Large Background Processes
Avoid running large processes in the background at the same time you are
executing your program in MATLAB. This frees more CPU time for your

MATLAB session.

Clearing Unused Variables from Memory
If you use pack and there is still not enough free memory to proceed, you
probably need to remove some of the variables you are no longer using from

memory. Use clear to do this.


Correct code is always more important than speed

Vectorization sometimes makes MATLAB code hard to read. If you find yourself puzzling over the code, or more importantly if you find yourself wondering if the code performs the correct calculation, then stop trying to optimize performance.
Always remember


Code that gives incorrect, or inaccurate results is useless, no matter how fast it executes.

Wednesday, February 22, 2017

Methods to Improve the Calculation Speed of MATLAB Programs - Part 1



Introduction

Speed of execution of code is an important issue for all coders. In this lecture we will discuss some programming practices to improve the performance of MATLAB code.
Analyzing Performance of the Code 

MATLAB has some built is tools to help the coder to analyse the performance of his code. The two main tools are The M-file Profiler graphical user interface and the stopwatch timer. These tools help the coder to check information on how the program is performing

The M-File Profiler

Profiler help you to analyse the performance of the code and determine where you can modify your code to improve performance. M-file Profiler, a graphical user interface that shows you where your program is spending its time during execution. The Profiler can be more useful in measuring relative execution time and in identifying specific performance bottlenecks in your code.

Running the Profiler
Use one of the following methods:
  • On the Editor tab, in the Run section, click Run and Time. If you use this method, the Profiler automatically profiles the code in the current Editor tab.
  • On the Home tab, in the Code section, click Run and Time.
  • In the Command Window, type profile viewer.



Stopwatch Timer Functions ( tic and toc)
Stopwatch functions is a tool which provide absolute time measurements of execution for your code.If you just need to get an idea of how long your program takes to run, or to compare the speed of different implementations of a program, you can use the stopwatch timer functions.. Invoking tic starts the timer, and the first subsequent toc stops it and reports the time elapsed between the two.

Syntax:
tic
run the program section to be timed
toc

Shorter programs will run too fast it may be difficult to use timer function. In this case, we can measure the time by running the program repeatedly in a loop, and then average to find the time for a single run.
tic
for k = 1:100
run the program
end
toc

Sunday, January 15, 2017

User Defined Functions in MATLAB Part-4



Function Function, Local Function, and Private Function



Function Function

A MATLAB function that accepts another function as an input is called a function function. Function handles are used for passing functions to function functions. Syntax for function function is same as simple functions, but one or more input arguments will be function handles.

Local function - Sub Function

Multiple functions within one function file is called local function. Name of function file should be name of main function. Main function can be called from the command window or any other function. Local functions are typed in any order after the main function. Local functions are only visible to other functions in the same file. 


Private Function

A private function is a function residing in a sub directory with the name private. Private functions are visible only to functions in the parent directory. They are useful when we want to limit the scope of a function. You cannot call the private function from the command line or from functions outside the parent of the private folder.

Wednesday, January 4, 2017

Anonymous and Inline Functions in MATLAB



Anonymous Functions

MATLAB's anonymous functions provide an easy way to specify a function. An anonymous function is a function defined without using a separate function file. It is a MATLAB feature that lets you define a mathematical expression of one or more inputs and either assign that expression to a function. This method is good for relatively simple functions that will not be used that often and that can be written in a single expression.
An anonymous function of any number of variables can be created by giving the @ symbol, followed by a variable list, followed by the MATLAB expression. Anonymous function can be written in Command Window, script file, or inside user-defined function. Anonymous functions can only have one expression and can only return a single variable.

To give the anonymous function a name, simply put the function's name on the left side of an equal sign and the anonymous function on the right. 
                     NAME = @(ARGLIST)EXPRESSION 
  • NAME is name of the function. (using rules for names of user-defined functions) 
  • @ - a function handle, an object that has information about the function 
  • ARGLIST is the input arguments (a comma-separated list). 
  • The body of the function, to the right of the parentheses, is a single line MATLAB expression.

Inline Functions

The inline command lets you create a function of any number of variables by giving a string containing the function followed by a series of strings denoting the order of the input variables. This method is good for relatively simple functions that can be written in a single expression. It is similar to an Anonymous Function. Inline functions cannot access variables in the work space, even if those variables are global – this is the main difference with the anonymous functions.
The expression to be evaluated is defined in single quotes, followed in order by the variables of the function also surrounded by single quotes. 
                       Name = inline(‘expression', 'arg1', ‘arg2')


Monday, December 26, 2016

User Defined Functions in MATLAB Part -2



Previous post we discussed what is a user defined function in MATLAB. This lecture we will discuss how to define a function and how to call the function in a script.



Components of a function is discussed in the previous lecture. The first statement in a function must be function definition.The basic syntax of a function definition is:

function[a, b, c]= basicmath(x,y)

Basically a function accepts an input vector, perform the operation, and returns a result. The sample function given has two input variables and three output variables. But we can also have functions without input or/and output.

Under some special occasions we may need some functions without any input or output.

Some functions will take some input arguments but will not return any output arguments. Functions to plot some shapes or curves and functions to display some message are examples.

A function can be called from the command window or inside a script or function. To run a function in command window, just type the name of comment with proper input and output arguments.