Seminar to the lecture Computer-based Engineering Mathematics ...

9 downloads 51 Views 847KB Size Report
10. 2.1.3. Plotting rapidly changing mathematical functions . . . . . . . . . 11. 2.2 Three-Dimensional ..... 16 12 8 32 24 16. 6 12 18 12 24 36 .... Line styles, markers and colors may be selected for a graph with a string argument to plot, e.g. plot(x, y ...
Seminar to the lecture Computer-based Engineering Mathematics

N T S Prof. Dr.-Ing. A. Czylwik

Amanuel Geda, M.Sc. Room: BA 240, Tel:49(0)203 379-4408, e-Mail: [email protected] Department 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

Handling Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

1.3.1

Entering Matrices and Addressing the 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

Skalar-Array Mathematics . . . . . . . . . . . . . . . . . . . . . .

6

1.3.7

Array-Array Mathematics . . . . . . . . . . . . . . . . . . . . . .

7

1.3

2 Basic Plotting using Matlab 2.1

2.2

Two-Dimensional Graphics . . . . . . . . . . . . . . . . . . . . . . . . . .

9

2.1.1

Multiple plots on the same axis . . . . . . . . . . . . . . . . . . .

10

2.1.2

Line styles, markers and color . . . . . . . . . . . . . . . . . . . .

10

2.1.3

Plotting rapidly changing mathematical functions . . . . . . . . .

11

Three-Dimensional graphics . . . . . . . . . . . . . . . . . . . . . . . . .

11

2.2.1

Curve plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11

2.2.2

Mesh and surface plots . . . . . . . . . . . . . . . . . . . . . . .

12

2.2.3

Multiple plots in a figure: subplot . . . . . . . . . . . . . . . . .

13

3 Functions 3.1

9

Domain, Range and Symmetries . . . . . . . . . . . . . . . . . . . . . . .

15 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 Factorizations

. .

45

6.5

Matlab solutions for Systems of Linear Equations . . . . . . . . . . . . .

47

7 Eigenvalues and Eigenvectors

61

7.1

Eigenvalues, Eigenvectors . . . . . . . . . . . . . . . . . . . . . . . . . . .

61

7.2

MATLAB methods to determine eigenvalues and eigenvectors . . . . . .

63

7.3

Linear independence of eigenvectors

. . . . . . . . . . . . . . . . . . . .

65

7.4

Orthonormal set, Symmetric matrices . . . . . . . . . . . . . . . . . . . .

68

7.5

Diagonalization of a matrix, canonical form

. . . . . . . . . . . . . . . .

70

7.6

Cayley-Hamilton theorem . . . . . . . . . . . . . . . . . . . . . . . . . .

72

8 Nonlinear Equations

73

8.1

Polynomial roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

73

8.2

Solving real function using fzero . . . . . . . . . . . . . . . . . . . . . . .

74

8.3

Solving Equations using Symbolic Toolbox . . . . . . . . . . . . . . . . .

79

8.4

System of nonlinear equations . . . . . . . . . . . . . . . . . . . . . . . .

81

9 Differentiation

86

9.1

Polynomial derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . .

86

9.2

Approximate derivative

. . . . . . . . . . . . . . . . . . . . . . . . . . .

88

9.3

Symbolic derivative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

90

10 Integration 10.1 Introduction to Numerical Integration

92 . . . . . . . . . . . . . . . . . . .

92

10.2 Numerical integration using Matlab . . . . . . . . . . . . . . . . . . . . .

93

10.3 Symbolic Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

98

11 Ordinary Differential Equations

100

11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

100

11.2 Numerical methods to solve initial value problems (IVP) of ODEs . . . .

102

11.3 Solving ODEs in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . .

104

11.4 Systems of First order ODEs . . . . . . . . . . . . . . . . . . . . . . . . .

106

11.5 Homogeneous Linear Systems of ODEs . . . . . . . . . . . . . . . . . . .

109

1

Chapter 1

Introduction to Matlab

1.1

What is Matlab?

Matlab1 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 n x m. Column vectors an row vectors are represented consistently by n x 1 and 1 x n matrices, respectively. Matlab operations can be classified into the followng types of operations: • arithmetic and logical operations, • mathematical functions, • graphical functons, 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 >> books = 10 creates a 1-by-1 matrix named books 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. A and a 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 i or j as a suffix. Some examples of legal numbers are: 7 -55 0.0041 9.657838 6.10220e-10 7.03352e21 2i -2.71828j 2e3i 2.5+1.7j

1.2.3

Operators

Expressions use familar arithmetic operators and precedence rules. Some examples are:

+ * / ’ ()

1.2.4

Addition Subtraction Multiplication Division Complex conjugate transpose Brackets to specify the evaluation order.

Functions

Matlab provides a large number of standard elementary mathematical functions, including sin, sqrt, exp and abs. Taking the square root or logarithm of a negative number does not lead to an error; the appropriate komplex 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 >> help elfun Some of the functions, like sqrt and sin 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 gamma, 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 vektors are matrices with only one row or column. Matlab has other ways to store both numeric and nonnumeric 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 the 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.: >> A = [1 2 3; 8 6 4; 3 6 9] After pressing "‘Enter"’ Matlab displays the numbers entered in the command line

>> A =

1 2 3 8 6 4 3 6 9

Addressing an element of a matrix is also very easy. The n-th element of the m-th column in matrix A from above is A(n,m). So typing >> A(1,3) + A(2,1) + A(3,2) will compute the answer ans = 17 The k-th to 1-th elements of the m-th to n-th columns can be addressed by A(k:1,m:n),e.g. >> A(2:3,1:2) ans = 8 6 3 6 Further exaples: >> A(1,1:2) addresses the first two elements of the first row. ans = 1 2 >> A(:,2) addresses all elements of the second column. ans = 8 6 4

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:

>> x = linspace(0, pi, 101) or >> x = (0:0.01:1)*pi In the first case, the Matlab function linspace is used to create x. The function’s arguments are described by: linspace(first_value, last_value, number_of_values) with the default number_of_values = 100. In the second case, the colon notation (0:0.01:1) 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 x. Both of these array creation forms are common in Matlab. While the colon notation form allws to specify the increment between data elements directly, but not the number of data elements, the Matlab function linspace 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. (first_value:increment:last_value) creates an array starting at first_value, ending at last_value with an increment which can be negative as well, e.g. >> v = (10:-2:0) v =

10

8

6

4

2

0

If the increment is 1, then its usage is optional: >> w = (5:10) w =

5

6

7

8

9

10

Matlab also provides four functions that generate basic matrices: zeros, ones, rand and randn. 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 A, and form

>> F = [A A+10; A*2 A*4]. The result is an 6-by-6 matrix, obtained by joining the four submatrices. F = 1 2 3 8 6 4 3 6 9 2 4 6 16 12 8 6 12 18

1.3.4

11 18 13 4 32 12

12 16 16 8 24 24

13 14 19 12 16 36

Deleting rows and columns

To delete rows or columns of a matrix, just use a pair of square brackets, e.g. >> A(2,:) = [] deletes the second row of A. A =

1 3

2 6

3 9

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 ’: >> a = 0:3 a =

0

1

2

3

>> b = a’ b =

0 1 2 3

1.3.6

Skalar-Array Mathematics

Addition, subtraction, multiplication and division by a scalar apply the operation to all elements of the array:

>> c = [1 2 3 4; 5 6 7 8; 9 10 11 12] c = 1 2 3 4 5 6 7 8 9 10 11 12 >> 2*c-1 multiplies each element in c by two and subtracts one from each element of the result. ans =

1 3 5 7 9 11 13 15 17 19 21 23

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. >> d = [1 2 3; 4 5 6] d = 1 4

2 5

3 6

>> e = [2 2 2; 3 3 3] e =

2 3

2 3

2 3

>> f = d+e % adds d to e on an element-by-elenemt basis f = 3 7

4 8

5 9

>> g = 2*d-e % multiplies d by two and subtracts e from the result g = 0 5

2 7

4 9

Element-by-element multiplication an division work similary, but the notation is slightly different: >> h = d.*e h =

2 4 6 12 15 18

The element-by-element multiplication uses the dot multiplication symbol .*, the elementby-element array division uses either ./ or .\ >> d./e ans = 0.500 1.333

1.000 1.666

1.500 2.000

1.000 1.666

1.500 2.000

>> e.\d ans = 0.500 1.333

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. >> C = A * B Therefore the number of columns of A must be equal the number of rows of B. >> A = [1 2 3; 4 5 6] A =

1 4

2 5

3 6

>> B = [1 2; 3 4; 5 6] B =

1 3 5

2 4 6

>> C = A * B C =

22 49

28 64

9

Chapter 2

Basic Plotting using Matlab

2.1

Two-Dimensional Graphics

Graphs (in 2-D) are drawn with the plot statement. In its simplest form, it takes a single vector argument y as in plot(y). In this case the elements of y are plotted against their indexes, e.g. plot(rand(1, 20)) 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 plot([0 4], [1 3]) 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 plot(x,y) opens a graphics window and draws the elements of y versus the elements of x. For example, x = -4 : 0.01 : 4 ; y = sin(x) ; plot(x, y) 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 sin(x) 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(−x2 ) and x varies from -10 to 10. • Plot y versus t for y = sin(2πf t), t varies from 0 to 1, for f = 2 and f = 5. • Plot y versus t for y = cos(2πf t), t varies from 0 to 1, for f = 2 and f = 5.

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. plot(x1, y1, x2, y2, x3, y3, ... ) 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, x = -2*pi : 0.01 : 2*pi ; plot(x,cos(x),x,sin(x)) %Equivalent to this command is: plot(x,cos(x)) hold on plot(x,sin(x),’-r’) hold off Exercise 3 Draw x2 , x3 , x4 , exp(−x2 ) on the same figure using both methods as described above. Add legend using Matlab command legend(’x^2,x^3,x^4,exp(-x^2)’)

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. plot(x, y, ’--’) joins the plotted points with dashed lines, whereas plot(x, y, ’o’)

draws circles at the data points with no lines joining them. You can specify all three properties, e.g. plot(x,sin(x), x, cos(x), ’--mo’) 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, x = 0.01:.001:.1; plot(x, sin(1./x)) MATLAB has a function called fplot which uses a more elegant approach. Whereas the above method evaluates sin(1/x) at equally spaced intervals, fplot evaluates it more frequently over regions where it changes more rapidly. Here is how to use it: fplot(’sin(1/x)’,[0.01 0.1]) % 1./x not needed! Exercise 4 Now draw the functions from exercise 3 using fplot. Try also using ezplot. For more information check the MATLAB help by typing at command window help ezplot.

2.2

Three-Dimensional graphics

MATLAB’s primary commands for creating three dimensional graphics of numericallydefined functions are plot3, mesh, surf, and surfl.

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 plot3(x,y,z) 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,

t = 0.01 : 0.01 : 20*pi ; x = cos(t) ; y = sin(t) ; z = t.^3 ; plot3(x, y, z) %produces a helix that is compressed near the x-y plane

2.2.2

Mesh and surface plots

The mesh command draws three-dimensional wire mesh surface plots.Similarly, threedimensional faceted surface plots are drawn with the command surf. The following MATLAB M-File generate a mexican hut using the mesh command.

% Mexican hut as an example for 3D plot. [x y] = meshgrid (-8: 0.5 : 8) ; r = sqrt(x.^2 + y.^2) ; z = sin(r)./r ; mesh(x,y,z) title(’Mexican hut’) Mexican hut

1

0.5

0

−0.5 10 5

10 5

0

0

−5

−5 −10

−10

The following script shows how to graph the function f (x, y) = x − 1 + y 2

(2.1)

(x − 1)2 + (y − 3)2 = 4

(2.2)

over the disk

% Example for 3D Plot from the book ’A Matlab comapanion for a multivariable calculus’ % by J. Cooper, University of Maryland % First make a meshgrid in r, theta coordinates r = linspace(0,2,21); theta = linspace(0, 2*pi, 41);

[R,TH] = meshgrid(r,theta) % Now convert into a curvilinear meshgrid in x,y % coordinates X = 1 + R.*cos(TH); Y = 3 + R.*sin(TH); Z = X-1 + Y.^2; surf(X,Y,Z) hold on % add the plane z = -5 with the curvilinear % meshgrid surf(X,Y,-5+0*Z) hold off 2

2

2

Plot of f(x,y) = x−1 +y over the disk (x−1) + (y−3) = 4

30

z axis

20

10

0

−10 5 4

3 2

3

1

2 y axis

0 1

−1

x axis

Exercise 5 Plot a Gaussian probability density function fx,y (x, y) with zero mean and variance 1 within the range x ∈ [−5, 5], y ∈ [−5, 5] : fx,y (x, y) =

1 − 1 (x2 +y2 ) e 2 2π

(2.3)

Try out the commands mesh, surf and contour to plot the graph! Exercise 6 Plot the function g(r, θ) = (r/2) sin(θ) + r3 cos(3θ)

(2.4)

over the unit disk, centered at the origin.

2.2.3

Multiple plots in a figure: subplot

You can show a number of plots in the same figure window with the subplot function. The statement subplot(m,n,p) divides the figure window into m X n small sets of axes, and selects the pth 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 mesh in subplot(2,2,1), surf in subplot(2,2,2), surfl in subplot(2,2,3) and contour in subplot(2,2,4). Give title to each plot as for instance title(’Plot of Gaussian pdf using mesh’)

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) = q 1− g(x) = q

x2 4

1

|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 xlabel(’x \rightarrow’) ; ylabel(’f(x) , g(x) \rightarrow’)

