Matlab Report (1)
Matlab Report (1)
BACHELOR OF TECHNOLOGY
In
ELECTRICAL ENGINEERING
MtTRC
MODERN INSTITUTE OF TECHNOLOGY & RESEARCH CENTRE
ALWAR
NOVEMBER, 2024
(2024-25)
BIKANER TECHNICAL UNIVERSITY, BIKANER
CERTIFICATE
SIGNATURE SIGNATURE
Department of EE Department of EE
i
ACKNOWLEDGEMENT
Firstly, we would like to express our gratitude to our advisor for the beneficial comments
and remarks.
We express our sincere thanks to Professor, Dr. S. K. Sharma (Director)of the Modern
Institute of Technology & Research Centre, Alwar.
We pay our deep sense of gratitude to Professor, Prashant Tyagi, Head of the
Department, Electrical Engineering of Modern Institute of Technology & Research Centre,
Alwar for encouraging us to the highest peak and providing us the opportunity to present
the Project.
We acknowledge a deep sense of gratitude to Dr. Prem Pal Singh, Supervisor, Department
of Electrical Engineering, Modern Institute of Technology & Research Centre, Alwar
(Rajasthan) for her constant support and guidance during this work. His honesty,
thoroughness, and perseverance have been a constant source of inspiration for us.
We would like to thank our institute and faculty members without them this project would
have been a distant reality, we also extend our heartfelt thanks to our families and well-
wishers.
Finally, but not least, our parents are also an important inspiration for us. With due respect,
we express our gratitude to them.
ii
TABLE OF CONTENTS
ACKNOWLEDGEMENT………………………………………………………................ii
TABLE OF CONTENTS………………………………………………………………iii
FeaturesofMATLAB
5
Following are the basic features of MATLAB:
• It is a high-level language for numerical computation, visualization
and application development.
• It also provides an interactive environment for iterative exploration,
•
2. ENVIRONMENT
designand problem solving.
It provides vast library of mathematical functions for linear algebra,
statistics, Fourier analysis, filtering, optimization, numerical
integration andsolving ordinary differential equations.
• It provides built-in graphics for visualizing data and tools for creating
custom plots.
• MATLAB's programming interface gives development tools for
improving code quality, maintainability, and maximizing performance.
• It provides tools for building applications with custom graphical interfaces.
• It provides functions for integrating MATLAB based algorithms with
external applications and languages such as C, Java, .NET and
Microsoft Excel.
Uses of MATLAB
MATLAB is widely used as a computational tool in science and
engineering encompassing the fields of physics, chemistry, math
and all engineering streams. It is used in a range of applications
including:
• signal processing and Communications
• image and video Processing
• control systems
• test and measurement
• computational finance
6
2.Environment
LocalEnvironmentSetup
Setting up MATLAB environment is a matter of few clicks. The installer can be
downloaded from http://in.mathworks.com/downloads/web_downloads:
This progrtm wlll •nNII Mtthl'/O<b products on you, computtr. Vov m,y •bo be rcqu,rcd to
MATLAB'
~yoursoftw.re.
Slt\lfULINK.
O lnlhD U$1J>Q t/lt lnttmei R2013o
lnlhU wrthou1 using I/It lnttrntt
MAllA8 •nd Swnu6nk ••t reglSltrcd tnclttNrlcs of The MothWO<lc$. In<. Plust stt
www.molhwo<b.com/boc!emor1cs fo, a list of •clcf<tJoNI ttoc!emor1cs. Othu p<oduct or bf•nd
n,mes m,y be tt..Stf'nlrlcs o, reg!Sltrcd ttoc!emorlcs of t/lor rtspt<IN'< holdtn.
•) Marh\\brks
7
4t. 1%~t• Cl El
1-
•) Math\\orks·
UnderstandingtheMATLABEnvironment
MATLAB development IDE can be launched from the icon created on the
desktop. The main working window in MATLAB is called the desktop. When
MATLAB is started, the desktop appears in its default layout:
~, I
nut
~----------------------~-·
The desktop has the following panels:
Current Folder - This panel allows you to access the project folders and
files.
8
Command Window - This is the main area where commands can be
entered at the command line. It is indicated by the command prompt
(>>).
Workspace - The workspace shows all the variables created and/or
imported from files.
Command History - This panel shows or rerun commands that are
entered at the command line.
9
3.Basic Syntax
MATLAB environment behaves like a super-complex calculator. You can
enter commands at the >> command prompt.
MATLAB is an interpreted environment. In other words, you give a command
and MATLAB executes it right away.
HandsonPractice
Type a valid expression, for example,
5 + 5
When you click the Execute button, or type CTRL+E, MATLAB executes
it immediately and the result returned is:
Ans = 10
When you click the Execute button, or type CTRL+E, MATLAB executes
it immediately and the result returned is:
Ans = 9
Another example,
When you click the Execute button, or type CTRL+E, MATLAB executes
it immediately and the result returned is:
10
Ans = 1
Another example,
When you click the Execute button, or type CTRL+E, MATLAB executes
it immediately and the result returned is:
Ans = Inf
Another example,
732 * 20.3
When you click the Execute button, or type CTRL+E, MATLAB executes
it immediately and the result returned is:
Ans = 1.4860e+04
UseofSemicolon(;)inMATLAB
Semicolon (;) indicates end of statement. However, if you want to
suppress and hide the MATLAB output for an expression, add a semicolon
after the expression.
For example,
x = 3;
y = x + 5
11
When you click the Execute button, or type CTRL+E, MATLAB
executes it immediately and the result returned is:
y = 8
Adding Comments
The percent symbol (%) is used for indicating a comment line. For example,
x = 9 % assign the value 9 to x
You can also write a block of comments using the block comment
operators % { and % }.
The MATLAB editor includes tools and context menu items to
help you add, remove, or change the format of comments.
CommonlyusedOperatorsandSpecialCharacters
MATLAB supports the following commonly used operators and special
characters:
Operator Purpose
\ Left-division operator.
/ Right-division operator.
12
.\ Array left-division operator.
. Decimal point.
= Assignment operator.
13
SpecialVariablesandConstants
MATLAB supports the following special variables and constants:
Name Meaning
Inf Infinity.
14
NamingVariables
Variable names consist of a letter followed by any number of letters,
digits or underscore.
MATLAB is case-sensitive.
SavingYourWork
The save command is used for saving all the variables in the
workspace, as a file with .mat extension, in the current
directory.
For example,
save myfile
You can reload the file anytime later using the load command.
load myfile
15
4.Variables
In MATLAB environment, every variable is an array or
example,
MATLAB will execute the above statement and return the following result:
x =3
It creates a 1-by-1 matrix named x and stores the value 3 in its element.
Let us check another example,
MATLAB will execute the above statement and return the following result:
x = 4
sqrt(78)
MATLAB will execute the above statement and return the following result:
Ans = 8.318
16
You can use this variable Ans:
9876/Ans
MATLAB will execute the above statement and return the following result:
Let's look at another example:
Ans = 1.1182e+03
x = 7 * 8;
y = x * 7.89
MATLAB will execute the above statement and return the following result:
y = 441.8400
Multiple Assignments
You can have multiple assignments on the same line. For example,
a = 2; b = 7; c = a * b;
MATLAB will execute the above statement and return the following result:
c = 14
MATLAB will execute the above statement and return the following result:
whos
MATLAB will execute the above statement and return the following result:
The clear command deletes all (or the specified) variable(s) from the
memory.
Long Assignments
Long assignments can be extended to another line by using an ellipsis (...).
For example,
initial velocity = 0;
acceleration = 9.8;
time = 20;
+ acceleration * time 18
MATLAB will execute the above statement and return the following result:
final velocity =
196
x = 7 + 10/3 + 5 ^ 1.2
MATLAB will execute the above statement and return the following result:
x =17.231981640639408
17.231981640639408
Another example,
format short
x = 7 + 10/3 + 5 ^ 1.2
19
MATLAB will execute the above statement and return the following result:
x = 17.2320
The format bank command rounds numbers to two decimal places. For
example,
format bank
daily_wage = 177.45;
weekly_wage = daily_wage * 6
MATLAB will execute the above statement and return the following result:
weekly_wage =
1064.70
format short e
4.678 * 4.9
20
MATLAB will execute the above statement and return the following result:
ans =
2.2922e+01
The format long e command allows displaying in exponential form with four
decimal places plus the exponent. For example,
format long e x = pi
MATLAB will execute the above statement and return the following result:
x =3.141592653589793e+00
The format rat command gives the closest rational expression resulting from a
calculation. For example,
format rat
4.678 * 4.9
21
MATLAB will execute the above statement and return the following result:
Ans = 2063/90
2063/90
Creating Vectors
A vector is a one-dimensional array of numbers. MATLAB allows creating two
types of vectors:
Row vectors Column vectors
Row vectors are created by enclosing the set of elements in square
brackets, using space or comma to delimit the elements.
For example,
r = [7 8 9 10 11]
MATLAB will execute the above statement and return the following result:
r =
7 8 9 10
Column
5
22
Another example,
r = [7 8 9 10 11];
t = [2, 3, 4, 5, 6];
res = r + t
MATLAB will execute the above statement and return the following result:
res =
9 11 13 15
Column
5
Column vectors are created by enclosing the set of elements in square brackets,
using semicolon (;) to delimit the elements.
MATLAB will execute the above statement and return the following result:
c =
10
11
23
Creating Matrices
A matrix is a two-dimensional array of numbers.
In MATLAB, a matrix is created by entering each row as a sequence of
space or comma separated elements, and end of a row is demarcated by
m = [1 2 3; 4 5 6; 7 8 9}
MATLAB will execute the above statement and return the following result:
m=1 2 3
4 5 6
7 8 9
24
5.Operators
Arithmetic Operators
MATLAB allows two different types of arithmetic operations: Matrix
arithmetic operations
Array arithmetic operations
Ope Description
rat
or
+ Addition or unary plus. A+B adds the values
stored in variables A and B. A and B must
have the same size, unless one is a scalar.
A scalar can be added to a matrix of any
size.
25
- Subtraction or unary minus. A-B subtracts
the value of B from A. A and B must have the
same size, unless one is a scalar. A scalar
can be subtracted from a matrix of any size.
..,
C(i,J) • I,ACi,k)Bll<,11
Example
The following examples show the use of arithmetic operators on scalar
data. Create a script file with the following code:
27
a = 10;
b = 20;
c = a + b
d = a - b
e = a * b
f = a / b
g = a \ b
x = 7;
y = 3;
z = x ^ y
c =
d =
-10
e =
20
0
f = 0.5000
g =
f =
2
0.5000
z =
34
3
Functions for Arithmetic Operations
Apart from the above-mentioned arithmetic operators, MATLAB provides the
28
following commands/functions used for similar purpose:
Function Description
30
6.Plotting
To plot the graph of a function, you need to take the following step:
x = [0:5:100];
y = x;
plot(x, y)
When you run the file, MATLAB displays the following plot:
100--------------------
~
ro
~
10
31
Let us take one more example to plot the function y = x2. In this
example, we will draw two graphs with the same function, but in second
time, we will reduce the value of increment. Please note that as we
decrease the increment, the graph becomes smoother.
x = [1 2 3 4 5 6 7 8 9 10];
x = [-100:20:100];
y = x.^2;
plot(x, y)
When you run the file, MATLAB displays the following plot:
10000~~-~-~-~-~-~-~-~--~
9000
8000
7000
6000
5000
4000
3000
2000
1000
32
x = [-100:5:100];
y = x.^2;
plot(x, y)
9000
8000
7000
6000
5000
4000
3000
2000
1000
x = [0:0.01:10];
y = sin(x);
3 ...........•...... .
. ..........................
' .
2 ...........•...... .............
'••···· ...... ....... ······~···
'
'
'
'
icii 0
' .
·1
:::::::::::::::::::~..... ::···r···· .• _····r··-r···r .••
. .. ..
. .. ..
···················-····· .' .' '' .. .. .
..........................................
·2
'
''' ''' .. ....
... ... ...
'
.
'
' '
' '
'
..
.
.3
.. ..
• • • • • : • • • • • • ~-- · - - · ~ · - · · ·
' '
...
'
.. ...
•••••• : ······~····· ·-:·-··· ··!"···. --~---··
0 2 3 s 6 8 10
,c
Example
Create a script file and type the following code:
34
x = [0 : 0.01: 10];
y = sin(x);
g = cos(x);
08
06
04
02
-0 2
-0 J
-0 6
-0 8
2 3 J 5 6 7 8 9 10
Code Color
w White
k Black
35
b Blue
r Red
c Cyan
g Green
m Magenta
y Yellow
Example
Let us draw the graph of two polynomials
f(x) = 3x4 + 2x3+ 7x2 + 2x + 9 and g(x) = 5x3 + 9x + 2
Create a script file and type the following code:
g = 5 * x.^3 + 9 * x + 2;
When you run the file, MATLAB generates the following graph:
36
OS
-0 s
-1
-10 -8 -4 -2 0 2 4 6 8 10
37
7.Simulink
Simulink is a simulation and model-based design environment for dynamic
and embedded systems, integrated with MATLAB. Simulink, also developed by
MathWorks, is a data flow graphical programming language tool for
modeling, simulating and analyzing multi-domain dynamic systems. It is
basically a graphical block diagramming tool with customizable set of
block libraries.
It allows you to incorporate MATLAB algorithms into models as well as
export the simulation results into MATLAB for further analysis.
Simulink supports:
• system-level design
• simulation
• automatic code generation
• testing and verification of embedded systems
UsingSimulink
To open Simulink, type in the MATLAB work space:
Simulink
Simulink opens with the Library Browser. The Library Browser is used for
building simulation models.
38
~ Simulink Library Browser
fol• fdit '[t~v Help
» Enter search term
Lookup Tables
Math Operations
Model Verification ~ Oi1,c:ontinuitiff
Model-Wide Utilities
Ports & Subsystems
Signal Attributes i:
~ Oi><n:•
~
Signal Routing Logie and Sit
Sinks ~ Opemforu
Sources
,
Us"·Defined Functions
• Additional Math & Oiscr,
._ Aerospace Blockset
El L-pTabl.,
1
~
Communications Systen M•tll
Computer Vision Syste 1
X Opti•tloru
OPCToolbox
Real• Time Windows Tari
[§ Slgn1t Altnbut..
~
Report Generator
Sign1t Routing
Robust Control Toolbox ~
111
Showing: Smuink
On the left side window pane, you will find several libraries categorized on the
basis of various systems, clicking on each one will display the design blocks on the
right window pane.
Building Models
To create a new model, click the New button on the Library Browser's toolbar. This opens a
new untitled model window
39
--- - ----~- - ,.. ... .. -
Iii< t• ii.- ~
,......~
--~-
.z • II
-
li5@ • Iii
•.:i.-
,a
0
!J • ~-J ~-iii,·
=s
B
·- •
- -
A Simulink model is a block diagram.
Model elements are added by selecting the appropriate elements from the Library Browser and
dragging them into the Model window.
Alternately, you can copy the model elements and paste them into the model window.
Examples
Drag and drop items from the Simulink library to make your project.
For the purpose of this example, two blocks will be used for the simulation - A Source (a
signal) and a Sink (a scope). A signal generator (the source) generates an analog signal,
which will then be graphically visualized by the scope(the sink).
40
..A>nlrleS LC>tory ,,,11n s..,c1111Muls (none) Fr enllyU-S,
• .....
~ ~
Commonly Used Blocks Commonl)'I.Md
Con~
Continuous
e-.
Discontinuities
Oisc,ete
L09i< and B~ OpmtioM ~ Chtoc)(lt,n,.ut,t:t
~ auc,;o
§ ~
Math M
Pl>rts & Subsystems It ep.,.~, \Wlfl
Signal Attributes """
• n~IRouttn
Sinks
Sources
EJ Mod4if.Yt1ff
UbhlM'.'I G Pon,
S.bt' ,.,...
~ I@
su• e 1n unctions Slgn.ol....,,l""t" Sig RO<rtlng
P Additional Math & Discrttt E
~'-
>; Atr0>paet Blockset
Communications Systm1 Toolbox
Computtr Vision Syrttm Toolbox
Control Systtm Toolbo•
Som
ffil
' OSP Systtm Toolbox
Embtddtd Coder
B UW-0.finff
Funaions [El ,.
Fuz.;, Logic Toolbo•
.,_ uni u • .:r~-
Begin by dragging the required blocks from the library to the project window. Then, connect
the blocks together which can be done by dragging connectors from connection points on one
block to those of another.
Let us drag a 'Sine Wave' block into the model.
41
Logic and Bit Operations
Lookup Tables
at perations
71 Counta Free--
Running
Model Verification
Model-Wide Utilities
Digital Clodc
Ports & Subsystems
Signal Attribute.s
Signal Routing
From File
Sinks
Sources
User-Defined Functions
t Additional Math & Discrete
- Ground
Select 'Sinks' from the library and drag a 'Scope' block into the model.
uwaries Library: SITllint/Snks Searcll Resuls: (none) freq ◄
• Simulink
Commonly Used Blocks
Continuous
i I Ohplay
B FlooilngSco«>e
Discontinurties
Discrete
Logic and Bit Op~ations
>o=) Outt
le So,pe
lookup Tables
Math Operations
Model Verification
e Step s,mulation
@] Termi.natct
Model-Wide Utilities
Ports & Subsystems
Signal Attributes
i~•*rwi To FIie
~ ToW~
~
~nal Routinl )(;<(G<•pl,
I e
Sources
User-Defined Functions
"
Drag a signal line from the output of the Sine Wave block to the input of the Scope block.
42
file sdit )liew Qisplay Diagram ~imulation 8nalysis ~ode Jools ]:felp
(_!) • 10.0
[@.)
~ . ffi
Sine Wave
BScope
::::;
;~
[]]
::::;
ru ·B
Sine Wave Scope
Run the simulation by pressing the 'Run' button, keeping all parameters default (you can
change them from the Simulation menu)
You should get the below graph from the scope.
43
,=.,0 WJ• • Ill
r,
/
o.5 I
o'
/
44