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

Mat Lab

The document discusses starting and quitting the MATLAB program and provides instructions on how to do so. It also discusses MATLAB's desktop interface and how to enter matrices into MATLAB by listing elements, loading from files, or using built-in functions.

Uploaded by

pavan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Mat Lab

The document discusses starting and quitting the MATLAB program and provides instructions on how to do so. It also discusses MATLAB's desktop interface and how to enter matrices into MATLAB by listing elements, loading from files, or using built-in functions.

Uploaded by

pavan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 27

MAT LAB.

TRG

CHAPTE R 3
MATLAB
3.1 Starting and Quitting the MATLAB Program

In this section...
“Starting a MATLAB Session” on page 1-7
“Quitting the MATLAB Program” on page 1-8

Starting a MATLAB Session

On Microsoft Windows® platforms, start the MATLAB program by


double-clicking the MATLAB shortcut on your Windows desktop.On Apple
Macintosh® platforms, start MATLAB by double-clicking the MATLAB icon
in the Applications folder.On UNIX® platforms, start MATLAB by typing
matlab at the operating system prompt.
When you start MATLAB, by default, MATLAB automatically loads all the
program files provided by MathWorks for MATLAB and other MathWorks
products. You do not have to start each product you want to use. There are
alternative ways to start MATLAB, and you can customize MATLAB startup.
For example, you can change the folder in which MATLAB starts or
automatically execute MATLAB statements upon startup.

For More Information See “Startup and Shutdown” in the Desktop Tools
and Development Environment documentation.

The Desktop
When you start MATLAB, the desktop appears, containing tools
(graphical user interfaces) for managing files, variables, and applications
associated with MATLA MAT LAB.TRG

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE


1
MATLAB.IND.TRG

 The following illustration shows the default desktop. You can customize
the arrangement of tools and documents to suit your needs. For more information

 2.1.1 Quitting the MATLAB Program

 To end your MATLAB session, select File > Exit MATLAB in the
desktop, or type quit in the Command Window. You can run a script file named
finish.m each time MATLAB quits that, for example, executes functions to save
the workspace.

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

5
1
MATLAB.IND.TRG

Confirm Quitting
MATLAB can display a confirmation dialog box before quitting. To set
this option, select File > Preferences > General > Confirmation Dialogs, and
select the check box for Confirm before exiting MATLAB.

5
2
Systems Applications & Products

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

5
3
MATLAB.IND.TRG

2.2 Matrices and Arrays

About Matrices

In the MATLAB environment, a matrix is a rectangular array of numbers.

Special meaning is sometimes attached to 1-by-1 matrices, which are scalars, and

to matrices with only one row or column, which are vectors. MATLAB has other

ways of storing both numeric and nonnumeric data, but in the beginning, it is

usually best to think of everything as a matrix. The operations in MATLAB are

designed to be as natural as possible. Where other programming languages work

with numbers one at a time, MATLAB allows you to work with entire matrices

quickly and easily. A good example matrix, used throughout this book, appears in

the Renaissance engraving Melencolia I by the German artist and amateur

mathematician Albrecht Dürer.

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

5
4
MATLAB.IND.TRG

This image is filled with mathematical symbolism, and if you look carefully, you
will see a matrix in the upper right corner. This matrix is known as a magic square
and was believed by many in Dürer’s time to have genuinely magical properties. It
does turn out to have some fascinating characteristics worth exploring.

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE


5
3
MATLAB.IND.TRG

Entering Matrices

The best way for you to get started with MATLAB is to learn how to handle
matrices. Start MATLAB and follow along with each example.
You can enter matrices into MATLAB in several different ways:
• Enter an explicit list of elements.
• Load matrices from external data files.
• Generate matrices using built-in functions.
• Create matrices with your own functions and save them in files.
Start by entering Dürer’s matrix as a list of its elements. You only have to
follow a few basic conventions:
• Separate the elements of a row with blanks or commas.
• Use a semicolon, ; , to indicate the end of each row.
• Surround the entire list of elements with square brackets, [ ].
To enter Dürer’s matrix, simply type in the Command Window
A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
MATLAB displays the matrix you just entered:
A=
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
This matrix matches the numbers in the engraving. Once you 
have entered the matrix, it is automatically remembered in the 
MATLAB workspace. You can refer to it simply as A. Now that you have
A in the workspace, take a look at what makes it so interesting. Why is 
it magic?

