SlideShare a Scribd company logo
1 of 51
Download to read offline
An Introduction to MATLAB




Santosh K Venu                 1
What is MATLAB?


• MATLAB
  – MATrix LABoratory: 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.



                                                                      2
•   Stands for MATrix LABoratory
•   Interpreted language
•   Scientific programming environment
•   Very good tool for the manipulation of matrices
•   Great visualisation capabilities
•   Loads of built-in functions
•   Easy to learn and simple to use


                                                      3
MATLAB Overview



• Strengths of MATLAB
• Weaknesses of MATLAB




                         4
Strengths of MATLAB


• MATLAB is relatively easy to learn
• MATLAB code is optimized to be relatively quick
  when performing matrix operations
• MATLAB may behave like a calculator or as a
  programming language
• MATLAB is interpreted, errors are easier to fix




                                                5
Weaknesses of MATLAB


• MATLAB is NOT a general purpose programming
  language
• MATLAB is an interpreted language (making it for the
  most part slower than a compiled language such as C, C++)
• MATLAB is designed for scientific computation and is
  not suitable for some things (such as design an interface)




                                                           6
Matlab Desktop

• Command Window
   – type commands
• Workspace
   – view program variables
   – clear to clear
       • clear all: removes all variables, globals, functions and MEX
         links
       • clc: clear command window
   – double click on a variable to see it in the Array Editor
• Command History
   – view past commands
• Launch Pad
   – access help, tools, demos and documentation
                                                                        7
Matlab Desktop - con’t

                                 Launch Pad




                               Workspace


 Current
DIrectory
               Command
                Window                History




                                                8
How to Resume Default Desktop




                                9
Matlab Help


• Different ways to find information
   – help
   – help general, help mean, sqrt...
   – helpdesk - an html document with links to further
     information




                                                         10
Matlab Help - con’t




                      11
Matlab Help - con’t




                      12
Command window

•       The MATLAB environment is command oriented somewhat like
        UNIX. A prompt (>>) appears on the screen and a MATLAB
        statement can be entered. When the <ENTER> key is pressed, the
        statement is executed, and another prompt appears.
•       If a statement is terminated with a semicolon ( ; ), no results will be
        displayed. Otherwise results will appear before the next prompt.

    » a=5;
    » b=a/2

    b=

        2.5000

    »
                                                                                  13
MATLAB Special Variables


ans       Default variable name for results
pi        Value of 
inf       Infinity
NaN       Not a number          e.g. 0/0
i and j   i = j = square root of minus one: (-1) (imaginary number)
          e.g. sqrt(-1)       ans= 0 + 1.0000i
realmin   The smallest usable positive real number
realmax   The largest usable positive real number




                                                               14
Variables
• No need for types. i.e.,

        int a;
        double b;
        float c;
• All variables are created with double precision unless
  specified and they are matrices.
        Example:
        >>x=5;
        >>x1=2;
• After these statements, the variables are 1x1 matrices with
  double precision
Working with Matrices and Arrays


• Since Matlab makes extensive use of matrices, the best
  way for you to get started with MATLAB is to learn how
  to handle matrices.
   – Separate the elements of a row with blanks or commas.
   – Use a semicolon ; to indicate the end of each row.
   – Surround the entire list of elements with square brackets, [ ].


       A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
MATLAB displays the matrix you just entered:
  A=
       16   3    2     13
       5    10   11    8
       9    6     7    12
       4    15    14   1

• Once you have entered the matrix, it is automatically
  remembered in the MATLAB workspace. You can
  simply refer to it as A.

• Keep in mind, variable names are case-sensitive
Manipulating Matrices
                                                   A=
                                                   16 3   2     13
                                                   5 10   11    8
• Access elements of a matrix                      9 6     7    12
>>A(1,2)                                           4 15    14   1

ans=
3                indices of matrix element(s)
• Remember Matrix(row,column)
• Naming convention Matrix variables start with a capital
  letter while vectors or scalar variables start with a simple
  letter



                               18
MATLAB Relational Operators

• MATLAB supports six relational operators.

  Less Than                   <
  Less Than or Equal          <=
  Greater Than                >
  Greater Than or Equal       >=
  Equal To                    ==
  Not Equal To                ~=




                                              19
MATLAB Logical Operators



