Seminar to the lecture Computer-based Engineering Mathematics

Similar documents
SKMM 3023 Applied Numerical Methods

SME 3023 Applied Numerical Methods

Lund Institute of Technology Centre for Mathematical Sciences Mathematical Statistics

Chapter 5. Linear Algebra. A linear (algebraic) equation in. unknowns, x 1, x 2,..., x n, is. an equation of the form

Fundamentals of Engineering Analysis (650163)

OR MSc Maths Revision Course

Chapter 2. Linear Algebra. rather simple and learning them will eventually allow us to explain the strange results of

A matrix is a rectangular array of. objects arranged in rows and columns. The objects are called the entries. is called the size of the matrix, and

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 1 x 2. x n 8 (4) 3 4 2

A matrix is a rectangular array of. objects arranged in rows and columns. The objects are called the entries. is called the size of the matrix, and

MTH Linear Algebra. Study Guide. Dr. Tony Yee Department of Mathematics and Information Technology The Hong Kong Institute of Education

Finite Difference Methods for Boundary Value Problems

Systems of Linear Equations and Matrices

Prof. Dr.-Ing. A. Czylwik

Systems of Linear Equations and Matrices

(Linear equations) Applied Linear Algebra in Geoscience Using MATLAB

Applied Linear Algebra in Geoscience Using MATLAB

A Review of Matrix Analysis

Linear Algebra Using MATLAB

Linear Algebra. The analysis of many models in the social sciences reduces to the study of systems of equations.

Linear Algebra: Matrix Eigenvalue Problems

L3: Review of linear algebra and MATLAB

LINEAR SYSTEMS, MATRICES, AND VECTORS

MAT Linear Algebra Collection of sample exams

Gaussian Elimination and Back Substitution

Introduction to Matrices and Linear Systems Ch. 3

Review Questions REVIEW QUESTIONS 71

Chapter 7. Linear Algebra: Matrices, Vectors,

a11 a A = : a 21 a 22

F(jω) = a(jω p 1 )(jω p 2 ) Û Ö p i = b± b 2 4ac. ω c = Y X (jω) = 1. 6R 2 C 2 (jω) 2 +7RCjω+1. 1 (6jωRC+1)(jωRC+1) RC, 1. RC = p 1, p

Linear Algebra Homework and Study Guide

APPENDIX: MATHEMATICAL INDUCTION AND OTHER FORMS OF PROOF

Examples and MatLab. Vector and Matrix Material. Matrix Addition R = A + B. Matrix Equality A = B. Matrix Multiplication R = A * B.

SKMM 3023 Applied Numerical Methods

Linear Algebra. Matrices Operations. Consider, for example, a system of equations such as x + 2y z + 4w = 0, 3x 4y + 2z 6w = 0, x 3y 2z + w = 0.

LECTURE NOTES ELEMENTARY NUMERICAL METHODS. Eusebius Doedel

Chapter 9: Systems of Equations and Inequalities

Algebra II Vocabulary Alphabetical Listing. Absolute Maximum: The highest point over the entire domain of a function or relation.

Math 307 Learning Goals. March 23, 2010

SME 3023 Applied Numerical Methods

Chapter 1: Systems of linear equations and matrices. Section 1.1: Introduction to systems of linear equations

Mathematics for Graphics and Vision

Linear Algebra. Min Yan

Math 302 Outcome Statements Winter 2013

Quaternion Dynamics, Part 1 Functions, Derivatives, and Integrals. Gary D. Simpson. rev 00 Dec 27, 2014.

Digital Workbook for GRA 6035 Mathematics

Finite Mathematics Chapter 2. where a, b, c, d, h, and k are real numbers and neither a and b nor c and d are both zero.

MATRICES. a m,1 a m,n A =

Introduction - Motivation. Many phenomena (physical, chemical, biological, etc.) are model by differential equations. f f(x + h) f(x) (x) = lim

Elementary Linear Algebra

Lectures 9-10: Polynomial and piecewise polynomial interpolation

106 Chapter 5 Curve Sketching. If f(x) has a local extremum at x = a and. THEOREM Fermat s Theorem f is differentiable at a, then f (a) = 0.

Homework and Computer Problems for Math*2130 (W17).

B553 Lecture 5: Matrix Algebra Review

Queens College, CUNY, Department of Computer Science Numerical Methods CSCI 361 / 761 Spring 2018 Instructor: Dr. Sateesh Mane.

New Mexico Tech Hyd 510

(Mathematical Operations with Arrays) Applied Linear Algebra in Geoscience Using MATLAB

MATRICES. knowledge on matrices Knowledge on matrix operations. Matrix as a tool of solving linear equations with two or three unknowns.

Linear Algebra March 16, 2019

Fundamentals of Linear Algebra. Marcel B. Finan Arkansas Tech University c All Rights Reserved

TI89 Titanium Exercises - Part 10.

ENGI 9420 Lecture Notes 2 - Matrix Algebra Page Matrix operations can render the solution of a linear system much more efficient.

Review of matrices. Let m, n IN. A rectangle of numbers written like A =

Introduction to Matrices

Lecture Notes in Linear Algebra

ÇÙÐ Ò ½º ÅÙÐ ÔÐ ÔÓÐÝÐÓ Ö Ñ Ò Ú Ö Ð Ú Ö Ð ¾º Ä Ò Ö Ö Ù Ð Ý Ó ËÝÑ ÒÞ ÔÓÐÝÒÓÑ Ð º Ì ÛÓ¹ÐÓÓÔ ÙÒÖ Ö Ô Û Ö Ö ÖÝ Ñ ¹ ÝÓÒ ÑÙÐ ÔÐ ÔÓÐÝÐÓ Ö Ñ

CS123 INTRODUCTION TO COMPUTER GRAPHICS. Linear Algebra /34

Note: The command name is upper case in the description given by help, but must be lower case in actual use. And the backslash Anb is dierent when A i

MATH2210 Notebook 2 Spring 2018

Linear Systems and Matrices

3. Array and Matrix Operations

8.5 Taylor Polynomials and Taylor Series

Extra Problems for Math 2050 Linear Algebra I

1 - Systems of Linear Equations

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

Math Assignment 3 - Linear Algebra

APPENDIX A. Background Mathematics. A.1 Linear Algebra. Vector algebra. Let x denote the n-dimensional column vector with components x 1 x 2.

G1110 & 852G1 Numerical Linear Algebra

Equality: Two matrices A and B are equal, i.e., A = B if A and B have the same order and the entries of A and B are the same.

Some Notes on Linear Algebra

Linear Algebra for Machine Learning. Sargur N. Srihari

Elementary Linear Algebra

Linear Systems of n equations for n unknowns

8. Diagonalization.

Lab 2: Static Response, Cantilevered Beam

CS123 INTRODUCTION TO COMPUTER GRAPHICS. Linear Algebra 1/33

OHSx XM511 Linear Algebra: Solutions to Online True/False Exercises

Contents. 1 Vectors, Lines and Planes 1. 2 Gaussian Elimination Matrices Vector Spaces and Subspaces 124

A Review of Linear Algebra

LS.1 Review of Linear Algebra

Jim Lambers MAT 610 Summer Session Lecture 2 Notes

Next topics: Solving systems of linear equations

Foundations of Matrix Analysis

Chapter 4. Vector Space Examples. 4.1 Diffusion Welding and Heat States

CPE 310: Numerical Analysis for Engineers

Using MATLAB. Linear Algebra

Scientific Computing: Dense Linear Systems

Matrix & Linear Algebra

1 Matrices and Systems of Linear Equations. a 1n a 2n

ADDITIONAL MATHEMATICS

Transcription:

Seminar to the lecture Computer-based Engineering Mathematics N T S Prof. Dr.-Ing. A. Czylwik Mohammad Abdelqader, M.Sc. Room: BA 249, Tel: +49-203-379-3474 E-Mail: abdelqader@nts.uni-duisburg-essen.de Chair of Communication Systems University of Duisburg-Essen

Contents 1 Introduction to Matlab 1 1.1 What is MATLAB?................................ 1 1.2 Expressions................................... 2 1.2.1 Variables................................. 2 1.2.2 Numbers................................. 2 1.2.3 Operators................................ 2 1.2.4 Functions................................ 3 1.3 Handling matrices............................... 3 1.3.1 Entering matrices and addressing elements............. 3 1.3.2 Generating matrices.......................... 4 1.3.3 Concatenation.............................. 5 1.3.4 Deleting rows and columns...................... 6 1.3.5 Array orientation............................ 6 1.3.6 Scalar-array mathematics....................... 6 1.3.7 Array-array mathematics....................... 7 2 Basic plotting using Matlab 10 2.1 Two-dimensional Graphics.......................... 10 2.1.1 Multiple plots on the same axis.................... 11 2.1.2 Line styles, markers and color.................... 11 2.1.3 Plotting rapidly changing mathematical functions......... 12 2.2 Three-dimensional graphics.......................... 12 2.2.1 Curve plots............................... 12 2.2.2 Mesh and surface plots......................... 13 2.2.3 Multiple plots in a figure: subplot.................. 14 3 Functions 15 3.1 Domain, range and symmetries........................ 15