sum, transpose, and diag

5
4
A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

5
5
MATLAB.IND.TRG

You are probably already aware that the special properties of a magic 
square have to do with the various ways of summing its elements. If you take 
the sum along any row or column, or along either of the two main diagonals,
you will always get the same number. Let us verify that using 
MATLAB. The first statement to try is sum(A)
MATLAB replies with ans = 34 34 34 34

When you do not specify an output variable, MATLAB uses the variable
ans, short for answer, to store the results of a calculation. You have computed a
row vector containing the sums of the columns of A. Each of the columns has the
same sum, the magic sum, 34. How about the row sums? MATLAB has a
preference for working with the columns of a matrix, so one way to get the row
sums is to transpose the
matrix, compute the column sums of the transpose, and then transpose the
result. For an additional way that avoids the double transpose use the
dimension argument for the sum function.
MATLAB has two transpose operators. The apostrophe operator (e.g., A)

2.2.1 Operators
Expressions use familiar arithmetic operators and precedence rules.
+ Addition
- Subtraction
* Multiplication
/ Division
\ Left division (described in “Linear Algebra” in the
MATLAB documentation)
^ Power
' Complex conjugate transpose
( ) Specify evaluation order

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

55
MATLAB.IND.TRG
2.2.2 Functions
MATLAB provides a large number of standard elementary mathematical
functions, including abs, sqrt, exp, and sin. Taking the square root or logarithm of a
negative number is not an error; the appropriate complex result
is produced automatically. MATLAB also provides many more advanced
mathematical functions, including Bessel and gamma functions. Most of these
functions accept complex arguments. For a list of the elementary
mathematical functions, type help elfun
For a list of more advanced mathematical and matrix functions, type

help specfun
help elmat

Some of the functions, like sqrt and sin, are built in. Built-in functions are part of
the MATLAB core so they are very efficient, but the computational details are not
readily accessible. Other functions are implemented in the MATLAB programing
language, so their computational details are accessible. There are some differences
between built-in functions and other functions.

For example, for built-in functions, you cannot see the code. For other functions,
you can see the code and even modify it if you want. Several special functions
provide values of useful constants.
Pi 3.14159265...
i Imaginary unit,
j Same as i
eps Floating-point relative precision,
realmin Smallest floating-point number,
realmax Largest floating-point number,
Inf Infinity
NaN Not-a-number

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

56
MATLAB.IND.TRG

2.3 SimPowerSystems

Introduction
SimPowerSystems software and other products of the Physical Modeling
product family work together with Simulink software to model electrical,
mechanical, and control systems. SimPowerSystems software operates in the
Simulink environment. Therefore, before starting this user’s guide, make yourself
familiar with Simulink documentation. Or, if you perform signal processing and
communications tasks (as opposed to control system design tasks), see the DSP
System
The Role of Simulation in Design

Electrical power systems are combinations of electrical circuits and


electromechanical devices like motors and generators. Engineers working in this
discipline are constantly improving the performance of the systems. Requirements
for drastically increased efficiency have forced power system designers to use
power electronic devices and sophisticated control system concepts that tax
traditional analysis tools and techniques. Further complicating the analyst’s role is
the fact that the system is often so nonlinear that the only way to understand it is
through simulation.

Land-based power generation from hydroelectric, steam, or other devices is


not the only use of power systems. A common attribute of these systems is their
use of power electronics and control systems to achieve their performance
objectives.

SimPowerSystems software is a modern design tool that allows scientists and


engineers to rapidly and easily build models that simulate power systems. It uses the
Simulink environment, allowing you to build a model using simple click and drag
procedures. Not only can you draw the circuit topology rapidly, but your analysis of
the circuit can include it

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

57
MATLAB.IND.TRG
can you draw the circuit topology rapidly, but your analysis of
the circuit can include it interactions with mechanical, thermal, control, and other
disciplines. This is possible because all the electrical parts of the simulation
interact with the extensive Simulink modeling library. Since Simulink uses the
MATLAB® computational engine, designers can also use MATLAB toolboxes
and Simulink blocksets. SimPowerSystems software belongs to the Physical
Modeling product family and uses similar block and connection line interface.
Required and Related Products
SimPowerSystems software requires the following products:
• MATLAB
• Simulink

