Tutorial 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

MATLAB WORKSHOP

MATLAB
An Introductory Workshop
AKHILESH SHENDE
Department of Chemistry,
Indian Institute of Technology Kanpur
14th 19th October 2013
Organized by
Promotion of Work Experience & Research
(PoWER)

Akhilesh Shende

MATLAB WORKSHOP

Workshop Flow Chart


2. Language Fundamentals

1. Introduction
Desktop Basics

Matrix and Array Operations

Command Line Operations

In-build functions

Matrix and Array

Expression : Variables and Constants

2D & 3D Plots

Structures

Scripts & Functions

Command Line Expression

3. Mathematical Applications

4. Graphics

Eigen Values

Basic Plotting

System with linear equations

2D Plots

Data Analysis & Regression Model

Mesh & Surf Plots


5. Programming

Control Flow
Scripts
Functions

Akhilesh Shende

Introduction

MATLAB WORKSHOP

MATLAB
An Introductory Workshop
Tutorial 1

Introduction

Tutorial 1

Akhilesh Shende

MATLAB WORKSHOP

What is MATLAB
Numerical Computing environment and Programming Language
Developed by MathWorks organization www.mathworks.in
MATLAB - MATrix LABoratory
It allows us to perform
1. Mathematical operations : Matrix manipulations
2. Plotting of functions and data
3. Implementation of algorithms
4. Creation of user interfaces and interfacing with programs written in other languages
Fortran , C, C++ and Java
Used by
Academic and Research Institutions
Industrial Enterprises.

Akhilesh Shende

MATLAB WORKSHOP

Uses & Applications

Akhilesh Shende

MATLAB WORKSHOP

Fundamental unit of MATLAB


Array
A collection of data values organized into rows and columns

Row 1
Row 2

arr(3,2)

Row 3
Row 4

Arrays can be classified as vectors and matrices.


Vectors : Array with one dimension
Matrix : Array with more than one dimension

Col 1 Col 2 Col 3 Col 4

Scalars are also treated as arrays by MATLAB (1 row and 1 column).


Row and column indices of an array start from 1 and not from 0.
Akhilesh Shende

Introduction

MATLAB WORKSHOP

MATLAB at Computer Center (CC)


Window Users

Install Xming on windows


www.iitk.ac.in/cc/windows/mailimages/mathematica/mathematica-xming.htm
IP Addresses for Linux Computers
www.iitk.ac.in/cc/linuxlab.htm

Linux Users
ssh X [email protected] - [email protected]
172.31.4.141 - 172.31.4.210

% To run the MATLAB


>> matlab

Tutorial 1

Akhilesh Shende

Introduction

MATLAB WORKSHOP

Desktop Basics

Tutorial 1

Akhilesh Shende

Introduction

MATLAB WORKSHOP

Command Line Operations


Entering the data in Command Line : MATLAB as Calculator
1. 4 + 3;
2. 3*2;
3. 4^2;
4. a = 3;
5. b = 2;
6. c = a + b;
7. d = a - b;
8. e = a*b;
9. f = a/b;
10. g = a\b;
11. h = a^b;

% Clearing the workspace


clear
% Clearing the command window
clc
% Clearing a variable
clear variable_ name

% Closing MATLAB
exit
% Saving the workspace as .mat file

Scalar quantities are treated as array of 1 row and 1 column


Tutorial 1

Akhilesh Shende

Introduction

MATLAB WORKSHOP

Matrix & Array : Array Generation


Row, Column vectors and matrix dimension

% Entering a Row Vector


1. Starts from [
2. Space or , between 2 numbers of a row
3. Ends with ]
Example : v = [1 4 7 10 13];
v = [1, 4, 7, 10, 13];

% Entering a Matrix
1. Starts for [
2. Space or , between 2 numbers of a row
3. ; between 2 numbers of a column
4. Ends with ]
Example : A = [1 2 3; 4 5 6; 7 8 10];

Tutorial 1

% Entering a Column Vector


1. Starts from [
2. ; between 2 numbers of a column
3. Ends with ]
Example : v = [1 ; 4 ; 7 ;10 ;13];

% Dimension of a Matrix
1. [m, n] = size(A)
2. or size(A)

Akhilesh Shende

Introduction

MATLAB WORKSHOP

Matrix & Array Cont ...


Indexing, Scalar Operation and Colon Operator
% Indexing of a matrix
1. Indexing starts from 1 & not from 0
2. [m,n] = size(A)
3. A(m, n )
where 1 m m
1 n n

% Scalar Operation on a Matrix


1. Matrix A of size(m,n)
2. B = A scalar quantity (constant )
3. A = A scalar quantity
Examples : B = A - 8.5

% Colon Operator (:)


Use to create a variable with defined size m or n
x = 1:5, x = 1:7 , size 5 and 7 (default difference 1)
x = 1:0.5: 5, x = 1:0.5: 7 , size 9 and 13 (difference 0.5)
To represent the data with it size
A(3,5) - size 3, 5.
A(:,5) - all the row elements of 5th Column
A(3,:) - all the column element of 3rd Row
A(m, n:n) - all elements of mth row, from nth to nth columns
Tutorial 1

