Top Banner
Introduction to MATLAB Md. Menhazul Abedin Lecturer, Statistics Discipline Khulna university [email protected]
35

Introduction to matlab

Apr 15, 2017

Download

Education

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to matlab

Introduction to MATLAB

Md. Menhazul AbedinLecturer, Statistics Discipline

Khulna [email protected]

Page 2: Introduction to matlab

What is MATLAB

• High level language for technical computing• Stands for MATrix LABoratory

• Everything is a matrix - easy to do linear

algebra

Page 3: Introduction to matlab

MATLAB DesktopMenu and toolbar

CommandHistory

Workspace

Page 4: Introduction to matlab

Valid variables Names

• A valid variable name starts with a letter, followed by letters, digits, or underscores.

• MATLAB® is casesensitive, so A and a are not the same variable.

• The maximum length of a variable name is the value that the namelengthmax command returns.

• You cannot define variables with the same names as MATLAB keywords, such as if or end. For a complete list, run the iskeyword command.

Page 5: Introduction to matlab

Matlab keywords….

• Keywords are must not variable name• 'break' 'case' 'catch' 'classdef' 'continue' 'else'

'elseif' 'end' 'for' 'function' 'global' 'if' 'otherwise' 'parfor' 'persistent' 'return' 'spmd' 'switch' 'try'

'while'

Page 6: Introduction to matlab

How to run?

• If program in command space press “ enter” • If in open script then block command and

presss F9.

Page 7: Introduction to matlab

Matrices & Vectors

• All (almost) entities in MATLAB are matrices• Easy to define:

• Use ‘,’ or ‘ ’ to separate row elements• use ‘;’ to separate rows

>> A = [1 3; 5 8] A = 1 3 5 8

>> A = [1,3; 5,8] A = 1 3 5 8

Page 8: Introduction to matlab

Matrices & Vectors

• Order of Matrix - – m=no. of rows,– n=no. of columns

• Vectors - special cases – n = 1 column vector– m = 1 row vector

mn

Page 9: Introduction to matlab

Simple algebraic operations

Addition +Subtraction -Multiplication *Division /Power ^Logarithm log() Exponential Exp() Square root sqrt()Absolute value abs()

Page 10: Introduction to matlab

Element wise operations…

.*element-by-element multiplication

./element-by-element division

.^ element-by-element power

Page 11: Introduction to matlab

Operators (relational, logical)

• == Equal to• ~= Not equal to• < Strictly smaller• > Strictly greater• <= Smaller than or equal to• >= Greater than equal to• & And operator• | Or operator

Page 12: Introduction to matlab

Sequences……. • t =1:10

t = 1 2 3 4 5 6 7 8 9 10• k =2:-0.5:-1

k = 2 1.5 1 0.5 0 -0.5 -1 • y=[2:2:12] y= 2 4 6 8 10 12

• B = [1:4; 5:8]

x = 1 2 3 4 5 6 7 8

Page 13: Introduction to matlab

Some practice…….

• Do some mathematical operations as like as calculator (addition, subtraction, multiplication, division, log, square root, power and so on)

Page 14: Introduction to matlab

Matrix...• a vector x = [1 2 5 1]

x = 1 2 5 1

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

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

Page 15: Introduction to matlab

Special matrix….

• Identity matrix= eye (row,column)• One matrix= ones(row,column)• Zero marix= zeros (row,column)

Page 16: Introduction to matlab

Matrix operations…• Let A be a matrix….• Determinant= det(A)• Transpose= A’• Rank = rank(A)• Inverse= inv(A)• Trace = trace(A)• Eigen value=eig(A)• Eigen value and vector= [V,D] = eig(A)• Matrix multiplication= A*B• Matrix multiplication= A.*B [elementwise]• Singular values= svd(A)

Page 17: Introduction to matlab

Cont…• diag diagonal atrix• triu upper triangular matrix• tril lower triangular matri• rand randomly generated matrix• C=rand(5,4) • triu(C)• tril(C)• diag([0.9092;0.5163;0.2661])

Page 18: Introduction to matlab

Cont…

• Length of a vector= length(x)• Size or dimension of matrix= size(A)• Replication of same number=

repmat(number,m,n)

Page 19: Introduction to matlab

Basic statistical building function… • Let be a sample • Mean= mean(x)• Median=median(x)• Mode= mode(x)• Variance=var(x)• Standard deviation=std(x)• Minimum= min(x)• Maximum= max(x)• Summation=sum(x)

nxxxx ,...,, 21

Page 20: Introduction to matlab

Data preprocessing…(Missing value)• a = magic(3); a(2,2) = NaN• a = 8 1 6 3 NaN 7 4 9 2sum(a) ans = 15 NaN 15

sum(a) ans = 15 NaN 15