3.2

Transformation of Coordinates

Consider the function g(x) drawn in the interval −5 ≤ x ≤ 5: clc; clear;close all x = -5 : .05 : 5 ; g_x = 1 ./ sqrt( abs(1 - 0.25*x.^2 ) ) ; plot( x , g_x , ’-r.’ ) hold on;

A simple coordinate transformation is obtained by shifting g(x) to g2 (x) = g(x − x0 ). For x0 = 1: x_0 = 1; g2_x = 1 ./ sqrt( abs(1 - (x-x_0).^2) ) ; g2_x = 1 ./ sqrt( abs(1 - .25*(x-x_0).^2) ) ; plot( x , g2_x , ’-g’) hold off; Coordinate transformation 5

g(x) g(x−1)

4.5 4

g(x), g2(x) →

3.5 3 2.5 2 1.5 1 0.5 0 −5

−4

−3

−2

−1

0

x→

1

2

3

4

5

Exercise • Draw on the same figure the coordinate transformation g3 (x) = g(ax) for a = 2 (scaling of the x-axis). • Draw on the same figure the coordinate transformation g4 (x) = g(ax−x0 ) for a = 2 and x0 = 1. • Draw a new figure with the non-linear coordinate transformation g5 (x) = g(x2 ).

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:

H(ω) =

jωL/R Uo (ω) = . Ui (ω) 1 − ω 2 LC + jωL/R

R

Ui (ω)

L

C

Uo (ω)

The squared magnitude of the RLC filter using the technical frequency f is given by

|H(f )|2 = 

(2πf L/R)2 . 2 1 − (2πf )2 LC + [2πf L/R]2

Expressing: f in kHz, L in mH, C in mF, R in Ω results in: 2 f L/mH 2π kHz R/Ω |H(f )|2 = h i i2 . h  2 L C 2 f L/mH f 1 − 2π kHz mH mF + 2π kHz R/Ω 

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 f (x + ∆x) − f (x) = Lim =0 dx ∆x→0 ∆x A graphical approximation of the derivate can be calculated in Matlab using the diff command. For a vector y, diff(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 ≤ π:

clc;clear;close all Delta = 0.01; x = -pi : Delta : pi; y = x .* cos( x ); plot(x,y); grid on;hold on plot( x(1:end-1) , diff(y)./diff(x) , ’r’) xlabel(’x \rightarrow’) ylabel(’f(x) \rightarrow’) title(’Approximate derivation’) legend(’y = xcos(x)’, ’dy/dx’) Approximate derivation 4

y = xcos(x) dy/dx

3 2

Local Maximum

f(x) →

1 0 −1

Local Minimum

−2 −3 −4 −4

−2

0 x→

2

4

Notice that the command diff( y ) 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 x(1:end-1). Exercise • Determine the local minima and maxima of the function 2  f L/mH 2π kHz R/Ω |H(f )|2 = h i i2 2 L C 2 h f f L/mH 1 − 2π kHz + 2π mH mF kHz R/Ω

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) = x3 − 2x − 5 is represented by: p = [ 1 0 -2 -5 ]; The derivative q(x) =

dp(x) dx

is calculated using the Matlab command polyder:

q = polyder( p ); 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 roots: r = roots( q ); Finally, the value of the local minima and maxima of p(x) are evaluated by means of the command polyval: polyval( p , r );

−1 −2 −3

x = 0.81 y = −6.01

p(x) →

−4 −5 −6 −7

x = −0.81 y = −3.91

−8 −9 −2

−1

0 x→

1

2

(x) More complex functions can be represented by the ratio of two polynomials H(x) = pp21 (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 q1 (x) and q2 (x), that is:

dH(x) q1 (x) = . dx q2 (x) The polynomials q1 (x) and q2 (x) are evaluated using the command polyder as follows:

[ q_1 , q_2 ] = polyder( p_1 , p_2 ); Finally, the location of the local minima and maxima can be determined by calculating the roots of q1 (ω) using the command roots, as it was done before. Exercise • Determine the local minima and maxima of the function  2 f L/mH 2π kHz R/Ω |H(f )|2 = h i2 2 L C i2 h f L/mH f + 2π 1 − 2π kHz mH mF kHz R/Ω for L = 100µH, C = 10µF and R = 10Ω. Compare this result with the resonant frequency f = 2π√1LC .

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: H(x) = k

(x − XN,1 ) (x − XN,2 ) · · · (x − XP,1 ) (x − XP,2 ) · · ·

Exercise • Draw the poles and zeros diagram of 2  f L/mH 2π kHz R/Ω |H(f )|2 = h i i2 2 L C 2 h f L/mH f + 2π 1 − 2π kHz mH mF kHz R/Ω 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 polyfit finds the coefficients of a polynomial that fits a set of data in a least-squares sense: p = polyfit(x, y, n) finds the best-fit n-degree polynomial that approximates the data points x and y. x and y are vectors containing the x and y data to be fitted, and n is the degree of the polynomial to return. For example, consider the x-y test data: x = [1 2 3 4 5]; y = [5.5 43.1 128 290.7 498.4]; %A third degree polynomial that approximately fits the data is : p = polyfit(x,y,3) p = -0.1917 31.5821 -60.3262 35.3400 Let us now compute the values of the polyfit estimate over a finer range, and plot the estimate over the real data values for comparison: x2 = 1:.1:5; y2 = polyval(p,x2); plot(x,y,’o’,x2,y2) grid on legend(’measured points’,’polyfit of degree 3 ’) title(’\bf{Polynomial Curve Fitting}’) Note: The command polyval(p,x2) evaluates the polynomial p for each element of the vector x2. For more information check Matlab help by writting at command window help polyval The figure on the next page shows the measured data and the curve fitting to it.

Polynomial Curve Fitting 500 measured points polyfit of degree 3

450 400 350

y→

300 250 200 150 100 50 0

1

1.5

2

2.5

3 x→

3.5

4

4.5

5

Exercise 1 Consider the following test data : x = [0 1 2 3 4 5] y = [0 0.632 0.865 0.95 0.982 0.993] Find a fifth degree polynomial that fits the data, compute the values of the polyfit 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 polyfit function. Example Let’s approximate y = sin(x) between 0 and 2π using a polynomial of degree 5 and plot both on the same figure for comaprison. % % % %

Example: function approxiamtion using polynomial the given function is y = sin(x) the objective is to approximate the function b/n 0 and 2*pi using poly nomial of degree 5.

close all x = 0:0.1:2*pi ;

y = sin(x); plot(x,y) xlabel(’x \rightarrow’) ylabel(’y \rightarrow’) hold on p = polyfit(x,y,5) xx = 0: 0.1: 2*pi ; yy = polyval(p,xx); plot(xx,yy,’-sr’) legend(’sin(x)’, ’poly. of degree 5 approx.’)

Polynomial approximation of the sin(x) over one period 1.5 sin(x) poly. of degree 5 approx. 1

y→

0.5

0

−0.5

−1

−1.5

0

1

2

3

x→

4

5

6

7

Exercise 2 • Approximate y = sin(x) between −2π and 2π using a polynomial of degree 5 and plot both on the same figure for comaprison. • Now approximate y = sin(x) between −2π and 2π using a polynomial of degree 7 and plot both on the second figure for comaprison. Piecewise-polynomial interpolation is typically better than a single high-degree polynomial. The following example shows the difference between them. % comparison of piecewise-polynomial versus single high degree polynomial

%interpolations % first in fig1 single polynomial approximation of higher order close all n = 10; x = -5:0.1:5 ; y = 1./(x.^2 + 1) ; p = polyfit(x,y,n) figure(1) fplot(’1/(x^2 + 1)’, [-7 7], ’rs-’) hold on xx = -5:0.01:5 ; plot(xx,polyval(p,xx),’g*-’) legend(’f(x)=1/(x^2 + 1)’, ’Single polynomial approx. of f(x)’) title(’\bf{Single polynomial approximation of a function }’) % Now the piecewise polynomial interpolation using spline or spline + ppval figure(2) yy = spline(x,y,xx) ; fplot(’1/(x^2 + 1)’, [-7 7], ’rs-’) hold on plot(xx,yy,’g*-’) legend(’f(x)=1/(x^2 + 1)’, ’Piecewise-polynomial approx. of f(x)’) title(’\bf{Piecewise-polynomial approximation of a function }’) The result of the first plot using the single polynomial approximation is : Single polynomial approximation of a function 1.2 f(x)=1/(x2 + 1) Single polynomial approx. of f(x)

1

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. Piecewise−polynomial approximation of a function 1 f(x)=1/(x^2 + 1} Piecewise−polynomial approx. of f(x)

0.9 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 a) Approximate the function y = sin3 (x) between −2π and 2π using a single polynomial of degree 7 and plot both the original function and the approximated polynomial on the same figure for comaprison. b) Approximate the function y = sin3 (x) between −2π and 2π using piecewise-polynomial interpolation and plot both the original function and the approximated polynomial on the same figure for comparison.

4.3

Taylor series expansion

The Taylor series for an analytic function f (x) about the base point x = a is given by : f (x) =

∞ X n=0

(x − a)n .

f (n) (a) 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 : taylor(f) is the fifth order Maclaurin polynomial approximation to f (x). taylor(f,n,v) returns the (n-1)-order Maclaurin polynomial approximation to f (x), where f is a symbolic expression representing a function and v specifies the independent variable in the expression. v can be a string or symbolic variable.

taylor(f,n,v,a) returns the Taylor series approximation to f (x) about a. The argument a can be a numeric value, a symbol, or a string representing a numeric value or an unknown. You can supply the arguments n, v, and a in any order. taylor determines the purpose of the arguments from their position and type. You can also omit any of the arguments n,v, and a. If you do not specify v, taylor uses findsym to determine the function’s independent variable.The default value to n is 6. Note: The independent variable in the above descriptions is symbolic and hence should first be declared as syms x. Example The taylor series expansion of f (x) = sin(x) at x = 0 up to the order of 5 is : syms x f = sin(x) f_taylor = taylor(f) Matlab answer to these commands is : f_taylor = x^5/120 - x^3/6 + x The taylor series expansion of f (x) = sin(x) at around x = 0 up to the order of 7 is : syms x f = sin(x) f_tr_7 = taylor(f,8) Matlab answer to these commands is : f_tr_7 = x^5/120 - x^7/5040 - x^3/6 + x We now plot the function sin(x) and the taylor series obtained above for comparison. fplot(’sin(x)’, [-2*pi 2*pi -1 1]) hold on fplot(’x^5/120 - x^7/5040 - x^3/6 + x’, [-2*pi 2*pi -1.5 1.5],’rs--’) Observe from the figure below how well the polynomial obtained through taylor series approximates the function for small values of x (−3 ≤ x ≤ 3).

Taylor series of a sin function 1.5 sin(x) 7

5

3

− x /5040 + x /120 − x /6 + x 1

0.5

0

−0.5

−1

−1.5

−6

−4

−2

0

2

4

6

Exercise 4 Determine the taylor series expansion of the following functions at around 0 up to the order of 9 and plot both the function and the approximate taylor series on the same figure like the above example. • exp(x) • cosh(x) • sinh(x) Taylor series approximation using the Matlab command taylortool Syntax taylortool taylortool(’f’) Description taylortool 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 taylortool are f = x · cos(x) , N = 7, a = 0, and [−2π, 2π], respectively. taylortool(’f’) initiates the GUI for the given expression f . Example taylortool(’exp(x*sin(x))’) , this command opens the following GUI:

Taylor Series Approximation 12 10 8 6 4 2 0 −6

−4

−2

0

2

4

6

TN(x) = x6/120 + x4/3 + x2 + 1

Exercise 5 Now use the command taylortool, like the example above, and determine the taylor series approximations near the origin 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 the basic opration 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 ones zeros eye diag magic rand randn

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 help matfun. Examples Here are some examples of how you can use these functions. Creating a Random Matrix. The rand function creates a matrix or array with elements uniformly distributed between zero and one. This example multiplies each element by 20:

A = rand(5)*20 A = 16.2945 18.1158 2.5397 18.2675 12.6472

1.9508 5.5700 10.9376 19.1501 19.2978

3.1523 19.4119 19.1433 9.7075 16.0056

2.8377 8.4352 18.3147 15.8441 19.1898

13.1148 0.7142 16.9826 18.6799 13.5747

Creating a Diagonal Matrix. Use diag 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: >> A = [1 2 3 4 1] A = 1

2

3

4

1

0 2 0 0 0

0 0 3 0 0

0 0 0 4 0

0 0 0 0 1

0 0 0 3 0 0

0 0 0 0 4 0

0 0 0 0 0 1

>> diag(A) ans = 1 0 0 0 0

>> diag(A,-1) ans = 0 1 0 0 0 0

0 0 2 0 0 0

0 0 0 0 0 0

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 magic function as shown. >> A = magic(5) A = 17 23 4 10 11

24 5 6 12 18

1 7 13 19 25

8 14 20 21 2

15 16 22 3 9

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 C = [A B] horizontally concatenates matrices A and B. The expression C = [A; B] vertically concatenates them. >> A = ones(2, 5) * 6; % 2-by-5 matrix of 6’s >> B = rand(3, 5); >> C = [A; B] % Vertically concatenate A and B C = 6.0000 6.0000 0.7577 0.7431 0.3922

6.0000 6.0000 0.6555 0.1712 0.7060

6.0000 6.0000 0.0318 0.2769 0.0462

6.0000 6.0000 0.0971 0.8235 0.6948

6.0000 6.0000 0.3171 0.9502 0.0344