• MATLAB supports three logical operators.

  not          ~      % highest precedence
  and          &      % equal precedence with or
  or           |      % equal precedence with and




                                                    20
MATLAB Matrices


• 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 (1,1)are matrices with only one row AND one
  column


                                                              21
MATLAB Matrices


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

» a=23

a=

  23


                                                           22
MATLAB Matrices


• 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] or rowvec = [12 14 63]

rowvec =

  12   14   63

                                                         23
MATLAB Matrices

• 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


                                                          24
MATLAB Matrices

• 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

                                                           25
Extracting a Sub-Matrix

• A portion of a matrix can be extracted and stored in a smaller
  matrix by specifying the names of both matrices, the rows and
  columns. The syntax is:

      sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ;

  where r1 and r2 specify the beginning and ending rows and c1
  and c2 specify the beginning and ending columns to be
  extracted to make the new matrix.



                                                             26
MATLAB Matrices

•   A column vector can be        •   Here we extract column 2 of
    extracted from a matrix. As       the matrix and make a
    an example we create a            column vector:
    matrix below:

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

matrix =                          col_two =
  1 2      3                         2
  4 5      6                         5
  7 8      9                         8



                                                                    27
MATLAB Matrices

•   A row vector can be extracted   •   Here we extract row 2 of the
    from a matrix. As an example        matrix and make a row vector.
    we create a matrix below:           Note that the 2:2 specifies the
                                        second row and the 1:3
» matrix=[1,2,3;4,5,6;7,8,9]            specifies which columns of the
                                        row.
matrix =
                                    » rowvec=matrix(2 : 2 , 1 : 3)
    1   2   3
    4   5   6                       rowvec =
    7   8   9

                                        4   5   6


                                                                          28
Matrices transpose

•   a vector        x = [1 2 5 1]

    x =
          1     2    5   1




•   transpose       y = x’          y =
                                          1
                                          2
                                          5
                                          1
                                              29
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


                                                 30
Scalar - Matrix Subtraction

» a=3;
» b=[1, 2, 3;4, 5, 6]
b=
    1 2 3
    4 5 6
» c = b - a %Subtract a from each element of b
c=
   -2 -1 0
    1 2 3

                                                 31
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
                                                32
Scalar - Matrix Division


» a=3;
» b=[1, 2, 3; 4, 5, 6]
b=
    1 2 3
    4 5 6
» c = b / a % Divide each element of b by a
c=
   0.3333 0.6667 1.0000
   1.3333 1.6667 2.0000
                                              33
Math & Assignment Operators

Power             ^    or .^   a^b   or     a.^b
Multiplication    *    or .*   a*b   or     a.*b
Division          /    or ./   a/b   or     a./b




 - (unary) + (unary)
 Addition      +           a+b
 Subtraction -             a-b
 Assignment =                    a=b      (assign b to a)
                                                            34
Other operators


[ ] concatenation           x = [ zeros(1,3) ones(1,2) ]
                            x =
                                 0 0 0 1 1


( ) subscription            x = [ 1 3 5 7 9]
                            x =
                                 1 3 5 7 9

                            y = x(2)
                            y =
                                 3
                            y = x(2:4)
                            y =
                                 3 5 7                35
The : operator


• VERY important operator in Matlab
• Means ‘to’
>> 1:10
ans =
   1 2 3 4 5 6 7 8 9 10
>> 1:2:10
                                    Try the following
ans =                               >> x=0:pi/12:2*pi;
                                    >> y=sin(x)
   1 3 5 7 9
Introduction to Matlab           36
Sumitha Balasuriya
•   Length
•   Max
•   Mean
•   Median
•   Min
•   Prod
•   Size
•   Var
•   Sum
•   Det
•   Rank
•   Eig
•   sort/flipr   37
Matlab Graphics


x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
xlabel('x = 0:2pi')
ylabel('Sine of x')
title('Plot of the
  Sine Function')




                                38
Multiple Graphs


t = 0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2);
plot(t,y1,t,y2)
grid on




                                 39
• Plotting Multiple Data Sets in One Graph
   – Multiple x-y pair arguments create multiple graphs with a
     single call to plot.
  For example:         x = 0:pi/100:2*pi;
                     y = sin(x);
                     y2 = sin(x-.25);
                     y3 = sin(x-.5);
                     plot(x,y,x,y2,x,y3)
