matlab – a computational methods by rohit khokher department of computer science, sharda...

34
MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

Upload: iyana-lyman

Post on 01-Apr-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

By

Rohit Khokher

Department of Computer Science, Sharda University,

Greater Noida, India

MATLAB – A Computational Methods

Page 2: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Outline of the Talk

Page 3: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Introduction

• MATLAB is a program for doing numerical computation. It was originally designed for solving linear algebra type problems using matrices. It’s name is derived from MATrix LABoratory.

• MATLAB has since been expanded and now has built-in functions for solving problems requiring data analysis, signal processing, optimization, and several other types of scientific computations. It also contains functions for 2-D and 3-D graphics and animation.

Page 4: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

MatLab : Matrix Laboratory

Numerical Computations with matrices

• Every number can be represented as matrix

Why Matlab?

• User Friendly (GUI)

• Easy to work with

• Powerful tools for complex mathematics

Page 5: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

Workspace

Command History

Command Window

Page 6: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Array and Function Files

• MATLAB treats all variables as matrices. For our purposes a matrix can be thought of as an array, in fact, that is how it is stored.

• Vectors are special forms of matrices and contain only one row OR one column.

• Scalars are matrices with only one row AND one column

MATLAB Matrices

Page 7: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

• A matrix with only one row AND one column is a scalar. A scalar can be created in MATLAB as follows:

» a_value=23

a_value =

23

Page 8: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

• A matrix with only one row is called a row vector. A row vector can be created in MATLAB as follows (note the commas):

» rowvec = [12 , 14 , 63]

rowvec =

12 14 63

Page 9: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

• A matrix with only one column is called a column vector. A column vector can be created in MATLAB as follows (note the semicolons):

» colvec = [13 ; 45 ; -2]

colvec =

13

45

-2

Page 10: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

• A matrix can be created in MATLAB as follows (note the commas AND semicolons):

» matrix = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9]

matrix =

1 2 3 4 5 6 7 8 9

Page 11: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

• a vector x = [1 2 5 1]

x =1 2 5 1

• transpose y = x.’

y = 12

51

• a matrix x = [1 2 3; 5 1 4; 3 2 -1]

x = 1 2 3 5 1 4 3 2 -1

Page 12: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

• x(i,j) subscription

• whole row

y=x(2,3)

y =

4

y=x(3,:)

y =

3 2 -1

y=x(:,2)

y =

2

1

2

• whole column

Page 13: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Files

• Use predefined functions or write your own functions

• Reside on the current directory or the search path

– add with File/Set Path

• Use the Editor/Debugger to edit, run

MatLab Files (.m)

Page 14: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

Reading Data from files

• MATLAB supports reading an entire file and creating a matrix of the data with one statement.

>> load mydata.dat; % loads file into matrix.

% The matrix may be a scalar, a vector, or a

% matrix with multiple rows and columns. The

% matrix will be named mydata.

>> size (mydata) % size will return the number

% of rows and number of

% columns in the matrix

>> length (myvector) % length will return the total

% no. of elements in myvector

Page 15: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Programming in MATLAB

MATLAB Variable Names

• Variable names ARE case sensitive

• Variable names can contain up to 63 characters (as of MATLAB 6.5 and newer)

• Variable names must start with a letter followed by letters, digits, and underscores.

Page 16: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

To get started, type one of these commands: A simple program

» a=5;» b=a/2

b =

2.5000

»

Page 17: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

Computational Methods MATLAB

Contd…

MATLAB symbols

>> prompt

. . . continue statement on next line

, separate statements and data

% start comment which ends at end of line

; (1) suppress output(2) used as a row separator

in a matrix

: specify range

Page 18: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

Computational Methods MATLAB

Contd…

MATLAB Math & Assignment Operators

Operator Symbol Syntax Syntax

Power ^ or .^ a^b a.^b

Multiplication * or .* a*b a.*b

Division

or

/ or ./

\ or .\

a/b

b\a

a./b

b.\a

NOTE: 56/8 = 8\56

Page 19: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

Computational Methods MATLAB

Contd...

- (unary) + (unary)

Operator Symbol Syntax

Addition + a + b

Subtraction - a - b

Assignment = a = b (assign b to a)