Accessing Single Elements We have already discussed in the previous sessions how to reference single elements of a matrix. The syntax is : A(row, column), where A is a variable matrix. Linear Indexing You can refer to the elements of a MATLAB matrix with a single subscript, A(k). MATLAB 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 A >> A = [2 6 9; 4 2 8; 3 5 1]

A = 2 4 3

6 2 5

9 8 1

is actually stored in memory as the sequence 2, 4, 3, 6, 2, 5, 9, 8, 1 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 A(3,2) syntax, or you can use A(6), 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 >> A = magic(4); >> A(1,4) + A(2,4) + A(3,4) + A(4,4) ans = 34 You can reduce the size of this expression using the colon operator. Subscript expressions involving colons refer to portions of a matrix. The expression A(1:m, n) 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: sum(A(1:4, 4)) Nonconsecutive Elements To refer to nonconsecutive elements in a matrix, use the colon operator with a step value. The m:3:n in this expression means to make the assignment to every third element in the matrix. Note that this example uses linear indexing:

>> B = A; >> B(1:3:16) = -10 B = -10 5

2 11

3 -10

-10 8

9 -10

-10 14

6 15

12 -10

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: >> A = 5:5:50 A = 5

10

15

20

25

35

50

30

35

40

45

50

>> B = [1 3 6 7 10]; >> A(B) ans = 5

15

30

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 B(1:3:end) = -10 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: >> A = magic(4); >> sum(A(:,2)) ans = 34 By using the colon with linear indexing, you can refer to all elements in the entire matrix. The command A(:) displays all the elements of matrix A, returning them in a columnwise 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 A(B): >> A = [1 2 3; 4 5 6; 7 8 9] A = 1 4 7

2 5 8

3 6 9

>> B = logical([0 1 0; 1 0 1; 0 0 1]); >> A(B) ans = 4 2 6 9 The find function can be useful with logical arrays as it returns the linear indices of nonzero elements in B, and thus helps to interpret A(B): >> find(B) ans = 2 4 8 9

5.2

Inverses and Determinants

If A and B are square matrices, each of order n × n, which satisfy the equations AB = BA = In 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       d x1 a11 a12 , d= 1 , x= A= d2 x2 a21 a22 Thus a11 x1 + a12 x2 = d1 a21 x1 + a22 x2 = d2 These are linear equations in the unknowns x1 and x2 . x1 and x2 are given by, provided that a11 a22 − a21 a12 6= 0, x1 =

a22 d1 − a12 d2 , a11 a22 − a21 a12

x2 =

−a21 d1 + a11 d2 a11 a22 − a21 a12

   1 a22 − a12 d1 x= −a + a d2 a11 a22 − a21 a12 21 11 The number a11 a22 − a21 a12 is known as the determinat of the matrix A, det(A).   1 a22 −a12 −1 −1 Ax = d ⇔ x = A d ⇒ A = det(A) −a21 a11

The Matlab command to find the determinant of the square matrix A is det(A) and the inverse of A can be obtained using the command inv(A) if it exists, i.e. if A is non-singular. If A is square and nonsingular, then, without roundoff error, X = inv(A)*d is theoretically the same as X = A\d and Y = d*inv(A) is theoretically the same as Y = d/A. 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. Exercise 5.2.1 A) Decide whether A, where   1 3 , A= −1 4

is singular or not. If it is non-singular, find its inverse. B)Given  :   are the matrices 1 2 1 3 , B= A= 1 −1 −1 4 −1 −1 −1 Find A , B , (AB) , and B−1 A−1 .  2 1 0 C) Find A−1 , where A =  1 −1 5 −1 −1 2 Find the determinants of AT , A and A−1 

D) Given that   1 3 0 , A= 2 1 1

 1 0 B =  2 1 , −1 −1 



2  C = −1 0

 1 1 1

verify the distributive law A(B + C) = AB + AC for the three matrices. E)Given are the matrices :   4 2 , A= 2 1

  −2 −1 B= 4 2

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 

Find a matrix C such that A + C is the identity matrix I3 , i.e. 3 × 3 identity matrix. Determine AC, CA and A2 + C2 . Check whether AC = CA. G)For a general n × n matrix it could be shown that A + AT is a symmetric matrix, and A − AT 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 pinv function. The Matalab syntax is: B = pinv(A) 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 >> rand(’state’, 0); >> A = rand(3,2) A = 0.9501 0.2311 0.6068

0.4860 0.8913 0.7621

>> B = pinv(A) B = 1.1459 -0.5022

-0.7411 1.0729

0.1360 0.3776

-0.1827 0.7850 0.3680

0.3127 0.3680 0.3703

-0.1827

0.3127

>> A*B ans = 0.8447 -0.1827 0.3127 >> (A*B)’ ans = 0.8447

-0.1827 0.3127

0.7850 0.3680

0.3680 0.3703

>> B*A ans = 1.0000 0.0000

-0.0000 1.0000

>> (B*A)’ ans = 1.0000 -0.0000

0.0000 1.0000

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 Let now A is given by : A = 10*rand(2,3) 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 kxkp =

X

p

|xi |

1/p

is computed by norm(x,p). 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 >> v = [2 0 -1]; >> [norm(v,1) norm(v) norm(v,inf)] ans = 3.0000

2.2361

2.0000

Exercise 5.3.1 Compute the 1st , 2nd , 3rd , 10th and the infinity norms of the following vectors :

  • w= 1 1 1   • x= 2 2 1   • y = 1 1 10   • z = −1 −1 1 The scalar product of two vectors Suppose that two vectors a and b are given in component form in 3D as : a = (a1 , a2 , a3 ) and b = (b1 , b2 , b3 ) The scalar product is defined as : a · b = a1 b 1 + a2 b 2 + a3 b 3 The Matlab equivalent to this is element by element multiplication of two vectors of the same size. 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 trace diag tril triu

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 pascal(3) creates a 3 × 3 symmetric matrix. Now, create 4 × 4 symmetric matrix using the function pascal 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 x1 , x2 , · · · , xn is a set of equations of the form a11 x1 + · · · + a1n xn = b1 a21 x1 + · · · + a2n xn = b2 .. .. . . am1 x1 + · · · + amn xn = bm

(6.1)

Thus, a system of two equations in three unknown is a11 x1 + a12 x2 + a13 x3 = b1 a21 x1 + a22 x2 + a23 x3 = b2 The ajk are given numbers, which are called the coefficients of the system. If the bi are all zero , then (6.1) is called a homogeneous system.If at least one bi is not zero, then (6.1) is called a nonhomogeneous system. A solution of (6.1) is a set of numbers x1 , · · · , xn that satisfies all the m equations. A solution vector of (6.1) is a verctor x whose components constitute a solution of (6.1). If the system is homogeneous, it has at least the trivial solution x1 = 0, · · · , xn = 0. 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.2) where the coefficient matrix A = [ajk ] is the m × n matrix 

a11 a12  a21 a22  A =  .. ..  . . am1 am2



· · · a1n · · · a2n   , . . . ..  .  · · · amn

 x1 ·    x= · · xn 

and

  b1 ·    b= · · bn

are column verctors. It is assumed that A is not a zero matrix, which means not all the ˜ of (6.1) is coefficients ajk are zero.The augumented matrix A 

a11 a12 a21 a22    ˜ = A b = A  .. ..  . . am1 am2

· · · a1n · · · a2n . . . .. . · · · amn

 b1 b2   ..  .

bn

The method of solving such a system by determinants(Cramer’s rule) is not practical, but the Gauss elimination methos is often used and discussed brifely below.

6.2

Solutions of Linear Systems: Existence and Uniqueness

The necessary and sufficient conditions for the existence of solutions and for the unique˜ ness can be given using the rank of the coefficient matrix A and A. (a) Existence A linear system of m equations in n unknowns(6.1) has solutions if and only if the coef˜ have the same rank. ficient matrix A and the augmented matrix A (b) Uniqueness ˜ The system(6.1) has precisely one solution if and only if this common rank r of A and A 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 determinig 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 solutionx1 = 0, · · · , xn = 0. Nontrivial solutions exist if and only if rank A < n. Exercise 6a Determine the existance of the solutions to the following systems of linear equations and proof also the uniqueness of the solutions 1. 2x1 + 2x2 + 4x3 = −2 4x1 + 5x2 +13x3 = −7 10x1 +14x2 +43x3 = −25

2. 3x1 −4x2 = −2 −x1 +5x2 = 4 5x1 +2x2 = 12 3. 2x1 +3x2 + 2x3 = 2 −x1 − x2 − 3x3 = −5 3x1 +5x2 + 5x3 = 3 4. x1 + x2 + x3 = 1 −x1 −2x2 + x3 = 2 x1 − x2 + 5x3 = 0 5. x1 +x2 + x3 + 3x4 2x2 + 2x4 −x1 −x2 − 2x3 − 2x4 2x1 +4x2 + 2x3 +8x4

6.3

=0 =5 =4 =5

Numerical methods in Linear Algebra

Gauss Elimlination This standard method for solving linear systems (6.1) is a systematic process of elimination that reduces (6.1) to ”triangular form” because the sytem can easily solved by ”back substitution.” For instance, a triangular system is 3x1 + 5x2 + 2x3 = 8 8x2 + 2x3 = −7 6x3 = 3 LU-Factorization A is now assumed to be a square matrix, n × n. Below is briefly discussed three related methods 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 expalined first. An LU-factorization of a given square matrix A is of the form A = LU

(6.3)

where L is lower triangular and U For example,  2 A= 8

is upper triangular matrices.     1 0 2 3 3 = LU = 4 1 0 −7 5

It could be proved that for any nonsingular matrix (interested reader can refer to Sec.6 of advanced engineering mathematics by Kreyszig,E.)the rows can be reordered so that the resulting matrix A has an LU-factrorization (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 mjk 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.4a)

where (6.4b)

Ux = y

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 is the same as back substitution in the Gausss elimination. 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 6 2 8

  b = 8 −7 3

Solve the system using Doolittle’s method (LU factorization with main diagonal elements of L are 1 ). Solution.      1 0 0 u11 u12 u13 3 5 2 A = 0 8 2 = m21 1 0  0 u22 u23  0 0 u33 m31 m32 1 6 2 8 ⇒ u11 = 3,

m21 u11 = 0 ⇔ m21 = 0,

     1 0 0 3 5 2 3 5 2 A = 0 8 2 = 0 1 0 0 8 2 2 −1 1 0 0 6 6 2 8

···

First solve for Ly = b,    1 0 0 y1 0 1 0 y2  2 −1 1 y3

 8 ⇒ y = −7 3 

Now solve Ux = y    3 5 2 x1 0 8 2 x2  0 0 6 x3

 8 ⇒ x = −7 3 

Cholesky’s Method For a symmetric, positive definite matrix A(thus A = AT , xT Ax > 0 for all x 6= 0) U(sometimes also called R) in (3.6) can even be choose R = LT , thus ujk = mkj but impose no conditions on the main diagonal entries. The popular method of solving Ax = b based on this factorization A = LLT 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: 4x1 + 2x2 + 14x3 = 14 2x1 + 17x2 − 5x3 = −101 14x1 − 5x2 + 83x3 = 155 Solution.     l11 l21 l31 l11 0 0 4 2 14 A =  2 17 −5 = l21 l22 0   0 l22 l32  0 0 l33 l31 l32 l33 14 −5 83 

⇒ l11 =



2 a21 = =1 l11 2    2 0 4 2 14 A =  2 17 −5 = 1 4 7 −3 14 −5 83

a11 = 2

l21 =

By solving first Ly = b, that  2 1 7

is,

l31 =  0 2 0 0 5 0

a31 14 =7 = l11 2  1 7 4 −3 0 5

  0 y1 0 y2  5 y3

 7 ⇒ y = −27 5

   x1 2 1 7 0 4 −3 x2  x3 0 0 5

 3 ⇒ x = −6 1

0 4 −3



Now solve LT x = y 

···

6.4

Matlab syntax and descriptions for LU- and Cholesky Factorizations

LU matrix factorization Syntax [L,U] = lu(A) [L,U,P] = lu(A) Description The lu function expresses a matrix A as the product of two essentially triangular matrices, one of them a permutation of a lower triangular matrix and the other an upper triangular matrix. The factorization is often called the LU, or sometimes the LR, factorization. A can be rectangular. For a full matrix A, lu uses the Linear Algebra Package (LAPACK) routines. [L,U] = lu(A) returns an upper triangular matrix in U and a permuted lower triangular matrix in L such that A = L*U. Return value L is a product of lower triangular and permutation matrices. [L,U,P] = lu(A) returns an upper triangular matrix in U, a lower triangular matrix L with a unit diagonal, and a permutation matrix P, such that L*U = P*A. The statement lu(A,’matrix’) returns identical output values.

Exercise 6b Find the L and U matrices of the LU-factorization of the system matrix A which are needed to solve the following linear systems. (see Example 6.2.1, Sec. 6.2) 1. 4x1 + 5x2 = 7 12x1 +14x2 = 18 2. 2x1 + 2x2 +4x3 = −2 4x1 + 5x2 +13x3 = −7 10x1 +14x2 +43x3 = −25

