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

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.



Thursday, December 1, 2016

User Defined Functions in MATLAB - Part 1



User-defined functions are similar to the MATLAB predefined functions. A function is a MATLAB program that can accept inputs and produce outputs. A function can be called or executed by another program or function. Code for a function is done in an Editor window or any text editor same way as script and saved as m-file. The m-file must have the same name as the function.

It is important that you give meaningful variable names to variables inside a function that you write, so that you and others can understand what the function does. The function M-file must be saved in your current directory.


The declaration statement 'function' is used to define the file as a function. It must be typed in lower case letters. Rules for giving function name is same as the rules for variable names.
Input arguments are typed in side the parentheses ( ). They are used to transfer data into function from calling program. Can be zero or more input arguments. 
Output arguments are typed inside the square brackets [ ]. They are used to transfer data out of function to calling program. Can be zero or more output arguments. It is important to give a meaningful variable names.

Tuesday, November 22, 2016

MATLAB Programming - Loop Control Part 2




In this lecture we will discuss more about Loop control.

- Switch – case commands

- Nested lops

- break command

- continue command

switch - case Command

There is no conditional statement used in this looping structure. It choose the code to be executed based on value of scalar or string, not just true/false. Different codes can be executed based on the value of the scalar defined in the switch command. The codes are defined using the case command.

Nested Loop

A nested loop is a loop or conditional statement placed inside another loop or conditional statement. Simply, a loop within a loop. Both for and while loops can be nested.

break Statement

The break statement terminates execution of for or while loop. Statements in the loop that appear after the break statement are not executed. In nested loops, break exits only from the loop in which it occurs. Control passes to the statement following the end of that loop.



continue Statement


The continue statement is used for passing control to next iteration of for or while loop. The continue statement in MATLAB works somewhat like the break statement. Instead of forcing termination, however, 'continue' forces the next iteration of the loop to take place, skipping any code in between.



Wednesday, October 26, 2016

MATLAB Script - Loop Control Part 1




In this lecture we will discuss about another flow control method – Loop control. A loop control is used to execute a set of commands repeatedly The set of commands is called the body of the loop.

MATLAB has two loop control techniques 
  1. Counted loops - executes commands a specified number of times. 
  2. Conditional loops - executes commands as long as a specified expression is true.
The counted loops are called ‘for’ loop and the conditional loops are called ‘while’ loop. The keyword “end” is used to show the end of a Loop.


For loop is used when the numer of iteration is known. FOR Repeats for specified number of times ALWAYS executes computation loop at least once. For loop creates a vector of the variable defined in the command and execute the body of the loop for all the values of the vector. 

The while- loop used when the number of loop iterations is unknown. Number of iteration is controlled by a condition. The condition is tested in each iteration and stop looping when it is false. While loop will execute the body of loop ONLY if while condition is met. 

Both Scripts and functions also allow to loop using for and while loops. 

Conditional Control in MATLAB Scripts with Examples




In this lecture we discuss more about programming in MATLAB. One main section in programming is flow control. There are many flow control commands in MATLAB. In this lecture we discuss the conditional flow control commands.
Conditional statements are commands that allows MATLAB to decide whether or not to execute some code that follows the statement

Conditional statements use relational operators like ==,~=,>,< (Note that are all scalar tests).

The basic syntax is


žIf the conditional expression is true, MATLAB runs the lines of code that are between the line with if and the line with end.
If the conditional expression is false, MATLAB skips the code between if and the line with else and runs the code up to end.