3.2 Transformation of coordinates......................... 15 3.3 Normalization of physical quantities..................... 16 3.4 Local minima and maxima........................... 17 3.5 Poles and zeros................................. 20 4 Curve fitting, interpolation and approximation of functions using Polynomials 21 4.1 Polynomial curve fitting............................ 21 4.2 Polynomial interpolation............................ 22 4.3 Taylor series expansion............................. 25 5 Vectors and matrices 29 5.1 Creating matrices................................ 29 5.2 Inverses and determinants........................... 35 5.3 Vector norms and the scalar product in Matlab............... 38 5.4 Matlab functions that work on diagonals of a matrix............ 39 6 Systems of linear equations 40 6.1 Linear system, coefficient matrix, augmented matrix............ 40 6.2 Solutions of linear systems: Existence and uniqueness........... 41 6.3 Numerical methods in linear algebra..................... 42 6.4 Matlab syntax and descriptions for LU- and Cholesky-factorization... 45 6.5 Matlab solutions for systems of linear equations.............. 47 7 Eigenvalues and eigenvectors 51 7.1 Eigenvalues, eigenvectors........................... 51 7.2 MATLAB methods to determine eigenvalues and eigenvectors...... 53 7.3 Linear independence of eigenvectors..................... 54 7.4 Orthonormal set, Symmetric matrices.................... 57 7.5 Diagonalization of a matrix, canonical form................. 59 7.6 Cayley-Hamilton theorem........................... 61 8 Nonlinear equations 62

8.1 Polynomial roots................................ 62 8.2 Solving real function using fzero....................... 63 8.3 Solving equations using symbolic toolbox.................. 68 8.4 System of nonlinear equations......................... 70 9 Differentiation 75 9.1 Polynomial derivatives............................. 75 9.2 Approximate derivative............................ 77 9.3 Symbolic derivative............................... 79 10 Integration 81 10.1 Introduction to numerical integration.................... 81 10.2 Numerical integration using Matlab..................... 82 10.3 Symbolic integration.............................. 87 11 Ordinary differential equations 89 11.1 Introduction................................... 89 11.2 Numerical methods to solve initial value problems (IVP) of ODEs.... 91 11.3 Solving ODEs in Matlab............................ 93 11.4 Systems of first-order ODEs.......................... 95

1 CHAPTER 1 Introduction to Matlab 1.1 What is MATLAB? MATLAB 1 is a high-performance language for technical computing. It integrates computation, programming and visualisation in a user-friendly environment where problems and solutions are expressed in an easy-to-understand mathematical notation. MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. This allows the user to solve many technical computing problems, especially those with matrix and vektor operations, in less time than it would take to write a programm in a scalar noninteractive language such as C or Fortan. MATLAB features a family of application-specific solutions which are called toolboxes. It is very important to most users of MATLAB, that toolboxes allow to learn and apply specialized technology. These toolboxes are comprehensive collections of MATLAB functions, so-called M-files, that extend the MATLAB environment to solve particular classes of problems. MATLAB is a matrix-based programming tool. Although matrices often need not to be dimensioned explicity, the user has always to look carefully for matrix dimensiones. If it is not defined otherwise, the standard matrix exhibits two dimensiones Ò Ü Ñ. Column vectors an row vectors are represented consistently by Ò Ü ½ and ½ Ü Ò matrices, respectively. MATLAB operations can be classified into the following types of operations: arithmetic and logical operations, mathematical functions, graphical functions, and input/output operations In the following sections, individual elements of MATLAB operations are explained in detail. 1 MATLAB is a registered trademark of The MathWorks, Inc.

1.2 Expressions Like most other programming languages, MATLAB provides mathematical expressions, but unlike most programming languages, these expressions involve entire matrices. The building blocks of expressions are Variables Numbers Operators Functiones 1.2.1 Variables MATLAB does not require any type declarations or dimension statements. When a new variable name is introduced, it automatically creates the variable and allocates the appropriate amount of memory. If the variable already exists, MATLAB changes its contents and, if necessary, allocates new storage. For example ÓÓ ½¼ creates a 1-by-1 matrix named ÓÓ and stores the value 10 in its single element. In the expression above, constitutes the MATLAB prompt, where the commands can be entered. Variable names consist of a string, which start with a letter, followed by any number of letters, digits, or underscores. MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. and are not the same variable. To view the matrix assigned to any variable, simply enter the variable name. 1.2.2 Numbers MATLAB uses the conventional decimal notation. A decimal point and a leading plus or minus sign is optional. Scientific notation uses the letter e to specify a power-of-ten scale factor. Imaginary numbers use either or as a suffix. Some examples of legal numbers are: ¹ ¼º¼¼ ½ º º½¼¾¾¼ ¹½¼ º¼ ¾ ¾½ ¾ ¹¾º ½ ¾ ¾ ¾º ½º 1.2.3 Operators Expressions use familar arithmetic operators and precedence rules. Some examples are:

+ Addition - Subtraction * Multiplication / Division Complex conjugate transpose ( ) Brackets to specify the evaluation order. 1.2.4 Functions MATLAB provides a large number of standard elementary mathematical functions, including Ò, ÕÖØ, ÜÔ and. Taking the square root or logarithm of a negative number does not lead to an error; the appropriate complex result is produced automatically. MATLAB also provides a lot of advanced mathematical functions, including Bessel and Gamma functions. Most of these functions accept complex arguments. For a list of the elementary mathematical functions, type ÐÔ Ð ÙÒ Some of the functions, like ÕÖØ and Ò are build-in. They are a fixed part of the MATLAB core so they are very efficient. The drawback is that the computational details are not readily accessible. Other functions, like ÑÑ, are implemented in so called M-files. You can see the code and even modify it if you want. 1.3 Handling matrices MATLAB was mainly designed to deal with matrices. In MATLAB, a matrix is a rectangular array of numbers. So scalars can be interpreted to be 1-by-1 matrices and vectors are matrices with only one row or column. MATLAB has other ways to store both numeric and non-numeric data, but in the beginning of learning MATLAB, it is usually best to think of everything as a matrix. The operations in MATLAB are designed to be as natural as possible. Where other programming languages work only with single numbers, MATLAB allows to work with entire matrices quickly and easily. 1.3.1 Entering matrices and addressing elements The elements of a matrix must be entered one-by-one in a list where the elements of a row must be separated with commas or blank spaces and the rows are divided by semicolons. The whole list must be surrounded with square brackets, e.g.: ½ ¾ After pressing " Enter" MATLAB displays the numbers entered in the command line

½ ¾ Addressing an element of a matrix is also very easy. The Ò-th element of the Ñ-th column in matrix from above is Ò Ñµ. So typing ½ µ ¾ ½µ ¾µ will compute the answer Ò ½ The -th to ½-th elements of the Ñ-th to Ò-th columns can be addressed by ½ Ñ Òµ, e. g. ¾ ½ ¾µ Ò Further examples: ½ ½ ¾µ addresses the first two elements of the first row. Ò ½ ¾ ¾µ addresses all elements of the second column. Ò ¾ 1.3.2 Generating matrices There are different ways to generate matrices. Assigning elements explicitly was presented in the paragraph above. To create a row vector with 101 equidistant values starting at 0 and ending by π, this method would be very tedious. So two other possibilities are shown below:

Ü Ð Ò Ô ¼ Ô ½¼½µ or Ü ¼ ¼º¼½ ½µ Ô In the first case, the MATLAB function Ð Ò Ô is used to create Ü. The function s arguments are described by: Ð Ò Ô Ö Ø Ú ÐÙ Ð Ø Ú ÐÙ ÒÙÑ Ö Ó Ú ÐÙ µ with the default ÒÙÑ Ö Ó Ú ÐÙ ½¼¼. In the second case, the colon notation ¼ ¼º¼½ ½µ creates an array that starts at 0, increments by 0.01 and ends at 1. Afterwards each element in this array is multiplied by π to create the desired values in Ü. Both of these array creation forms are common in MATLAB. While the colon notation form allows to specify the increment between data elements directly, but not the number of data elements, the MATLAB function Ð Ò Ô allows to specify the number of data elements directly, but not the increment value between these data elements. The colon notation is very often used in MATLAB therefore a closer look should be taken on it. Ö Ø Ú ÐÙ ÒÖ Ñ ÒØ Ð Ø Ú ÐÙ µ creates an array starting at Ö Ø Ú ÐÙ, ending at Ð Ø Ú ÐÙ with an increment which can be negative as well, e. g. Ú ½¼ ¹¾ ¼µ Ú ½¼ ¾ ¼ If the increment is 1, then its usage is optional: Û ½¼µ Û ½¼ MATLAB also provides four functions that generate basic matrices: Þ ÖÓ, ÓÒ, Ö Ò and Ö Ò Ò. These functions will be discussed in chapter 5. 1.3.3 Concatenation Concatenation is a process of joining small matrices to make bigger ones. In fact, the first matrix A was created by concatenating its individual elements. The pair of square brackets, [ ], is the concatenation operator. For an example, start with the 3-by-3 matrix, and form