3. 5x1 +9x2 +2x3 =24 9x1 +4x2 +x3 =25 2x1 + x2 +x3 =11 chol Cholesky factorization Syntax R = chol(A) L = chol(A,’lower’) [R,p] = chol(A) [L,p] = chol(A,’lower’) Description R = chol(A) produces an upper triangular matrix R from the diagonal and upper triangle of matrix A,satisfying the equation R’*R=A. The lower triangle is assumed to be the (complex conjugate) transpose of the upper triangle. Matrix A must be positive definite; otherwise, MATLAB software displays an error message. L = chol(A,’lower’) produces a lower triangular matrix L from the diagonal and lower triangle of matrix A, satisfying the equation L*L’=A. When A is sparse, this syntax of chol is typically faster. Matrix A must be positive definite; otherwise MATLAB displays an error message. [R,p] = chol(A) for positive definite A, produces an upper triangular matrix R from the diagonal and upper triangle of matrix A, satisfying the equation R’*R=A and p is zero. If A is not positive definite, then p is a positive integer and MATLAB does not generate an error. When A is full, R is an upper triangular matrix of order q=p-1 such that R’*R=A(1:q,1:q). When A is sparse, R is an upper triangular matrix of size q-by-n so that the L-shaped region of the first q rows and first q columns of R’*R agree with those of A. [L,p] = chol(A,’lower’) for positive definite A, produces a lower triangular matrix L from the diagonal and lower triangle of matrix A, satisfying the equation L*L’=A and p is zero. If A is not positive definite, then p is a positive integer and MATLAB does not generate an error. When A is full, L is a lower triangular matrix of order q=p-1 such that L*L’=A(1:q,1:q). When A is sparse, L is a lower triangular matrix of size q-by-n so that the L-shaped region of the first q rows and first q columns of L*L’ agree with those of A.

Exercise 6c Find the LT and L matrices of the Cholsky-factorization of the system matrix A which are needed to solve the following linear systems using Cholsky’s method. Proof first the conditions A = AT (symmetric) and xT Ax > 0 (Positive definite) 1. 4x1 + 6x2 +8x3 =0 6x1 +34x2 +52x3 = − 160 8x1 +52x2 +129x3 = − 452 2. 4x1 +2x2 +4x3 =10 2x1 +2x2 +3x3 + 2x4 =18 4x1 +2x2 +6x3 + 3x4 =30 2x2 +3x3 + 9x4 =61 3. x1 − 2x2 +3x3 + 2x4 −x1 + 5x2 −5x3 − 2x4 3x1 −5x2 +19x3 + 3x4 2x1 −2x2 +3x3 + 21x4

6.5

= 15 =−35 = 94 = 1

Matlab solutions for Systems of Linear Equations

Computational Considerations One of the most important problems in technical computing is the solution of systems of simultaneous linear equations. It is instructive to consider a 1-by-1 example. For example, does the equation 7x = 21 has a unique solution ? The answer, of course, is yes. The equation has the unique solution x = 3. The solution is easily obtained by division: x = 21/7 = 3 The solution is not ordinarily obtained by computing the inverse of 7, that is 7−1 = 0.142857..., and then multiplying 7−1 1 by 21. This would be more work and, if 7−1 is represented to a finite number of digits, less accurate. Similar considerations apply to

sets of linear equations with more than one unknown; the MATLAB software solves such equations without computing the inverse of the matrix. Although it is not standard mathematical notation, MATLAB uses the division terminology familiar in the scalar case to describe the solution of a general system of simultaneous equations. The two division symbols, slash, /, and backslash, \, correspond to the two MATLAB functions mldivide and mrdivide. mldivide and mrdivide are used for the two situations where the unknown matrix appears on the left or right of the coefficient matrix: x = A\b returns the solution to the matrix equation Ax = b x = A/b returns the solution to the matrix equation xA = b The coefficient matrix A need not be square. If A is m × n, there are three cases: m=n Square system. Seek an exact solution. m>n Overdetermined system. Find a least squares solution. m N and b is a column vector with M components, or a matrix with several such columns, then x = A\b is the solution in the least squares sense to the under- or overdetermined system of equations Ax = b. The effective rank, r, of A is determined from the QR decomposition with pivoting. A solution x is computed which has at most r nonzero components per column. If r < N this will usually not be the same solution as pinv(A)*B. A\eye(size(A)) produces a generalized inverse of A. Square Matrices If A is symmetric and has real, positive diagonal elements, MATLAB attempts a Cholesky factorization. If the Cholesky factorization fails, MATLAB performs a symmetric, indefinite factorization. If A is upper Hessenberg, MATLAB uses Gaussian elimination to

reduce the system to a triangular matrix. If A is square but is neither permuted triangular, symmetric and positive definite, or Hessenberg, MATLAB performs a general triangular factorization using LU factorization with partial pivoting . General Solution The general solution to a system of linear equations AX = b describes all possible solutions. You can find the general solution by: 1 Solving the corresponding homogeneous system AX = 0. Do this using the null command, by typing null(A). This returns a basis for the solution space to AX = 0. Any solution is a linear combination of basis vectors. 2 Finding a particular solution to the nonhomogeneous system AX = b. You can then write any solution to AX = b as the sum of the particular solution to AX = b, from step 2, plus a linear combination of the basis vectors from step 1. The rest of this section describes how to use MATLAB to find a particular solution to AX = b, as in step 2. Square Systems The most common situation involves a square coefficient matrix A and a single right-hand side column vector b. Nonsingular Coefficient Matrix A If the matrix A is nonsingular, the solution, x = A\b, is then the same size as b. For example: >> A = pascal(3)

% 3 by 3 symmetric matrix

A = 1 1 1

1 2 3

1 3 6

>> b = [3 1 4]’ % column vector of dim. 3 b = 3 1 4 >> r1 = rank(A)

% A is 3 by 3 i.e., n = 3

r1 = 3 >> r2 = rank([A b]) % r2 is the rank of the Augmented (’Erweiterte’) matrix r2 = 3 >> % rank(A)=rank([A b])= n, hence we expect a unique solution >> x = A\b % this is the solution to the sytem eqn: Ax = b x = 10 -12 5 >> % Proof Ax is indeed equal to b >> Ax = A*x Ax = 3 1 4 >> % A*x is indeed equal to b and x is therefore the unique solution to Ax = b. Exercise 6d Now, following the above example, determine the existance of a unique solution (i.e. precisely one exact solution) and find it if it exists. 1. 2x1 + 2x2 + 4x3 = −2 4x1 + 5x2 + 13x3 = −7 10x1 +14x2 + 43x3 = −25 2. 2x1 +3x2 + 2x3 = 2 −x1 − x2 − 3x3 = −5 3x1 +5x2 + 5x3 = 3

3. 4x1 +2x2 +4x3 =10 2x1 +2x2 +3x3 + 2x4 =18 4x1 +2x2 +6x3 + 3x4 =30 2x2 +3x3 + 9x4 =61 4. x1 − 2x2 +3x3 + 2x4 −x1 + 5x2 −5x3 − 2x4 3x1 −5x2 +19x3 + 3x4 2x1 −2x2 +3x3 + 21x4

= 15 =−35 = 94 = 1

If A and B are square and the same size, X = A\B is also that size: Example : >> A = pascal(3); >> B = magic(3) B = 8 3 4

1 5 9

6 7 2

-3 4 0

-1 13 -6

>> X = A\B X = 19 -17 6

>> % proof that A*X = B >> AX = A*X AX = 8 3 4

1 5 9

6 7 2

>> Proof = AX == B

Proof = 1 1 1

1 1 1

1 1 1

>> % yes indeed AX = B This example has exact, integer solution. This is because the coefficient matrix was chosen to be pascal(3), which has a determinant equal to 1. Singular Coefficient Matrix A A square matrix A is singular if it does not have linearly independent columns. If A is singular, the solution to AX = B either does not exist, or is not unique. The backslash operator, A\B, issues a warning if A is nearly singular and raises an error condition if it detects exact singularity. If A is singular and AX = b has a solution, you can find a particular solution that is not unique, by typing x = pinv(A)*b If AX = b does not have an exact solution, pinv(A) returns a least-squares solution. Example

>> A = [1 3 7 ; -1 4 4 ; 1 10 18] A = 1 -1 1

3 4 10

7 4 18

>> detA = det(A) %singularity is proofed using det(A) detA = 0 >> % A is singular Exact Solutions. For b =[5;2;12], the equation Ax = b has an exact solution, the Matlab steps are :

>> b =[5;2;12] b = 5 2 12 >> r1= rank(A) % existance of solution is proofed by checking the condition: is rank(A) equal to rank([A b]) r1 = 2 >> r2= rank([A b]) r2 = 2 >> % therefore the system has a solution but as r = rank(A)=rank([A b]) is less than n(n=3) no unique solution ! the pinv(A)*b returns exact but not unique solution >> x = pinv(A)*b x = 0.3850 -0.1103 0.7066 >> % proof Ax = b >> Ax = A*x Ax = 5.0000 2.0000 12.0000 >> % indeed Ax = b

Least-Squares Solutions. Now, let b = [3;6;0], AX = b does not have an exact solution. Matlab steps :

>> b = [3 ; 6 ; 0] b = 3 6 0 >> r2 = rank([A b]) r2 = 3 >> r1 = rank(A) r1 = 2 >> % As r1 is different from r2, there is no exact solution, but pinv(A)*b returns a least square solution >> x = pinv(A)*b x = -1.0892 1.2512 -0.5235 >> % It can be seen that A*x doesn’t return the exact b >> Ax = A*x Ax = -1.0000 4.0000 2.0000 Another alternative way using Matlab to determine whether AX = b has an exact solution is by finding the row reduced echelon form of the augmented matrix [A b]. To do so for this example, enter : >> rref([A b]) ans =

1.0000 0 0

0 1.0000 0

2.2857 1.5714 0

0 0 1.0000

Since the bottom row contains all zeros except for the last entry, the equation does not have a solution. In this case, pinv(A) returns a least-squares solution. Exercise 6e By following the examples above, determine the unique solution if it exists using mldivide or the exact solutions if it exists or the least-square solutions using the methods discussed above. 1. x1 +x2 + x3 + 3x4 2x2 + 2x4 −x1 −x2 − 2x3 − 2x4 2x1 +4x2 + 2x3 +8x4 2.

3.



1 −2 A= −1 2

1 2 −1 4

1 −2 −2 2

=0 =5 =4 =5

  0 5  b= 4 5

 3 2 , −2 8

2x1 +5x2 − 3x3 =0 4x1 −4x2 + x3 =0 4x1 −2x2 =0 4.

 1 4 A= 1 0

−2 1 −2 −1

0 1 1 4

 −1 1 , 3 4

  0 0  b= 0 0

Solutions to Exercise 6e 1. >> %solution to No 1 of Exercise 6e >> A = [1 1 1 3 ; 0 2 0 2 ; -1 -1 -2 -2; 2 4 2 8] A = 1 0 -1 2

1 2 -1 4

1 0 -2 2

3 2 -2 8

>> b = [0 5 4 5]’ b = 0 5 4 5 >> % check rank(A)==rank(A b) and whether r == n, here n = 4 >> r1 = rank(A) r1 = 3 >> r2 = rank([A b]) r2 = 3 >> % there is exact but not unique solution and can be solved using pinv(A)*b >> x = pinv(A)*b x = -1.2500 1.5833 -3.0833 0.9167

>> % now proof Ax is exactly equal to b >> Ax = A*x Ax = -0.0000 5.0000 4.0000 5.0000 >> % indeed Ax gives back b >> % now let’s do using the row reduced echelon form rref([A b]) Gauss_e = rref([A b]) Gauss_e = 1.0000 0 0 0

0 1.0000 0 0

0 0 1.0000 0

3.0000 1.0000 -1.0000 0

1.5000 2.5000 -4.0000 0

>> % x3 - x4 = -4 ; choose x4 = 1, x3 = -3, x2 = 2.5-x4 = 1.5, x1= 1.5-3 = -1.5 >> % this set of solution is just one solution, and now proof Ax gives back b >> A*[-1.5;1.5;-3;1] ans = 0 5 4 5 Indeed it is correct. 2. Here observe between the A matrix here and the one in the above question >> A = [1 1 1 3; -2 2 -2 2 ; -1 -1 -2 -2; 2 4 2 8] A = 1 -2 -1 2

1 2 -1 4

>> r1 = rank(A)

1 -2 -2 2

3 2 -2 8

r1 = 4 >> b = [0 5 4 5]’ b = 0 5 4 5 >> r2 = rank([A b]) r2 = 4 r1 = r2 = 4 = n , therfore there is unique and exact solution. Therfore, we use the mldivide Algorithm, i.e., x = A\b >> x = A\b x = 5.2500 3.7500 -5.2500 -1.2500 % Now we proof A*x is indeed equal to b >> A*x ans = 0 5.0000 4.0000 5.0000

3. The homogeneous system >> A = [2 5 -3; 4 -4 1; 4 -2 0] A =

2 4 4

5 -4 -2

-3 1 0

>> b = [0 0 0]’ b = 0 0 0 >> if >> >>

% this is a homogeneous eqn and therfore rank(A)=rank([A b]), rank(A)=n, then there is only exact trivial solution. % on the other hand if rank(A) is less than n, there are infinitely many solutions r1 = rank(A)

r1 = 2 >> % r1 is less than n (n=3), therefore pinv(A)*b returns a soln which could be the trivial solution. >> % but as discussed in section 6.5, we can use the better command for homogeneous eqn: x = null(A) >> x = null(A) x = -0.2182 -0.4364 -0.8729 >> % proof now A*x is indeed equal to b >> Ax = A*x Ax = 1.0e-014 * -0.1332 0.0333 -0.0111 >> % This is infact can be considerd as [0 0 0]’; it is just computational round off error, 1e-14 >> % using the pinv(A)*b

>> x_p = pinv(A)*b x_p = 0 0 0 >> % as expected this is the trivial solution >> Gauss_e = rref([A b]) Gauss_e = 1.0000 0 0

