TUTORIAL 1 Introduction to Matrix Calculation using MATLAB TUTORIAL 1 INTRODUCTION TO MATRIX CALCULATION USING MATLAB

Similar documents
Lecture 2: Variables, Vectors and Matrices in MATLAB

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Introduction to MatLab. Introduction to MatLab K. Craig 1

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Introduction to MATLAB

Introduction to MATLAB

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

LAB 2 VECTORS AND MATRICES

Introduction to Matlab

0_PreCNotes17 18.notebook May 16, Chapter 12

Matrix Inverse 2 ( 2) 1 = 2 1 2

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

MATLAB for beginners. KiJung Yoon, 1. 1 Center for Learning and Memory, University of Texas at Austin, Austin, TX 78712, USA

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

Introduction to Matlab

AMATH 352: MATLAB Tutorial written by Peter Blossey Department of Applied Mathematics University of Washington Seattle, WA

x = 12 x = 12 1x = 16

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

University of Alberta

Mathematics 4330/5344 #1 Matlab and Numerical Approximation

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

MATLAB COURSE FALL 2004 SESSION 1 GETTING STARTED. Christian Daude 1

Matlab and Octave: Quick Introduction and Examples 1 Basics

1 Overview of the standard Matlab syntax

Introduction to MATLAB 7 for Engineers

Introduction to MATLAB for Engineers, Third Edition

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

Dr Richard Greenaway

Some elements for Matlab programming

A General Introduction to Matlab

Chapter 2. MATLAB Fundamentals

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

ME305: Introduction to System Dynamics

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

Introduction to MATLAB Programming

Finite Math - J-term Homework. Section Inverse of a Square Matrix

MATLAB Fundamentals. Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University

MATLAB Lecture 1. Introduction to MATLAB

Introduction to MATLAB

Teaching Manual Math 2131

A Quick Introduction to MATLAB/Octave. Kenny Marino, Nupur Chatterji

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

MATLAB for Experimental Research. Fall 2018 Vectors, Matrices, Matrix Operations

Introduction to MATLAB for Numerical Analysis and Mathematical Modeling. Selis Önel, PhD

Introduction to MATLAB

Computer Packet 1 Row Operations + Freemat

ECE Lesson Plan - Class 1 Fall, 2001

Computational Photonics, Seminar 01 on Introduction into MATLAB, Page 1

TUTORIAL MATLAB OPTIMIZATION TOOLBOX

NatSciLab - Numerical Software Introduction to MATLAB

Vector: A series of scalars contained in a column or row. Dimensions: How many rows and columns a vector or matrix has.

Matrices 4: use of MATLAB

Lecture 2. Arrays. 1 Introduction

Introduction to MATLAB

AH Matrices.notebook November 28, 2016

1 Introduction to MATLAB

MATH 423 Linear Algebra II Lecture 17: Reduced row echelon form (continued). Determinant of a matrix.

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

An Introduction to MATLAB II

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

2.0 MATLAB Fundamentals

Introduction to Matlab for Econ 511b

Introduction to Engineering gii

How to Use MATLAB. What is MATLAB. Getting Started. Online Help. General Purpose Commands

Colorado State University Department of Mechanical Engineering. MECH Laboratory Exercise #1 Introduction to MATLAB

Identity Matrix: >> eye(3) ans = Matrix of Ones: >> ones(2,3) ans =

Introduction to MATLAB

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

EGR 111 Introduction to MATLAB

Matlab Tutorial: Basics

CME 192: Introduction to Matlab

Fundamentals of MATLAB Usage

STAT/MATH 395 A - PROBABILITY II UW Winter Quarter Matlab Tutorial

4. Linear Algebra. In maple, it is first necessary to call in the linear algebra package. This is done by the following maple command

MATLAB TUTORIAL WORKSHEET

Getting To Know Matlab

1 Introduction to Matlab

Matlab and Matrices. Math 45 Linear Algebra. David Arnold

MATLAB = MATrix LABoratory. Interactive system. Basic data element is an array that does not require dimensioning.

1 Introduction to MATLAB

Introduction and MATLAB Basics

A Guide to Using Some Basic MATLAB Functions

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Arrays and Matrix Operations

6.094 Introduction to MATLAB January (IAP) 2009

What is MATLAB and howtostart it up?

Computer Vision. Matlab

CITS2401 Computer Analysis & Visualisation

To start using Matlab, you only need be concerned with the command window for now.

Laboratory 1 Octave Tutorial

An Introduction to MATLAB and the Control Systems toolbox Aravind Parchuri, Darren Hon and Albert Honein