In addition to SimPowerSystems software, the Physical Modeling product


family includes other products for modeling and simulating mechanical and
electrical systems. Use these products together to model physical systems in
Simulink environment. There are also a number of closely
related toolboxes and other products from MathWorks® that you can use
with SimPowerSystems software.

2.3.1 Building and Simulating a Simple


Circuit
Introduction
SimPowerSystems software allows you to build and simulate electrical
circuits containing linear and nonlinear elements. In this section you
• Explore the powerlib library
• Learn how to build a simple circuit from the powerlib library
• Interconnect Simulink blocks with your circuit
The circuit below represents an equivalent power system feeding a 300 km
transmission line. The line is compensated by a shunt inductor at its receiving end.
A circuit breaks

58
A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

59
MATLAB.IND.TRG

energizing and de-energizing of the line. To simplify matters, only one of


the three phases is represented. The parameters shown in the figure are typical of a
735 kV power system.

Circuit to Be Modeled

Building the Electrical Circuit with powerlib


Library

The graphical user interface makes use of the Simulink functionality to


interconnect various electrical components. The electrical components are grouped
in a library called powerlib.

1 Open the SimPowerSystems main library by entering the following


command at the MATLAB prompt powerlib
This command displays a Simulink window showing icons of different
block libraries.

You can open these libraries to produce the windows containing the blocks to be
copied into your circuit. Each component is represented by a special icon having
one or several inputs and outputs corresponding to the different terminals of the
component
60
A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

61
MATLAB.IND.TRG.

2 From the File menu of the powerlib window, open a new window to
contain your first c ircuit and save it as circuit1.
3 Open the Electrical Sources library and copy the AC Voltage Source
block into the circuit1 window.
4 Open the AC Voltage Source dialog box by double-clicking the icon
and enter the Amplitude, Phase, and Frequency parameters according to the values
shown in Circuit to BeModeled on page 1-8. Note that the amplitude to be
specified for a sinusoidal source is its peak value (424.4e3*sqrt(2) volts in this
case).
5 Change the name of this block from AC Voltage Source to Vs.
6 Copy the Parallel RLC Branch block, which can be found in the
Elements library of powerlib, set its parameters as shown in Circuit to Be
Modeled on page 1-8, and name it Z_eq.
7 The resistance Rs_eq of the circuit can be obtained from the Parallel
RLC Branch block. Duplicate the Parallel RLC Branch block, which is already in
your circuit1 window. Select R for the Branch Type parameter and set the R
parameter according to Circuit to Be Modeled

Once the dialog box is closed, notice that the L and C components have
disappeared so that the icon now shows a single resistor.

8 Name this block Rs_eq.


9 Resize the various components and interconnect blocks by dragging
lines from outputs to inputs of appropriate blocks.

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

62
MATLAB.IND.TRG

10 To complete the circuit of Circuit to Be Modeled on page 1-8, you need


to add a transmission line and a shunt reactor. You add the circuit breaker later in
“Simulating Transients” . The model of a line with uniformly distributed R, L, and
C parameters
normally consists of a delay equal to the wave propagation time along the
line. This model cannot be simulated as a linear system because a delay
corresponds to an infinite number of states. However, a good approximation of the
line with a finite number of states can be obtained by cascading several PI circuits,
each representing a small section of the line. A PI section consists of a series R-L
branch and two shunt C branches. The
model accuracy depends on the number of PI sections used for the model.
Copy the PI Section Line block from the Elements library into the circuit window,
set its parameters as shown in Circuit to Be Modeled and specify one line section.
11 The shunt reactor is modeled by a resistor in series with an inductor. You
could use a Series RLC Branch block to model the shunt reactor, but then you
would have to manually calculate and set the R and L values from the quality factor
and reactive power specified in Circuit to Be Modeled Therefore, you might find it
more convenient to use a Series RLC Load block that allows you to specify directly
the active and reactive powers
absorbed by the shunt reactor. Copy the Series RLC Load block, which can
be found in the Elements library of powerlib. Name this block 110 Mvar. Set its
parameters as follows:

