0% found this document useful (0 votes)
15 views

Activity-1_Maghari-Intro-to-MATLAB

This document serves as an introduction to MATLAB, outlining its capabilities for solving power system problems and developing programs. It covers essential features, including data types, conditional statements, plotting, and the use of M-files for function creation. The document concludes with exercises aimed at reinforcing the skills necessary for utilizing MATLAB in power system engineering.

Uploaded by

s2302810
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Activity-1_Maghari-Intro-to-MATLAB

This document serves as an introduction to MATLAB, outlining its capabilities for solving power system problems and developing programs. It covers essential features, including data types, conditional statements, plotting, and the use of M-files for function creation. The document concludes with exercises aimed at reinforcing the skills necessary for utilizing MATLAB in power system engineering.

Uploaded by

s2302810
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

INTRODUCTION TO MATLAB

Expt.No:1
Date :

OBJECTIVES

(i) To procure sufficient knowledge in MATLAB to solve the power system


Problems

(ii) To write a MATLAB program

SOFTWARE REQUIRED

(i) MATLAB

1. INTRODUCTION TO MATLAB

MATLAB is a high performance language for technical computing. It integrates


computation, visualization and programming in an easy-to-use environment where problems and
solutions are expressed in familiar mathematical notation.

MATLAB is numeric computation software for engineering and scientific calculations.


MATLAB is primary tool for matrix computations. MATLAB is being used to simulate random
process, power system, control system and communication theory.

MATLAB comprising lot of optional tool boxes and block set like control system,
optimization, and power system and so on.

1.1. TYPICAL USES INCLUDE

 Math and computation

 Algorithm development

 Modeling, simulation and prototype

 Data analysis, exploration and Visualization

 Scientific and engineering graphics

 Application development, including graphical user interface building

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.

1.2. GETTING STARTED WITH MATLAB

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.

Fig-1: MATLAB window space

To quit from MATLAB type…

>> 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,

i) Script M-files – Main file contains commands and from which


functions can also be called
ii) Function M-files – Function file that contains function command at the
first line of the M-file
M-files to be created by you should be placed in your default directory. The M-files
developed can be loaded into the work space by just typing the M-file name.

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.

See also SQRTM.

6
Overloaded methods
help sym/sqrt.m

1.3 MATLAB WORKSPACE


The workspace is the window where you execute MATLAB commands (Ref. figure-1). The
best way to probe the workspace is to type whos. This command shows you all the variables that
are currently in workspace. You should always change working directory to an appropriate
location under your user name.

Another useful workspace-like command is


>>clear all

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,

1) arrays 2) strings 3) structures


As for as MATLAB is concerned a scalar is also a 1 x 1 array. For example clear your
workspace and execute the commands.

>>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.

>>B= [3 0 -1; 4 4 2;7 2 11];


(use semicolon(;) to represent the end of a row)
>>B
Arrays can be constructed automatically. For instance to create a time vector where the
time points start at 0 seconds and go up to 5 seconds by increments of 0.001

>>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.

1.5 Scalar versus Array Mathematical Operation

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]

1.6 Conditional Statements

Like most Programming languages, MATLAB supports a variety of conditional statements


and looping statements. To explore these simply type

>>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.

Graphs can be plotted and can be saved in different formulas.

>>clear all
>>t=0:10:360;
>>y=sin (pi/180 * t);

To see a plot of y versus t simply type,


>>plot(t,y)
To add label, legend, grid and title use

>>xlabel („Time in sec‟);


>>ylabel („Voltage in volts‟)
>>title („Sinusoidal O/P‟);
>>legend („Signal‟);

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

As mentioned earlier, a M-file can be used to store a sequence of commands or a user-


defined function. The commands and functions that comprise the new function must be put in a
file whose name defines the name of the new function, with a filename extension of '.m'.A function
is a generalized input/output device. That is you can give some input.(arguments) and provides
some output. MATLAB functions allow you much capability to expand MATLAB‟s usefulness.
We will just touch on function here as you may find them beneficial later.
We will start by looking at the help on functions :
>>help function
We will create our own function that given an input matrix returns a vector containing
theadmittance matrix(y) of given impedance matrix(z)‟

z=[5 2 4;
1 4 5] as input, the output would be,

y=[0.2 0.5 0.25;


1 0.25 0.2] which is the reciprocal of each elements.
To perform the same name the function “admin” and noted that “admin” must be
stored in a function M-file named “admin.m”. Using an editor, type the following commands and
save as “admin.m”.
admin.m :
function y = admin(z)
y = 1./z
return
Simply call the function admin from the workspace as follows,
>>z=[5 2 4;
1 4 5]
>>admin(z)

The output will


be,
ans = 0.2 0.5 0.25
1 0.25 0.2
Otherwise the same function can be called for any times from any script file provided the function
M-file is available in the current directory.

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:

Exercise 1: Find a short MATLAB expression to build the matrix

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:

b) the last row of B


answer:

c) a version of B with rows 2 and 3 swapped


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

You might also like