Multiple Plots


t = 0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2);
subplot(2,2,1)
plot(t,y1)
subplot(2,2,2)
plot(t,y2)




                                 41
Graph Functions (summary)


•   plot      linear plot
•   stem      discrete plot
•   grid      add grid lines
•   xlabel    add X-axis label
•   ylabel    add Y-axis label
•   title     add graph title
•   subplot   divide figure window
•   figure    create new figure window
•   pause     wait for user response

                                         42
Some Useful MATLAB commands


•   who         List known variables
•   whos        List known variables plus their size
•   help        >> help sqrt    Help on using sqrt
•   lookfor     >> lookfor sqrt
                Search for keyword sqrt in on MATLABPATH.
•   what        >> what ('directory')
                List MATLAB files in directory
•   clear       Clear all variables from work space
•   clear x y   Clear variables x and y from work space
•   clc         Clear the command window

                                                          43
Flow Control

•   if
•   for
•   while
•   break
•   ….
Control Structures
                          Some Dummy Examples
• If Statement Syntax
                          if ((a>3) & (b==5))
                               Some Matlab Commands;
if (Condition_1)          end
        Matlab Commands   if (a<3)
elseif (Condition_2)           Some Matlab Commands;
                          elseif (b~=5)
        Matlab Commands        Some Matlab Commands;
elseif (Condition_3)      end
        Matlab Commands
                          if (a<3)
else                           Some Matlab Commands;
        Matlab Commands   else
                               Some Matlab Commands;
end                       end
Control Structures
                       Some Dummy Examples

                       for i=1:100
• For loop syntax      end
                           Some Matlab Commands;


                       for j=1:3:200
for i=Index_Array          Some Matlab Commands;
   Matlab Commands     end

end                    for m=13:-0.2:-21
                           Some Matlab Commands;
                       end

                       for k=[0.1 0.3 -13 12 7 -9.3]
                           Some Matlab Commands;
                       end
Control Structures


• While Loop Syntax
                       Dummy Example
while (condition)
                       while ((a>3) & (b==5))
  Matlab Commands
                          Some Matlab Commands;
end                    end
Classification of flow
%|-------------------------------------|
This function classifies a flow |
according to the values of the Reynolds (Re) and Mach (Ma). |
Re <= 2000, laminar flow
2000 < Re <= 5000, transitional flow
Re > 5000, turbulent flow
Ma < 1, sub-sonic flow
Ma = 1, sonic flow
Ma > 1, super-sonic flow
%|-------------------------------------|




                                                                48
Vector Function


Consider now a vector function f(x) = [f1(x1,x2,x3) f2(x1,x2,x3)
f3(x1,x2,x3)]T, where x =
[x1,x2,x3]T (The symbol []T indicates the transpose of a matrix).
Specifically,
f1(x1,x2,x3) = x1 cos(x2) + x2 cos(x1) + x3
f2(x1,x2,x3) = x1x2 + x2x3 + x3x1
f3(x1,x2,x3) = x1
2 + 2x1x2x3 + x3
2
A function to evaluate the vector function f(x) is shown below.
                                                                49
Summation

%Check if m or n are matrices
if length(n)>1 | length(m)>1 then
error('sum2 - n,m must be scalar values')
abort
end
%Calculate summation if n and m are scalars
S = 0; %initialize sum
for i = 1:n                %sweep by index i
for j = 1:m                %sweep by index j
S = S + 1/((i+j)^2+1);
end
end
                                                   50
Thank U

          51

More Related Content

What's hot

Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLABAshish Meshram
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLABRavikiran A
 
Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingMatlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingDr. Manjunatha. P
 
Matlab practical and lab session
Matlab practical and lab sessionMatlab practical and lab session
Matlab practical and lab sessionDr. Krishna Mohbey
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabTarun Gehlot
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersMurshida ck
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMohd Esa
 
Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4Randa Elanwar
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introductionAmeen San
 
Matlab (Presentation on MATLAB)
Matlab (Presentation on MATLAB)Matlab (Presentation on MATLAB)
Matlab (Presentation on MATLAB)Chetan Allapur
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to MatlabAmr Rashed
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlabaman gupta
 

What's hot (20)