0 1.0000 0

-0.2500 -0.5000 0

0 0 0

>> % This result shows, many solutions with the option of one parameter. From the above result using rref, we can use back substitution as follows: x2 − 0.5x3 = 0 ⇔ 2x2 − x3 = 0 and x1 − 0.25x3 = 0 ⇔ 4x1 − x3 = 0 choose x3 = 4, then x1 = 1 and x2 = 2 x = [1

2

Proof: >> A*[1 2 4]’ ans = 0 0 0

4] is one solution.

61

Chapter 7

Eigenvalues and Eigenvectors

7.1

Eigenvalues, Eigenvectors

Let A be a given n × n matrix and consider the vector equation Ax = λx

(7.1)

where x is an unknown vector and λ is an unknown scalar and we want to determine both. Obviously, x = 0 is a solution, giving 0 = 0, but this is of no practical interest. A value of λ for which (7.1) has a solution x 6= 0 is called an eigenvalue or characteristic value of the matrix A. The corresponding solutions x 6= 0 of (7.1) are called eigenvectors or characteristics vectors of A corresponding to that eigenvalue λ. The set of simultaneous equations Ax = λx can be written in the form : (A − λI)x = 0

(7.2)

(λI − A)x = 0.

(7.3)

where I is n × n identity matrix. This is equivalent to :

The matrix equation (7.3) represents simply a set of homogeneous equations, and we know from last chapter that a non-trivial solution exists if p(λ) = det(λI − A) = 0

(7.4)

where p(λ) is the expansion of the determinant and is a polynomial of degree n in λ; it is known as the characteristic polynomial of A. The equation p(λ) = 0 is called the characteristic equation of A. Example 7.1.1 Find the characteristics equation and the eigenvalues of   1 3 A= 2 2 Solution The characteristic equation is given by

λ − 1 −3 =0 p(λ) = −2 λ − 2

⇒ (λ − 1)(λ − 2) − 6 = 0 ⇔ λ2 − 3λ − 4 = 0 ⇒ λ1 = −1, λ2 = 4 are the eigenvalues of the matrix A. It is well known from linear algebra that λ1 + λ2 = trace(A), here −1 + 4 = 1 + 2, and λ1 · λ2 = det(A) Corresponding to each eigenvalue λ , there is an eigenvector x, which is sometimes refered as v in Malab references, that satisifies equation (7.1). Example 7.1.2 Find the eigenvectors of the matrix A from Example 7.1.1 above.   x let x1 = 11 be the eigenvector corresponding to λ1 = −1. x21 Ax1 = λ1 x1      x 1 3 x11 − λ1 11 = 0 x21 2 2 x21    x11 1 − λ1 3 =0 ⇔ 2 2 − λ1 x21    2 3 x11 =0 ⇔ 2 3 x21 ⇒ 2x11 + 3x21 = 0 ⇒ x1 1 = − 32 x21  3  − 2 x21 ⇒ x1 = x21 Any nonzero value of x21 will give an eigenvector. We choose a convenient value for the parameter to give one solution. The others are multiples of this. Choose x21 = 2, x11 = −3   −3 x1 = 2

Sometimes it is convenient to scale the eigenvectors according to some convention. A convention frequently used is to normalize the eigenvectors so that they are uniquely determined up to a scale of ±1. Let x1,n be the normalized eigenvector corresponding to x1 . x1,n

=√

1 x1 (−3)2 +22

=

√1 13

  −3 2

Similarly we can solve for the second eigenvector x2 corresponding to λ2 = 4.   x x2 = 12 x22 Ax2 = λ1 x2    x12 1 − λ2 3 =0 ⇒ 2 2 − λ2 x22    x12 1−4 3 =0 ⇔ 2 2 − 4 x22    x12 −3 3 =0 ⇔ 2 −2 x22 ⇒ −3x12 + 3x22 = 0 and ⇒ x22 = x12 Choose x12 = 1 ⇒ x22 = 1

2x12 − 2x22 = 0

  1 x2 = 1 x2,n =

7.2

√1 2

  1 1

MATLAB methods to determine eigenvalues and eigenvectors

For n × n matrix A the Matlab command p = poly(A) generates an n + 1 element row vector whose elements are the coefficients of the characteristic polynomial p(λ)of A, the coefficients being ordered in descending powers. The eigenvalues of A are the roots of the polynomial and generated using the command roots(p).

The command [V, D] = eig(A) generates the normalized eigenvectors of A as the columns of the matrix V and its corresponding eigenvalues as the diagonal elements of the diagonal matrix D. In the absence of the left-hand arguments , the command eig(A) by itself simply generates the eigenvalues of A. Example 7.2.1 Consider  matrix from example 7.1.1  the 1 3 A= 2 2

>> A = [1 3; 2 2] A = 1 2

3 2

>> [V, D] = eig(A) V = -0.8321 0.5547

-0.7071 -0.7071

D = -1 0

0 4

>>

D is a diagonal matrix whose diagonal elements are the eigenvalues of the given square matrix A. V is a square matrix whose column vectors are the normalized eigenvectors x1,n and x2,n . These results are the same as the calculated results from Example 7.1.1 and 7.1.2.

7.3

Linear independence of eigenvectors

Let λ1 , λ2 , · · · , λn be distinct eigenvalues of an n × n matrix. Then corresponding eigenvectors x1 , x2 , · · · , xn form a linearly independent set. But if the eigenvalues are not different, some of the eigenvectors may not be linearly independent. Example 7.3.1  1 1 −2 a) Given is a matrix A = −1 2 1 . 0 1 −1 

The eigenvalues of A are : >> A = [1

1

-2; -1

1 2 1

-2 1 -1

2

1 ; 0

1

-1]

A = 1 -1 0

>> d = eig(A) d = 2.0000 1.0000 -1.0000 Three distinct eigenvalues, which means we expect three linearly independent eigenvectors. >> [V, D] = eig(A) V = 0.3015 0.9045 0.3015

D =

-0.8018 -0.5345 -0.2673

0.7071 0.0000 0.7071

2.0000 0 0

0 1.0000 0

0 0 -1.0000

The three column vectors of the matrix V are the three linearly independent eigenvectors. Because these eigenvectors are linearly independent, the matrix V has an inverse.  1 2 2 b) Given is a matrix A =  0 2 1. −1 2 2 

>> A = [1

2

2; 0

2 2 2

2 1 2

2

1 ; -1

2

2]

A = 1 0 -1

>> [V, D] = eig(A) V = 0.8944 0.4472 + 0.0000i 0.0000 + 0.0000i

0.8944 0.4472 - 0.0000i 0.0000 - 0.0000i

0.5774 0.5774 -0.5774

2.0000 + 0.0000i 0 0

0 2.0000 - 0.0000i 0

0 0 1.0000

D =

λ1 = λ2 = 2 and the corresponding eigenverctors, which are the first and second column vectors of the matrix V, are exactly the same. Therefore, the matrix A does not possess a full set of linearly independent eigenvectors.  3 −3 2 c) Given is a matrix A = −1 5 −2. −1 3 0 

>> A = [3

-3

2; -1

5

-2 ; -1

3

0]

A = 3 -1 -1

-3 5 3

2 -2 0

>> [V, D] = eig(A) V = 0.5774 -0.5774 -0.5774

-0.5774 -0.5774 -0.5774

-0.7518 0.1735 0.6361

4.0000 0 0

0 2.0000 0

0 0 2.0000

D =

λ2 = λ3 = 2 but the eigenvectors, the column vectors of V, are linearly independent. Exercise 7a Determine the eigenvalues and eigenvectors of the following matrices and proof also whether the eigenvectors form a linearly independent set.

1)

2)

3)

4)

  2 2 1 A = 1 3 1 1 2 2  −3 −7 A= 2 4 1 2  2 1  A = −1 0 −1 −1  0 −2  A = −1 1 −1 −1

 −5 3 2  −1 1 2  −2 2 2

 1 0 5) A =  0 0  1 6) A = 0 0

7.4

 0 0 0 0  1 0 0 1  2 3 3 5 0 7 0 1 0 0

Orthonormal set, Symmetric matrices

A square matrix A is said to be symmetric if AT = A. • The eigenvalues of a real symmetric matrix are real. • For an n×n real symmetric matrix it is always possible to find n linearly independent eigenvectors x1 , x2 , · · · , xn that are mutually orthogonal, xTi · xj = 0 for i 6= j. A matrix is called skew-symmetric or anti-symmetric if AT = −A. A matrix is called orthogonal if AT = A−1 ⇔ AT A = I, where I is n × n identity matrix. Example 7.4.1   1 1 A= 1 2   1 1 T =A A = 1 2 ⇒ A is real symmetric matrix. A = 1 1

1 2

>> [V, D] = eig(A) V = -0.8507 0.5257

D =

0.5257 0.8507

0.3820 0 0 2.6180 >> norm(V(:,1)) ans = 1.0000 >> norm(V(:,2)) ans = 1.0000 If the above V matrix has orthonormal eigenvectors, then VVT = I2 , where I2 is 2 × 2 identity matrix. In other words vij · vji = δij , where δij is the kronecker delta which is equal to 1 for i = j and 0 otherwise, here i, j = 1, 2 and in general i, j = 1, · · · , n for n × n real symmetric matrix. >> VVT = V*V’ VVT = 1.0000 0

0 1.0000

Indeed the eigenvectors, which are the columns of the matrix V, are orthonormal. Exercise 7b Obtain the eigenvalues and the corresponding orthogonal eigenvectors of the symmetric matrices  2  a) A = 1 0  2 b) A = 2 0

 0 0 2  2 0 5 0 0 3

1 2 0

c) >> A = pascal(3)

A = 1 1 1

7.5

1 2 3

1 3 6

Diagonalization of a matrix, canonical form

If an n×n matrix A possess a full set of n linearly independent eigenvectors x1 , x2 , · · · , xn , then : V−1 AV = Λ

(7.5)

  where the matrix V having the n eigenvectors as its columns V = x1 x2 · · · xn and Λ = D is the diagonal matrix with the eigenvalues λ1 , λ2 , · · · , λn of A as its diagonal elements. To proof (7.5), let us begin from (7.1) Ax1 = λ1 x1 , ⇒ ⇒ ⇒ ⇒

Ax2 = λ2 x2 ,

··· ,

Axn = λn xn

    A x1 x2 · · · xn = x1 x2 · · · xn Λ AV = VΛ V−1 AV = Λ A = VΛV−1

Therfore, (7.5) is always true if V has linearly independent eigenvectors as its columns, which means V inverse exists. Power of Square matrices For matrices which can be written as (7.5) their powers , eigenvalues and eigenvectors can easily be calculated as follows : Let A be a 3 × 3 matrices with three distinct eigenvalues λ1 , λ2 , λ3 ,   λ1 0 0 Λ =  0 λ2 0  0 0 λ3

 λ1 Λ2 =  0 0  2 λ1 2  Λ = 0 0

  λ1 0 0 0 0 λ2 0   0 λ2 0  0 0 λ3 0 λ3  0 0 λ22 0  0 λ23

But, from (7.5) : Λ2 = Λ · Λ = (V−1 AV)(V−1 AV) = V−1 AI3 AV = V−1 AAV , where I3 is 3 × 3 identity matrix. ⇒ A2 = VΛ2 V−1 ⇒ eigenvalues of A2 are λ21 , λ22 and λ23 and the eigenvectors of A2 are the same as the eigenvectors of the matrix A. Thus, A10 = A · A · A · A · A · A · A · A · A · A can be calculated as : A10 = VΛ10 V

−1

Exercise 7c Given are the matrices : 

1 1 A= 1 1  3 2 B= 1 0

 1 4  10 20  0 0  0 3

1 2 3 4

1 3 6 10

2 3 1 0

1 1 3 0

• Proof that both matrices are real symmetric. • Proof that the eigenvalues of A5 are equal to λ5i , i = 1, · · · , 5, where λi are the eigenvalues of A. • Proof that the eigenvectors of B5 are the same as the eigenvectors of B. • Proof that A3 = VΛ3 V−1 , where Λ and V are the diagonal eigenvalues and the

eigenvector matrices of A respectively. • Proof that B5 = VΛ5 V−1 , where Λ and V are the diagonal eigenvalues and the eigenvector matrices of B respectively.

7.6

Cayley-Hamilton theorem

A square matrix A satisifies its own characteristic equation ; that is, if λn + cn−1 λn−1 + · · · + c1 λ + c0 = 0 is the characteristic equation of an n × n matrix A then An + cn−1 An−1 + · · · + c1 A + c0 I = 0 where I is the n × n identity matrix. The proof of this theorem is not trivial, and is not included here. We shall illustrate the theorem using a simple exercises. Exercise 7d Verify the Cayley-Hamilton theorem for the following matrices :

  3 4 • A= 1 2   1 1 1 • B = 1 2 3 1 3 6

73

Chapter 8

Nonlinear Equations

8.1

Polynomial roots

Polynomials are represented as vectors of their coefficients, so p(x) = x3 − 2x − 5 is given by p = [1 0 -2 -5] p = 1

0

-2

-5

The solutions of the equation p(x) = 0 is given by r = roots(p) r = 2.0946 -1.0473 + 1.1359i -1.0473 - 1.1359i Given a vector of roots r, poly(r) constructs the coefficients of the polynomial with those roots. With a little bit of roundoff error, you should see the original polynomial. Try it. The poly function also computes the characteristic polynomial of a matrix whose roots are the eigenvalues of the matrix. Example 8.1.1 A = magic(3) A =