Chapter 1 Introduction to MATLAB

COMPUTER SCIENCE 314 Numerical Methods SPRING 2013 ASSIGNMENT # 2 (25 points) January 22

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

Grace days can not be used for this assignment

MATH 5520 Basics of MATLAB

Transcription:

INTRODUCTION TO MATRIX CALCULATION USING MATLAB Learning objectives Getting started with MATLAB and it s user interface Learn some of MATLAB s commands and syntaxes Get a simple introduction to use of MATLAB to collect data in matrix or array form Learn how to manipulate data in matrix form 1. Introduction This section introduces MATLAB through a step-by-step procedure of simple matrix operations. The name MATLAB stands for MATrix LABoratory. The program is specially developed for numerical calculation and visualization or plotting of data that is mostly represented in matrix form. Matrices that are also symbolized by arrays represent the most important data elements in MATLAB. A list of elementary matrices and matrix operations is available by writing the following command in the command window. >> Help elmat Starting MATLAB: Start Matlab R2014 (server) MATLAB starts with the following standard windows. 1. Command Window this window contains command lines where we write codes and execute MATLAB expressions. 2. Workspace shows the variables that are generated since the current session started. Prepared by Hirpa Lemu./UiS - 31.07.2017 1

3. Current Directory shows which directory MATLAB is currently using as a working directory and the files that are saved in the directory. 4. Command History shows all executed commands both in the current and previous sessions. Note that new sessions start at every start up of MATLAB. Before starting the step-by-step tutorial, please notice the use of the following three symbols: >>: Command line(s) coming after this symbol are executable. Write the commands after this symbol in the command window and press the enter-key, and observe how MATLAB reacts. : Exercise questions appear right after this symbol. Users are expected to write appropriate MATLAB codes/commands to answer these questions. %: Comment line - everything that comes to the right of the percent symbol % on a line is comment and cannot be executed by MATLAB. As a user, you do not need to write these comments as part of solving the exercise in this tutorial. The comments are for your information. 2 Prepared by Hirpa Lemu./UiS - 31.07.2017

2. Constructing matrices A matrix has the dimensions mxn where m = number of rows (lines) and n = number of columns. An element in matrix A located at row i and column j is denoted by A(i, j). Rows are separated by using either a semicolon (;) or by the enter-key. Columns or elements in a row are separated by a space or a comma (,). For example: to setup the 2 x 2 matrix >>A=[1 2;3 4] é1 2ù A = ê ú in MATLAB ë3 4 û % Here the two columns are separated by spaces and the two rows % are separated by a semicolon. >> A=[1,2;3,4] % Here the columns are separated by a comma. >> A=[1 2 3 4] %Here the columns are separated by spaces and rows by the enter-key. Notice that MATLAB will always produce screen outputs for each input. One can suppress the display of outputs on the screen on any command using a semicolon (;) at the end of the command (i.e., before pressing the enter-key). This is particularly important for large matrices. For example, compare how MATLAB reacts for the following two commands: >> A1=[1 2;3 4] >> A2=[1 2;3 4]; An element of a matrix can be a real number, a complex number or an expression. For example, the matrix >> x = sqrt (10) >> B=[1 2i;3 4+x] é1 2i ù B = ê ú is valid and can be given in MATLAB as follows ë3 4 + x û A row vector is a 1-by-n matrix. That means it contains only one row. A row vector can be constructed, for example, using the following command line: or >>L1 = [2 3 4 5 6] >>L2 = [2:6] % A vector with element increment by 1. A column vector is an m-by-1 matrix. That means it contains only one column. A column vector can be constructed, for example, using the following command line: >>L3 = [2; 3; 4; 5; 6] Prepared by Hirpa Lemu./UiS - 31.07.2017 3

Concatenation of a matrix: A larger matrix can be constructed by concatenating existing matrices or vectors. For example, >> x=[1;3;6 ]; y=[2;4;6]; % Constructs two column vectors, x and y >> M =[x,y] % Matrix M is constructed from matrix x in column 1 and y in column 2 >> L =[x;y] % Constructs a 6x1 matrix (a column vector) with elements from x and y. Construct a row vector from the above two vectors x and y. Dimension of a matrix the size of a matrix, for example, that of matrices M and L constructed above can be found using the command: >>[m,n] = size(m) >>[m,n] = size(l) Addressing elements of a matrix an element of a matrix can be accessed as follows >>w = M(3,1) % This gives the element in row 3 and column 1 of matrix M and % assigns it to w. The value of a given element of a matrix is given as follows: >>M(5,1)=7 % This allocates the value of element x 51 in M to 7. 3. Simple matrix operations MATLAB uses the following simple arithmetic operations on matrices. The precedence rules of the operations follow the given sequence. Operator Matrix operations Array operations + Addition - Subtraction * Multiplication.* Element for element multiplication / Division./ Element for element division \ Left division ^ Power Transpose 4 Prepared by Hirpa Lemu./UiS - 31.07.2017

