0% found this document useful (0 votes)
14 views18 pages

DIP Unit I

MATLAB is a high-performance software package designed for mathematical computation, visualization, and programming, primarily using matrices and arrays. It offers an interactive environment with built-in functions, supports multiple programming paradigms, and is widely used for technical computing. While MATLAB provides significant advantages like ease of use and platform independence, it also has drawbacks such as being an interpreted language and relatively high cost.

Uploaded by

omkarsanmukrao83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views18 pages

DIP Unit I

MATLAB is a high-performance software package designed for mathematical computation, visualization, and programming, primarily using matrices and arrays. It offers an interactive environment with built-in functions, supports multiple programming paradigms, and is widely used for technical computing. While MATLAB provides significant advantages like ease of use and platform independence, it also has drawbacks such as being an interpreted language and relatively high cost.

Uploaded by

omkarsanmukrao83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

What is MATLAB?

MATLAB is a software package for high-performance mathematical computation,


visualization, and programming environment. It provides an interactive environment
with hundreds of built-in functions for technical computing, graphics, and animations.

MATLAB stands for Matrix Laboratory. MATLAB was written initially to implement a
simple approach to matrix software developed by the LINPACK (Linear system package)
and EISPACK (Eigen system package) projects.

MATLAB is a modern programming language environment, and it has refined data


structures, includes built-in editing and debugging tools, and supports object-oriented
programming.

MATLAB is Multi-paradigm. So, it can work with multiple types of programming


approaches, such as Functional, Object-Oriented, and Visual.

Besides an environment, MATLAB is also a programming language.

As its name contains the word Matrix, MATLAB does its' all computing based on
mathematical matrices and arrays. MATLAB's all types of variables hold data in the form
of the array only, let it be an integer type, character type or String type variable.

The development of the MATLAB started in the late 1970s by Cleve Moler, the chairman
of the Computer Science department at the University of New Mexico. Cleve wanted to
make his students able to use LINPACK & EISPACK (software libraries for numerical
computing, written in FORTRAN), and without learning FORTRAN. In 1984, Cleve Moler
with Jack Little & Steve Bangert rewrote MATLAB in C and founded MathWorks. These
libraries were known as JACKPAC at that time, later these were revised in 2000 for matrix
manipulation and named as LAPACK.

Advantages of MATLAB
Ease of Use

The program can be used as a scratchpad to evaluate expressions typed at the


command line, or it can be used to execute large prewritten programs. Applications may
be written and changed with the built-in integrated development environment and
debugged with the MATLAB debugger. Because the language is so simple to use, it is
optimal for the fast prototyping of new applications.
Many program development tools are supported to make the program easy to use.
They contain an integrated editor/debugger, on-line documentation and manuals, a
workspace browser, and extensive demos.

Platform Independence

MATLAB is supported on different computer systems, providing a considerable measure


of platform independence. The language is provided on Windows 2000/XP/Vista, Linux,
various versions of UNIX, and the Macintosh. Applications written on any platform will
run on the other entire platform, and information files written on any platform may be
read apparently on any other platform. As a result, programs written in MATLAB can
shift to new platforms when the needs of the user change.

Predefined Functions

MATLAB comes complete with a huge library of predefined functions that provides
tested and prepackaged solutions to many primary technical tasks. For example,
suppose that we are writing a program that must evaluate the statistics associated with
an input data set. In most languages, we would need to write our subroutines or
functions to implement calculations such as the arithmetic mean, standard deviation,
median, and so on. These and hundreds of other services are built right into the
MATLAB language, making your job much more comfortable.

In addition to the vast libraries of services built into the basic MATLAB language, there
are many special-purpose toolboxes applicable to help solve complex problems in
particular areas. For example, a user can buy standard toolkits to solve problems in
signal processing, control systems, communications, image processing, and neural
networks, etc. There is also a broad compilation of free user-contributed MATLAB
programs that are shared through the MATLAB Web site.

Device-Independent Plotting

MATLAB has many basic plotting and imaging commands. The plots and pictures can be
displayed on any graphical output device provided by the computer on which MATLAB
is running. This facility makes MATLAB an outstanding tool for visualizing technical
information.
Graphical User Interface

MATLAB contains a tool that allows a programmer to interactively design a Graphical


User Interface (GUI) for his program. With this capability, the programmer can design
refined data-analysis programs that can be operated by relatively inexperienced users.

MATLAB Compiler

MATLAB's adaptability and platform independence are produced by compiling MATLAB


applications into a machine-independent p-code and then interpreting the p-code
instruction at runtime. This method is equivalent to that used by Microsoft's Visual Basic
language. Unfortunately, the resulting applications can sometimes execute slowly
because the MATLAB code is interpreted rather than compiled.

