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

Matlab PPT 2

Uploaded by

Bhupendra Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
265 views

Matlab PPT 2

Uploaded by

Bhupendra Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

MATLAB ®

Submitted by: submitted to:R2012a


(7.14)
Bhupendra yadav Durgesh chaudhary

MathWork

Introduction
What is MATLAB ?
(MATrix LABoratory)
MATLAB is a powerful computer software application
program for solving engineering problems. It is applicable
for all engineering desciplines and many natural sciences as
well.
MATLAB handles numerical calculations and high quality
graphics , provides a convenient interface to built in state of
the art subroutine libraries, and incorporates a high level
programming language.

MATLAB is a numerical computing environment and


language , developed by Math works , MATLAB allows matrix
manipulations , plotting of functions and data ,
implementation of algoritms , creation of user interfaces ,
and interfacing with programs written in other languages ,
including C,C++,java, and Fortran.
Features of MATLAB:
 High level language for numerical computation,
visualization and application development.
 Interactive environment for iteractive exploration,
design and problem solving.
 Mathematical functions for linear algebra, statistics ,
fourier analysis, filtering, optimization, numerical
integration and solving ordinary differiantial equation.
 Built in graphics for visualization data and tool for
creating customs plots.
 Developments tools for improvements code quality and
maintainability amd maximum performance .
 Tools for building application with custom graphical
interfaces.
 Functions for integrating MATLAB based algorithms
with external applications and languages such as C,java,
.net , and Microsoft Exel.
MATLAB Components

Fig. MATLAB desktop made up of several independent windows i.e., command


window, history window, edit window, workspace, graphic window, directory window.
Types of MATLAB files
1. .m- files
M- files are standard ASCII text files . All the programs written in editor
window are assigned .m extenshion are known as M-files . M-files can be
classified as Script files and Function file.
2. .mat files
MAT files are binary data files which are created by MATLAB using save
command and saved by extenshion of .mat. The save command saves the
available data from current MATLAB workspace into a disk file. Syntax is :
>>save<file name><var1 var2 var3>
3. .fig files
All the figure windows are automatically saved with an extenshion of .fig.
These files can be opened any directly by typing filename on command
window or by double clicking on it for further editing.
4. .mdl files
Modles built in Simulink are saved with an extenshion of .mdl are called
MDL files.
5. .mex files
MEX files are the interface files written in C & FORTRAN.
Arithmetic and built in MATLAB
functions
Using MATLAB , we can easily perform various arithmetic operations on
numbers like addition, subtraction, multiplication, and exponentiation
,etc.Arithmatic operations are are performed between two or more quantities
by using mathematical operator.

Symbol Operation Example

+ Addition 6+5=11

- Subtraction 6.7-2=4.7

* Multiplication 4*5=20

/ Right division 8/2=4

\ left division 2\8=4(8/2)

^ Exponent 4^2=16(42)

Examples-

Ques. Add , multiply, divide, and squaring the given no. 5 and 6.
order of precedence is: Parenthesis , Exponents, Multiplication and division,

addition and subtraction.


Example2 . evaluate : 9^(1/3)+16^0.3

 Constants and Variables-


Constants refers to a fixed value which does not change during the execution of program.
In MATLAB constants can be two types:
(1) Numeric constants (2) Character constants
1. Numeric constants-
Integer, real, complex ; integer constants consists of 0 to 9, with an optional ‘-’ or ‘+’ sign
For example: 789 -125 +968 475268
Real constants can be defined in exponential notation;
For example: 0.065 -45.58 435.25 4.3525e+2 4352e-2 here e+2 or e-2 means
multiplication by 10+-2
A complex constant consist of two parts, real and imaginary
For example: 6+j*3 -4+j*95-i*8
2. Character constants-
Character constants can be a single character constant or a string constant. Characters may
be a n alphabets, numbers, special characters, or blank space.
For example: ‘E’ ‘Good’ ‘2013’ ‘GOne’ ‘G & R’ ‘y’ etc.

Variables is a name made up of a letter or a combination of several letters and numbers and

Assigned a numerical value. For example:


Variables Values

Pi 3.14159 (number π)

i, j Square root of -1 (imaginary part)

Inf ∞ (infinity)

Eps Number small enough equivalent to zero

Nan Not a number

Ans Default output variable

Elementary math fuctions and Trignometric functions


Exp Calculate exponential
Sqrt Calculates square roots
Log Calculate natural logarithm
Rem Calculates remainder
Round Roundup towards nearest integer
Floor Roundup towards negative
Ceil Roundup towards positive
Conj Calculates complex conjugates
Real Returns real parts of complex
number
Imag returns imaginary part of complex
number
Abs Returns the absolutes part of
complex number
Angle Returns the angle of complex number

Sin(x) Calculates the sin of x in radians

Cos(x) Calculates the cos of x in radians

Asin(x) Calculates the inverse of sin of x in


radians
Sinh(x) Calculates the hyperbolic of sin of x
in radians
Asinh(x) Calculates inverse oh hyperbolic sin
of x in radians
Sind(x) Calculates sin of x in degrees

Asind(x) Calculates inverse of sin of x in


