MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Similar documents
MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

MAT 343 Laboratory 2 Solving systems in MATLAB and simple programming

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

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

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

Teaching Manual Math 2131

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Getting To Know Matlab

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

Matrix Inverse 2 ( 2) 1 = 2 1 2

LAB 2: Linear Equations and Matrix Algebra. Preliminaries

2 Second Derivatives. As we have seen, a function f (x, y) of two variables has four different partial derivatives: f xx. f yx. f x y.

x = 12 x = 12 1x = 16

7 Control Structures, Logical Statements

Dr Richard Greenaway

Matrices. A Matrix (This one has 2 Rows and 3 Columns) To add two matrices: add the numbers in the matching positions:

Repetition Structures Chapter 9

MATLAB Premier. Middle East Technical University Department of Mechanical Engineering ME 304 1/50

Introduction to MATLAB for Engineers, Third Edition

Introduction to MATLAB

MATLAB GUIDE UMD PHYS401 SPRING 2011

BEGINNING MATLAB. R.K. Beatson Mathematics Department University of Canterbury. 2 Matlab as a simple matrix calculator 2

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

Introduction to MATLAB

Summer 2009 REU: Introduction to Matlab

MATH 5520 Basics of MATLAB

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

What is MATLAB and howtostart it up?

Computational Mathematics

Introduction to Matlab

Introduction to MATLAB 7 for Engineers

Math Scientific Computing - Matlab Intro and Exercises: Spring 2003

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

A GUIDE FOR USING MATLAB IN COMPUTER SCIENCE AND COMPUTER ENGINEERING TABLE OF CONTENTS

Introduction to Matlab. By: Hossein Hamooni Fall 2014

Introduction to Matlab

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

MATLAB Premier. Asst. Prof. Dr. Melik DÖLEN. Middle East Technical University Department of Mechanical Engineering 10/30/04 ME 304 1

Lecture 2: Variables, Vectors and Matrices in MATLAB

Introduction to Matlab

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

MATLAB GUIDE UMD PHYS401 SPRING 2012

0_PreCNotes17 18.notebook May 16, Chapter 12

Matlab and Octave: Quick Introduction and Examples 1 Basics

MATH 3511 Basics of MATLAB

MBI REU Matlab Tutorial

University of Alberta

2.0 MATLAB Fundamentals

Introduction to Matlab

1 Overview of the standard Matlab syntax

Mathematics 4330/5344 #1 Matlab and Numerical Approximation

Matlab (Matrix laboratory) is an interactive software system for numerical computations and graphics.

function [s p] = sumprod (f, g)

Introduction to Matlab. CSE555 Introduction to Pattern Recognition Spring 2011 recitation

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

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

Introduction to MATLAB

Introduction to MatLab. Introduction to MatLab K. Craig 1

Computer Packet 1 Row Operations + Freemat

Chapter 18 out of 37 from Discrete Mathematics for Neophytes: Number Theory, Probability, Algorithms, and Other Stuff by J. M. Cargal.

Linear, Quadratic, Exponential, and Absolute Value Functions

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

Introduction to MATLAB programming: Fundamentals

Therefore, after becoming familiar with the Matrix Method, you will be able to solve a system of two linear equations in four different ways.

CME 192: Introduction to Matlab

EE 301 Signals & Systems I MATLAB Tutorial with Questions

AN INTRODUCTION TO MATLAB

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

6-2 Matrix Multiplication, Inverses and Determinants

MATLAB GUIDE UMD PHYS375 FALL 2010

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

LAB 2 VECTORS AND MATRICES

Numerical Methods Lecture 1

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

Introduction to Matlab for Econ 511b

MATLAB Lecture 1. Introduction to MATLAB

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

Computational Photonics, Summer Term 2012, Abbe School of Photonics, FSU Jena, Prof. Thomas Pertsch

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

Some elements for Matlab programming

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

MATLAB BASICS. Macro II. February 25, T.A.: Lucila Berniell. Macro II (PhD - uc3m) MatLab Basics 02/25/ / 15

