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

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')