Activity-1_Maghari-Intro-to-MATLAB
Activity-1_Maghari-Intro-to-MATLAB
Expt.No:1
Date :
OBJECTIVES
SOFTWARE REQUIRED
(i) MATLAB
1. INTRODUCTION TO MATLAB
MATLAB comprising lot of optional tool boxes and block set like control system,
optimization, and power system and so on.
Algorithm development
MATLAB is a widely used tool in electrical engineering community. It can be used for
simple mathematical manipulation with matrices for understanding and teachin basic mathematical
and engineering concepts and even for studying and simulating actual power system and electrical
system in general. The original concept of a small and handy tool has evolved to become an
4
engineering work house. It is now accepted that MATLAB and its numerous tool boxes replace
and/or enhance the usage of traditional simulation tool for advanced engineering applications.
Engineering personnel responsible for studies of electrical power system, control system
and power electronics circuits will benefit from the MATLAB. To expertise in Electrical System
Simulation one should have a basic understanding of electric circuits, power system and power
electronics.
To open the MATLAB applications double click the Matlab icon on the desktop. This will
open the MATLAB window space with Matlab prompt as shown in the fig.1.
>> quit
(Or)
>>exit
To select the (default) current directory click ON the icon […] and browse for the folder
named “D:\SIMULAB\xxx”, where xxx represents roll number of the individual candidate
inwhich a folder should be created already.
When you start MATLAB you are presented with a window from which you can enter
commands interactively. Alternatively, you can put your commands in an M- file and execute it at
the MATLAB prompt. In practice you will probably do a little of both. One good approach is to
incrementally create your file of commands by first executing them.
M-files can be classified into following 2 categories,
5
To load and run a M-file named “ybus.m” in the workspace
>>ybus
These M-files of commands must be given the file extension of “.m”. However M-files are
not limited to being a series of commands that you don‟t want to type at the MATLAB window,
they can also be used to create user defined function. It turns out that a MATLAB tool box is
usually nothing more than a grouping of M-files that someone created to perform a special type of
analysis like control system design and power system analysis. Any of the matlab commands (eg:
sqrt) is really an M-file.
One of the more generally useful matlab tool boxes is simulink – a drag and-drop dynamic
system simulation environment. This will be used extensively in laboratory, forming the heart of
the computer aided control system design (CACSD) methodology that is used.
>>simulink
At the matlab prompt type simulink and brings up the “Simulink Library Browser”. Each of
the items in the Simulink Library Browser are the top level of a hierarchy of palette of elements
that you can add to a simulink model of your own creation. At this time expand the “simulink”
pallete as it contains the majority of the elements you will use in this course. Simulink has built
into it a variety of integration algorithm for integrating the dynamic equations. You can place the
dynamic equations of your system into simulink in four ways.
1 Using integrators
2. Using transfer functions
3. Using state space equations
4. Using S- functions (the most versatile approach)
Once you have the dynamics in place you can apply inputs from the “sources” palettes and
look at the results in the “sinks” palette.
Finally the most important MATLAB features are its help. At the MATLAB Prompt
simply typing helpdesk gives you access to searchable help as well as all the MATLAB manuals.
>>helpdesk
To get the details about the command name sqrt, just type…
>>help sqrt
Where sqrt is the command name and you will get pretty good description in the MATLAB
window as follows.
/SQRT Square root.
SQRT(X) is the square root of the elements of X. Complex
results are produced if X is not positive.
6
Overloaded methods
help sym/sqrt.m
It eliminates all the variables in your workspace. For example start MATLAB and execute
the following sequence of commands
>>a=2;
>>b=5;
>>whos
>>clear all
The first two commands loaded the two variables a and b to the workspace and assigned
value of 2 and 5 respectively. The clear all command clear the variables available in the work
space. The arrow keys are real handy in MATLAB. When typing in long expression at the
command line, the up arrow scrolls through previous commands and down arrow advances the
other direction. Instead of retyping a previously entered command just hit the up arrow until you
find it. If you need to change it slightly the other arrows let you position the cursor anywhere.
Finally any DOS command can be entered in MATLAB as long as it is preceded by any
exclamination mark.
>>!dir
1.4 MATLAB Data Types
The most distinguishing aspect of MATLAB is that it allows the user to manipulate vectors
(like 5+j8) and matrices with the same ease as manipulating scalars (like5,8). Before diving into
the actual commands everybody must spend a few moments reviewing the main MATLAB data
types. The three most common data types you may see are,
>>a=4.2:
>>A=[1 4;6 3];
>>whos
Two things should be evident. First MATLAB distinguishes the case of a variable name
and that both a and A are considered arrays. Now let‟s look at the content of A and a.
7
>>a
>>A
Again two things are important from this example. First anybody can examine the
contents of any variables simply by typing its name at the MATLAB prompt. Second, when typing
in a matrix space between elements separate columns, whereas semicolon separate rows. For
practice create the matrix in your workspace by typing it in all the MATLAB prompt.
>>mytime =0:0.001:5;
Automatic construction of arrays of all ones can also be created as follows,
>>myone=ones (3,2)
Note:
Any MATLAB command can be terminated by a semicolon, which suppressed any
echo information to the screen.
Since MATLAB treats everything as an array, you can add matrices as easily as
scalars.
Example:
>>clear all
>> a=4;
>> A=7;
>>alpha=a+A;
>>b= [1 2; 3 4];
>>B= [6 5; 3 1];
>>beta=b+B
Of course cannot violate the rules of matrix algebra which can be understood from the following
example.
>>clear all
>>b=[1 2;3 4];
>>B=[6 7];
>>beta=b*B
In contrast to matrix algebra rules, the need may arise to divide, multiply, raise to a
power one vector by another, element by element. The typical scalar commands are used for this
“+,-,/, *, ^” except you put a “.” in front of the scalar command. That is, if you need to multiply the
elements of [1 2 3 4] by [6 7 8 9], just type...
8
>>[1 2 3 4].*[6 7 8 9]
>>help if
>>help for
>>help while
Example
: >>if z=0
>>y=0
>>else
>>y=1/z
>>end
Looping
:
>>for n=1:2:10
>>s=s+n^2
>>end
- Yields the sum of 1^2+3^2+5^2+7^2+9^2
1.7 PLOTTING
MATLAB‟s potential in visualizing data is pretty amazing. One of the nice features is that
with the simplest of commands you can have quite a bit of capability.
>>clear all
>>t=0:10:360;
>>y=sin (pi/180 * t);
9
The commands above provide the most plotting capability and represent several
shortcuts to the low-level approach to generating MATLAB plots, specifically the use of handle
graphics. The helpdesk provides access to a pdf manual on handle graphics for those really
interested in it.
1.8 Functions
z=[5 2 4;
1 4 5] as input, the output would be,
10
With this introduction anybody can start programming in MATLAB and can be
updated themselves by using various commands and functions available. Concerned with the
“Power System Simulation Laboratory”, initially solve the Power System Problems manually, list
the expressions used in the problem and then build your own MATLAB program or function.
EXERCISE:
Answer:
Exercise 2: Give a MATLAB expression that uses only a single matrix multiplicationwith B to
obtain
a) the sum of column 5 and 7 of matrix B.
answer:
11
Exercise 3: Write down the function g(t) that has the shape of a sine wave that increases linearly in
frequency from 0 Hz at t = 0 s to 5 Hz at t = 10 s.
a) Plot the graph of this function using MATLAB’s plot command.
b) Add to the same figure (this can be achieved using the hold command) in a different color a
graph of the same function sampled at 5 Hz, using the stem command.
Kindly submit your answer not later than next lab meeting.
CONCLUSION:
In summary, the objectives are focused on acquiring proficiency in MATLAB for solving power system problems
and developing MATLAB programs. The emphasis is on building the necessary skills and knowledge to effectively
apply MATLAB as a tool for addressing challenges within the realm of power systems. These objectives highlight the
practical aspect of using MATLAB for problem-solving and program development in the field of power system
engineering.
12
13