Seminar on MATLAB
Seminar on MATLABSeminar on MATLAB
Seminar on MATLAB
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Matlab Basic Tutorial
Matlab Basic TutorialMatlab Basic Tutorial
Matlab Basic Tutorial
 
Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingMatlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processing
 
Matlab practical and lab session
Matlab practical and lab sessionMatlab practical and lab session
Matlab practical and lab session
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
Matlab
MatlabMatlab
Matlab
 
Learn Matlab
Learn MatlabLearn Matlab
Learn Matlab
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
 
Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
Matlab basic and image
Matlab basic and imageMatlab basic and image
Matlab basic and image
 
Matlab (Presentation on MATLAB)
Matlab (Presentation on MATLAB)Matlab (Presentation on MATLAB)
Matlab (Presentation on MATLAB)
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Fundamentals of matlab
Fundamentals of matlabFundamentals of matlab
Fundamentals of matlab
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlab
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlab
 

Viewers also liked

Matlab practice
Matlab practiceMatlab practice
Matlab practiceZunAib Ali
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabMohan Raj
 
Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Randa Elanwar
 
Introduction to matlab_application_to_electrical_engineering
Introduction to matlab_application_to_electrical_engineeringIntroduction to matlab_application_to_electrical_engineering
Introduction to matlab_application_to_electrical_engineeringzulfikar37
 
Simulink - Introduction with Practical Example
Simulink - Introduction with Practical ExampleSimulink - Introduction with Practical Example
Simulink - Introduction with Practical ExampleKamran Gillani
 

Viewers also liked (6)

Matlab practice
Matlab practiceMatlab practice
Matlab practice
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4
 
Introduction to matlab_application_to_electrical_engineering
Introduction to matlab_application_to_electrical_engineeringIntroduction to matlab_application_to_electrical_engineering
Introduction to matlab_application_to_electrical_engineering
 
Simulink - Introduction with Practical Example
Simulink - Introduction with Practical ExampleSimulink - Introduction with Practical Example
Simulink - Introduction with Practical Example
 

Similar to Introduction to matlab

MATLAB_CIS601-03.ppt
MATLAB_CIS601-03.pptMATLAB_CIS601-03.ppt
MATLAB_CIS601-03.pptaboma2hawi
 
Lines and planes in space
Lines and planes in spaceLines and planes in space
Lines and planes in spaceFaizan Shabbir
 
INTRODUCTION TO MATLAB for PG students.ppt
INTRODUCTION TO MATLAB for PG students.pptINTRODUCTION TO MATLAB for PG students.ppt
INTRODUCTION TO MATLAB for PG students.pptKarthik537368
 
MATLAB workshop lecture 1MATLAB work.ppt
MATLAB workshop lecture 1MATLAB work.pptMATLAB workshop lecture 1MATLAB work.ppt
MATLAB workshop lecture 1MATLAB work.pptssuserdee4d8
 
From zero to MATLAB hero: Mastering the basics and beyond
From zero to MATLAB hero: Mastering the basics and beyondFrom zero to MATLAB hero: Mastering the basics and beyond
From zero to MATLAB hero: Mastering the basics and beyondMahuaPal6
 
TRAINING PROGRAMME ON MATLAB ASSOCIATE EXAM (1).pptx
TRAINING PROGRAMME ON MATLAB  ASSOCIATE EXAM (1).pptxTRAINING PROGRAMME ON MATLAB  ASSOCIATE EXAM (1).pptx
TRAINING PROGRAMME ON MATLAB ASSOCIATE EXAM (1).pptxanaveenkumar4
 
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxMATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxprashantkumarchinama
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functionsjoellivz
 

Similar to Introduction to matlab (20)

Introduction to Matlab.ppt
Introduction to Matlab.pptIntroduction to Matlab.ppt
Introduction to Matlab.ppt
 
Matlab
MatlabMatlab
Matlab
 
Matlab pt1
Matlab pt1Matlab pt1
Matlab pt1
 
MatlabIntro (1).ppt
MatlabIntro (1).pptMatlabIntro (1).ppt
MatlabIntro (1).ppt
 
MATLAB_CIS601-03.ppt
MATLAB_CIS601-03.pptMATLAB_CIS601-03.ppt
MATLAB_CIS601-03.ppt
 
Lines and planes in space
Lines and planes in spaceLines and planes in space
Lines and planes in space
 
