DAY 1 Basics of MATLAB Programming
DAY 1 Basics of MATLAB Programming
B.HANUMANTHA RAO,
Research Scholar, EEE
Contents:
1. Features of Matlab
2. Matlab Screen
3. MATLAB variables
4. Entering Arrays
5. Entering Tables
6. Plotting
7. Flow control statements
8. Tutorials
Features of Matlab:
High-level language of technical computing
Custom GUIs
• You can also provide the filename as a part of the edit command.
>> edit newScript
• Not only does the edit command create a new file, but it also opens existing files.
>> edit filename
• You may run a script by typing the file name without the extension in the
Command Window.
Code Sections:
How to divide the code into sections:
• To create a sections in script file type %% followed by space and then enter
section title.
• You can create several sections in your scripts this way.
• If your file is large, you can navigate your sections using goto drop down menu in
menu bar>choose one of the section titles you provided earlier>note that the
current section is highlighted in yellow.
Comments:
• Text following a percent sign, %, is a comment. A comment is not executed in the
code file. Comments can appear on lines by themselves, or they can be appended
to the end of a line.
• The first contiguous block of comments is reserved for help information which is
displayed when you use the help or doc commands.
• >>help filename
Creating vectors:
Creating Normal vectors:
• V=[1 2 3 4 5] A vector with 5 elements.
• V=[first:increment:last] A vector with equally spaced elements
and the 'increment' can be negative.
Creating Evenly-Spaced Vectors:
• In some cases, when creating an evenly-spaced vector, you may know the
number of elements you want the vector to contain, and not the spacing
between the elements. In these cases, you can use linspace.
• M=linspace(first,last,n) A vector with 'n' elements linearly spaced.
• Note that both the colon operator and linspace create row vectors.
TASK2: Execute all the commands in from serial no.4 to 5.
Vector Indexing:
• The location of each element in a vector is referred to as an index.
• In MATLAB, the index of the first element in a vector is 1. You can access an element by
providing the index within the parenthesis after the variable name.
• Instead of using a number as an index directly, you can use a variable that contains a number as an
index.
• The following command assigns the value of the variable y to the fourth element of the vector x.
>> x(4) = y;
• You can create new elements in a vector by assigning value to an index that doesn’t exist.
• When you separate the values by semicolons ; MATLAB creates a column vector (n-by-1)
>> varName = [1;3];
• The operator “ ' ” can also be used to calculate the complex conjugate transpose.
• If you want to transpose the vector of complex numbers without calculating the complex
conjugate, use the operator “ .' ”
• TASK8: Use all the above commands to import and export matrices.
Introduction of Table:
• You can use the table function with the workspace variables as inputs to create a table. The
following code creates a table, data with variables a, b, and c.
>> T = table(a,b,c)
• You can use the ‘array2table function’ to convert from a homogenous array to a table. The
following code creates a table, data from a matrix A.
>>T= array2table(A)
• The following code creates a table named data with custom variable names.
>> data = array2table(A, ‘VariableNames', {'X','Y','Z'})
• you can import the data programmatically in the command window using the readtable function.
>> EPL = readtable('EPLresults.xls')
• Use the writetable function to create a file from a table.
>> writetable(tableName,'fileName')
• The sortrows function returns the values sorted in ascending order rather than descending order.
You can supply the optional 'descend' parameter to the sortrows function to sort in descending
order.
>> tSort = sortrows(tableName, 'SortingVariable','descend')
• To sort on two variables, supply them in order to the sortrows function as a cell array.
>> tSort = sortrows(tableName, {'var1','var2'},'descend')
a=1
b=2.5
c=pi; ’ ; ’ suppresses display
3. Operations with the scalars:
c=a+b Addition.
c=a-b Subtraction.
c=a*b Multiplication.
c=a/b Division.
c=2+3i, d=3+j*4 Complex numbers.
4. Entering Vectors:
5. Operations on Vectors:
if x=[1:1:10]; y=[2:2:20]; (NOTE: Vectors must be of identical dimension)
Ex: x=[12 13 5 8]
a=x(2:3); a=[13 5];
b=x(1:end-1); b=[12 13 5];
MATLAB contains functions to help you find desired values within a
vector or matrix. Ex: vec = [5 3 1 9 7] To get the minimum value and
its index: [minVal,minInd] = min(vec); (max works the same way)
To find any the indices of specific values or ranges. ind = find(vec
== 9); ind = find(vec ≥ 2&vec ≤ 6)
7. Entering matrices:
If you want to change the color of your plot, you can use an additional
input to the plot function to do this. The following command will
plot y versus x in magenta.
plot(x,y,’m’)
Some other color codes are: ’b’ blue ,’g’ green, ’r’ red, ’c’ cyan, ’k’
black.
You can also change the line style using an optional argument. The
following command will plot y versus x as a dotted line.
plot(x,y,’:’)
Some other line style codes are: ’’ solid, ’ ’ dashed, ’.’ dash-dot.
You can even combine these codes! Generally, the order doesn’t
matter, so the following two commands produce the same plot using
a red dotted line.
plot(x,y,’r:’) plot(x,y,’:r’)
In addition to color and line style, you can specify a marker style.
The following command will plot y versus x using stars.
plot(x,y,’*’)
Some other marker style codes are:’.’ Points, ’o’ circles, ’x’ crosses.
19. Extracting Portions of a Table::
for - loop:
f or k = f irst : increment : last
−−−−
−−−−
end
if - statement:
if (condition)
−−−−
end
if -else statement:
if (condition1)
−−−−
elseif (condition2)
−−−−
else
−−−−
end
end
while -loop statement:
while(condition)
−−−−
end
switch switch expression
case case expression
statements
case case expression
statements
otherwise
statements
end
while expression
statements
end
a = 10;
while (a ¡ 20 )
fprintf(’value of a: a = a+1;
if( a ≥ 15)
% terminate the loop using break statement
break;
end
a = 10;
while a ¡ 20
if a == 15
a = a + 1;
continue;
end fprintf(’value of a: a = a + 1;
end
The electrical and electronics appliances installed in a residential building and their duration of operation,
power rating are given in the following tables. The maximum consumption limit (MCL) of a particular consumer
is assumed to be 2.5 kW throughout a day. The energy cost is Rs. 3/unit. The consumer has to pay a penalty of
Rs. 6 per unit for consumption above MCL.
Table -1 Table-2
Must_run Loads
Flexible Loads
Power Duration of Number
S.No Loads Without Scheduling
(kW) Operation of Loads
0.00 to 6.00 4 Power Starting
S.No Loads
6.00 to 9.00 2 (kW) Runtime Time of
1 Fan 0.1 (h)
17.00 to 21.00 2 Operation
21.00 to 24.00 4 Well
1 1.50 1 06.00
Tube 5.00 to 7.00 3 pump
2 0.06 PHEV
Light 18.00 to 22.00 6 2 2.30 3 06.00
0.00 to 5.00 3 charging
5.00 to 7.00 6 Washing
3 CFL 0.04 3 2.70 2 18.00
18.00 to 22.00 6 Machine
22.00 to 24.00 3
6.00 to 8.00 1
4 TV 0.25
17.00 to 22.00 1
6.00 to 7.00 2
Laptop/
7.00 to 8.00 2
5 Mobile 0.05
17.00 to 18.00 2
charging
18.00 to 19.00 2
Must_run=[0.52 0.52 0.52 0.52 0.52 0.82 0.92 0.55 0.2 0 0 0 0 0 0 0 0 0.55 1.15 1.05 1.05 1.05 0.52 0.52]
(Q) Compute the per day electricity bill payable to the utility.
<MCL >MCL
Total Consumption (kWh)
Electricity Bill (Rs.)
Total Electricity Bill Per Day (Rs.)
2. COMPUTATION OF POWER AND POWER FACTOR
Use voltage and current samples given in the excel file to compute the real power and power factor.
Write a MATLAB program.
V (rms)
I1 (rms)
P1
pf1
I2 (rms)
P2
pf2
I3 (rms)
P3
pf3
Questions
Table -1
Line No. From To Bus Resistance Reactance Half line
Bus (p.u.) (p.u.) charging
admittance
(p.u)
1 1 2 0.01008 0.05040 0.05125
2 1 3 0.00744 0.03720 0.03875
3 2 4 0.00744 0.03720 0.03875
4 3 4 0.01272 0.06360 0.06375