SlideShare una empresa de Scribd logo
1 de 51
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Basics of matlab
Basics of matlabBasics of matlab
Basics of matlabAnil Maurya
 
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 matlabBilawalBaloch1
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMohd Esa
 
Matlab Overviiew
Matlab OverviiewMatlab Overviiew
Matlab OverviiewNazim Naeem
 
Matlab Introduction
Matlab IntroductionMatlab Introduction
Matlab IntroductionDaniel Moore
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLABRavikiran A
 
Matlab day 1: Introduction to MATLAB
Matlab day 1: Introduction to MATLABMatlab day 1: Introduction to MATLAB
Matlab day 1: Introduction to MATLABreddyprasad reddyvari
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to MatlabAmr Rashed
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlabaman gupta
 
Matlab matrices and arrays
Matlab matrices and arraysMatlab matrices and arrays
Matlab matrices and arraysAmeen San
 
Importance of matlab
Importance of matlabImportance of matlab
Importance of matlabkrajeshk1980
 
Basic matlab and matrix
Basic matlab and matrixBasic matlab and matrix
Basic matlab and matrixSaidur Rahman
 
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 introduction
Matlab introductionMatlab introduction
Matlab introductionAmeen San
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersMurshida ck
 

La actualidad más candente (20)

MATLAB INTRODUCTION
MATLAB INTRODUCTIONMATLAB INTRODUCTION
MATLAB INTRODUCTION
 
Basics of matlab
Basics of matlabBasics of matlab
Basics of matlab
 
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
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
 
Matlab Overviiew
Matlab OverviiewMatlab Overviiew
Matlab Overviiew
 
Matlab Introduction
Matlab IntroductionMatlab Introduction
Matlab Introduction
 
Matlab Basic Tutorial
Matlab Basic TutorialMatlab Basic Tutorial
Matlab Basic Tutorial
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Matlab basic and image
Matlab basic and imageMatlab basic and image
Matlab basic and image
 
Matlab day 1: Introduction to MATLAB
Matlab day 1: Introduction to MATLABMatlab day 1: Introduction to MATLAB
Matlab day 1: Introduction to MATLAB
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlab
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlab
 
Matlab matrices and arrays
Matlab matrices and arraysMatlab matrices and arrays
Matlab matrices and arrays
 
Importance of matlab
Importance of matlabImportance of matlab
Importance of matlab
 
Basic matlab and matrix
Basic matlab and matrixBasic matlab and matrix
Basic matlab and matrix
 
Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingMatlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processing
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
 
MATLAB Programming
MATLAB Programming MATLAB Programming
MATLAB Programming
 

Destacado

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
 

Destacado (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 a 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
 
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 Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxMATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxprashantkumarchinama
 

Similar a 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 introduction
Matlab introductionMatlab introduction
Matlab introduction
 
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
 
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 anilkumar
Matlab  anilkumarMatlab  anilkumar
Matlab anilkumar
 
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxMATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
 

Último

JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 

Último (20)

JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 

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