INTRODUCTION TO MATLAB for PG students.ppt
INTRODUCTION TO MATLAB for PG students.pptINTRODUCTION TO MATLAB for PG students.ppt
INTRODUCTION TO MATLAB for PG students.ppt
 
Matlab
MatlabMatlab
Matlab
 
Matlab Tutorial.ppt
Matlab Tutorial.pptMatlab Tutorial.ppt
Matlab Tutorial.ppt
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
MATLAB workshop lecture 1MATLAB work.ppt
MATLAB workshop lecture 1MATLAB work.pptMATLAB workshop lecture 1MATLAB work.ppt
MATLAB workshop lecture 1MATLAB work.ppt
 
From zero to MATLAB hero: Mastering the basics and beyond
From zero to MATLAB hero: Mastering the basics and beyondFrom zero to MATLAB hero: Mastering the basics and beyond
From zero to MATLAB hero: Mastering the basics and beyond
 
EE6711 Power System Simulation Lab manual
EE6711 Power System Simulation Lab manualEE6711 Power System Simulation Lab manual
EE6711 Power System Simulation Lab manual
 
TRAINING PROGRAMME ON MATLAB ASSOCIATE EXAM (1).pptx
TRAINING PROGRAMME ON MATLAB  ASSOCIATE EXAM (1).pptxTRAINING PROGRAMME ON MATLAB  ASSOCIATE EXAM (1).pptx
TRAINING PROGRAMME ON MATLAB ASSOCIATE EXAM (1).pptx
 
Matlab tut2
Matlab tut2Matlab tut2
Matlab tut2
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Matlab anilkumar
Matlab  anilkumarMatlab  anilkumar
Matlab anilkumar
 
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxMATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
 
Matlab summary
Matlab summaryMatlab summary
Matlab summary
 

Recently uploaded

How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 

Recently uploaded (20)

How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 