8 1 3 5 4 9 P = poly(A)

6 7 2

P = 1.0000

-15.0000

-24.0000

360.0000

p(λ) = λ3 − 15λ2 − 24λ + 360 is the characteristic equation of the matrix A. eigValues = roots(poly(A)) eigValues = 15.0000 -4.8990 4.8990 % check using the command eig eigValues2 = eig(A) eigValues2 = 15.0000 4.8990 -4.8990

8.2

Solving real function using fzero

The fzero funcction finds a numerical solution to f (x) = 0 when f (x) is a real function over a real domain. You must give either an initial guess, or two values of x for which the function differs in sign. Description x = fzero(fun, x0) tries to find a zero of f un near x0, if x0 is a scalar. f un is a function handle for either an M-file function or an anonymous function. The value x returned by fzero is near a point where f un changes sign, or NaN if the search fails. In this case, the search terminates when the search interval is expanded until an Inf, NaN, or complex value is found.

Examples (From Matlab help ) FUN can be specified using @: X = fzero(@sin,3) returns pi. X = fzero(@sin,3,optimset(’Display’,’iter’)) returns pi, uses the default tolerance and displays iteration information. FUN can also be an anonymous function: X = fzero(@(x) sin(3*x),2) Example 8.2.1 Given is : f (x) = x2 − 2 √ √ f (x) = 0 has two solutions x1 = 2 and x2 = − 2 Now using fzero we can compute x1 and x2 : x_1 = fzero(@(x) x^2 -2 , 1) % function handle and solution search near 1 x_1 = 1.4142 x_2 = fzero(@(x) x^2 -2 , -1) % solution search near -1 x_2 = -1.4142 As f(x) is a polynomial we can also use the function root. >> f = [1 0 -2] f = 1

0

-2

>> x = roots(f) % this returns the solution of f(x) = 0 x = 1.4142 -1.4142 The function handle for f (x) = x2 − 2 can be written as anonymous function :

>> f = @(x) x.^2 -2 f = @(x)x.^2-2 This f now can take both scalar and vector argument : >> f(-sqrt(2)) ans = 4.4409e-016 >> f(sqrt(2)) ans = 4.4409e-016 >> f([-sqrt(2) sqrt(2)]) ans = 1.0e-015 * 0.4441

0.4441

>> v = [-2 -1 0 1 2] v = -2

-1

0

1

2

-1

-2

-1

2

>> f(v) ans = 2

One can also used ’inline function’ to handle a function. >> g = inline(’x.^2 -2’) g =

Inline function: g(x) = x.^2 -2 This g(x) can also take both scalar and vector as argument : >> g([-1.414 1.414]) ans = 1.0e-003 * -0.6040

-0.6040

>> g([-sqrt(2) sqrt(2)]) ans = 1.0e-015 * 0.4441

0.4441

The fzero function can only find an x for which f (x) crosses the x-axis. If the sign of f (x) does not differ on either side of x, the zero point x will not be found. Example 8.2.2 Let us create two anonymous functions (regular M-files can also be used): >> fa = @(x) (x-2)^2 fa = @(x)(x-2)^2 >> fb = @(x) (x-2)^2 - 1e-12 fb = @(x)(x-2)^2-1e-12 The zero of f a cannot be found even for initial guess of 1.999 or 2.001, and neither can a zero of f b be found if your initial guess is too far from the solution.Observe the following results of f a : >> fzero(fa , 1.999)

Exiting fzero: aborting search for an interval containing a sign change because NaN or Inf function value encountered during search. (Function value at -1.71534e+154 is Inf.) Check function or try again with a different starting value. ans = NaN

An initial guess of 2, which is the exact solution of f a, returns the result but that makes no sense for functions whose solutions we don’t know from the beginning. If we know the solutions, then there is no need to make the computation at all.

>> fzero(fb, 2.001) Exiting fzero: aborting search for an interval containing a sign change because NaN or Inf function value encountered during search. (Function value at -1.71706e+154 is Inf.) Check function or try again with a different starting value. ans = NaN >> fzero(fb, [2 3]) ans = 2.0000 Exercise 8a 1) Given is a matrix :   1 1 1 A = 1 2 3 1 3 6 • Determine the characteristic polynomial using the Matlab command poly. • Find the roots of the characteristic polynomial using the Matlab command roots. • Find a diagonal matrix with the roots of the characteristic polynomial as diagonal elements. The Matlab command diag returns a diagonal matrix when it applies on a vector.

• Now determine the eigenvector and eigenvalue Matrices using the standard Matlab command [V, D] = eig(A) . • Compare the eigenvalues and the diagonal Matrix D = Λ you obtained with the two methods above. 2) Given is the following equation : f (x) = x3 − 9x2 + 9x − 1 • Find at least one solution of this equation using the Matlab command fzero. • Find the polynomial roots with the command roots and compare the results with the solutions using fzero . The zero of the sine function near 3 using the Matlab command fzero is given by : >> x = fzero(@sin, 3) x = 3.1416 3)Find the zero of the cosine between 1 and 2. Note that cos(1) and cos(2) differ in sign. 4) Given is the equation : f (x) = x3 − 2x − 5 • Find the zero near 2 using the command fzero. • Find the polynomial roots of f (x) using the command roots.

8.3

Solving Equations using Symbolic Toolbox

For a symbolic expression S, the statement solve(S) will attempt to find the values of the symbolic variable for which the symbolic expression is zero. The solve function cannot solve all equations. It does well with polynomial equations, but can have difficulty with trigonometric or other transcendental equations. If an exact symbolic solution is indeed found, you can convert it to a floating-point solution, if desired. Examples 8.3.1 Given is the equation f (t) = t3 − 9 ∗ t2 + 9 ∗ t − 1. To solve it using the command solve, we first define the variable t as symbolic variable.

>> syms t >> solve(t^3 -9*t^2 + 9*t -1) ans = 1 4 - 15^(1/2) 15^(1/2) + 4 >> double(ans) ans = 1.0000 0.1270 7.8730 >>

h = @(t) t^3 -9*t^2 + 9*t -1

% This is function handle to proof say h(1) = 0

h = @(t)t^3-9*t^2+9*t-1 >> h(1) ans = 0 The inputs to solve can be quoted strings or symbolic expressions. To solve an equation whose right-hand side is not zero, use a quoted string or rearrange the equation. Example 8.3.2 Given is the equation log(x) = x − 2. The solution using solve is : >> X = solve(’log(x) = x - 2’) X = 1/(exp(2)*exp(wrightOmega(pi*i - 2))) >> double(X)

ans = 0.1586 >> X = solve(log(x) - x + 2) % this is equivalent to the above one. X = 1/(exp(2)*exp(wrightOmega(pi*i - 2))) >> X = double(X) X = 0.1586 >> fx = @(x) log(x) - x + 2 % Function handle fx = @(x)log(x)-x+2 >> fx(0.1586) ans = 3.0030e-005 Exercise 8b Solve the equation f = cos(x) + tan(x) using the command solve.

8.4

System of nonlinear equations

The Matlab command fsolve finds a root(zero) of a system of nonlinear equations. The following Matlab syntax description are available at help fsolve. x = fsolve(fun, x0) starts at x0 and tries to solve the equations described in fun . x = fsolve(fun,x0,options) solves the equations with the optimization options specified in the structure options. Use optimset to set these options. [x,fval] = fsolve(fun,x0) returns the value of the objective function fun at the solution x.

[x,fval,exitflag] = fsolve(...) returns a value exitflag that describes the exit condition. [x,fval,exitflag,output] = fsolve(...) returns a structure output that contains information about the optimization. The input argument fun in the above equations is a function that accepts a vector x and returns a vector F, the nonlinear equations evaluated at x. The function fun can be specified as a function handle for an M-file function. x = fsolve(@myfun,x0) where myfun is a MATLAB function such as function F = myfun(x) F = ... % Compute function values at x fun can also be a function handle for an anonymous function. x = fsolve(@(x)sin(x.*x),x0); If the user-defined values for x and F are matrices, they are converted to a vector using linear indexing. The output argument exitflag equal to 1 means function converged to a solution x. For the other values of exitflag please refer to the Matlab help. Example 8.4.1 This example finds a zero of the system of two equations and two unknowns: 2x1 − x2 = e−x1 −x1 +2x2 = e−x2 We rewrite the above equations in the form that we will use it in an M-File that computes F, the values of the equations at x. 2x1 − x2 −e−x1 = 0 −x1 +2x2 −e−x2 = 0 We assume a staring value x0 = [-5 ; -5]. The first important step is to write an M-File, which returns F. % This is a function called myfun3 which takes vector x as argument

function F = myfun3(x) F = [2*x(1) - x(2) - exp(-x(1)) ; -x(1) + 2*x(2) - exp(-x(2))] ; Next, we call an optimization routine. >> x0 = [-5; -5]; % initial guess to the solution >> options = optimset(’Display’,’off’); % option to turn off display >> [x,fval, exitflag] = fsolve(@myfun3,x0,options) % Call optimizer x = 0.5671 0.5671

fval = 1.0e-006 * -0.4059 -0.4059

exitflag = 1 In order to see the iterations that it takes to have the solution we set the options as follows: >> x0 = [-5; -5]; % initial guess to the solution >> options = optimset(’Display’,’iter’); % option to display the output >> [x,fval, exitflag] = fsolve(@myfun3,x0,options) % Call optimizer

Iteration 0 1 2 3 4 5 6 7

Func-count 3 6 9 12 15 18 21 24

f(x) 47071.2 12003.4 3147.02 854.452 239.527 67.0412 16.7042 2.42788

Norm of step 1 1 1 1 1 1 1

First-order optimality 2.29e+004 5.75e+003 1.47e+003 388 107 30.8 9.05 2.26

Trust-region radius 1 1 1 1 1 1 1 1

8 27 0.032658 0.759511 0.206 2.5 9 30 7.03149e-006 0.111927 0.00294 2.5 10 33 3.29525e-013 0.00169132 6.36e-007 2.5 Optimization terminated: first-order optimality is less than options.TolFun. x = 0.5671 0.5671

fval = 1.0e-006 * -0.4059 -0.4059

exitflag = 1 It took 10 iteraions to converge to the solution, but if one chooses an initial value x0 near to the solution then the number of iterations for convergence to the solution reduces. For instance, let us take x0 = [0 ; 0] >> x0 = [0 ; 0]; % initial guess to the solution >> options = optimset(’Display’,’iter’); % option to display the output >> [x,fval, exitflag] = fsolve(@myfun3,x0,options) % Call optimizer Norm of step

First-order Trust-region Iteration Func-count f(x) optimality radius 0 3 2 2 1 1 6 0.0226976 0.707107 0.171 1 2 9 3.40349e-006 0.0937779 0.00204 1.77 3 12 7.72066e-014 0.00117685 3.08e-007 1.77 Optimization terminated: first-order optimality is less than options.TolFun. x = 0.5671 0.5671

fval =

1.0e-006 * -0.1965 -0.1965

exitflag = 1 If we choose x0 even closer, we will get even lesser iterations to converge to the solution. Try for x0 = [0.5, 0.5]. Exercise 8c 1) Find the matrix X that satisifies   1 2 X ∗X ∗X = 3 4 with starting matrix of X0 = [1, 1 ; 1, 1] Note: First write an M-File that computes the equation to be solved, which looks like : function F = myfun4(X) F = X*X*X - [1, 2 ; 3, 4]; Next, call the optimization routine just like in Example 8.4.1 2) Find the matrix X that satisifies  1 2 X ∗X = 3 4 

with starting matrix of X0 = [1, 1 ; 1, 1]. 3) Solve the following system of equations. 2x1 + x2 = log(x1 ) x1 +2x2 = log(x2 ) Set options = optimset(’Display’,’iter’) and use starting values of : • x0 = [1; 1] • x0 = [1; 10] • x0 = [10; 10]

86

Chapter 9

Differentiation

9.1

Polynomial derivatives

In Matlab the polyder function computes the derivative of any polynomial. For instance, f (x) = x3 − 3x We know from calculus that the derivative of f (x) with respect to x can easily be found as : df (x) = 3x2 − 3 . dx In Matlab we can addditionally make graphs to visualize both the function and its derivatives at some relevant points, say at local minima and maxima. >> f = @(x) x.^3 - 3*x

% This is the given function

f = @(x)x.^3-3*x >> p = [1 0 -3 0] % polynomial representation of f(x) p = 1

0

-3

0

>> df = polyder(p) % the derivative of p df = 3

0

-3

% the coefficients of the polynomial of the derivative of f(x)

>> dfx = @(x) 3*x^2 - 3 % function representation of the derivative function dfx = @(x)3*x^2-3

>> >> >> >> >> >>

fplot(f, [-2*pi 2*pi -10 10]) hold on fplot(dfx, [-2*pi 2*pi -10 10], ’-rs’) plot([0 0], [-10, 10],’-k’, [-2*pi, 2*pi], [0, 0], ’-k’) title(’Graphs to visualize f(x) = x^3 -3x and its derivative df(x)/dx’) legend(’f(x) = x^3 -3x’, ’df(x)/dx’) 3

Graphs to visualize f(x) = x −3x and its derivative df(x)/dx 10 f(x) = x3 −3x df(x)/dx

8 6 4 2 0 −2 −4 −6 −8 −10

−6

−4

−2

0

2

4

6