What We Will Learn Today

CS388C: Combinatorics and Graph Theory

Flow Control. Statements We Will Use in Flow Control. Statements We Will Use in Flow Control Relational Operators

Lecturer: Keyvan Dehmamy

Introduction to MATLAB

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

1 Introduction to Matlab

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

Dynamics and Vibrations Mupad tutorial

To add or subtract, just add or subtract the numbers in the same column and row and place answer accordingly.

Introduction to Matlab

A Look Back at Arithmetic Operators: the Increment and Decrement

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Introduction to Matlab

Chapter 7: Programming in MATLAB

Introduction to MATLAB

Stokes Modelling Workshop

Transcription:

MATLAB sessions: Laboratory MAT 75 Laboratory Matrix Computations and Programming in MATLAB In this laboratory session we will learn how to. Create and manipulate matrices and vectors.. Write simple programs in MATLAB Matrices and Linear Algebra Matrices can be constructed in MATLAB in different ways. For example the matrix can be entered as or >> A=[8,,6;,5,7;4,9,] >> A=[8,,6;,5,7; 4,9,] or defined as the concatenation of rows >> row=[8,,6]; row=[,5,7]; row=[4,9,]; A=[row;row;row] or columns >> col=[8;;4]; col=[;5;9]; col=[6;7;]; A=[col,col,col] Note the use of, and ;. Concatenated rows/columns must have the same length. Larger matrices can be created from smaller ones in the same way: >> C=[A,A] % Same as C=[A A] C =

MATLAB sessions: Laboratory The matrix C has dimension 6 ( by 6 ). On the other hand smaller matrices (submatrices) can be extracted from any given matrix: >> A(,) % coefficient of A in nd row, rd column 7 >> A(,:) % st row of A >> A(:,) % rd column of A 6 7 >> A([,],[,]) % keep coefficients in rows & and columns & 6 9 Some matrices are already predefined in MATLAB: >> I=eye() % the Identity matrix I = 0 0 0 0 0 0 >> magic() (what is magic about this matrix?) Matrices can be manipulated very easily in MATLAB (unlike Maple). Here are sample commands to exercise with: >> A=magic(); >> B=A % transpose of A, i.e, rows of B are columns of A B = 8 4 5 9 6 7 >> A+B % sum of A and B 6 4 0 4 0 6 0 6 4 >> A*B % standard linear algebra matrix multiplication 0 7 5 7 8 7 5 7 0 >> A.*B % coefficientwise multiplication 64 4 5 6 4 6 4

MATLAB sessions: Laboratory Try A*A, A^, A.^. The MATLAB command A is especially relevant when studying the solution of linear systems of differentials equations:. determines the solution x = A b of the linear system Ax = b. The array b must have as many rows as the matrix A.. [S,D]=eig(A) determines the eigenvectors of A (columns of S) and associated eigenvalues (diagonal coefficients of D) (note that the eig function has one input and two output arguments). As an example consider the matrix magic() again and x = : >> A=magic() >> x=[,,] % same as x=[;;] x = >> b=a*x b = 8 4 8 >> A\b % this is x! >> inv(a)*b % less efficient and accurate.0000.0000.0000 >> [S,D]=eig(A) S = -0.5774-0.8-0.46-0.5774 0.474-0.474-0.5774 0.46 0.8 D = 5.0000 0 0 0 4.8990 0 0 0-4.8990 >> A*S(:,)-D(,)*S(:,) % test st eigenvector-eigenvalue pair.0e-04 * -0.776 0.55-0.55