Introduction to matlab

  • 1. An Introduction to MATLAB Santosh K Venu 1
  • 2. What is MATLAB? • MATLAB – MATrix LABoratory: 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. 2
  • 3. Stands for MATrix LABoratory • Interpreted language • Scientific programming environment • Very good tool for the manipulation of matrices • Great visualisation capabilities • Loads of built-in functions • Easy to learn and simple to use 3
  • 4. MATLAB Overview • Strengths of MATLAB • Weaknesses of MATLAB 4
  • 5. Strengths of MATLAB • MATLAB is relatively easy to learn • MATLAB code is optimized to be relatively quick when performing matrix operations • MATLAB may behave like a calculator or as a programming language • MATLAB is interpreted, errors are easier to fix 5
  • 6. Weaknesses of MATLAB • MATLAB is NOT a general purpose programming language • MATLAB is an interpreted language (making it for the most part slower than a compiled language such as C, C++) • MATLAB is designed for scientific computation and is not suitable for some things (such as design an interface) 6
  • 7. Matlab Desktop • Command Window – type commands • Workspace – view program variables – clear to clear • clear all: removes all variables, globals, functions and MEX links • clc: clear command window – double click on a variable to see it in the Array Editor • Command History – view past commands • Launch Pad – access help, tools, demos and documentation 7
  • 8. Matlab Desktop - con’t Launch Pad Workspace Current DIrectory Command Window History 8
  • 9. How to Resume Default Desktop 9
  • 10. Matlab Help • Different ways to find information – help – help general, help mean, sqrt... – helpdesk - an html document with links to further information 10
  • 11. Matlab Help - con’t 11
  • 12. Matlab Help - con’t 12
  • 13. Command window • The MATLAB environment is command oriented somewhat like UNIX. A prompt (>>) appears on the screen and a MATLAB statement can be entered. When the <ENTER> key is pressed, the statement is executed, and another prompt appears. • If a statement is terminated with a semicolon ( ; ), no results will be displayed. Otherwise results will appear before the next prompt. » a=5; » b=a/2 b= 2.5000 » 13
  • 14. MATLAB Special Variables ans Default variable name for results pi Value of  inf Infinity NaN Not a number e.g. 0/0 i and j i = j = square root of minus one: (-1) (imaginary number) e.g. sqrt(-1) ans= 0 + 1.0000i realmin The smallest usable positive real number realmax The largest usable positive real number 14
  • 15. Variables • No need for types. i.e., int a; double b; float c; • All variables are created with double precision unless specified and they are matrices. Example: >>x=5; >>x1=2; • After these statements, the variables are 1x1 matrices with double precision
  • 16. Working with Matrices and Arrays • Since Matlab makes extensive use of matrices, the best way for you to get started with MATLAB is to learn how to handle matrices. – Separate the elements of a row with blanks or commas. – Use a semicolon ; to indicate the end of each row. – Surround the entire list of elements with square brackets, [ ]. A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
  • 17. MATLAB displays the matrix you just entered: A= 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1 • Once you have entered the matrix, it is automatically remembered in the MATLAB workspace. You can simply refer to it as A. • Keep in mind, variable names are case-sensitive
  • 18. Manipulating Matrices A= 16 3 2 13 5 10 11 8 • Access elements of a matrix 9 6 7 12 >>A(1,2) 4 15 14 1 ans= 3 indices of matrix element(s) • Remember Matrix(row,column) • Naming convention Matrix variables start with a capital letter while vectors or scalar variables start with a simple letter 18
  • 19. MATLAB Relational Operators • MATLAB supports six relational operators. Less Than < Less Than or Equal <= Greater Than > Greater Than or Equal >= Equal To == Not Equal To ~= 19
  • 20. MATLAB Logical Operators • MATLAB supports three logical operators. not ~ % highest precedence and & % equal precedence with or or | % equal precedence with and 20
  • 21. MATLAB Matrices • 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 (1,1)are matrices with only one row AND one column 21
  • 22. MATLAB Matrices • A matrix with only one row AND one column is a scalar. A scalar can be created in MATLAB as follows: » a=23 a= 23 22
  • 23. MATLAB Matrices • 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] or rowvec = [12 14 63] rowvec = 12 14 63 23
  • 24. MATLAB Matrices • 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 24
  • 25. MATLAB Matrices • 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 25
  • 26. Extracting a Sub-Matrix • A portion of a matrix can be extracted and stored in a smaller matrix by specifying the names of both matrices, the rows and columns. The syntax is: sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ; where r1 and r2 specify the beginning and ending rows and c1 and c2 specify the beginning and ending columns to be extracted to make the new matrix. 26
  • 27. MATLAB Matrices • A column vector can be • Here we extract column 2 of extracted from a matrix. As the matrix and make a an example we create a column vector: matrix below: » matrix=[1,2,3;4,5,6;7,8,9] » col_two=matrix( : , 2) matrix = col_two = 1 2 3 2 4 5 6 5 7 8 9 8 27
  • 28. MATLAB Matrices • A row vector can be extracted • Here we extract row 2 of the from a matrix. As an example matrix and make a row vector. we create a matrix below: Note that the 2:2 specifies the second row and the 1:3 » matrix=[1,2,3;4,5,6;7,8,9] specifies which columns of the row. matrix = » rowvec=matrix(2 : 2 , 1 : 3) 1 2 3 4 5 6 rowvec = 7 8 9 4 5 6 28
  • 29. Matrices transpose • a vector x = [1 2 5 1] x = 1 2 5 1 • transpose y = x’ y = 1 2 5 1 29
  • 30. 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 30
  • 31. Scalar - Matrix Subtraction » a=3; » b=[1, 2, 3;4, 5, 6] b= 1 2 3 4 5 6 » c = b - a %Subtract a from each element of b c= -2 -1 0 1 2 3 31
  • 32. 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 32
  • 33. Scalar - Matrix Division » a=3; » b=[1, 2, 3; 4, 5, 6] b= 1 2 3 4 5 6 » c = b / a % Divide each element of b by a c= 0.3333 0.6667 1.0000 1.3333 1.6667 2.0000 33
  • 34. Math & Assignment Operators Power ^ or .^ a^b or a.^b Multiplication * or .* a*b or a.*b Division / or ./ a/b or a./b - (unary) + (unary) Addition + a+b Subtraction - a-b Assignment = a=b (assign b to a) 34
  • 35. Other operators [ ] concatenation x = [ zeros(1,3) ones(1,2) ] x = 0 0 0 1 1 ( ) subscription x = [ 1 3 5 7 9] x = 1 3 5 7 9 y = x(2) y = 3 y = x(2:4) y = 3 5 7 35
  • 36. The : operator • VERY important operator in Matlab • Means ‘to’ >> 1:10 ans = 1 2 3 4 5 6 7 8 9 10 >> 1:2:10 Try the following ans = >> x=0:pi/12:2*pi; >> y=sin(x) 1 3 5 7 9 Introduction to Matlab 36 Sumitha Balasuriya
  • 37. Length • Max • Mean • Median • Min • Prod • Size • Var • Sum • Det • Rank • Eig • sort/flipr 37
  • 38. Matlab Graphics x = 0:pi/100:2*pi; y = sin(x); plot(x,y) xlabel('x = 0:2pi') ylabel('Sine of x') title('Plot of the Sine Function') 38
  • 39. Multiple Graphs t = 0:pi/100:2*pi; y1=sin(t); y2=sin(t+pi/2); plot(t,y1,t,y2) grid on 39
  • 40. • Plotting Multiple Data Sets in One Graph – Multiple x-y pair arguments create multiple graphs with a single call to plot. For example: x = 0:pi/100:2*pi; y = sin(x); y2 = sin(x-.25); y3 = sin(x-.5); plot(x,y,x,y2,x,y3)
  • 41. Multiple Plots t = 0:pi/100:2*pi; y1=sin(t); y2=sin(t+pi/2); subplot(2,2,1) plot(t,y1) subplot(2,2,2) plot(t,y2) 41
  • 42. Graph Functions (summary) • plot linear plot • stem discrete plot • grid add grid lines • xlabel add X-axis label • ylabel add Y-axis label • title add graph title • subplot divide figure window • figure create new figure window • pause wait for user response 42
  • 43. Some Useful MATLAB commands • who List known variables • whos List known variables plus their size • help >> help sqrt Help on using sqrt • lookfor >> lookfor sqrt Search for keyword sqrt in on MATLABPATH. • what >> what ('directory') List MATLAB files in directory • clear Clear all variables from work space • clear x y Clear variables x and y from work space • clc Clear the command window 43
  • 44. Flow Control • if • for • while • break • ….
  • 45. Control Structures Some Dummy Examples • If Statement Syntax if ((a>3) & (b==5)) Some Matlab Commands; if (Condition_1) end Matlab Commands if (a<3) elseif (Condition_2) Some Matlab Commands; elseif (b~=5) Matlab Commands Some Matlab Commands; elseif (Condition_3) end Matlab Commands if (a<3) else Some Matlab Commands; Matlab Commands else Some Matlab Commands; end end
  • 46. Control Structures Some Dummy Examples for i=1:100 • For loop syntax end Some Matlab Commands; for j=1:3:200 for i=Index_Array Some Matlab Commands; Matlab Commands end end for m=13:-0.2:-21 Some Matlab Commands; end for k=[0.1 0.3 -13 12 7 -9.3] Some Matlab Commands; end
  • 47. Control Structures • While Loop Syntax Dummy Example while (condition) while ((a>3) & (b==5)) Matlab Commands Some Matlab Commands; end end
  • 48. Classification of flow %|-------------------------------------| This function classifies a flow | according to the values of the Reynolds (Re) and Mach (Ma). | Re <= 2000, laminar flow 2000 < Re <= 5000, transitional flow Re > 5000, turbulent flow Ma < 1, sub-sonic flow Ma = 1, sonic flow Ma > 1, super-sonic flow %|-------------------------------------| 48
  • 49. Vector Function Consider now a vector function f(x) = [f1(x1,x2,x3) f2(x1,x2,x3) f3(x1,x2,x3)]T, where x = [x1,x2,x3]T (The symbol []T indicates the transpose of a matrix). Specifically, f1(x1,x2,x3) = x1 cos(x2) + x2 cos(x1) + x3 f2(x1,x2,x3) = x1x2 + x2x3 + x3x1 f3(x1,x2,x3) = x1 2 + 2x1x2x3 + x3 2 A function to evaluate the vector function f(x) is shown below. 49
  • 50. Summation %Check if m or n are matrices if length(n)>1 | length(m)>1 then error('sum2 - n,m must be scalar values') abort end %Calculate summation if n and m are scalars S = 0; %initialize sum for i = 1:n %sweep by index i for j = 1:m %sweep by index j S = S + 1/((i+j)^2+1); end end 50
  • 51. Thank U 51