½¼ ¾ º The result is an 6-by-6 matrix, obtained by joining the four submatrices. ½ ¾ ½½ ½¾ ½ ½ ½ ½ ½ ½ ½ ¾ ½¾ ½ ½¾ ¾ ¾ ½ ½¾ ½ ½¾ ¾ 1.3.4 Deleting rows and columns To delete rows or columns of a matrix, just use a pair of square brackets, e. g. ¾ µ deletes the second row of. ½ ¾ It is not possible to delete a single element of a matrix, because afterwards it would not still be a matrix. (Exception: vectors, since here deleting an element is the same as deleting a row/column.) 1.3.5 Array orientation The orientation of an array can be changed with the MATLAB transpose operator : ¼ ¼ ½ ¾ ³ ¼ ½ ¾ 1.3.6 Scalar-array mathematics Addition, subtraction, multiplication and division by a scalar apply the operation to all elements of the array:

½ ¾ ½¼ ½½ ½¾ ½ ¾ ½¼ ½½ ½¾ ¾ ¹½ multiplies each element in c by two and subtracts one from each element of the result. Ò ½ ½½ ½ ½ ½ ½ ¾½ ¾ 1.3.7 Array-array mathematics When two arrays have the same dimensions, which means that they have the same number of rows and columns, addition, subtraction, multiplication and division apply on an element-by-element basis in MATLAB. ½ ¾ ½ ¾ ¾ ¾ ¾ ¾ ¾ ¾ ± ØÓ ÓÒ Ò Ð Ñ Òع ݹ Ð Ñ ÒØ ¾ ¹ ± ÑÙÐØ ÔÐ Ý ØÛÓ Ò Ù ØÖ Ø ÖÓÑ Ø Ö ÙÐØ ¼ ¾ Element-by-element multiplication an division work similarly, but the notation is slightly different: º ¾ ½¾ ½ ½

The element-by-element multiplication uses the dot multiplication symbol.*, the elementby-element array division uses either º» or º º» Ò ¼º ¼¼ ½º¼¼¼ ½º ¼¼ ½º ½º ¾º¼¼¼ º Ò ¼º ¼¼ ½º¼¼¼ ½º ¼¼ ½º ½º ¾º¼¼¼ In both cases, the elements of the array in front of the slash is divided by the elements of the array behind the slash. To compute a matrix multiplication only the asterisk * must be used, e. g. Therefore the number of columns of must be equal the number of rows of. ½ ¾ ½ ¾ ½ ¾ ½ ¾ ¾¾ ¾

Übungen Variablen erzeugen 1.1) Definieren Sie eine skalare Variable und weisen Sie der Variablen den Wert ( 2 1 + 0.2 j) zu. 1.2) Definieren Sie einen Spaltenvektor mit 5 Elementen, die linear von 80 bis 60 absteigen. 1.3) Welche Ergebnisse liefern die Befehle: Ð Ò Ø µ und Þ µ für die Definitionen in 1.1 und 1.2? 1.4) Definieren Sie den Zeilenvektor mit c = ( 1 1 1 ). 1.5) Ergänzen Sie zu dem Vektor eine zweite Zeile, so dass die Matrix ( ) 1 1 1 C = 0 1 1 entsteht. 1.6) Fügen Sie der Matrix C weitere Spalten hinzu, so dass sich die Matrix ( ) 1 1 1 2 2 D = 0 1 1 2 2 ergibt. Matrixoperationen Gegeben sind die Matrix ¾ ½ ½ ¾ und der Spaltenvektor ¾ ½. Bestimmen Sie die Ergebnisse folgender Matlab-Befehle: 2.1) ³ 2.2) 2.3) µ³ 2.4) ³ ³ 2.5) ÓÒ ¾µº 2.6) Þ ÖÓ ¾ µ

10 CHAPTER 2 Basic plotting using Matlab 2.1 Two-dimensional Graphics Graphs (in 2-D) are drawn with the ÔÐÓØ statement. In its simplest form, it takes a single vector argument y as in ÔÐÓØ Ýµ. In this case the elements of y are plotted against their indexes, e. g. ÔÐÓØ Ö Ò ½ ¾¼µµ plots 20 random numbers against the integers 1 to 20, joining successive points with straight lines. Straight-line graphs are drawn by giving the x and y coordinates of the end-points in two vectors. For example, to draw a line between the point with cartesian coordinates (0, 1) and (4, 3) use the statement ÔÐÓØ ¼ ½ µ Exercise 1 Draw lines joining the following points: (0, 0), (1, 1), (2, 0), (1,-1) and (0, 0). If x and y are vectors of the same length, the command ÔÐÓØ Ü Ýµ opens a graphics window and draws the elements of y versus the elements of x. For example, Ü ¹ ¼º¼½ Ý Ò Üµ ÔÐÓØ Ü Ýµ The vector x is a partition of the domain with mesh size 0.01, and y is a vector giving the values of sine at the nodes of this partition (recall that Ò Üµ operates entry-wise). When plotting a curve, the plot routine is actually connecting consecutive points induced by the partition with line segments. Thus, the mesh size should be chosen sufficiently small to render the appearance of a smooth curve.

Exercise 2 Plot y versus x for y = exp( x 2 ) and x varies from -10 to 10. Plot y versus t for y = sin(2π f t), f = 2 and t varies from -2 to 2. Plot y versus t for y = cos(2π f t), f = 2 and t varies from -2 to 2. 2.1.1 Multiple plots on the same axis The easiest way is simply to use hold to keep the current plot on the axes. All subsequent plots are added to the axes until hold is released, either with hold off, or just hold, which toggles the hold state. The second way is to use plot with multiple arguments, e. g. ÔÐÓØ Ü½ ݽ ܾ ݾ Ü Ý ººº µ plots the (vector) pairs (x1, y1), (x2, y2), etc. The advantage of this method is that the vector pairs may have different lengths. MATLAB automatically selects a different color for each pair. For example, Ü ¹¾ Ô ¼º¼½ ¾ Ô ÔÐÓØ Ü Ó Üµ Ü Ò Üµµ ± ÕÙ Ú Ð ÒØ ØÓ Ø ÓÑÑ Ò ÔÐÓØ Ü Ó Üµµ ÓÐ ÓÒ ÔÐÓØ Ü Ò Üµµ ÓÐ Ó Exercise 3 Draw x 2, x 3, x 4, exp( x 2 ) on the same figure using both methods as described above. Add a legend using Matlab command Ð Ò ³Ü ¾ Ü Ü ÜÔ ¹Ü ¾µ³µ. 2.1.2 Line styles, markers and color Line styles, markers and colors may be selected for a graph with a string argument to plot, e. g. ÔÐÓØ Ü Ý ³¹¹³µ joins the plotted points with dashed lines, whereas ÔÐÓØ Ü Ý ³Ó³µ draws circles at the data points with no lines joining them. You can specify all three properties, e. g.

ÔÐÓØ Ü Ò Üµ Ü Ó Üµ ³¹¹ÑÓ³µ plots sin (x) in the default style and color and cos (x) with circles joined with dashes in magenta. The available colors are denoted by the symbols c, m, y, k, r, g, b, w. You can have fun trying to figure out what they mean, or you can use help plot to see the full range of possible symbols. 2.1.3 Plotting rapidly changing mathematical functions In all the graphing examples so far, the x coordinates of the points plotted have been incremented uniformly, e. g. x = 0 : 0.01 : 4. If the function being plotted changes very rapidly in some places, this can be inefficient, and can even give a misleading graph. For example, Ü ¼º¼½ º¼¼½ º½ ÔÐÓØ Ü Ò ½º»Üµµ MATLAB has a function called ÔÐÓØ which uses a more elegant approach. Whereas the above method evaluates sin(1/x) at equally spaced intervals, ÔÐÓØ evaluates it more frequently over regions where it changes more rapidly. Here is how to use it: ÔÐÓØ ³ Ò ½»Üµ³ ¼º¼½ ¼º½ µ ± ½º»Ü ÒÓØ Ò Exercise 4 Now draw the functions from exercise 3 using ÔÐÓØ. Try also using ÞÔÐÓØ. For more information check the MATLAB help by typing at command window ÐÔ ÞÔÐÓØ. 2.2 Three-dimensional graphics MATLAB s primary commands for creating three dimensional graphics of numericallydefined functions are ÔÐÓØ Ñ ÙÖ Ò ÙÖ Ð. 2.2.1 Curve plots Completely analogous to plot in two dimensions, the command plot3 produces curves in three-dimensional space. If x, y, and z are three vectors of the same size, then the command ÔÐÓØ Ü Ý Þµ produces a perspective plot of the piecewise linear curve in threespace passing through the points whose coordinates are the respective elements of x, y, and z. These vectors are usually defined parametrically. For example, Ø ¼º¼½ ¼º¼½ ¾¼ Ô Ü Ó Øµ Ý Ò Øµ Þ Øº ÔÐÓØ Ü Ý Þµ ±ÔÖÓ Ù Ð Ü Ø Ø ÓÑÔÖ Ò Ö Ø Ü¹Ý ÔÐ Ò