Vn 424.4e3 V
Fn 60 Hz
P 110e6/300 W (quality factor = 300)
QL 110e6 vars
Qc 0

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

61
MATLAB.IND.TRG.

Note that, as no reactive capacitive power is specified, the capacitor


disappears on the block icon when the dialog box is closed. Interconnect the new
blocks as shown.

12 You need a Voltage Measurement block to measure the voltage


at node B1. This block is found in the Measurements library of powerlib. Copy it
and name it U1. Connect its positive input to the node B1 and its negative input to
a new Ground block.
13 To observe the voltage measured by the Voltage Measurement
block named U1, a display system is needed. This can be any device found in the
Simulink Sinks library. Open the Sinks library and copy the Scope block into your
circuit1 window. If the scope were connected directly at the output of the voltage
measurement, it would display the voltage in volts. However, electrical engineers
in power systems are used to working with normalized quantities (per unit
system). The voltage is normalized by dividing the value in volts by a base
voltage corresponding to the peak value of the system nominal voltage. In this
case the scaling factor K is

14 Copy a Gain block from the Simulink library and set its gain as
above. Connect its output to the Scope block and connect the output of the Voltage
Measurement block to the Gain block. Duplicate this voltage measurement system
at the node B2, as shown below

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

62
MATLAB.IND.TRG

15 Add a Powergui block to your model. The purpose of this block is


discussed in “Using the Powergui Block to Simulate SimPowerSystems Models” .
16 From the Simulation menu, select Start.
17 Open the Scope blocks and observe the voltages at nodes B1 and B2.
18 While the simulation is running, open the Vs block dialog box and
modify
the amplitude. Observe the effect on the two scopes. You can also modify
the frequency and the phase. You can zoom in on the waveforms in the
scope windows by drawing a box around the region of interest with the left
mouse button.

To simulate this circuit, the default integration algorithm (ode45) was used.
However, for most SimPowerSystems applications, your circuits contain switches
and other nonlinear models. In such a case, you must specify a different integration
algorithm. This is discussed in “Simulating Transients” , where a circuit breaker is
added to your circuit.

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

63
MATLAB.IND.TRG

2.3.2 Interfacing the Electrical Circuit with


Other Simulink Blocks

The Voltage Measurement block acts as an interface between the


SimPowerSystems blocks and the Simulink blocks. For the system shown above,
you implemented such an interface from the electrical system to the Simulink
system. The Voltage Measurement block converts the measured
voltages into Simulink signals.
Similarly, the Current Measurement block from the Measurements library
of powerlib can be used to convert any measured current into a Simulink signal.
You can also interface from Simulink blocks to the electrical system. For example,
you can use the Controlled Voltage Source block to inject a voltage in an electrical
circuit, as shown in the following figure.

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

64
MATLAB.IND.TRG.

2.4 Simulink
Simulink software models, simulates, and analyzes dynamic systems. It
enables you to pose a question about a system, model the system, and seewhat
happens. With Simulink, you can easily build models from scratch, or modify
existing models to meet your needs. Simulink supports linear and nonlinear
systems, modeled in continuous time, sampled time, or a hybrid of the two.
Systems can also be multirate — having different parts that are sampled or
updated
at different rates. Thousands of scientists and engineers around the world
use Simulink to model and solve real problems in a variety of industries,
including:
• Aerospace and Defense
• Automotive
• Communications
• Electronics and Signal Processing
• Medical Instrumentation
2.4.1 Tool for Model-Based Design
           With Simulink, you can move beyond idealized linear models to
explore more realistic nonlinear models, factoring in friction, air resistance, gear
slippage, hard stops, and the other things that describe real-world phenomena.
Simulink turns your computer into a laboratory for modeling and analyzing
systems that would not be possible or practical otherwise.
Whether you are interested in the behavior of an automotive clutch
system, the flutter of an airplane wing, or the effect of the monetary supply on the
economy, Simulink provides you with the tools to model and simulate almost any
real-world problem. Simulink also provides demos that model a wide variety of
real-world phenomena
Simulink provides a graphical user interface (GUI) for building models as block
diagrams, allowing you to draw models as you would with pencil and paper.
Simulink also

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

