matlab – a computational methods by rohit khokher department of computer science, sharda...
TRANSCRIPT
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
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.
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
MATLAB – A Computational Methods
Contd…
Workspace
Command History
Command Window
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
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
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
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
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
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
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
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)
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
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.
MATLAB – A Computational Methods
Contd…
To get started, type one of these commands: A simple program
» a=5;» b=a/2
b =
2.5000
»
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
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
Computational Methods MATLAB
Contd...
- (unary) + (unary)
Operator Symbol Syntax
Addition + a + b
Subtraction - a - b
Assignment = a = b (assign b to a)
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 ~=
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
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
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
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
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.
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,
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.
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')
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')
MATLAB – A Computational Methods
Contd…
Types of 2-D Graphs
• Line Graphs
• Bar Graphs
• Area Graphs
• Direction Graphs
• Radial Graphs
• Scatter Graphs
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)
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)
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)