2.2.2 Mesh and surface plots The Ñ command draws three-dimensional wire mesh surface plots. Similarly, threedimensional faceted surface plots are drawn with the command ÙÖ. The following MATLAB M-File generate a mexican hut using the mesh command. ± Å Ü Ò ÙØ Ò Ü ÑÔÐ ÓÖ ÔÐÓغ Ü Ý Ñ Ö ¹ ¼º µ Ö ÕÖØ Üº ¾ ݺ ¾µ Þ Ò Öµº»Ö Ñ Ü Ý Þµ Ø ØÐ ³Å Ü Ò Ùسµ Mexican hut 1 0.5 0 0.5 10 5 0 5 10 10 5 0 5 10 The following script shows how to graph the function f(x, y) = x 1+ y 2 (2.1) over the disk (x 1) 2 +(y 3) 2 = 4 (2.2) ± Ü ÑÔÐ ÓÖ ÈÐÓØ ÖÓÑ Ø ÓÓ ³ Å ØÐ ÓÑ Ô Ò ÓÒ ÓÖ ÑÙÐØ Ú Ö Ð ÐÙÐÙ ³ ± Ý Âº ÓÓÔ Ö ÍÒ Ú Ö ØÝ Ó Å ÖÝÐ Ò ± Ö Ø Ñ Ñ Ö Ò Ö Ø Ø ÓÓÖ Ò Ø Ö Ð Ò Ô ¼ ¾ ¾½µ Ø Ø Ð Ò Ô ¼ ¾ Ô ½µ Ê ÌÀ Ñ Ö Ö Ø Ø µ ± ÆÓÛ ÓÒÚ ÖØ ÒØÓ ÙÖÚ Ð Ò Ö Ñ Ö Ò Ü Ý ± ÓÓÖ Ò Ø ½ ʺ Ó ÌÀµ ʺ Ò ÌÀµ ¹½ º ¾ ÙÖ µ

ÓÐ ÓÒ ± Ø ÔÐ Ò Þ ¹ Û Ø Ø ÙÖÚ Ð Ò Ö ± Ñ Ö ÙÖ ¹ ¼ µ ÓÐ Ó Plot of f(x,y) = x 1 +y 2 over the disk (x 1) 2 + (y 3) 2 = 4 30 20 z axis 10 0 10 5 4 3 y axis 2 1 1 0 1 x axis 2 3 Exercise 5 Plot a Gaussian probability density function f x,y (x, y) with zero mean and variance 1 within the range x [ 5, 5], y [ 5, 5]: f x,y (x, y) = 1 2π e 1 2 (x2 +y 2 ) Try out the commands Ñ, ÙÖ and ÓÒØÓÙÖ to plot the graph! Exercise 6 Plot the function (2.3) over the unit disk, centered at the origin. 2.2.3 Multiple plots in a figure: subplot g(r,θ) = (r/2) sin(θ)+r 3 cos(3θ) (2.4) You can show a number of plots in the same figure window with the subplot function. The statement Ù ÔÐÓØ Ñ Ò Ôµ divides the figure window into m n small sets of axes, and selects the p-th set for the current plot (numbered by row from the left of the top row). Exercise 7 Plot the function from Exercise 5 in four subplots using the commands Ñ in Ù ÔÐÓØ ¾ ¾ ½µ, ÙÖ in Ù ÔÐÓØ ¾ ¾ ¾µ, ÙÖ Ð in Ù ÔÐÓØ ¾ ¾ µ and ÓÒØÓÙÖ in Ù ÔÐÓØ ¾ ¾ µ. Give title to each plot as for instance Ø ØÐ ³ÈÐÓØ Ó Ù Ò Ô Ù Ò Ñ ³µ

15 CHAPTER 3 Functions This chapter analyzes important characteristics of functions using Matlab. 3.1 Domain, range and symmetries Consider the functions 1 f(x) = 1 x2 4 1 g(x) =. 1 x2 4 Exercise Draw on the same figure the functions f(x) and g(x) for 5 x 5 with distinct colors, markers and legends. Determine the domain, range and symmetry points of f(x) and g(x) so that these are real-valued. Name the x- and y-axis using the Matlab commands ÜÐ Ð ³Ü Ö Ø ÖÖÓÛ³µ ÝÐ Ð ³ ܵ ܵ Ö Ø ÖÖÓÛ³µ 3.2 Transformation of coordinates Consider the function g(x) drawn in the interval 5 x 5: Ð Ð Ö ÐÓ ÐÐ Ü ¹ º¼ Ü ½ º» ÕÖØ ½ ¹ ¼º¾ ܺ ¾ µ µ ÔÐÓØ Ü Ü ³¹Öº³ µ ÓÐ ÓÒ A simple coordinate transformation is obtained by shifting g(x) to g 2 (x) = g(x x 0 ). For x 0 = 1:

Ü ¼ ½ ¾ Ü ½º» ÕÖØ ½¹¼º¾ Ü¹Ü ¼µº ¾µµ ÔÐÓØ Ü ¾ Ü ³¹ ³µ ÓÐ ÓÒ 5 4.5 Coordinate transformation g(x) g(x 1) 4 3.5 g(x), g 2 (x) 3 2.5 2 1.5 1 0.5 0 5 4 3 2 1 0 1 2 3 4 5 x Exercise Draw on the same figure the coordinate transformation g 3 (x) = g(ax) for a = 2 (scaling of the x-axis). Draw on the same figure the coordinate transformation g 4 (x) = g(ax x 0 ) for a = 2 and x 0 = 1. Draw a new figure with the non-linear coordinate transformation g 5 (x) = g(x 2 ). Add a legend for all five curves. 3.3 Normalization of physical quantities Physical quantities are normalized with the purpose of simplifying calculations and eliminate the use of the physical units of distinct variables. Consider the the transfer function of the following RLC filter: R U 1 (ω) L C U 2 (ω)

H(ω) = U 2(ω) U 1 (ω) = jωl/r 1 ω 2 LC+jωL/R. The squared magnitude of the RLC filter using the technical frequency f is given by H( f) 2 = (2π f L/R) 2 [ ] 2+[2π. 1 (2π f) 2 LC f L/R] 2 Expressing: f in khz, L inµh, C inµf, R in Ω results in: H( f) 2 = ( 2π f L/µH khz R/Ω 10 3) 2 ( ( ) ) 1 2π f 2 2 L C khz µh µf 10 6 + ( ). 2π f L/µH 2 khz R/Ω 10 3 Exercise Draw H( f) 2 in the range 0 f 20kHz for L = 100µH, C = 10µF and R = 10Ω with the corresponding labels in the x- and y-axis. 3.4 Local minima and maxima For a function y = f(x), local minima and maxima are determined by solving the expression dy dx = lim x 0 f(x+ x) f(x) x = 0 A graphical approximation of the derivate can be calculated in Matlab using the command. For a vector y, ݵ calculates the differences between adjacent elements of y. As an example, we would like to determine graphically the local minima and maxima of the function y(x) = x cos(x) for π x π:

Ð Ð Ö ÐÓ ÐÐ ÐØ ¼º¼½ Ü ¹Ô ÐØ Ô Ý Ü º Ó Ü µ ÔÐÓØ Ü Ýµ Ö ÓÒ ÓÐ ÓÒ ÔÐÓØ Ü ½ Ò ¹½µ ݵº» ܵ ³Ö³µ ÜÐ Ð ³Ü Ö Ø ÖÖÓÛ³µ ÝÐ Ð ³ ܵ Ö Ø ÖÖÓÛ³µ Ø ØÐ ³ ÔÔÖÓÜ Ñ Ø Ö Ú Ø ÓÒ³µ Ð Ò ³Ý ÜÓ Üµ³ ³ Ý» ܳµ 4 3 Approximate derivation y = xcos(x) dy/dx f(x) 2 1 0 1 2 3 Local Minimum Local Maximum 4 4 2 0 2 4 x Notice that the command ݵ reduces the dimension of the vector y by one element. Therefore, the figure is drawn by reducing the elements of the vector x by one element - using the command Ü ½ Ò ¹½µ. Exercise Determine the local minima and maxima of the function ( ) 2π f L/µH 2 khz R/Ω 10 3 H( f) 2 = ( ( ) ) 1 2π f 2 2 L C khz µh µf 10 6 + ( ). 2π f L/µH 2 khz R/Ω 10 3 for 0 f 20kHz, L = 100µH, C = 10µF and R = 10Ω with the corresponding labels in the x- and y-axis. The location of the local minima and maxima can be solved analytically in the case that a function can be expressed in a polynomial form. The procedure is described in the following.