A separate MATLAB compiler is available. This compiler can compile MATLAB programs
into a real executable that runs faster than the interpreted code. It is a great technique
to convert a prototype MATLAB program into an executable suitable for sale and
distribution to users

Disadvantage of MATLAB
There is two major disadvantage of MATLAB programming language

Interpreted language

The first disadvantage is that it is an interpreted language and, therefore, may execute
more slowly than compiled language. This problem can be check by properly structuring
the MATLAB program.

Cost

A full copy of MATLAB is five to ten times more costly than a conventional C or
FORTRAN compiler. This comparatively high cost is more than offset by the decreased
time necessary for an engineer or scientist to create a working program, so MATLAB is
cost-effective for businesses. However, it is too expensive for most individuals to
consider purchasing. Fortunately, there is also an inexpensive Student Edition of
MATLAB, which is an excellent tool for students wishing to learn the language. The
Student Edition of MATLAB is virtually identical to the full edition.
MATLAB Environment
The structural unit of data in any MATLAB program is the array. An array is a collection
of record values organized into rows and columns and known by a single name.
Individual data values within an array may be accessed by including the name of the
array, followed by subscripts in parentheses that identify the row and column of the
particular value. Even scalars are acted as arrays by MATLAB: they are arrays with only
one row and one column. When MATLAB executes, it can display various types of
windows that accept commands or display data.

MATLAB Desktop
When we start MATLAB Version 7.4, a specific window known as the MATLAB desktop
appears. The desktop is a window that includes other windows display MATLAB data,
plus toolbars and a "Start" button similar to that used by Windows XP. By default, most
MATLAB tools are "docked" to the desktop so that they develop inside the desktop
window. However, the user can choose to "undock" any or all tools, making them
appear in windows separate from the desktop.

It integrates various tools for managing data, variables, and applications within the
MATLAB environment.

The main tools within or accessible from the MATLAB desktop are

o Command Window
o Command History Window
o Start Button
o Documents Window, containing the Editor/Debugger and the Array Editor
o Figure Windows
o Workspace Browser
o Help Browser
o Path Browser

Command Window
This is the main window. It is characterized by the MATLAB command prompt (>>).
When you launch the function program, MATLAB puts you in this window. All
commands containing those for running user-written programs are typed in this window
at the MATLAB prompt. In MATLAB, this window is a part of the MATLAB that contains
other smaller windows or panes.

Here is an image of the screen that appears when you open the MATLAB.

MATLAB Command Window Drop Downs-Show


Command Window Actions
o On the top bar, where 'Command Window' is written, there is a drop-down menu
located on the right side; this is called 'Show Command Window Actions.'
o When you click on it, a drop-down menu appears with some options, like Clear
Command Window, Find, Print, etc.
o These options can be used for specific tasks, such as Clear Command Window
option erases all the data from the command window, Find option can be used to
find any word, letter, variable or function on the Command Window.

MATLAB Command Window Drop Downs-Browse for


Functions

o On the left side, inside the command window, there is something visible in small
fonts, and that is fx, an abbreviated form for function.
o When you click on the fx, a drop-down search bar gets opened.
o As shown in the image, these all are the name of the products we have installed.
o Click on any of the products to get a list of all the related functions.
o 'Browse for Functions' is a function search pop-up, and we can browse for the
functions related to all the installed products.
o The keyboard shortcut for the function search pop-up is Shift + F1.

MATLAB Command Window-Command Line


o Here is the Command-Line started just in front of fx, indicated with the prompt,
>>, two consecutive greater than signs, and a blinking cursor ahead.
o Use the command line to enter a variable, to call a function, or to express a
calculation.

MATLAB Command Window-Working at Command


Line
Type here anything you want. Anything entered at the Command Line is treated as a
command. For example, there are six types of commands entered to explain their
meaning.

i)
o Whenever we enter any character or word or sentence and press enter, it will
show error like this-Undefined function or variable 'jdjdkjdk45'.
o So the text should be enclosed either in double quotes (" ") or single quotes (' ').

ii)

When we entered the above text, many things happened internally:

o It creates a default variable, named as ans.


o ans stands for the answer, so it is the answer or output of our command.
o This variable is stored in the workspace and declared as String data type.
o Whenever we enter this variable as a command, its previous stored value gets
displayed as the output.
o As this is the default variable, so whenever any valid command comes at the
command line without providing any variable to hold the result, its value is stored
in the ans variable only, and previous value gets replaced with the new one.

Using MATLAB Scratch Pad

MatLab: Scratch Pad & Arithmetic Operations


Major Reference Source: MatLab Verson 7.0

Using MatLab package as a scratch pad interactively is one of the common


application of the MatLab package. MatLab commands are entered at the prompt
command, >>, of the Command Window through the PC keyboard. Both entered
commands and evaluation results are displayed in the Command Window. All new
line or lines of commands or instructions after the prompt command will be passed
to MatLab for evaluation after pressing the Enter key no matter the position of
mouse cursor is at the end of line or lines or not.
Arithmetic Operators