4 MATLAB sessions: Laboratory Note the multiplicative factor 0 4 in the last computation. MATLAB performs all operations using standard IEEE double precision. Important!: Because of the finite precision of computer arithmetic and roundoff error, vectors or matrices that are zero (theoretically) may appear in MATLAB in exponential form such as.0e-4 M where M is a vector or matrix with entries between and. This means that each component of the answer is less than 0 4 in absolute value, so the vector or matrix can be treated as zero (numerically) in comparison to vectors or matrices that are on the order of in size. MATLAB Programming Several constructs are used in MATLAB:. repetitive loops (fixed number of times) for <expression>. repetitive loops (indefinite number of times) while <expression>. conditional branching if expression elseif expression : else The following examples illustrate the use of each construct:. for loop: determine the sum of the squares of integers from to 0 S = 0; % initialize running sum for k = :0 S = S+k^; What is S? Verify with MATLAB.. while loop: determine the sum of the inverses of squares of integers from until the inverse of the integer square is less than 0 0 S = 0; % initialize running sum k = ; % initialize current integer incr = ; % initialize test value while incr>=e-0 S = S+incr; k = k+; incr = /k^;

MATLAB sessions: Laboratory 5 What is the value of S returned by this script? Compare to Can k and incr be both initialized by 0?. if statement: evaluate y = x function y=f(x) if x== disp( y is undefined at x = ) else y=/(x-); Try f(), f(). k= for a given (but unknown) scalar x k = π 6. Whenever possible all these construct should be avoided and built in MATLAB functions used instead to improve efficiency. In particular lengthy do loops introduce a substantial overhead. Compare >> n = 0^7; >> tic; S=0; for k=:n; S=S+/k^; ; toc; S Elapsed time is 0.04094 seconds. S =.6449 and >> tic; S=sum(./(:n).^); toc; S Elapsed time is 0.4895 seconds. S =.6449 Note that the Elapsed time, i.e. the computational time, is longer with the for loop than it is by using the built in MATLAB function sum. Note also that the Elapsed time varies deping on the machine you are using. Programming with MATLAB is fairly easy. Still a script or function may not execute properly due to some programming error. In this case MATLAB returns an error indicating where it stopped and why. Most of the time this is sufficient to find out the error and correct it, especially when you get used to it. But keep in mind that even non-fatal mistakes may eventually force a program to later crash. Exercises Instructions: For your lab write-up follow the instructions of LAB.. Enter the following matrices and vectors in MATLAB 4 7 5 8, B = 4 5 6, c = [ 4 ], d = 6 9 7 8 9 (a) Perform the following operations: AB, BA, cb and Ad [ B (b) Construct a 6 matrix C = [ A B ] and a 4 matrix D = c ]. (c) Replace A(, ) with 0. (d) Extract the rd row of the matrix A.

6 MATLAB sessions: Laboratory (e) A row or a column of a matrix can be deleted by assigning the empty vector [] to the row or the column. For instance A(,:)=[] deletes the second row of the matrix A. Delete the third row of the matrix B.. Recall that a geometric sum is a sum of the form a + ar + ar + ar +.... (a) Write a function file that accepts the values of r, a and n as arguments and uses a for loop to return the sum of the first n terms of the geometric series. Test your function for a =, r = / and n = 0. (b) Write a function file that accepts the values of r, a and n as arguments and uses the built in command sum to find the sum of the first n terms of the geometric series. Test your function for a =, r = / and n = 0. Hint: Start by defining the vector e=0:n- and then evaluate the vector R = r.^e. It should be easy to figure out how to find the sum from there.. Write a function file that creates the following piecewise function: x +, x f(x) = e x, < x 5 x x 0, x > 5 Assume x is a scalar. The function file should contain an if statement to distinguish between the different cases. The function should also display the function is undefined at x = 0 if the input is x = 0. Test your function by evaluating f(), f(4), f(7) and f(0). 4. Write a script file that creates a row vector v containing all the powers of below 000. The output vector should have the form: v = [, 4, 8, 6... ]. Use a while loop. 5. The counter in a for or while loop can be given explicit increment: for i =m:k:n to advance the counter i by k each time. In this problem we will evaluate the product of the first 0 odd numbers 5... 9 in two ways: (a) Write a script file that evaluates the product of the first 0 odd numbers using a for loop. (b) Evaluate the product of the first 0 odd numbers using a single MATLAB command. Use the MATLAB command prod. 6. Write a function file which takes as input an n vector x and an integer m, and returns as x x output the vector y =.. mx Test your file for x = and m =.