First, a polynomial is defined in Matlab by a row vector that contain the coefficients of the polynomial. As an example, the polynomial p(x) = x 3 2x 5 is represented by: Ô ½ ¼ ¹¾ ¹ The derivative q(x) = dp(x) dx is calculated using the Matlab command ÔÓÐÝ Ö: Õ ÔÓÐÝ Ö Ôµ The calculation of the roots of q(x), that is q(x) = 0, provides the location of the local minima and maxima. This takes place using the command ÖÓÓØ : Ö ÖÓÓØ Õµ Finally, the value of the local minima and maxima of p(x) are evaluated by means of the command ÔÓÐÝÚ Ð: ÔÓÐÝÚ Ð Ô Öµ 1 2 3 4 x = 0.81 y = 6.01 p(x) 5 6 7 8 x = 0.81 y = 3.91 9 2 1 0 1 2 x More complex functions can be represented by the ratio of two polynomials H(x) = p 1 (x) p 2 (x) (as is the case of the transfer function of most linear time invariant systems, for example). The derivative of H(x) results in the ratio of two polynomials q 1 (x) and q 2 (x), that is: dh(x) dx = q 1(x) q 2 (x). The polynomials q 1 (x) and q 2 (x) are calculated using the command ÔÓÐÝ Ö as follows:

Õ ½ Õ ¾ ÔÓÐÝ Ö Ô ½ Ô ¾µ Finally, the location of the local minima and maxima can be determined by calculating the roots of q 1 (x) using the command ÖÓÓØ, as it was done before. Exercise Determine the local minima and maxima of the function ( ) 2π f L/µH 2 khz R/Ω 10 3 H( f) 2 = ( ( ) ) 1 2π f 2 2 L C khz µh µf 10 6 + ( ). 2π f L/µH 2 khz R/Ω 10 3 for L = 100µH, C = 10µF and R = 10Ω. Compare this result with the resonant frequency f 0 = 1 2π LC. 3.5 Poles and zeros The poles and zeros of a function H(x) are obtained by calculating the roots of the denominator and the numerator H(x), respectively: Exercise Draw the poles and zeros diagram of H( f) 2 = H(x) = k (x x N,1)(x x N,2 ) (x x P,1 )(x x P,2 ) ( ) 2π f L/µH 2 khz R/Ω 10 3 ( ( ) ) 1 2π f 2 2 L C khz µh µf 10 6 + ( ). 2π f L/µH 2 khz R/Ω 10 3 for L = 100µH, C = 10µF and R = 10Ω.

21 CHAPTER 4 Curve fitting, interpolation and approximation of functions using Polynomials This chapter covers the basic MATLAB tools for curve fitting, interpolation and approximation of functions using polynomials. 4.1 Polynomial curve fitting The Matlab command ÔÓÐÝ Ø finds the coefficients of a polynomial that fits a set of data in a least-squares sense: Ô ÔÓÐÝ Ø Ü Ý Òµ finds the best-fit Ò-degree polynomial that approximates the data points x and y. x and y are vectors containing the Ü and Ý data to be fitted, and Ò is the degree of the polynomial to return. For example, consider the x-y test data: Ü ½ ¾ Ý º º½ ½¾ ¾ ¼º º ± Ø Ö Ö ÔÓÐÝÒÓÑ Ð Ø Ø ÔÔÖÓÜ Ñ Ø ÐÝ Ø Ø Ø Ô ÔÓÐÝ Ø Ü Ý µ Ô ¹¼º½ ½ ½º ¾½ ¹ ¼º ¾ ¾ º ¼¼ Let us now compute the values of the ÔÓÐÝ Ø estimate over a finer range, and plot the estimate over the real data values for comparison: ܾ ½ º½ ݾ ÔÓÐÝÚ Ð Ô Ü¾µ ÔÐÓØ Ü Ý ³Ó³ ܾ ݾµ Ö ÓÒ Ð Ò ³Ñ ÙÖ ÔÓ ÒØ ³ ³ÔÓÐÝ Ø Ó Ö ³µ Ø ØÐ ³ ßÈÓÐÝÒÓÑ Ð ÙÖÚ ØØ Ò Ð³µ Note: The command ÔÓÐÝÚ Ð Ô Ü¾µ evaluates the polynomial p for each element of the vector x2. For more information check Matlab help by writing at command window ÐÔ ÔÓÐÝÚ Ð. The figure on the next page shows the measured data and the curve fitting to it.

500 450 400 Polynomial Curve Fitting measured points polyfit of degree 3 350 300 y 250 200 150 100 50 0 1 1.5 2 2.5 3 3.5 4 4.5 5 x Exercise 1 Consider the following test data : Ü ¼ ½ ¾ Ý ¼ ¼º ¾ ¼º ¼º ¼º ¾ ¼º Find a fifth degree polynomial that fits the data, compute the values of the ÔÓÐÝ Ø estimate over a finer range and plot the estimate over the real data values for comparison. 4.2 Polynomial interpolation Polynomials are useful as easier-to-compute approximations of more complicated functions, via a Taylor series expansion or by a low-degree best-fit polynomial using the ÔÓÐÝ Ø function. Example: Let us approximate y = sin(x) between 0 and 2π using a polynomial of degree 5 and plot both in the same figure for comparison. ± Ü ÑÔÐ ÙÒØ ÓÒ ÔÔÖÓÜ Ñ Ø ÓÒ Ù Ò ÔÓÐÝÒÓÑ Ð ± Ø Ú Ò ÙÒØ ÓÒ Ý Ò Üµ ± Ø Ó Ø Ú ØÓ ÔÔÖÓÜ Ñ Ø Ø ÙÒØ ÓÒ ØÛ Ò ¼ Ò ¾ Ô Ù Ò ± ÔÓÐÝÒÓÑ Ð Ó Ö º ÐÓ ÐÐ

Ü ¼ ¼º½ ¾ Ô Ý Ò Üµ ÔÐÓØ Ü Ýµ ÜÐ Ð ³Ü Ö Ø ÖÖÓÛ³µ ÝÐ Ð ³Ý Ö Ø ÖÖÓÛ³µ ÓÐ ÓÒ Ô ÔÓÐÝ Ø Ü Ý µ ÜÜ ¼ ¼º½ ¾ Ô ÝÝ ÔÓÐÝÚ Ð Ô Üܵ ÔÐÓØ ÜÜ ÝÝ ³¹ Ö³µ Ð Ò ³ Ò Üµ³ ³ÔÓÐݺ Ó Ö ÔÔÖÓܺ³µ 1.5 1 Polynomial approximation of the sin(x) over one period sin(x) poly. of degree 5 approx. 0.5 y 0 0.5 1 1.5 0 1 2 3 4 5 6 7 x Exercise 2 Approximate y = sin(x) between 2π and 2π using a polynomial of degree 5 and plot both in the same figure for comparison. Now approximate y = sin(x) between 2π and 2π using a polynomial of degree 7 and plot both in the second figure for comparison. Piecewise-polynomial interpolation is typically better than a single high-degree polynomial. The following example shows the difference between them. ± ÓÑÔ Ö ÓÒ Ó Ô Û ÔÓÐÝÒÓÑ Ð Ú Ö Ù Ò Ð ¹ Ö ÔÓÐÝÒÓÑ Ð

± ÒØ ÖÔÓÐ Ø ÓÒ ± ± Ö Ø Ò º ½ Ò Ð ÔÓÐÝÒÓÑ Ð ÔÔÖÓÜ Ñ Ø ÓÒ Ó Ö ÓÖ Ö ÐÓ ÐÐ Ò ½¼ Ü ¹ ¼º½ Ý ½º» ܺ ¾ ½µ Ô ÔÓÐÝ Ø Ü Ý Òµ ÙÖ ½µ ÔÐÓØ ³½» Ü ¾ ½µ³ ¹ ³Ö ¹³µ ÓÐ ÓÒ ÜÜ ¹ ¼º¼½ ÔÐÓØ ÜÜ ÔÓÐÝÚ Ð Ô Üܵ ³ ¹³µ Ð Ò ³ ܵ ½» Ü ¾ ½µ³ ³Ë Ò Ð ÔÓÐÝÒÓÑ Ð ÔÔÖÓܺ Ó Üµ³µ Ø ØÐ ³ ßË Ò Ð ÔÓÐÝÒÓÑ Ð ÔÔÖÓÜ Ñ Ø ÓÒ Ó ÙÒØ ÓÒ Ð³µ ± ÒÓÛ Ø Ô Û ÔÓÐÝÒÓÑ Ð ÒØ ÖÔÓÐ Ø ÓÒ Ù Ò ÔÐ Ò ÓÖ ÔÐ Ò ÔÔÚ Ð ÙÖ ¾µ ÝÝ ÔÐ Ò Ü Ý Üܵ ÔÐÓØ ³½» Ü ¾ ½µ³ ¹ ³Ö ¹³µ ÓÐ ÓÒ ÔÐÓØ ÜÜ ÝÝ ³ ¹³µ Ð Ò ³ ܵ ½» Ü ¾ ½µ³ ³È Û ÔÓÐÝÒÓÑ Ð ÔÔÖÓܺ Ó Üµ³µ Ø ØÐ ³ ßÈ Û ÔÓÐÝÒÓÑ Ð ÔÔÖÓÜ Ñ Ø ÓÒ Ó ÙÒØ ÓÒ Ð³µ The result of the first plot using the single polynomial approximation is: 1.2 1 Single polynomial approximation of a function f(x)=1/(x 2 + 1) Single polynomial approx. of f(x) 0.8 0.6 0.4 0.2 0 0.2 6 4 2 0 2 4 6 From this figure it could be observed that as n increases the error in the center improves but increases near the endpoints.