Since all types of data in MatLab are stored in the form of arrays, MatLab provides
two different kinds of arithmetic operations, i.e. array operations and matrix
operations. MatLab array operations are just ordinary arithmetic operations that
supports multidimenstional arrays for processing element by elemt operations.
While MatLab matrix operation are ordinary matrix operations that following the
rules of linear algebra. Both array operations and matrix operations share the
similar symbols of operation, a period characater, "." is used to distinguish the array
operations from the matrix operations. However, as the addition and subtraction
for both matrix operation and array operation are the same, the period characater,
"." is not necessary and the character pairs ".+" and ".-" are not used. Besides, the 1-
by-1 array, scalar, is also a special type of MatLab array. A 1-by-1 array, scalar can
have array operation with an array of any size also. A 1-by-1 matrix, scalar can also
have martrix operation with a matrix of any size, but limited by the matrix
multiplication, the 1-by-1 matrix, scalar can only be the divisor of the right and left
division.

The matrix and array arithmetic includes seven types of operations:

Element-Wise Array
Matrix
Arithmetic Arithmetic
Arithmetic Operations
Operators Operations

Syntax Description Syntax Description

Addition A+B Addition A+B Addition


+ +A Unary Plus +A Unary Plus

Subtraction A-B Subtraction A-B Subtraction


- -A Unary Minus -A Unary Minus

Multiplication Matrix Array


A*B A.*B
* Multiplication Multiplication

Right Division Forward Slash or Matrix Right Array


A/B A./B
/ Division Right Division

Left Division A\B Backslash or Matrix Left Division A.\B Array


\ Left Division

Power
A^B Matrix Power A.^B Array Power
^

Transpose Matrix Transpose or


A' A.' Array Transpose
' Complex Conjugate Transpose

Matrix Arithmetic Operations


+ Addition

The + addition operator of the expression A+B means adds matrix B to matrix A.
Since only a scalar can be added by or added to a matrix of any size, unless either A
or B is a scalar, A and B must have the same size.

Examples
 m-by-n matrix A + m-by-n matrix B
 scalar S + matrix A + scalar S

+ Unary Plus

The structural unit of data in any MATLAB program is the array. An array is a collection
of record values organized into rows and columns and known by a single name. Arrays
can be allocated as either vectors or matrices. The term "vector" is generally used to
define an array with only one dimension, while the term "matrix" is usually used to
determine an array with two or more dimensions.

The numbers of the row state the size of an array, and the numbers of the column in the
array, with the numbers of the row, mentioned first. The total number of items in the
array will be the product of the number of rows and the number of the column.

For example, the size of the following arrays are:


Individual items in an array are addressed by the array name followed by the row and
column of the particular item. If the array is a row or column vectors, then only one
subscripts are needed. For example, in the preceding array a(2,1) is 3 and c(2) = 2.

M
Facebook Temporarily Allows Violent Anti-Russian Posts

A MATLAB variable is an area of memory, including an array, which is called by a


customer-specified name. The content of the array may be used or modified at any time
by containing its name in an appropriate MATLAB command.

MATLAB variables name must start with a letter, followed by any sequence of letters,
numbers, and the underscore (_) character. Only the first 63 characters are essential; if
more than 63 are used, the remaining characters will be ignored. If two variables are
stated with names that only differ in the 64th character, MATLAB will treat them as the
same variable.
Creating and Initializing Variables in MATLAB
MATLAB variables are automatically generated when they are initialized.

There are three methods to initialize a variable in MATLAB:

1. It assigns data to the variable in an assignment statement.


2. It input data into the variable from the keyboard.
3. It read data from a file.

Initializing Variables in Assignment Statement


The simplest method to initialize a variable is to assign it one or more value in an
assignment statement.

An assignment statement has the standard form

var = expression;

where var is the name of the variables and expression is a scalar constant, an array, or a
combination of constants, other variables, and mathematical operations (+, -, etc.). The
value of the expression is computed using the standard rules of mathematics, and the
resulting values are saved in the named variable. The semicolon at the last of the
statement is optional. If the semicolon is absent, the values assigned to var will be
echoed in the command window. If it is present, nothing will be shown in the Command
Window, even though the assignment has appeared.

Examples of initializing variables with assignment statements contain

var = 40i;
var2 = var/5;
x = 1; y = 2;
array = [1 2 3 4];

The first example generates a scalar variable of type double and saves the imaginary
number 40i in it.

The second example generates a scalar variable and saves the result of the expression
var/5 in it.
The third example shows that multiple assignment statements can be placed on a single
line, supported that they are divided by semicolons or commas.

