Sei sulla pagina 1di 24

Chapter 3

Introduction MATLAB
MATLAB - Overview
MATLAB (matrix laboratory) is a fourth-generation
high-level programming language and interactive
environment for numerical computation, visualization
and programming.
Objectives used in MATLAB
• It allows matrix manipulations.
• plotting of functions and data
• implementation of algorithms.
• creation of user interfaces.
• interfacing with programs written in other
languages, including C, C++, Java, and FORTRAN .
• analyze data.
• developed algorithms.
Applications of MATLAB
• MATLAB is widely used as a computational tool in
science and engineering 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
Basic syntax of MATLAB
• 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.
Type a valid expression, for example,
5+5
And press ENTER
• ans = 10
Let us take up few more examples
• 3 ^ 2 % 3 raised to the power of 2
• ans = 9
• sin(pi /2) % sine of angle 90
• When you click the Execute button, or type Ctrl+E,
MATLAB executes it immediately and the result.
• ans = 1
• 7/0 % Divide by zero
• ans = Inf warning: division by zero
Use of Semicolon (;) in MATLAB
• 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.
• x = 3; y = x + 5
y=8
• The percent symbol (%) is used for indicating a comment
line. For example,
x = 9 % assign the value 9 to x
Special Variables and Constants
• MATLAB supports the following special variables
and constants:
1.InfInfinity.NaNUndefined numerical result (not a number).
piThe number π.
Naming Variables
Variable names consist of a letter followed by any
number of letters, digits or underscore.
MATLAB is case-sensitive.
Variables must have values before they are used.
Saving Your Work
• The save command is used for saving all the
variables in the workspace, as a file with .mat
extension, in the current directory.
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 limit the elements.
Examples of raw vector
Ex:r = [7 8 9 10 11]
MATLAB will execute the above statement result is:
r=7 8 9 10 11.
r = [7 8 9 10 11]; t = [2, 3, 4, 5, 6]; res = r + t
after MATLAB execution result will be
res = 9 11 13 15 17
Column Vector
Column vectors: are created by enclosing the set of
elements in square brackets, using semicolon(;) to limit the
elements.
Ex.c = [7; 8; 9; 10; 11]
After executing MATLAB the result will be
C= 7
8
9
10
11
Referencing the Elements of a vector
• You can be reference one or more of the elements
of a vector in several ways.
The ith component of a vector v is referred as v(i)
Ex1: v=[1;2;3;4;5;6;];
v(3)
After execution ans=3
Ex2: v=[1;2;3;4;5;6;];
V(:)
It will display all the elements in column.
MATLAB MATRIX
• 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 using semicolons to mark the end of each row.
• For example, let us create a 3-by-3 matrix as:
• m = [1 2 3; 4 5 6; 7 8 9]
• M= 1 2 3
4 5 6
• 7 8 9
multi-dimensional arrays:
• it is possible in MATLAB to handle multi-
dimensional arrays.
• MATLAB's internal data structure is limited to a
two-dimensional matrix.
• But to handle multi-dimensional arrays in
MATLAB, you can create your own functions in
MATLAB language.
What is Simulink in matlab?
• Simulink is an add-on product to MATLAB, it
provides an interactive, simulating, graphical
environment for modeling and analyzing of
dynamic systems.
MATLAB consists of five main parts
1.MatLab Language
2.MatLab working environment
3.Handle Graphics
4.MatLab function library
5.MatLab Application Program Interface (API)

• MatLab API :is a library that enables you


to write Fortran and C programs that interact with
MatLab. It contains the facilities for calling routines
from MatLab, for reading and writing Mat files and
calling Matlab as a computational engine.
MATLAB provides the following
three type of loops:
• While Loop
• For Loop
• Nested Loops

MATLAB allows following Operators:


• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operations
• Set Operations
How polynomials can be represented in
MATLAB
• A polynomial in MATLAB is denoted by a vector.
• To create a polynomial in MATLAB enter each co-
efficient of the polynomial into the vector in
descending order.
the type of program files that MATLAB
allows to write
MATLAB allows two types of program files.
• • Scripts: It is a file with .m extension. In these
files, it writes series of command that you
want to execute together. It does not accept
inputs and do not return any outputs
• • Functions: They are also files with .m
extension. Functions can accept inputs and
return outputs.
The graphic system used in MATLAB
• Graphic system used in MATLAB is known as
handle graphics.
• . It has a high level and low-level commands.
• • High Level Commands: High level
command performs image processing, data
visualization and animation for 2D and 3D
presentation graphics.
• Low Level Commands: Full customization of
the appearance of graphics and building of
complete graphical user interface
Interpolation and Extrapolation in
MATLAB
• • Interpolation: Taking out function values
between different data points in an array is
referred as Interpolation
• • Extrapolation: Finding function values
beyond the endpoints in array is referred as
Extrapolation
• The two types of Interpolation and
Extrapolation are
• • Linear Interpolation and Extrapolation
• Quadratic Interpolation and Extrapolation
Difference of Get and Set in
MATLAB
• Get and Set are referred as getter and setter
functions.
• The difference is that setter functions are used
For assigning properties, while getter functions
are used for accessing properties.

• Quitting MATLAB :To end your MATLAB session,


type quit in the Command Window, or select
File —► Exit MATLAB in the desktop main
menu.
Creating MATLAB variables
• MATLAB variables are created with an assignment
statement.
• The syntax of variable assignment is:
• variable name = a value (or an expression)
• For example,
• >> x = expression
• where expression is a combination of numerical values,
mathematical operators, variables, and function calls.
• There is a long list of mathematical functions that are
built into MATLAB. These functions are called Build-ins.

Potrebbero piacerti anche