The second figure, which is shown below, using the piecewise polynomial interpolation shows a better result. 1 0.9 Piecewise polynomial approximation of a function f(x)=1/(x^2 + 1} Piecewise polynomial approx. of f(x) 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 6 4 2 0 2 4 6 Exercise 3 Approximate the function y = sin 3 (x) between 2π and 2π using a single polynomial of degree 7 and plot both the original function and the approximated polynomial in the same figure for comparison. Approximate the function y = sin 3 (x) between 2π and 2π using piecewise polynomial interpolation and plot both the original function and the approximated polynomial in the same figure for comparison. 4.3 Taylor series expansion The Taylor series for an analytic function f(x) at x = a is given by: f(x) = (x a) n f(n) (a) n=0 n! (4.1) The Matlab syntax and its description about Taylor series is available from the Matlab help under Symbolic Math Toolbox. The main commands are: Ø ÝÐÓÖ µ is the fifth order Taylor polynomial approximation of f(x) at x = 0 (also known as fifth order Maclaurin polynomial). Ø ÝÐÓÖ Ü ³È Ê Å½³ Ú Ð½ ³È Ê Å¾³ Ú Ð¾ ºººµ returns the Taylor series approximation of f(x) with some optional parameters. Please have a look at possible options using the Matlab command ÐÔ Ø ÝÐÓÖ.

Note: The independent variable in the above descriptions is symbolic and hence should first be declared as ÝÑ Ü. Example The Taylor series expansion of f(x) = sin(x) at x = 0 with the order of 5 is: ÝÑ Ü Ò Üµ Ø ÝÐÓÖ Ø ÝÐÓÖ µ The Matlab answer to these commands is: Ø ÝÐÓÖ Ü»½¾¼ ¹ Ü» Ü The Taylor series expansion of f(x) = sin(x) at x = 0 up to the order of 7 is: ÝÑ Ü Ò Üµ ØÖ Ø ÝÐÓÖ ³ÇÖ Ö³ µ The Matlab answer to these commands is: ØÖ ¹ Ü» ¼ ¼ Ü»½¾¼ ¹ Ü» Ü Now we plot the function sin(x) and the Taylor series obtained above for comparison. ÔÐÓØ ³ Ò Üµ³ ¹¾ Ô ¾ Ô ¹½ ½ µ ÓÐ ÓÒ ÔÐÓØ ³¹ Ü» ¼ ¼ Ü»½¾¼ ¹ Ü» ܳ ¹¾ Ô ¾ Ô ¹½º ½º ³Ö ¹¹³µ Observe from the figure below how well the polynomial obtained through Taylor series approximates the function for small values of x ( 3 x 3). Exercise 4 Determine the Taylor series expansion of the following functions at a = 1 with the order of 9 and plot both the function and the corresponding Taylor series in the same figure as the above example. exp(x)

cosh(x) sinh(x) 1.5 1 Taylor series of a sin function sin(x) x 7 /5040 + x 5 /120 x 3 /6 + x 0.5 0 0.5 1 1.5 6 4 2 0 2 4 6 Taylor series approximation using the Matlab command Ø ÝÐÓÖØÓÓÐ Syntax Ø ÝÐÓÖØÓÓÐ Ø ÝÐÓÖØÓÓÐ ³ ³µ Description Ø ÝÐÓÖØÓÓÐ initiates a GUI that graphs a function against the N th partial sum of its Taylor series about a base point x = a. The default function, value of N, base point, and interval of computation for Ø ÝÐÓÖØÓÓÐ are f = x cos(x), N = 7, a = 0, and [ 2π, 2π], respectively. Ø ÝÐÓÖØÓÓÐ ³ ³µ initiates the GUI for the given expression f. Example Ø ÝÐÓÖØÓÓÐ ³ ÜÔ Ü Ò Üµµ³µ, this command opens the GUI depicted below:

Taylor Series Approximation 12 10 8 6 4 2 0 6 4 2 0 2 4 6 T N (x) = x 6 /120 + x 4 /3 + x 2 + 1 Exercise 5 Now use the command Ø ÝÐÓÖØÓÓÐ, like the example above, and determine the Taylor series approximations at x = 0 for the following functions: tan(x) sin(x) cosh(x) sinh(x) sin(tan(x)) tan(sin(x))

29 CHAPTER 5 Vectors and matrices This chapter covers basic operations on vectors and matrices in the MATLAB environment. 5.1 Creating matrices The MATLAB environment uses the term matrix to indicate a variable containing real or complex numbers arranged in a two-dimensional grid. An array is, more generally, a vector, matrix, or higher-dimensional grid of numbers. All arrays in MATLAB are rectangular, in the sense that the component vectors along any dimension are all the same length. MATLAB has a number of functions that create different kinds of matrices. The functions shown in the table below create matrices for more general use. Function ÓÒ Þ ÖÓ Ý Ñ Ö Ò Ö Ò Ò Description Create a matrix or array of all ones. Create a matrix or array of all zeros. Create a matrix with ones on the diagonal and zeros elsewhere. Create a diagonal matrix from a vector. Create a square matrix with rows, columns, and diagonals that add up to the same number. Create a matrix or array of uniformly distributed random numbers. Create a matrix or array of normally distributed random numbers and arrays. For more information check ÐÔ Ñ Ø ÙÒ. Examples Here are some examples of how you can use these functions. Creating a Random Matrix. The Ö Ò function creates a matrix or array with elements uniformly distributed between zero and one. This example multiplies each element by 20:

Ö Ò µ ¾¼ ½ º¾ ½º ¼ º½ ¾ ¾º ½ º½½ ½ º½½ º ¼¼ ½ º ½½ º ¾ ¼º ½ ¾ ¾º ½¼º ½ º½ ½ º ½ ½ º ¾ ½ º¾ ½ º½ ¼½ º ¼ ½ º ½ ½ º ½¾º ¾ ½ º¾ ½ º¼¼ ½ º½ ½ º Creating a diagonal matrix. Use to create a diagonal matrix from a vector. You can place the vector along the main diagonal of the matrix, or on a diagonal that is above or below the main one, as shown here. The -1 input places the vector one row below the main diagonal: ½ ¾ ½ ½ ¾ ½ µ Ò ½ ¼ ¼ ¼ ¼ ¼ ¾ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ½ ¹½µ Ò ¼ ¼ ¼ ¼ ¼ ¼ ½ ¼ ¼ ¼ ¼ ¼ ¼ ¾ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ¼ ½ ¼ Creating a magic square matrix. A magic square is a matrix in which the sum of the elements in each column, or each row, or each main diagonal is the same. To create a

5-by-5 magic square matrix, use the Ñ function as shown. Ñ µ ½ ¾ ½ ½ ¾ ½ ½ ½ ¾¼ ¾¾ ½¼ ½¾ ½ ¾½ ½½ ½ ¾ ¾ Note that the elements of each row, each column, and each main diagonal add up to the same value: 65. Concatenating matrices Matrix concatenation is the process of joining one or more matrices to make a new matrix. The brackets operator which we have used often serves not only as a matrix constructor, but also as the MATLAB concatenation operator. The expression horizontally concatenates matrices and. The expression vertically concatenates them. ÓÒ ¾ µ ± ¾¹ ݹ Ñ ØÖ Ü Ó ³ Ö Ò µ ± Ú ÖØ ÐÐÝ ÓÒ Ø Ò Ø Ò º¼¼¼¼ º¼¼¼¼ º¼¼¼¼ º¼¼¼¼ º¼¼¼¼ º¼¼¼¼ º¼¼¼¼ º¼¼¼¼ º¼¼¼¼ º¼¼¼¼ ¼º ¼º ¼º¼ ½ ¼º¼ ½ ¼º ½ ½ ¼º ½ ¼º½ ½¾ ¼º¾ ¼º ¾ ¼º ¼¾ ¼º ¾¾ ¼º ¼ ¼ ¼º¼ ¾ ¼º ¼º¼ Accessing single elements We have already discussed in the previous sessions how to reference single elements of a matrix. The syntax is: ÖÓÛ ÓÐÙÑÒµ, where is a variable matrix. Linear indexing You can refer to the elements of a MATLAB matrix with a single subscript, µ. MAT- LAB stores matrices and arrays not in the shape that they appear when displayed in the MATLAB command window, but as a single column of elements. This single column is composed of all of the columns from the matrix, each appended to the last. So, matrix ¾ ¾ ½