Page 20: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

MATLAB Relational Operators

• MATLAB supports six relational operators.

Operator Symbol

Less Than <

Less Than or Equal <=

Greater Than >

Greater Than or Equal >=

Equal To ==

Not Equal To ~=

Page 21: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

MATLAB Logical Operators

• MATLAB supports three logical operators.

Operator Symbol Precedence

not ~ % highest precedence

and & % equal precedence with or

or | % equal precedence with and

Page 22: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

Computational Methods MATLAB

Contd…

• A for loop in MATLAB for i = 1:1:100

temp(1,i)=0; end

• A while loop in MATLAB while x <= 10

% execute these commands end

Page 23: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

Computational Methods MATLAB

Contd…

Scalar - Matrix Addition

» a=3;

» b=[1, 2, 3;4, 5, 6]

b =

1 2 3

4 5 6

» c= b+a % Add a to each element of b

c =

4 5 6

7 8 9

Page 24: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

Computational Methods MATLAB

Contd…

Scalar - Matrix Multiplication

» a=3;

» b=[1, 2, 3; 4, 5, 6]

b =

1 2 3

4 5 6

» c = a * b % Multiply each element of b by a

c =

3 6 9

12 15 18

Page 25: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

Computational Methods MATLAB

Importing and Exporting Data

• Importing Data into the Workspace

The first step in analyzing data is to import it into the MATLAB workspace. See the topics under Importing Data for detailed information about supported data formats and the functions.

When you work with time series data, use the Time Series Tools GUI (tstool) to import the data and create timeseries objects. The Time Series Tools Import Wizard makes it easy to import or define a time vector for your data.

Page 26: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

Computational Methods MATLAB

Contd…

• Exporting Data from the Workspace

When you analyze your data, you might create new variables or modified imported variables. You can export variables from the MATLAB workspace to various file formats, both character-based and binary. You can, for example, create HDF and Excel files containing your data,

Page 27: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Plots

• A plot is any graphic display you can create within a figure window. Plots can display tabular data, geometric objects, surface and image objects, and annotations such as titles, legends, and colorbars. Figures can contain any number of plots. Each plot is created within a 2-D or a 3-D data space called an axes.

Page 28: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

Computational Methods MATLAB

Contd…

Plotting with MATLAB

• There are commands in MATLAB to "annotate" a plot to put on axis labels, titles, and legends. For example:

>> % To put a label on the axes we would use:

>> xlabel ('X-axis label')

>> ylabel ('Y-axis label')

>> % To put a title on the plot, we would use:

>> title ('Title of my plot')

Page 29: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

x = 0:pi/100:2*pi;

y = sin(x);

plot(x,y)

xlabel('x = 0:2\pi')

ylabel('Sine of x')

title('Plot of the Sine Function')

Page 30: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

Types of 2-D Graphs

• Line Graphs

• Bar Graphs

• Area Graphs

• Direction Graphs

• Radial Graphs

• Scatter Graphs

Page 31: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

3-D Plots Here are some examples of surface plots in MATLAB.

Mesh Plot of Peaks

z=peaks(25);mesh(z);

Colormap(hsv)

Surface Plot of Peaksz=peaks(25);

surf(z); colormap(jet);

Contour Plot of Peaksz=peaks(25);

contour(z,16); colormap(hsv)

Page 32: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Applications of Matlab

Linear Algebra Solving a linear system Gaussian elimination finding eigenvalues and eigenvectors Matrix factorization

Curve Fitting and Interpolation Polynomial curve fitting on the fly Least squares curve fitting Interpolation

Data Analysis and Statistics

Numerical Integration

Ordinary Differential Equations

Nonlinear Algebraic Equations

Graphs ( 2 D and 3 D)

Page 33: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

Contd…

3-D Plots Here are some examples of surface plots in MATLAB.

Mesh Plot of Peaks

z=peaks(25);mesh(z);

Colormap(hsv)

Surface Plot of Peaksz=peaks(25);

surf(z); colormap(jet);

Contour Plot of Peaksz=peaks(25);

contour(z,16); colormap(hsv)

Page 34: MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods

MATLAB – A Computational Methods

THANK YOU!!!

For any queries contact:[email protected]