Akhilesh Shende

Introduction

MATLAB WORKSHOP

Matrix & Array Cont ...


Replacing and Deleting of elements in matrix and Concatenation of 2 Arrays
% Replacing an elements from a matrix
1. Matrix A of size(m,n)
2. A(m, n ) = number ;
3. (mth , nth ) element will be replaced

% Deleting a row or column


1. Matrix A of size(m,n)
2. A(1,:) = [ ];
3. A(:,3) = [ ];

% Concatenation of 2 Arrays
Joining of 2 arrays to make a single array
Matrices A of size (m,n) , B of size (m,n)
Two types of Concatenations
Vertical Concatenation (Increase the number of rows)
C = [A; B]; if (m == m or or mm and n==n)
Horizontal Concatenation (Increase the number of columns)
C = [A, B] ; ) if (m == m and n==n or nn )
% Swap the columns
1. Matrix A = magic(4) of size(4,4)
2. B = A(:, [1 3 2 4])
3. Swap the columns 2 & 3
Tutorial 1

% Swap the Rows


1. Matrix A = magic(4) of size(4,4)
2. B = A([1 3 2 4],:)
3. Swap the rows 2 & 3
Akhilesh Shende

Introduction

MATLAB WORKSHOP

Matrix & Array Cont ...


Logical Subscripting & find function

%Logical Subscripting: remove the missing/outliers values from data


Remove the missing data
function : true for all finite numerical value & false for Nan and Inf.
x = x(isfinite(x))
Example :
x = [2.1 1.7 1.6 1.5 NaN 1.9 1.8 1.5 5.1 1.8 1.4 2.2 1.6 1.8];
% Remove Outliers
x = x(abs(x-mean(x)) <= 3*std(x)) ;
% find function
It determines the indices of array elements with a given logical condition.
Example
% locations of primes
k = find(isprime(A)) ;

Tutorial 1

Akhilesh Shende

Introduction

MATLAB WORKSHOP

Matrix & Array Cont ...


Complex Numbers and Some Matrix Generators (Functions)

% Matrix with complex numbers


c = [3+4i, 4+3j; -i, 10j] % Either use i or j
c = [3+4i, 4+3i; -i, 10i]

% Complex Number in Matrix


c = sqrt(-1);

Matrix Generators

Matrix

eye (n); eye(m,n);

n-by-n or m-by-n identity matrix

zeros(m,n);

an m-by-n matrix of zeros

ones(m,n);

an m-by-n matrix of ones

rand(m,n);

an m-by-n uniformly distributed random elements

randn(m,n);

an m-by-n normally distributed random elements

hilb

Hilbert matrix

invhilb

Inverse Hilbert matrix

magic

Magic square

pascal

Pascal matrix

toeplitz

Toeplitz matrix

Tutorial 1

Akhilesh Shende

Introduction

MATLAB WORKSHOP

2D and 3D Plots
% 2D Line Plots
2 dimensional data (x, y)
Function y = f(x)
plot (x, y)
Examples :
A. x = [1 2 3 4 5 6];
y = [3 -1 2 4 5 1];
B. x = 0:pi/100:2*pi
y = sin(x);
plot(x, y)A
plot(x, y)B
% Labeling of Plots
xlabel('x');
ylabel('sin(x)');
title('Plot of the Sine Function');
% To add plots in existing Figure
hold on

% 3D Plots
3 dimensional data (x, y, z)
Function z = f(x, y)
surf(x, y, z)
Examples :
[X,Y] = meshgrid(-2:.2:2);
Z = X .* exp(-X.^2 - Y.^2);
surf(X,Y,Z)
mesh(X,Y,Z)
% Subplots
x=0:.1:2*pi;
subplot(2,2,1); plot(x, sin(x));
subplot(2,2,2); plot(x, cos(x));
subplot(2,2,3); plot(x,exp(-x));
subplot(2,2,4); plot(x,x.^2);
% Print Graphics
print -djpeg subplot.jpg
Tutorial 1

Akhilesh Shende

Introduction

MATLAB WORKSHOP

Scripts and Functions


% Scripts
Series of commands like program
Execute by an order
Examples :
n = 50;
r = rand(n,1);
plot(r)
my_script.m
n = 50;
r = rand(n,1);
plot(r)

% m = mean(r);
% hold on
% plot([0,n],[m,m])
% hold off
% title('Mean of Random Uniform Data')

% Functions
Function y = f(x)
y = sin(x) or cos(x) trignometric
y = x^2 or sqrt(x)
Example function :
y = 1./((x - 0.3).^2 + .01) + 1./((x-0.9).^2 +
.04) - 6;
function y = my_fun (x)
y = = 1./((x - 0.3).^2 + .01) + 1./((x-0.9).^2 +
.04) - 6;
end
save the file by name my_fun.m
command line
>> a = my_fun(x)

Tutorial 1

HELP
Akhilesh Shende

You might also like