¾ ¾ ½ is actually stored in memory as the sequence ¾ ¾ ½ The element at row 3, column 2 of matrix A (value = 5) can also be identified as element 6 in the actual storage sequence. To access this element, you have a choice of using the standard ¾µ syntax, or you can use µ, which is referred to as linear indexing. Accessing multiple elements For the 4-by-4 matrix A shown below, it is possible to compute the sum of the elements in the fourth column of A by typing Ñ µ ½ µ ¾ µ µ µ Ò You can reduce the size of this expression using the colon operator. Subscript expressions involving colons refer to portions of a matrix. The expression ½ Ñ Òµ refers to the elements in rows 1 through m of column n of matrix A. Using this notation, you can compute the sum of the fourth column of A more succinctly: ÙÑ ½ µµ Nonconsecutive elements To refer to nonconsecutive elements in a matrix, use the colon operator with a step value. The Ñ Ò in this expression means to make the assignment to every third element in the matrix. Note that this example uses linear indexing: ½ ½ µ ¹½¼ ¹½¼ ¾ ¹½¼ ½½ ¹½¼ ¹½¼ ½¾ ¹½¼ ½ ½ ¹½¼

MATLAB supports a type of array indexing that uses one array as the index into another array. You can base this type of indexing on either the values or the positions of elements in the indexing array. Here is an example of value-based indexing where array B indexes into elements 1, 3, 6, 7, and 10 of array A. In this case, the numeric values of array B designate the intended elements of A: ¼ ½¼ ½ ¾¼ ¾ ¼ ¼ ¼ ½ ½¼ µ Ò ½ ¼ ¼ The end keyword MATLAB provides the keyword end to designate the last element in a particular dimension of an array. This keyword can be useful in instances where your program does not know how many rows or columns there are in a matrix. You can replace the expression in the previous example with ½ Ò µ ¹½¼ Specifying all elements of a row or column The colon by itself refers to all the elements in a row or column of a matrix. Using the following syntax, you can compute the sum of all elements in the second column of a 4-by-4 magic square A: Ñ µ ÙÑ ¾µµ Ò By using the colon with linear indexing, you can refer to all elements in the entire matrix. The command µ displays all the elements of matrix A, returning them in a column-wise order. Check it! Using logicals in array indexing A logical array index designates the elements of an array A based on their position in the indexing array, B, not their value. In this masking type of operation, every

true element in the indexing array is treated as a positional index into the array being accessed. In the following example, B is a matrix of logical ones and zeros. The position of these elements in B determines which elements of A are designated by the expression µ: ½ ¾ ½ ¾ ÐÓ Ð ¼ ½ ¼ ½ ¼ ½ ¼ ¼ ½ µ µ Ò ¾ The Ò function can be useful with logical arrays as it returns the linear indices of nonzero elements in B, and thus helps to interpret µ: Ò µ Ò ¾

5.2 Inverses and determinants If A and B are square matrices, each of order n n, which satisfy the equations AB = BA = I n then B is called the inverse of A. We write B = A 1. Since the definition is symmetric, A is the inverse of B, that is A = B 1. Let us consider a general 2 2 matrix, and determine the inverse matrix and also look for the conditions for the existance of the inverse matrix. Ax = d, where Thus [ ] a11 a A = 12, x = a 21 a 22 [ x1 x 2 ], d = [ d1 d 2 ] a 11 x 1 + a 12 x 2 = d 1 a 12 x 1 + a 22 x 2 = d 2. These are linear equations in the unknowns x 1 and x 2. x 1 and x 2 are given by, provided that a 11 a 22 a 21 a 12 = 0, or x 1 = a 22d 1 a 12 d 2 a 11 a 22 a 21 a 12, x 2 = a 21d 1 + a 11 d 2 a 11 a 22 a 21 a 12 x = 1 a 11 a 22 a 21 a 12 [ a22 a 12 a 21 + a 11 ][ d1 The number a 11 a 22 a 21 a 12 is known as the determinant of the matrix A, det(a). [ ] Ax = d x = A 1 d A 1 1 a22 a = 12 det(a) a 21 a 11 The Matlab command to find the determinant of the square matrix A is Ø µ and the inverse of A can be obtained using the command ÒÚ µ if it exists, i.e. if A is non-singular. If A is square and nonsingular, then, without roundoff error, ÒÚ µ is theoretically the same as and ÒÚ µ is theoretically the same as». But the computations involving the backslash and slash operators are preferable because they require less computer time, less memory, and have better error-detection properties. d 2 ].

Exercise 5.2.1 a) Decide whether A, where A = [ ] 1 3 1 4 is singular or not. If it is non-singular, find its inverse. b) Given are the matrices: A = Find A 1, B 1,(AB) 1, and B 1 A 1. c) Find A 1, where [ ] [ ] 1 3 1 2, B = 1 4 1 1 2 1 0 A = 1 1 5. 1 1 2 Find the determinants of A T, A and A 1. d) Given that A = [ ] 1 0 1 3 0, B = 2 1, C = 2 1 1 1 1,. 2 1 1 1, 0 1 verify the distributive law A(B+C) = AB+AC for the three matrices. e) Given are the matrices: A = [ ] 4 2, B = 2 1 Find the inverse of (BA) and(ab) if they exist. f) Given is the matrix: 2 1 3 A = 1 1 2. 2 1 1 [ ] 2 1 4 2 Find a matrix C such that A + C is the identity matrix I 3, i. e. 3 3 identity matrix. Determine AC, CA and A 2 + C 2. Check whether AC = CA. g) For a general n n matrix it could be shown that A+A T is a symmetric matrix, and A A T is a skew-symmetric matrix. Now, express the matrix: 2 1 3 A = 2 0 1 3 1 2 as the sum of a symmetric matrix and a skew-symmetric matrix..

Pseudoinverses Rectangular matrices do not have inverses or determinants. At least one of the equations AX = I and XA = I does not have a solution. A partial replacement for the inverse is provided by the Moore-Penrose pseudoinverse, which is computed by the Ô ÒÚ function. The Matlab syntax is: Ô ÒÚ µ The Moore-Penrose pseudoinverse is a matrix B of the same dimensions as A satisfying the four conditions: A B A = A B A B = B A B is Hermitian B A is Hermitian Example Ö Ò ³ Ø Ø ³ ¼µ Ö Ò ¾µ ¼º ¼½ ¼º¾ ½½ ¼º ¼ ¼º ¼ ¼º ½ ¼º ¾½ Ô ÒÚ µ ½º½ ¹¼º ½½ ¼º½ ¼ ¹¼º ¼¾¾ ½º¼ ¾ ¼º Ò ¼º ¹¼º½ ¾ ¼º ½¾ ¹¼º½ ¾ ¼º ¼ ¼º ¼ ¼º ½¾ ¼º ¼ ¼º ¼ µ³ Ò

¼º ¹¼º½ ¾ ¼º ½¾ ¹¼º½ ¾ ¼º ¼ ¼º ¼ ¼º ½¾ ¼º ¼ ¼º ¼ Ò ½º¼¼¼¼ ¼º¼¼¼¼ ¹¼º¼¼¼¼ ½º¼¼¼¼ µ³ Ò ½º¼¼¼¼ ¹¼º¼¼¼¼ ¼º¼¼¼¼ ½º¼¼¼¼ B A is the 2-by-2 identity matrix, but A B is not the 3-by-3 identity matrix. However, A B acts like an identity on a portion of the space in the sense that A B is symmetric, A B A is equal to A, and B A B is equal to B. Exercise 5.2.2 Now A is given by ½¼ Ö Ò ¾ µ. Determine the Moore-Penrose pseudoinverse B, proof the four conditions described earlier. 5.3 Vector norms and the scalar product in Matlab The p-norm of a vector x ( ) 1/p x p = x i p is computed by ÒÓÖÑ Ü Ôµ. This is defined by any value of p > 1, but the most common values of p are 1, 2 and. The default value is p = 2, which corresponds to Euclidean length: Example Ú ¾ ¼ ¹½ ÒÓÖÑ Ú ½µ ÒÓÖÑ Úµ ÒÓÖÑ Ú Ò µ Ò º¼¼¼¼ ¾º¾ ½ ¾º¼¼¼¼