sum(a,'omitnan') ans = 15 10 15

Class of variables=class(A)

Page 21: Introduction to matlab

Random number generation….

• Normal distribution: normrnd(mu,sigma) for a single valuenormrnd(mu,sigma,m,n) ornormrnd(mu,sigma[m,n]) for matrix having m

row and ncolumn• Poisson distribution:poissrnd(lamda)poissrnd(lamda,m,n)poissrnd(lamda,[m,n])

Binomial distribution:binornd(N,P)binornd(N,P,m,n)binornd(N,P,[m,n])

Page 22: Introduction to matlab

Cont…

• Geometric distribution:geornd(p)geornd(p,m,n)geornd(p,[m,n])• Beta distribution:betarnd(A,B)betarnd(A,B,m,n)betarnd(A,B,[m,n])

Exponential distribution:exprnd(mu)exprnd(mu,m,n)exprnd(mu,[m,n])Gamma distribution:gamrd(A,B)gamrnd(A,B,m,n)gamrnd(A,B,[m,n])

Page 23: Introduction to matlab

Cont…

• Standard normal distribution:randn return single valuerandn(n) return n-by-n square matrixrandn(r,c) return r-by-c matrx

Page 24: Introduction to matlab

Covariance of Matrix

• C = cov(A,B) returns the covariance between two random

variables A and B.• C = cov(A) returns the matrix of correlation

coefficient for A, where the columns of A represent random variables and the rows represent observations.

Page 25: Introduction to matlab

Example…

• A = [5 0 3 7; 1 -5 7 3; 4 9 8 10]; C = cov(A)

• A = [3 6 4]; B = [7 12 -9]; cov(A,B)

Page 26: Introduction to matlab

Correlation coefficient…

• R = corrcoef(A) returns the matrix of correlation

coefficient for A, where the columns of A represent random variables and the rows represent observations.

• R= corrcoef(A,B) returns coefficients between two random

variables A and B.

Page 27: Introduction to matlab

Example…..

• x = randn(6,1); • y = randn(6,1); • A = [x y 2*y+3]; • R = corrcoef(A)

• A = randn(10,1);• B = randn(10,1);• R = corrcoef(A,B)

Page 28: Introduction to matlab

Ploting…• x vector• y vector• Z vector plot(x,y) 2D line diagram plot(x,y,z) 3D line diagram scatter(x,y) scatter diagram scatter(x,y,z) 3D scatter diagram bar(x) bar diagram bar3(x) 3D bar diagram hist(x) histogram boxplot(x) boxplot

• xlabel(‘text')• ylabel(‘text')

scatter(x,y,'d')scatter(x,y,'*')scatter(x,y,'b')scatter(x,y,'p')scatter(x,y,’+')scatter(x,y,‘x')

theta = linspace(0,1,500);x = exp(theta).*sin(100*theta);y = exp(theta).*cos(100*theta);s = scatter(x,y)

You may change color like scatter(x,y,’x’,’r’)

Page 29: Introduction to matlab

Regression…

load moorey=moore(:,6)x1=ones(length(y),1)x2=moore(:,1:5)x=[x1 x2]% now perform regression %[b,bint,r,rint,stats]=regress(y,x)

Page 30: Introduction to matlab

Test…• %Single mean test load stockreturns x = stocks(:,3) length(x) [h,p,ci,stats] = ttest(x) load stockreturns x = stocks(:,3); h = ttest(x,0,0.01) • %paired mean test load examgrades x = grades(:,1); y = grades(:,2); [h,p] = ttest(x,y)

Page 31: Introduction to matlab

Cont…%paired mean testload examgradesx = grades(:,1);y = grades(:,2);[h,p] = ttest2(x,y) load examgradesx = grades(:,1);y = grades(:,2);[h,p] = ttest(x,y,0.01) Similar way F-test , z-test, chi-square test etc

%t-Test for a Hypothesized Meanload examgradesx = grades(:,1);h = ttest(x,75) %One-Sided t-Testload examgradesx = grades(:,1);h = ttest(x,65,'right')

Page 32: Introduction to matlab

ANOVA…

% ANOVA one way y = meshgrid(1:5);y = y + normrnd(0,1,5,5)p = anova1(y)%ANOVA two way........load popcornpopcorn[p,tbl] = anova2(popcorn,3);

Page 33: Introduction to matlab

Importing data…

• Excel data• Watch the vedio• File import data fix direcory

open ( tick generate matlab code) next

finish

https://www.youtube.com/watch?v=-mZci3mNjlU

Page 34: Introduction to matlab

• clc clear commad window• whos data size, class, bytes etc • who provide variable names• help mean• help who• help median• help regression• help plot

Page 35: Introduction to matlab

Thanks