65
MATLAB.IND.TRG

includes a comprehensive block library of sinks, sources, linear and


nonlinear components, and connectors. If these blocks do not meet your needs,
however, you can also create your own blocks. The interactive graphical
environment simplifies the modeling process, eliminating the need to formulate
differential and difference equations in a language or program.
Models are hierarchical, so you can build models using both top-down and
bottom-up approaches. You can view the system at a high level, then double-click
blocks to see increasing levels of model detail. This approach provides insight into
how a model is organized and how its parts interact.

Tool for Simulation


After you define a model, you can simulate it, using a choice of
mathematical integration methods, either from the Simulink menus or by entering
commands in the MATLAB® Command Window. The menus are convenient for
interactive work, while the command line is useful for running a batch of
simulations (for example, if you are doing Monte Carlo simulations or want to
apply a parameter across a range of values).
Using scopes and other display blocks, you can see the simulation results
while the simulation runs. You can then change many parameters and see what
happens for “what if” exploration. The simulation results can be put in the
MATLAB workspace for postprocessing and visualization.

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

66
MATLAB.IND.TRG.

Tool for Analysis

Model analysis tools include linearization and trimming tools, which can
be accessed from the MATLAB command line, plus the many tools in MATLAB
and its application toolboxes. Because MATLAB and Simulink are integrated, you
can simulate, analyze, and revise your models in either environment
at any point.

How Simulink Software Interacts with the MATLAB


Environment
Simulink software is tightly integrated with the MATLAB environment. It
requires MATLAB to run, depending on it to define and evaluate model and block
parameters. Simulink can also utilize many MATLAB features. For example,
Simulink can use the MATLAB environment to:

• Define model inputs.


• Store model outputs for analysis and visualization.
• Perform functions within a model, through integrated calls to MATLAB
operators and functions.

Model-Based Design

Model-Based Design is a process that enables faster, more cost-effective


development of dynamic systems, including control systems, signal processing,
and communications systems. In Model-Based Design, a system model is at the
center of the development process, from requirements development, through
design, implementation, and testing. The model is an executable specification that
is continually refined throughout the development process.
After model development, simulation shows whether the model works correctly.
When software and hardware implementation requirements are included, such as
fixed-point and timing

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

67
MATLAB.IND.TRG

behavior, you can automatically generate code for embedded deployment


and create test benches for system verification saving time and avoiding the
introduction of manually coded errors.
Model-Based Design allows you to improve efficiency by

• Using a common design environment across project teams


• Linking designs directly to requirements
• Integrating testing with design to continuously identify and correct errors
• Refining algorithms through multidomain simulation
• Automatically generating embedded software code
• Developing and reusing test suites
• Automatically generating documentation
• Reusing designs to deploy systems across multiple processors and hardware
targets

Modeling Process

There are six steps to modeling any system:


1 Defining the System
2 Identifying System Components
3 Modeling the System with Equations
4 Building the Simulink Block Diagram
5 Running the Simulation
6 Validating the Simulation Results
You perform the first three steps of this process outside of the Simulink
software before you begin building your model.

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE

68
MATLAB.IND.TRG

a. CALL-BY-VALUE:

Functions are invoked by writing their name and an appropriate list of


arguments with in parentheses. These arguments match in number and type (or
compatible type) the parameters in the parameter list in the function definition. The
compiler enforces type compatibility. The basic argument-passing mechanism
inherited from the C language is call- by-value. That is, each argument is evaluated
and its value is used locally in place of the corresponding formal parameter. Thus, if
a variable is passed to a function, the stored value of that variable in the calling
environment will not be changed. Here is an example that clearly illustrates the
concept of call-by-value:

In file compute_sum.cpp
#include <iostream.h>
using namespace std;
int compute_sum(int n) // sum from 1 to n
{
int sum = 0;
for ( ; n > 0; --n)
sum += n; return sum; int main()
{
int n = 3, sum;
cout << n << endl:
sum = compute_sum(n);
cout << n << endl;
cout << sum << endl;
a}
Even though n is passed to compute_sum() and the value of n in the body of
that function is changed, the value of n in the calling environment remains unchanged.
It is the value of n that is being passed, not n itself.

THE END

A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE


69
30
31

You might also like