Exercise 5.3.1 Compute the 1 st, 2 nd, 3 rd, 10 th and the infinity norms of the following vectors: Û ½ ½ ½ Ü ¾ ¾ ½ Ý ½ ½ ½¼ Þ ¹½ ¹½ ½ The scalar product of two vectors Suppose that two vectors a and b are given in component form in 3D as: The scalar product is defined as: a = (a 1, a 2, a 3 ) and b = (b 1, b 2, b 3 ) a b = a 1 b 1 + a 2 b 2 + a 3 b 3 The Matlab equivalent to this is element-by-element multiplication of two vectors of the same size and summed up. Exercise 5.3.2 Let a be a vector on x z plane with values( 1, 0, 1) and b = (2, 3, 2). Determine the scalar product a b. Check the results of (a b) (a b) and a 2 b 2. 5.4 Matlab functions that work on diagonals of a matrix Function ØÖ ØÖ Ð ØÖ Ù Description Compute the sum of the elements on the main diagonal. Return the diagonals of a matrix. Return the lower triangular part of a matrix. Return the upper triangular part of a matrix. Exercise 5.4 The Matlab command Ô Ð µ creates a 3 3 symmetric matrix. Now, create 4 4 symmetric matrix using the function Ô Ð and then determine: The lower triangular part of this matrix. The upper triangular part of this matrix. A vector with the main diagnoal elements of this matrix. The sum of the elements on the main diagonal.

40 CHAPTER 6 Systems of linear equations 6.1 Linear system, coefficient matrix, augmented matrix A linear system of equations in n unknowns x 1, x 2,, x n is a set of m equations of the form a 11 x 1 +...+ a 1n x n = b 1 a 21 x 1 +...+ a 2n x n = b 2.. a m1 x 1 +...+a mn x n = b m. Thus, a system of two equations in three unknown is a 11 x 1 +a 12 x 2 +a 13 x 3 = b 1 a 21 x 1 +a 22 x 2 +a 23 x 3 = b 2. The a jk are given numbers, which are called the coefficients of the system. If the b i are all zero, then (6.1) is called a homogeneous system. If at least one b i is not zero, then (6.1) is called a nonhomogeneous system. A solution of (6.1) is a set of numbers x 1,..., x n that satisfies all the m equations. A solution vector of (6.1) is a vector x whose components constitute a solution of (6.1). Matrix form of the linear system (6.1). The m equations of (6.1) can be written as a single vector equation Ax = b, (6.1) where the coefficient matrix A = [a jk ] is the m n matrix a 11 a 12 a x 1 1n a 21 a 22 a 2n A =......, x = a m1 a m2 a mn x n b 1 and b = b n

are column vectors. It is assumed that A is not a zero matrix, which means not all the coefficients a jk are zero. The augmented matrix à of (6.1) is a 11 a 12 a 1n b 1 à = [ A b ] a 21 a 22 a 2n b 2 =....... a m1 a m2 a mn b n The method of solving such a system by determinants (Cramer s rule) is not practical, but the Gauss elimination method is often used and briefly discussed below. 6.2 Solutions of linear systems: Existence and uniqueness The necessary and sufficient conditions for the existence of solutions and for the uniqueness can be given using the rank of the coefficient matrix A and Ã. (a) Existence A linear system of m equations in n unknowns (6.1) has solutions if and only if the coefficient matrix A and the augmented matrix à have the same rank. (b) Uniqueness The system (6.1) has precisely one solution if and only if this common rank r of A and à equals n. (c) Infinitely many solutions If this rank r is less than n, the system has infinitely many solutions. All of these are obtained by determining r suitable unknowns in terms of the remaining n r unknowns, to which arbitrary values can be assigned. (d) Gauss elimination If solutions exist, they can be obtained by the Gauss elimination. A homogeneous linear system has always the trivial solution x 1 = 0,..., x n = 0. Nontrivial solutions exist if and only if rank A < n. Exercise 6a Determine the existence of the solutions to the following systems of linear equations and proof also the uniqueness of the solutions. 1. 2. 2x 1 + 2x 2 + 4x 3 = 2 4x 1 + 5x 2 +13x 3 = 7 10x 1 +14x 2 +43x 3 = 25 3x 1 4x 2 = 2 x 1 +5x 2 = 4 5x 1 +2x 2 = 12

3. 2x 1 +3x 2 +2x 3 = 2 x 1 x 2 3x 3 = 5 3x 1 +5x 2 +5x 3 = 3 4. x 1 + x 2 + x 3 = 1 x 1 2x 2 + x 3 = 2 x 1 x 2 + 5x 3 = 0 5. x 1 +x 2 + x 3 + 3x 4 =0 2x 2 + 2x 4 =5 x 1 x 2 2x 3 2x 4 =4 2x 1 +4x 2 + 2x 3 +8x 4 =5 6.3 Numerical methods in linear algebra Gauss elimination This standard method for solving linear systems (6.1) is a systematic process of elimination that reduces (6.1) to a triangular form because the system can easily solved by back substitution. For instance, a triangular system is 3x 1 + 5x 2 + 2x 3 = 8 8x 2 + 2x 3 = 7. 6x 3 = 3 LU-factorization A is now assumed to be an n n square matrix. Below three related methods are briefly discussed that are modifications of the Gauss elimination. They are named after Doolittle, Crout, and Cholesky and use the idea of LU-factorization, which will be explained first. An LU-factorization of a given square matrix A is of the form A = LU, (6.2) where L is a lower triangular and U is an upper triangular matrix. For example, [ ] [ ][ ] 2 3 1 0 2 3 A = = LU =. 8 5 4 1 0 7

It could be proved that for any nonsingular matrix (interested reader can refer to section 6 of Advanced engineering mathematics by E. Kreyszig), the rows can be reordered so that the resulting matrix A has an LU-factorization (6.3) in which U turns out to be the triangular system at the end of the Gauss elimination and L is the matrix of the multipliers m jk of the Gauss elimination, with diagonal (1,..., 1). The main idea here is that L and U in (6.3) can be computed directly, without solving the simultaneous equations thus, without using the Gauss elimination. Once we have (6.3), Ax = b can be solved in two steps, simply by noting that Ax = LUx = b can be written as: Ly = b, (6.3a) where Ux = y (6.3b) and solving first (6.4a) for y and then (6.4b) for x. This is called Doolittle s method. Both systems (6.4a) and (6.4b) are triangular, so their solution can be found by back substitution in the Gauss elimination method. A similar method, Crout s method, is obtained from (6.3) if U (instead of L) is required to have main diagonal (1,..., 1). In either case the factorization (6.3) is unique. Example 6.2.1 A system is given by a matrix A and the vector b as follows: 3 5 2 A = 0 8 2 b = [ 8 7 3 ]. 6 2 8 Solve the system using Doolittle s method (LU-factorization with main diagonal elements of L equals 1). Solution: 3 5 2 1 0 0 u 11 u 12 u 13 A = 0 8 2 = l 21 1 0 0 u 22 u 23 6 2 8 l 31 l 32 1 0 0 u 33 u 11 = 3, l 21 u 11 = 0 l 21 = 0,... First solve for Ly = b: 3 5 2 1 0 0 3 5 2 A = 0 8 2 = 0 1 0 0 8 2. 6 2 8 2 1 1 0 0 6 1 0 0 y 1 8 8 0 1 0 y 2 = 7 y = 7. 2 1 1 y 3 3 20

Now solve Ux = y: 3 5 2 x 1 8 119/24 0 8 2 x 2 = 7 x = 1/24. 0 0 6 x 3 20 20/6 Cholesky s method For a symmetric, positive definite matrix A (thus A = A T, x T Ax > 0 for all x = 0), U (sometimes also called R) in (6.3) can even be choose R = L T, thus u jk = l k j but impose no conditions on the main diagonal entries. The popular method of solving Ax = b based on this factorization A = LL T is called Cholesky s method. If A is symmetric but not positive definite, this method could still be applied, but then leads to a complex matrix L, so that it becomes impractical. Example 6.2.2 Solve by Cholesky s method: 4x 1 + 2x 2 + 14x 3 = 14 2x 1 + 17x 2 5x 3 = 101 14x 1 5x 2 + 83x 3 = 155 Solution: 4 2 14 l 11 0 0 l 11 l 21 l 31 A = 2 17 5 = l 21 l 22 0 0 l 22 l 32 14 5 83 l 31 l 32 l 33 0 0 l 33 l 11 = a 11 = 2 l 21 = a 21 = 2 l 11 2 = 1 l 31 = a 31 = 14 l 11 2 = 7... 4 2 14 2 0 0 2 1 7 A = 2 17 5 = 1 4 0 0 4 3. 14 5 83 7 3 5 0 0 5 By solving first Ly = b, that is: 2 0 0 y 1 1 4 0 y 2 = 7 3 5 y 3 14 101 y = 155 7 27 5 Now solve L T x = y: 2 1 7 x 1 7 3 0 4 3 x 2 = 27 x = 6. 0 0 5 x 3 5 1.