Mat Lab
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
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
The following illustration shows the default desktop. You can customize
the arrangement of tools and documents to suit your needs. For more information
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.
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
5
3
MATLAB.IND.TRG
About Matrices
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
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
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.
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?
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
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
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
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
58
A.A.N.M. & V.V.R.S.R. POLYTECHNIC, DEPARTMENT OF ECE
59
MATLAB.IND.TRG
Circuit to Be Modeled
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.
62
MATLAB.IND.TRG
Vn 424.4e3 V
Fn 60 Hz
P 110e6/300 W (quality factor = 300)
QL 110e6 vars
Qc 0
61
MATLAB.IND.TRG.
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
62
MATLAB.IND.TRG
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.
63
MATLAB.IND.TRG
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
65
MATLAB.IND.TRG
66
MATLAB.IND.TRG.
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.
Model-Based Design
67
MATLAB.IND.TRG
Modeling Process
68
MATLAB.IND.TRG
a. 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