The last example display that variables can also be initialized with arrays of data. Such
arrays are build up using brackets ([]) and semicolons. All of the items of an array are
listed in row order. In other words, the value in each row are recorded from left to the
right, with the top-most row first, and the bottom-most row last. The single value within
a row are separated by blank spaces or commas, and the rows themselves are divided
by semicolons or newlines.

The following statements are all legal arrays that can be used to initialize a variable:

The number of items in every row of an array must be the same, and the number of
items in every column must be the same. An expression such as

[1 2 3; 4 5];

is illegal because row 1 has three items, while row 2 has only two items.
Initializing with Built-In Functions
Arrays can also be initialize using built-in MATLAB function. For example, the function
zero can be used to generate an all-zero array of any desired size. There are a various
form of the zeros function. If the function has an individual scalar argument, it will
develop a square array using the single arguments as both the number of rows and the
number of columns. If the function has two scalar argument, the first arguments will be
the number of rows, and the second arguments will be the number of the columns.
Since the size function return two values including the number of row and column in an
array, it can be combined with the zero function to create an array of zeros that is the
same size of another array.

Some examples using the zeros function follow:

a = zeros(2);
b = zeros(2,3);
c = [1 2; 3 4];
d = zeros(size(c));

These statements generate the following arrays:

Similarly, the ones function can be used to generate array including all ones, and the
eye function can be used to generate arrays including identity matrices, in which all
on-diagonal items are one, while all off-diagonal items are zero.

Following the tables containing a list of standard MATLAB functions useful for initializing
variables.

MATLAB Functions Useful for Initializing Variables

Functions Purpose

zeros(n) Creates a n x n matrix of zeros.

zeros(m,n) Creates a m x n matrix of zeros


zeros(size(arr)) Create a matrix of zeros of the same size as arr.

ones(n) Creates a n x n matrix of ones.

ones(m,n) Creates a m x n matrix of ones.

ones(size(arr)) Creates a matrix of ones of the same size as arr.

eye(n) Creates a n x n identity matrix.

eye(m,n) Creates an m x n identity matrix.

length(arr) Return the length of a vector, or the longest dimension of a 2-


D array.

size(arr) Return two values specifying the number of rows and columns
in arr.

Initializing Variables with Keyboard Input


It is also possible to prompt a customer and initialize a variable with a record that he or
she type directly at the keyboard. This option enables a script file to prompt a customer
for input data value while it is executing. The input function shows a prompt string in
the Command Window and then waits for the user to type in a response.

For example, consider the following statements:

my_val = input('Enter an input value:');

When this function is executed, MATLAB prints out the string 'Enter an input value:,' and
then waits for the client to respond. If the customer enters a single number, it may just
be typed in. If the customer enter an array, it must be enclosed in brackets. In other
case, whatever is typed will be stored in variable my_val when the return key is entered.
If only the return key is entered, then an empty matrix will be generated and stored in
the variable.

If the input function contains the character 's' as a second argument, then the input data
is returned to the customer as a character string. Thus, the statement

» in1 = input('Enter data: ');


Enter data: 1.23
stores the value 1.23 into in1, while the statement
» in2 = input('Enter data: ','s');
Enter data: 1.23
save the character string '1.23' into in2.

Multidimensional Arrays
Try This ExampleCopy Command Copy Code

A multidimensional array in MATLAB® is an array with more than two dimensions. In a matrix, the two
dimensions are represented by rows and columns.

Each element is defined by two subscripts, the row index and the column index. Multidimensional arrays
are an extension of 2-D matrices and use additional subscripts for indexing. A 3-D array, for example,
uses three subscripts. The first two are just like a matrix, but the third dimension
represents pages or sheets of elements.

Creating Multidimensional Arrays


You can create a multidimensional array by creating a 2-D matrix first, and then extending it. For
example, first define a 3-by-3 matrix as the first page in a 3-D array.
A = [1 2 3; 4 5 6; 7 8 9]
A = 3×3

1 2 3
4 5 6
7 8 9

Now add a second page. To do this, assign another 3-by-3 matrix to the index value 2 in the third
dimension. The syntax A(:,:,2) uses a colon in the first and second dimensions to include all rows and
all columns from the right-hand side of the assignment.
A(:,:,2) = [10 11 12; 13 14 15; 16 17 18]
A =
A(:,:,1) =

1 2 3
4 5 6
7 8 9

A(:,:,2) =

10 11 12
13 14 15
16 17 18

The cat function can be a useful tool for building multidimensional arrays. For example, create a new 3-D
array B by concatenating A with a third page. The first argument indicates which dimension to concatenate
along.
B = cat(3,A,[3 2 1; 0 9 8; 5 3 7])
B =
B(:,:,1) =

1 2 3
4 5 6
7 8 9

B(:,:,2) =

10 11 12
13 14 15
16 17 18

B(:,:,3) =

3 2 1
0 9 8
5 3 7

You might also like