degree
MATLAB arrays(Vectors & Matrices)
MATLAB can handle all the data(numbers, variables,
characters, strings) in the form of arrays. An array is a
collection of data arranged in the form of rows and columns.
A single number or a scalar value is also a matrix of size 1X1
containing single element only. Arrays can be classified as :
1.one dimension arrays (vectors)
2.two dimension arrays (matrix)

1.VECTORS(1 D arrays)-
Vector is one dimension array. It contains only a row or a
column. If a vector is a row vector and it has n elements then
size of this vector is denoted by 1Xn. On the other hand if it is
a column vector and it has n elements then the size of this
vector is denoted by nX1.
Row vector:
V= [1,2,3,4,5,……..n]
Column vector:
V= [1;2;3;4;5;…….n]

Creating vectors of linear spacing:


(a) Using colon-
>>1:10
ans=
1 2 3 4 5 6 7 8 9 10
(b) Using linspace command-
>>y= linspace(a,b) %generates row vector y of 100 points
lineary spaced between and including a and b.
>>A= linspace (1,36,12);
It creates a vector of 12 lineary spaced numbers from 1 to 36.
2.MATRICES (multi dimension array)
Multi dimension arrays are called matrices. Their sizes of matrices
are denoted by mXn , where m is the number of rows and n is the
number of columns. A matrix is an array of numbers.
For example: to enter a matrix such as
1 2 3
A= 4 5 6
7 8 9
in MATLAB, we have to type:
>>A= [1 2 3 ; 4 5 6 ; 7 8 9]

(a)For substitution >>A(3,3)=0


(b)Pick out a certain row or column:

>>A(2, :) %returns the second row of the matrix A


>>A(:, 2:3) %extracts the column 2 and column 3 of matrix A

(c)for deleting rows and columns : >> A(:,2)=[]


(d)concatenation of matrices: >>A=[1,3;4,7]
>>B=[4,2;5,9]
>>C=[5,8;6,7]
>>D=[A,B] %horizontal concatenation
>>E=[C;B] %vertical concatenation
>>F=[A+1,B.*2;C-1,C+1]
(e)matrix operators:
Matrix operators includes addition, subtraction, multiplication,
division and exponent of matrices.
For example: A= [1 2;3 4] and B=[2 5;-1 3]
(f)matrix function:
Matrix function includes transpose(‘) , inverse , determinant, rank,
eigen values, size , sum of diagonal elements of given matrix we
have to find.
(g)vector functions:
Vector function includes largest component of given vector
,smallest component of vector, length of vector,sum of all the
elements, product of all the elements, sorting of elements,mean of
vector,median of vector,standard deviation of vector we have to
find.
Mathematics in MATLAB
1.POLYNOMIALS-
Polynomials are functions that have the form of:
F(x)= a0+a1x+a2x2+….+anxn
The coefficients a0 , a1, a2…..an are real numbers and n which is a
non negative integer, is the degree or order of polynomial.

(a)Representation of polynomials:
In MATLAB, polynomials are represented by a row vector of its
coefficients. If the polynomial has degree n,then the
corresponding representing vector has length n+1 and contains
the coefficients associated with decreasing powers from left to
right. For example: Y(x)=3x2+2x+1
>>y=[3 2 1]
And >>z=[3 0 -1 4] represents polynomial z=3x2-x+4

Operation Operator/Function Example


addition + m=y+z
subtraction - n=y-z
multiplication Conv o=conv(y,z)
division Deconv p=deconv(y,z)
(b) Addition and subtraction:
For example: two polynomials A= 3x6-2x5+10 x4-x3+7 & B=x3-2x-
4 are defined in MATLAB as:
>>a=[3 -2 10 -1 0 0 7]
>>b=[0 0 0 1 0 -2 -4]
>>a+b
>>a-b
(c) Multiplication and division:
Multiplications and division of polynomials A=2x3+7x2-5x-2 &
B=x+3
>>A=[2 7 -5 -2]
>>B=[1 3]
>>conv(A,B)
>>deconv[q,r]=deconv(A,B)
(c)roots of polynomials:
For example: for a polynomial p(x)=x3-3x2+1 roots can be
calculated as;
>>A=[1 -3 0 1 ]
>>roots(A)
>>roots([2 0 1 4])

(d)symbolic math:
We assign a MATLAB variable name to a string or characters that
define the mathematically function that we want to manipulate in
some manner. The key function in MATLAB to create a symbolic
representation of data is sym() and syms.
>>A=sym(‘x’)
>>syms a b c
If symbolic variables variables are defined we can construct
symbolic expressions using arithmetic operators.
>>f=5.1*x^3+a*x^2+b*x+c
We can also access standard built in MATLAB functions such as
trigonometric functions, matrix functions,exponentials etc.
>>g=sin(x)*exp(-x)-sinh(x)*x^2
>>f*g+a*c+1
(e)differentiation and integration:
Let x= sin(t) in MATLAB is
>>syms t;
>>x= sin(t);
>>diff(x)
>>diff(x,2)
For integration
>>int(x)
>>int(x,t)
>>int(x,a,b)