a) Addition/subtraction Addition and subtraction of matrices is defined element-by-element. These operations require that both matrices must have the same dimension, or one of them must be a scalar. Let s first construct two matrices A and B, having the same dimension. >>A=[2 3 5;0 3 4], B=[1 2 3;3 2 1] >>S1=A+B >>S2=B+A >>S3=A-B Show that matrix addition is both commutative and associative. b) Multiplication by a constant or a scalar >>s=10 % This assigns the value 10 to the variable s. >>Smul1 = s*a >>Smul2=10*B c) Matrix multiplication and element-by-element multiplication Let us construct two 3-by-3 mtrices using the built-in functions Pascal and Magic; P= pascal(3) % Defines a Pascal matrix of dimension 3-by-3. M = magic(3) Mmul = (M*P) Emul = (M.*P) % Defines a Magic matrix of dimension 3-by-3. What is the difference between the two multiplication operations M*P and M.*P? What conditions should be fulfilled to execute the two operations? d) Transpose of a matrix MATLAB uses the apostrophe operator ( ) as the transpose operator. The transpose operation flips the matrix about its main diagonal. For example, observe how MATLAB reacts for these two commands. >>T1 = P >>T2 = M Prepared by Hirpa Lemu./UiS - 31.07.2017 5

Construct two matrices A and B with arbitrary and compatible sizes and show that (A+B) T = A T +B T (A*B) T = B T *A T and Let s assume that the following two matrices are given: >>A = [2-1;0 3; -4 1]; B = [4-3;1 2; -2 5]; Determine the products: AB T, BA T, AA T and A T A. What do you observe from the results? 4. Special, square matrices MATLAB has many built-in functions and matrices that can be easily picked and used. Below are some of commonly used square matrices. a) Null matrix: >>NULL = zeros(n) % where n = size of a square matrix with all elements equal to zero (0). b) Identity matrix: >>ID = eye(n) % where n = size of a square matrix with all elements equal to one (1). c) Diagonal matrix: >>D = diag([3, 4, 6, 8]) % gives a diagonal matrix with 3, 4, 6 and 8 along the main diagonal. d) Scalar matrix: >>S = 5*ID e) Triangular matrices: We have two forms of triangular matrices 1. Lower trinagular matrix: A matrix with all null entries over the main diagonal. Let s assume a random matrix A. The operation tril(a) gives a lower triangular matrix. >>A=magic(4); L = tril(a) 2. Upper triangular matrix: A matrix with all null entries below the main diagonal. The operation triu(a) gives an upper triangular matrix. >> U = triu(a) f) Submatrices: Let s construct the following 4-by-4 matrix >>B=[10 15 20 25;30 35 40 45;50 55 60 65;70 75 80 90] We can create submatices from this matrix, for example >>B1= B(1:4,1:2) >>B1= B(3:4,3:4) % Constructs a submatrix from the two columns % Constructs a 2-by-2 submatrix from the lower right hand entries. 6 Prepared by Hirpa Lemu./UiS - 31.07.2017

Construct a 3-by-3 submatrix from matrix B. 5. Computation of determinants The determinant of a square matrix A can be found using this very simple command in MATLAB: >>A = [1 2 3;4 5 6;7 8 9]; >>d = det(a) % Notice that a determinant is a scalar value. >>B=A ; % This copies the entries of the transpose of matrix A to matrix B. >>db=det(b) % Evaluates the determinant of matrix B and saves the scalar value in db. What do you conclude from the results of the two determinants, det(a) and det(b)? 6. Lineære ligningssystemer A linear equation of the form Ax = b can be solved for the unknown variable x using the MATLAB command: >> x = A\b We have seen the following system of linear equations in the lecture: x 3x 2x 1 1 1 + 2x + 8x + 7x 2 2 2 + x + 7x + 9x 3 3 3 = 4 = 20 = 23 Formulate the equations in a matrix multiplication form (Ax = b) and find x values using MATLAB. 7. Inverting matrices As shown below, matrix inversion is a very simple operation in MATLAB. >> x = inv(a)*b Observe that x = A\b = inv(a)*b Prepared by Hirpa Lemu./UiS - 31.07.2017 7

8 Prepared by Hirpa Lemu./UiS - 31.07.2017