Exercise 9a We know from Chapter 4.3 that the symbolic Matlab tool taylor(f) returns for the symbolic function f the taylor series expansion at about x = 0 for the given function f (x). Now, given is the following : >> P = taylor(sym(’sin(x)’)) % taylor series expansion of sin(x) at about x = 0 P = x^5/120 - x^3/6 + x Determine the derivative of the above polynomial expansion, P (x), and plot both the derivative and P (x) for x ∈ [−2π, 2π]. polyder also computes the derivative of the product or quotient of two polynomials. For example, create two polynomials a and b:

>> a = [1 3 5] ; % a(x) = x^2 + 3x + 5 >> b = [2 4 6] ; Calculate the derivative of the product a*b by calling polyder with a single output argument: >> c = polyder(a,b) c = 8

30

56

38

Calculate the derivative of the quotient a/b by calling polyder with two output arguments: >> [q,d] = polyder(a,b) q = -2

-8

-2

4

16

40

d = 48

36

q/d is the result of the operation. Exercise 9b Given is f (x) =

x2

1 . +1

Determine the derivative of f (x) using the command polyder and plot both the function and its derivative on the same plot.

9.2

Approximate derivative

Matlab also provides a function for an approximate derivative , given tabulated data describing some function. This function, named diff, computes the difference between elements in an array. Since differentiation is defined as dy f (x + ∆x − f (x)) = lim dx ∆x→0 ∆x

The derivative of y = f (x) can be approximated by dy ∆y f (x + ∆x − f (x)) ≈ = lim dx ∆x ∆x→0 ∆x which is the forward finite difference in y divided by the finite difference in x. Since diff computes differences between array elements, differentiation can be approximated in Matlab. When the first element of x is thrown out, then this procedure gives a backward difference approximation, which uses information at x(n − 1) and x(n) to approximate the derivative at x(n). On the otherhand,throwing out the last element gives a forward difference approximation, which uses x(n + 1) and x(n) to compute results at x(n). Example 9.2.1 >> >> >> >> >> >> >>

x = linspace(0,2*pi) ; y = sin(x) ; dy = diff(y)./diff(x) ; % x is linearly spaced, hence x(2)-x(1) can be used instead of diff(x) xd = x(2: end) ; plot(x,y,xd,dy,’-rs’) % x(1) removed, backward-difference derivation axis tight Figure 9.2: Backward Difference Derivative Approximation sin(x) cos(x)= d(sin(x))/dx

0.8 0.6 0.4 0.2 0 −0.2 −0.4 −0.6 −0.8

0

1

2

3

4

5

6

x

Exercise 9c Assume the following measurement results in which y is measured corresponding to the input variable x.

x = [1,

2,

y = [0.5000,

3,

4,

0.2000,

5,

6,

0.1000,

7,

8,

9,

0.0588,

10] 0.0385,

0.0270,

0.0200,

0.0154,

0.0122,

1. Determine the approximate derivative using the command dy = diff(y)./diff(x) and plot(xd,dy), where xd = x(1:end-1). 2. Find the polynomial approximation of order 4 for the data using polyfit (as discussed in Chapter 4, p = polyfit(x,y,4)) and plot it together with the measured points. 3. Determine the polynomial derivative of the above piecewise polynomial using the command polyder and plot it on the same plot with the polynomial. 4. Compare the two derivative results you have just obtained by plotting both on the same plot.

9.3

Symbolic derivative

The function diff computes the symbolic derivative of a function defined by a symbolic expression. First, to define a symbolic expression, you should create symbolic variables and then proceed to build an expression as you would mathematically. For example, >> syms x >> f = sin(x) f = sin(x) >> df = diff(f) df = cos(x) >> g = tan(x) g = tan(x) >> dg = diff(g) dg =

0.0099]

tan(x)^2 + 1 >> diff(f*g) ans = cos(x)*tan(x) + sin(x)*(tan(x)^2 + 1) The commands pretty, simple or pretty(simple(...)) can be used to have readable and mathimatically simplified expressions. Try yourself by following the examples below. >> pretty(f*g) sin(x) tan(x) >> pretty(f/g) sin(x) -----tan(x) >> pretty(simple(f/g)) cos(x) >> pretty(diff(f*g)) 2 cos(x) tan(x) + sin(x) (tan(x) >> pretty(simple(diff(f*g)))

+ 1)

2 cos(x) tan(x) + sin(x) (tan(x)

+ 1)

92

Chapter 10

Integration

10.1

Introduction to Numerical Integration

Numerical integration means the numerical evaluation of integrals J=

Z

b

f (x) dx

(10.1)

a

where a and b are given and f is a function given analytically by a formula or emperically by a table of values. Geometrically, J is the area under the curve of f between a and b. A numerical integration method could be used where analytical evaluation would be complicated or even impossible. Rectangular,Trapezoidal and Simpson’s rules Numerical integration methods are obtained by approximating the integrand f by functions that can easily be integrated. The simplest formula, the rectangular rule, is obtained if we subdivide the interval of integration a ≤ x ≤ b into n subintervals of equal length h = (b − a)/n and in each subinterval approximate f by the constant f (x∗j ), the value of f at the midpoint x∗j of the j th subinterval. Then f is approximated by a step function (piecewise constant function), the n rectangles have the areas f (x∗1 )h, · · · , f (x∗n )h)), and the rectangular rule is J=

Z

a

b

f (x) dx ≈ h[f (x∗1 ) + f (x∗2 ) + · · · + f (x∗n )]

(10.2)

where h = (b − a)/n The trapezoidal rule is generally more accurate than the rectangular rule. We obtain it if we take the same subdivision as before and approximate f by a broken line of segments with endpoints [a, f (a)], [x1 , f (x1 )], · · · , [b, f (b)] , where x1 = a + h, x2 = a + 2h, and so on. The area under the curve of f between a and b is approximated by n trapezoids of areas. Z b 1 1 f (x) dx ≈ h[ f (a) + f (x1 ) + f (x2 ) + · · · + f (xn−1 ) + f (b)] J= (10.3) 2 2 a

where h = (b − a)/n, as in (10.1). The x′j s and a and b are called nodes. Piecewise constant approximation of f led to the rectangular rule (10.1), piecewise linear approximation to the trapezoidal rule (10.2), and piecewise quadratic approximation will give Simpson’s rule, which is of great practical importance as it is sufficiently accurate for most problems.

10.2

Numerical integration using Matlab

The function trapz performs a numerical integration using the trapezoidal method. Z = trapz(Y) computes an approximation of the integral of Y via the trapezoidal method (with unit spacing). To compute the integral for spacing other than one, multiply Z by the spacing increment. Input Y can be complex. Z = trapz(X,Y) computes the integral of Y with respect to X using trapezoidal integration. Inputs X and Y can be complex. Example 10.2.1 Rπ The exact value of 0 sin(x) dx is 2. A numerical approximation using trapezoidal method on a uniformly spaced grid is : >> x = 0:pi/100:pi; >> y = sin(x) ; >> Area = trapz(x,y) % This integral is the area under the curve of sin(x) for x b/n 0 and pi Area = 1.9998 >> The function cumtrapz performs cumulative trapezoidal numerical integration. Example 10.2.2 Given is a sin function between −2π and 2π and we want to determine the cumulative trapezoidal numerical integration with plot which describs the result. x = linspace(-2*pi, 2*pi,100) ; y = sin(x) ; z = cumtrapz(x,y) ; figure(2) plot(x,y,x,z,’-rs’), grid on

axis tight Figure 10.2: Function cumtrapz performs cumulative trapezoidal numerical integration sin(x) integral of sin(x) 1.5

1

0.5

0

−0.5

−6

−4

−2

0

2

4

6

Example 10.2.3 Given is the function :

1 x2 fx (x) = √ exp(− ) (10.4) 2 2π This equation is the probabilty density function (PDF) of a Gaussian distribtion with zero mean and variance equal to 1. We want to calculate R x the cumulative distribution function (CDF), describes as Fx (x), which is equal to −∞ fx (x) dx, using the cumulative trapezoidal numerical integration method. % Example for cumtrapz using Normal Gaussian PDF and CDF % given is fx(x), i.e the PDF with zero mean and var = 1 fx = @(x) exp(-x.^2 /2)/sqrt(2*pi) fx = @(x)exp(-x.^2/2)/sqrt(2*pi) x = linspace(-5, 5, 100) ; y = fx(x) ; z = cumtrapz(x,y) ; % z = CDF and y = fx(x) is the PDF figure(1), plot(x,y,x,z,’-rs’), grid on, axis tight

Fig. 10.1: Given the PDF of Normal distribtion, its CDF is calculated using cumtrapz

Gaussian PDF, mean=0 & var=1 Gaussian CDF, mean=0 & var=1

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0 −5

−4

−3

−2

−1

0

1

2

3

4

5

Exercise 10a Given is : y = sin2 (x) , Rx

x ∈ [−2π 2π]

Plot both −2π y dx for x ∈ [−2π 2π] and y = sin2 (x) on the same figure. Use the cumulative trapezoidal numerical integration method like in the two examples above. The Matlab function quad is more accurate than the trapz function as the former uses the adaptive Simpson quadrature method whereas the latter uses the trapezoidal integration method . The function quadl is also very effective. In addition there are also functions dblquad and triplquad for numerical evaluation of double and triple integrals. For more details syntax information please refer Matlab help. Here is the Matlab syntax for quad from Matlab help : quad Numerically evaluate integral, adaptive Simpson quadrature Syntax q = quad(fun,a,b) q = quad(fun,a,b,tol) q = quad(fun,a,b,tol,trace) [q,fcnt] = quad(...)

Description Quadrature is a numerical method used to find the area under the graph of a function, that is, to compute a definite integral. q = quad(fun,a,b) tries to approximate the integral of function fun from a to b to within an error of 1e-6 using recursive adaptive Simpson quadrature. fun is a function handle. Example 10.2.4 Given is the function,

1 x2 fx (x) = √ exp(− ) 2 2π

• Determine the integral from 0 to 10. • Determine the integral from 0 to 100. • Determine the integrat from −10 to 10. • Determine the integral from 0 to ∞. Solution >> fx fx = @(x)exp(-x.^2/2)/sqrt(2*pi) >> Area = quad(fx, 0, 10) Area = 0.5000 >> Area = quad(fx, 0, 100) Area = 0.5000 Try the last two. Exercise 10b Perform the following integral using quad and quadl, compare the resuts. 1.

Z



−2π

sin2 (x)dx

2.

Z

1

Z

1

−1

3. The exact analytical result of

−1

1 dx 1 + x2 1 dx 1 + x2

is π/2. Detrmine the errors when quad and quadl are used; which one performs better for this specific problem ? 4.

Z

1

xex dx

−1

In order to perform double integral the syntax is : q = dblquad(fun,xmin,xmax,ymin,ymax) calls the quad function to evaluate the double integral f un(x, y) over the rectangle xmin ≤ x ≤ xmax , ymin ≤ y ≤ ymax. fun is a function handle. q = dblquad(fun,xmin,xmax,ymin,ymax,tol) uses a tolerance tol instead of the default, which is 10−6 . Exercise 10c 1. Consider the following function fxy (x, y) = fx (x) · fy (y) =

1 x2 + y 2 exp(− ) 2π 2

(10.5)

f (x,y) = exp((−x2−y2)/2)/(2 π) xy

0.15

0.1

0.05

0 4 2

4 2

0 0 −2 y

−2 −4

−4

x

We are looking for the volume under the surface z = fxy (x, y)(> 0) and above a region R in the xy-plane. Here R is given by R : −2π ≤ x ≤ 2π • Determine

ZZ

, −2π ≤ y ≤ 2π

fxy (x, y)dxdy

R

the double integral of fxy (x, y) over the range of R. • Given fx (x) as in Example 10.2.4, try to figure out fy (y) from eqn.(10.5) and find its integral over −2π ≤ y ≤ 2π. 2. Let’s now consider the region in polar coordinates R∗ . The mathematical formula for the change of variables x, y to r, θ is ZZ ZZ ∂(x, y) drdθ f (r cos(θ), r sin(θ)) f (x, y)dxdy = ∂(r, θ) R

R∗

where J = ∂(x,y) is the absolute value of the Jacobian. ∂(r,θ) The transformation from cartesian coordinatess x and y to polar coordinates r and θ can be made using the relation ˙ x = rcos(θ) ˙ y = rsin(θ) .

∂(x, y) cos(θ) −r sin(θ) = =r J = ∂(r, θ) sin(θ) r cos(θ)

Now, we consider R∗ to be a circle, R∗ : 0 ≤ r ≤ 1, 0 ≤ θ ≤ 2π. The given function is: x2 + y 2 1 exp(− ) fxy (x, y) = 2π 2 .

• Describe the function fxy (x, y) in polar coordinates, fxy (r, θ). • Now determine the double integral of the function over R∗ . • Repeat the integral for R∗ now be R∗ : 0 ≤ r ≤ 10 , 0 ≤ θ ≤ 2π.

10.3

Symbolic Integration

The Symbolic Math Toolbox has a function called int for symbolic integration. Syntax and Description int(S,v) returns the indefinite integral of S with respect to the symbolic scalar variable v. int(S) returns the indefinite integral of S with respect to its symbolic variable as defined

by findsym. int(S,a,b) returns the definite integral from a to b of S with respect to the default symbolic variable. a and b are symbolic or double scalars. int(S,v,a,b) returns the definite integral of S with respect to v from a to b. Example 5.3.1 >> syms x >> int(-2*x/(1+x^2)^2) ans = 1/(x^2 + 1) >> int(-2*x/(1+x^2)^2, x) ans = 1/(x^2 + 1) >> int(x/(1+z^2),z) ans = x*atan(z) Exercise 10d Calculate the following integral equations using Symbolic Toolbox command int. 1.