(f)solutions of linear equations:


Linear equations can be transformed into a matrix equations,
making the system easier to solve. For example,
2x1-x2+x3=8
x1+2x2+3x3=9
3x1-x3=3
A system of this type has the form AX=B, so we can enter these
numbers into MATLAB using the following commands:
>>syms x1 x2 x3;
>>X=[x1;x2;x3]
>>A=[2 -1 1 ; 1 2 3 ; 3 0 -1];
>>B=[8;9;3];
>>X=inv(A)*B
MATLAB Graphics

MATLAB offers a variety of data plotting functions plus a set of


GUI tools to create and modify graphic displays. Every useful
features of MATLAB is its ability to generate high quality two
and three dimensional plots using simple and flexible functions.
A number of built in functions for both 2D and 3D plots are
available for creating different types of plots. They include line,
bar, area, direction, and vector field, radial, and scatter graphs.
(a) the plot function:
The plot function is used to create 2D plots using the syntax below
>>plot(x,y)
For example: >>x=[5 10 15 20 25 30]
>>y=[48 61 39 58 74 44]
>>plot(x, y)
(b)function plotting:
If we want to plot a function y=sin(x) for t=0 to 0.04 sec, for this
x=wt .the vectors x and y can be created using colon operator.
>>f=50;
>>w=2*pi*f;
>>t=0:1e-3:0.04;
>>x=w*t;
>>y=sin(x);
>>plot(x, y)
>>plot(x, y, ‘k--o’)

(c)Property name and value:


In order to format plots, MATLAB provides facility to specify the
other operational properties such as marker size, face color of
marker, color of marker’s edge, and line width.
>>plot(x, y, ‘k o-.’,’linewidth’,3)
>>plot(x, y,’k o-.’,’markeredgecolor’,’g’)
>>plot(x, y, ‘c o -.’,’markerfacecolor’,’r’)
>>plot(x, y,’c o-.’,’markersize’,12)
(d)Labelling of plot:
Commands Description
X label Used to label x axis
Y label Used to label y axis
Z label Used to label z axis
Title Used to give title to the graph
Legend Used to give legends to multiple graphs
in a plot
Text used to write text on the plot at specipic
location
Gtext Used to write text on the plot using
mouse curser
Grid Used to show grids on the plot

(e)Multiple plots:
There are 3 different methods to draw multiple plots:
1.using plot function ->>plot(x1,y1,x2,y2,x3,y3)
2.using hold function->>hold on
3.using line function->>line(xdata, ydata)
(f)subplots:
>>subplot(2, 1, 1)
>>plot (y)
>>subplot(2, 1, 2)
>>plot(m)
(f)bar function:
>>bar(y)
>>bar(x, y)
(g)pie plots

(h)3D plots

:
(i)Mesh and surface plots:
1.creating a x-y grid: >>[X Y]=meshgrid(x, y)
>>[X Y]=meshgrid(xstart:xinc:xend,
ystart:yinc:yend)
2.calculating the value of Z: >>z=f(x, y)
3.creating mesh and surface plots:
>>mesh(x, y, z)
Simulink
Simulink derived from the word simulation and link.
Simulink is available in the form of a toolbox and is an
integral part of MATLAB. Simulink is a software package
for modeling, simulating, and analyzing dynamical systems.
Key features of simulink:
(1)graphical editor for building and managing hierarchical block
diagram.
(2)libraries of predefines blocks for modeling continuous time and
discrete time systems.
(3) scopes and data display for viewing simulation results.
(4)project and data management tools for managing data files and
data.
(5) legacy tools for importing C and C++ code into modles.

Starting up simulink:
(a) by typing simulink at the MATLAB and then enter
>>simulink
(b) by clicking on the icon from toolbar menu
(c)selecting simulink library browser from the pop up menu
obtain by clicking the start menu.
Let us take a simple example to create simulink model, to plot a
sinusoidal waveform:
Step 1-open a new model file
(a)by clicking on the new model icon from the toolbar of simulink library
browser

(b) by selecting new Model from the file menu

(c) by pressing ctrl+N keys at the active simulink browser.

Step 2-collectig blocks


Block Purpose Location in library

Sine wave Sine wave generator Source

Scope To plot waveform Sink


Mux Signal multiplier Commonly used blocks
and signal routing
Derivative Differentiate the input Commonly used blocks
signal continuous

Step 3-Labelling and connection


Step 4-modifying block parameters

Step 5-runnig simulation and viewing output

Various other examples are follows:


Conclusion
MATLAB learning program training being an integral part of
engineering curriculum provides not only easier
understanding but also helps acquaint an individual with
technologies. It exposes an individual to practical aspect of all
things which differ considerably from theoretical models.
During my training, I gained a lot of practical knowledge
which otherwise could have been exclusive to me. The
practical exposure required here will pay rich dividends to me
when I will set my foot as an Engineer.
The training at CRISP, Bhopal was altogether an exotic
experience, since work, culture and mutual cooperation was
excellent here. Moreover fruitful result of adherence to quality
control awareness of safety and employees were fare which is
much evident here.

THANK YOU

You might also like