2.

Z

x ln(1 + x) d x Z1

x ln(x) d x

0

3.

Z1 sin(t)

2x d x

100

Chapter 11

Ordinary Differential Equations

This chapter describes how to use MATLAB built-in numerical ordinary differential equation (ODE) solvers to find approximate solutions to single ODE and system of ODEs.

11.1

Introduction

An ordinary differential equation (ODE) is an equation that contains one or several derivatives of an unknown function, which we call y(t) or y(x) and which we want to determine from the equation. The equation may also contain y itself as well as given functions and constants. For Example, y ′ = cos(x), y ′′ + 4y = 0, x2 y ′′′ y ′ + 2 exp(x)y ′′ = (x2 + 2)y 2 are ordinary differential equations. The first one is first-order, the second one is secondorder and the last one is third-oder ODEs. The word ordinary distinguishes them from partial differential equations, involving an unknown function of two or more variables and its partial derivatives. In this course we consider only ODEs. The ODE together with the initial conditions is called an initial-value-problem. We will consider only initial value problems in this course. A frst-order ODE is called linear, if it is written as y ′ + f (x)y = g(x). Examples of linear firsr-order ODEs are : y ′ − xy = 0 (Linear, homogeneous ODE) xy ′ + 2y = exp(x) (Linear , Inhomogeneous ODE) y ′ + 4y = 0 (Linear, first-order ODE with constant coefficients) y ′ + 2y = 2x2 − 4 (Inhomogeneous, linear, first-order ODE with cinstant coefficients) Example of non-linear first-order ODE is : y ′ = y 2 (Non-linear, first-order ODE). The basic analytical method (method of separation of variables) can be explained using the following initial value problem. y ′ + ty = 0;

y(0) = 1

Solution y ′ + ty = 0 dy = −ty dt dy = −t d t y Z Z dy = − tdt y t2 ⇒ ln |y| = − + C1 2 t2

⇔ |y| = e− 2 +C1 t2

⇔ y = Ce− 2

where C = ± eC1 ∈ R From the initial condition, y(0) = 1 ⇒ C = 1 t2

y = e− 2

Fig. 11.1 Analytical Solution to y’ + ty = 0, y(0) = 1 (IVP) 1 y(t), for y(0) = 0 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0

0

2

4

6

Exercise 11a 1) Given is a 1st order ODE with constant coefficients : y ′ + y = 2t Find the general solution to this equation analytically.

8

10

2) Given is the initial value problem : y ′ + y = 2t, y(0) = 0 Find the solution to this initial value problem analytically

11.2

Numerical methods to solve initial value problems (IVP) of ODEs

Numerical methods are of great importance as many practical problems often lead to differential equations that may not be solved analyticaly. An initial value problem consists of a differential equation and a condition the solution must satisify. It can be expressed as y ′ = f (x, y),

y(x0 ) = y0

assuming f to be such that the problem has a unique solution on some interval containing x0 . Euler method (step-by-step method) Given is an IVP : y ′ = f (x, y),

y(x0 ) = y0 , a ≤ x ≤ b

The first step is to divide the interval into n equal steps. h=

b−a n

h is called step size. Euler’s step by step metod starts from the given y0 = y(x0 ) and proceed as follows : x1 = x0 + h, x2 = x0 + 2h, x3 = x0 + 2h, · · · , where the step size h is a fixed number, for instance 0.2 or 0.1 . P0 = (x0 , y0 ) is a point on the exact solution curve; the tangent line at P0 approximates the solution in the interval x0 ≤ x ≤ x1 . If m0 is the slope of the tangent at P0 , which is equal to y ′ (P0 ), then the equation of the tangent line is : y − y0 = f (x0 , y0 ) ⇒ x − x0

y = y0 + (x − x0 ) · f (x0 , y0 ))

At the point x1 = x0 + h, the equation of the tangent line is y1 = y0 + (x1 − x0 ) · f (x0 , y0 ) = y0 + h · f (x0 , y0 ) y1 is the approximate value of the solution at x1 , y(x1 ) ≈ y1 = y0 + h · f (x0 , y0 ) y(x2 ) ≈ y2 = y1 + h · f (x1 , y1 )

.. . y(xk ) ≈ yk−1 = yk−1 + h · f (xk−1 , yk−1 ) Runge-Kutta Methods (4th Order) This is a more accurate method of great practical importance. Some of the Matlab ODE solvers uses this method to solve initial value problems which will be discussed in the next section. This method, like Euler’s method, solves initial value problems of the form : y ′ = f (x, y), y(x0 ) = 0 The steps to determine y(x1 ) ≈ y1 are : y(x1 ) ≈ y1 = y0 +

k1 + 2k2 + 2K3 + k4 6

k1 = h · f (x0 , y0 ) h k1 k2 = h · f (x0 + , y0 + ) 2 2 k2 h k3 = h · f (x0 + , y0 + ) 2 2 k4 = h · f (x0 + h, y0 + k3 ) The next approximation y(x2 ) ≈ y2 is determined just like the above as : y(x2 ) ≈ y2 = y1 +

k1 + 2k2 + 2K3 + k4 6

k1 = h · f (x1 , y1 ) k1 h k2 = h · f (x1 + , y1 + ) 2 2 k2 h k3 = h · f (x1 + , y1 + ) 2 2 k4 = h · f (x1 + h, y1 + k3 ) The procedure continues until the last one, y(xn ) ≈ yn = yn−1 +

k1 + 2k2 + 2K3 + k4 . 6

Both the methods we have discussed are one-step methods, which means each step uses only values from presceding step. In contrast, a multistep method is a method that in each step uses values from more than one of the precedings steps. Adams-Bashforth and Adams-Moulton are such methods.

11.3

Solving ODEs in Matlab

First Order ODEs An ordinary differential equation (ODE) contains one or more derivatives of a dependent variable y with respect to a single independent variable t, usually referred to as time. The derivative of y with respect to t is denoted as y ′ , the second derivative as y ′′ , and so on. Often y(t) is a vector, having elements y1, y2, · · · , yn. Matlab ODE solver ode45 is the best function to apply as a first try especially for non-stiff problems. It uses the Runge-Kutta numerical procedure. The ode15s is used for stiff problems. Matlab offers the following solvers for initial value problems of ODEs : ode23, ode45, ode113 , ode15s , ode23s , ode23t, ode23tb and ode15i. Detailed information about each solver can be read from Matlab help. Matlab solvers handle the following types of first-order ODEs: • Explizit ODEs of the form y ′ = f (t, y) • Linearly implicit ODEs of the form M (t, y)y ′ = f (t, y), where M (t, y) is a matrix • Fully implicit ODEs of the form f (t, y, y ′ ) = 0(ode15i only) Solver syntax All of the ODE solver functions, except for ode15i, share a syntax that makes it easy to try any of the different numerical methods, if it is not apparent which is the most appropriate. To apply a different method to the same problem, simply change the ODE solver function name. The simplest syntax, common to all the solver functions, is [T,Y] = solver(odefun,tspan,y0,options) where solver is one of the ODE solver functions listed previously. The basic input arguments are odefun : Handle to a function that evaluates the system of ODEs. The function has the form dydt = odefun(t,y), where t is a scalar, dydt and y are column vectors. tspan : Vector specifying the interval of integration. The solver imposes the initial conditions at tspan(1), and integrates from tspan(1) to tspan(end). y0 : Vector of initial conditions for the problem. options : Structure of optional parameters that change the default integration properties. The output arguments contain the solution approximated at discrete points: T : Column vector of time points. Y : Solution array. Each row in y corresponds to the solution at a time returned in the corresponding row of t. Example 11.2.1 Solve using the Matlab ODE solver the initial value problem (IVP) given by :

y ′ + ty = 0;

y(0) = 1

This equation was solved analytically in the previous section (see Figure 11.1). Here using ode45, which is the general purpose, almost the standard Matlab first trial ODE solver. First we rewrite the given equation in the standard form y ′ = f (t, y) y ′ = −ty. Hence f (t, y) = −ty. Step 1 Write a function for f (t, y). function dydt = odefun_1(t,y) dydt = -t*y ; Step 2 Now call the ODE solver : >> >> >> >> >>

[T, Y] = ode45(@odefun_1, [0 10], 1) ; plot(T,Y, ’-rs’), grid on, axis tight % for initial value of y(0) = 3, we will have another solution [T, Y] = ode45(@odefun_1, [0 10], 3) ; hold on, plot(T,Y, ’-go’), grid on, axis tight Fig. 11.2 : Numerical Soln of y’ + ty = 0 (IVP) using the Matlab function ode45 3 y(t), for y(0)=1 y(t), for y(0)=3 2.5

2

1.5

1

0.5

0

0

2

4

6

8

10

The ODE solver using the command ode45(@odefun_1, [0 10], 1) (without output argument) plots the solution of the given initial value problem directly, try it.

But if you want to plot the results of different initial value conditions with the colour and form of your choice then use the steps of Example 11.2.1 . Exercise 11b Solve the following initial value problems using the Matlab ODE solver ode45 and at least one other solver.

1. y ′ + 5y = −26 · sin(t),

y(0) = 0, t ∈ [0, 20]

2. y ′ + y = cos(t),

y(0) = 1, t ∈ [0, 10]

3. y ′ = y 2 · cos(t),

y(0) = 3, t ∈ [0, 3]

4. y ′ − y = exp(t),

y(0) = 1, t ∈ [0, 10]

The analytical solution to y ′ = y 2 can easily be calculated using the method of separation of variables. Solve y ′ = y 2 , y(0) = 1, analytically. Solve y ′ = y 2 , y(0) = 1, t ∈ [0, 10], with at least three different Matlab ODE solvers, beginning of course with ode45.

11.4

Systems of First order ODEs

Actually, systems are no harder to handle using ode45 or other solvers than are single equations. Consider the following system of n first order differential equations: ′

y1 = f1 (t, y1 , y2 , ..., yn ), ′

y2 = f2 (t, y1 , y2 , ..., yn ), .. .

(11.1)



yn = f2 (t, y1 , y2 , ..., yn ). System (11.1) can be written in the vector form 

y1  y2   ..  .

yn

′    



  ′ y1 f1 (t, [y1 , y2 , ..., yn ]T  y ′   f2 (t, [y1 , y2 , ..., yn ]T  2  =  ..  =  ..  .   . ′ fn (t, [y1 , y2 , ..., yn ]T yn

    

If we define the vector y = [y1 , y2 , ..., yn ]T , system (11.2) beccomes   f1 (t, y)  f2 (t, y)    y′ =   ..   . fn (t, y)

(11.2)

(11.3)

Finally, if we define F(t, y) = [f1 (t, y), f2 (t, y), ..., fn (t, y)]T then system (11.3) can be written as y′ = F(t, y) (11.4) which, most importantly, has a form identical to the single first order differential equation, y ′ = f (t, y) which was used in the previous section. Consequently, if function yprime = F(t, y) is the first line of a function ODE file, it is extremely important to understand that y is a vector with entries y(1), y(2), ..., y(n). Confused? Perhaps a few examples will clear the waters. Example 11.4.1 Use ode45 to solve the initial value problem

y1′ = y2 − y12 y2′ = −y1 − 2y1 y2 on the interval [0, 10], with initial conditions y1 (0) = 0 and y2 (0) = 1. Solution We can write Eq. (11.5) as the vector equation  ′   y1 y2 − y12 = . y2 −y1 − 2y1 y2

(11.5)

(11.6)

If we set F(t, [y1 , y2 ]T ) = [y2 − y12 , −y1 − 2y1 y2 ]T and y = [y1 , y2 ]T , then system (11.6) ′ takes the form y = F(t, y). The next step is to creat a function with the following ODE file. % Let odefun_7 be the name of the function. function yprime = odefun_7(t,y) yprime = zeros(2,1); % the output must be a column vector yprime(1)= y(2) - y(1)^2 ; yprime(2)= -y(1) - 2*y(1)*y(2) ; The initial condition vector is : y(0) =



y1 (0) y2 (0)



  0 = 1

Now, we call the ODE solver: >> [t, y] = ode45(@odefun_7, [0,10], [0 ; 1]); The command : plot(t,y) returns the figure below.

Initial value problem for system of first order 1.2 y 1

y

1 2

0.8 0.6 0.4 0.2 0 −0.2 −0.4 −0.6

0

2

4

6

8

10

Exercise 11c Plot the solution of the initial value problem x′1 = 9x1 + 4x2 x′2 = −2x1 + 2x2 x1 (0) = 1, x2 (0) = −1 over the interval [0, 10] Second Order Differential Equations MATLAB ODE solvers accept only first-order differential equations. To use the solvers with higher-order ODEs, you must rewrite each equation as an equivalent system of first-order differential equations of the form y ′ = f (t, y) You can write any ordinary differential equation y n = f (t, y, y ′ , y ′′ , · · · , y n−1 ) as a system of first-order equations by making the substitutions x1 = y, x2 = y ′ , · · · , xn = y n−1 The result is an equivalent system of n first-order ODEs. x′1 = x2 x′2 = x3 .. . ′ xn = f (x1 , x2 , ..., xn )

Second order ODE with initial values : y ′′ = f (t, y, y ′ ) with given inital values y(0) and y ′ (0). x1 = y, x2 = y ′ x′1 = x2 x′2 = f (t, x1 , x2 ) The next steps are like Example 11.2.1. Exercise 11d Plot the solution of the initial value problem y ′′ + yy ′ + y = 0 y(0) = 0, y ′ (0) = 1 on the interval [0, 10].

11.5

Homogeneous Linear Systems of ODEs