experiment 2: introduction to matlab...

10
1 Experiment 2: Introduction to MATLAB II 1.Vector, Matrix and Array Commands Some of MATLAB functions operate essentially on a vector (row or column), and others on an m-by-n matrix (m >= 2) . Array Commands find Finds indices of nonzero elements. length Computers number of elements. linspace Creates regularly spaced vector. logspace Creates logarithmically spaced vector. max Returns largest element. min Returns smallest element. prod Product of each column. reshape Change size size Computes array size. sort Sorts each column. sum Sums each column. >> X = [1 0 4 -3 0 0 0 8 6]; indices = find(X) indices = 1 3 4 8 9 >> indices = find(X>2) indices = 3 8 9 >> length(X) ans = 9 >> max(X) ans = 8 >> min(X) ans = -3 > sort(X) ans = -3 0 0 0 0 1 4 6 8 >> sum(X) ans = 16

Upload: others

Post on 01-Aug-2020

38 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Experiment 2: Introduction to MATLAB IIsite.iugaza.edu.ps/mmashharawi/files/2015/09/Experiment... · 2015-09-27 · 1 Experiment 2: Introduction to MATLAB II. 1. Vector, Matrix and

1

Experiment 2: Introduction to MATLAB II

1.Vector, Matrix and Array Commands Some of MATLAB functions operate essentially on a vector (row or column), and others on an m-by-n matrix (m >= 2) .

Array Commands

find Finds indices of nonzero elements. length Computers number of elements. linspace Creates regularly spaced vector. logspace Creates logarithmically spaced vector. max Returns largest element. min Returns smallest element. prod Product of each column. reshape Change size size Computes array size. sort Sorts each column. sum

Sums each column.

>> X = [1 0 4 -3 0 0 0 8 6]; indices = find(X) indices = 1 3 4 8 9 >> indices = find(X>2) indices = 3 8 9 >> length(X) ans = 9 >> max(X) ans = 8 >> min(X) ans = -3 > sort(X) ans = -3 0 0 0 0 1 4 6 8 >> sum(X) ans = 16

Mohammed
Sticky Note
y = linspace(a,b) Create a vector of 100 linearly spaced numbers from a to b: y = linspace(a,b,n) generates a row vector y of n points linearly spaced between and including a and b. For n < 2, linspace returns b. A = linspace(1,500); = linspace(1,36,12);
Mohammed
Sticky Note
y = logspace(a,b) generates a row vector y of 50 logarithmically spaced points between decades 10^a and 10^b. y = logspace(a,b,n) y = logspace(a,b,n) generates n points between decades 10^a and 10^b.
Mohammed
Sticky Note
M = 8 1 6 3 5 7 4 9 2 The product of the elements in each column is prod(M) = 96 45 84 The product of the elements in each row can be obtained by: prod(M,2) = 48 105 72
Mohammed
Sticky Note
B = reshape(A,m,n) returns the m-by-n matrix B whose elements are taken column-wise from A. An error results if A does not have m*n elements. Reshape a 3-by-4 matrix into a 2-by-6 matrix. A = 1 4 7 10 2 5 8 11 3 6 9 12 B = reshape(A,2,6) B = 1 3 5 7 9 11 2 4 6 8 10 12 B = reshape(A,2,[]) B = 1 3 5 7 9 11 2 4 6 8 10 12
Page 2: Experiment 2: Introduction to MATLAB IIsite.iugaza.edu.ps/mmashharawi/files/2015/09/Experiment... · 2015-09-27 · 1 Experiment 2: Introduction to MATLAB II. 1. Vector, Matrix and

2

>> sum(X,2) ans = 16 >> sum(X,1) ans = 1 0 4 -3 0 0 0 8 6 >>

Create a vector of 4 linearly spaced numbers from 1 to 12: >> A = linspace(1,12,4) A = 1.0000 4.6667 8.3333 12.0000

2. Matrix Functions Much of MATLAB’s power comes from its matrix functions. Some useful ones are: eig eigenvalues and eigenvectors inv inverse poly characteristic polynomial det determinant size size rank rank [V,D] = eig(A) produces matrices of eigenvalues (D) and eigenvectors (V) of matrix A M = magic(n) returns an n-by-n matrix constructed from the integers 1 through n^2 with equal row and column sums. The order n must be a scalar greater than or equal to 3.

>> A=magic(3) A = 8 1 6 3 5 7 4 9 2 >> [c d]=eig(A) c = -0.5774 -0.8131 -0.3416 -0.5774 0.4714 -0.4714 -0.5774 0.3416 0.8131 d = 15.0000 0 0 0 4.8990 0 0 0 -4.8990

Page 3: Experiment 2: Introduction to MATLAB IIsite.iugaza.edu.ps/mmashharawi/files/2015/09/Experiment... · 2015-09-27 · 1 Experiment 2: Introduction to MATLAB II. 1. Vector, Matrix and

3

3. Calculus

The Symbolic Math Toolbox provides functions to do the basic operations ofcalculus; differentiation, limits, integration, summation, and Taylor seriesexpansion. The following sections outline these functions. 3.1 Differentiation

diff(f)

differentiates f with respect to its symbolic variable (in this case x)

Let’s create a symbolic expression. >>syms a x f = sin(a*x) diff(f) f = sin(a*x) ans = a*cos(a*x)

To differentiate with respect to the variable a, type diff(f,a)

which returns df / da ans =

cos(a*x)*x

To calculate the second derivatives with respect to x and a, respectively, type diff(f,2)

or diff(f,x,2)

which return ans =

-sin(a*x)*a^2

3.2 Limits

The fundamental idea in calculus is to make calculations on functions as a Variable“gets close to”orapproaches a certain value. Recall that the definition of the derivative is given by a limit

provided this limit exists. The Symbolic Math Toolbox allows you to computethe limits of functions in a direct manner.

>>syms h n x limit( (cos(x+h) - cos(x))/h,h,0 ) ans = -sin(x)

And

Mohammed
Sticky Note
Shortcut for creating symbolic variables and functions
Page 4: Experiment 2: Introduction to MATLAB IIsite.iugaza.edu.ps/mmashharawi/files/2015/09/Experiment... · 2015-09-27 · 1 Experiment 2: Introduction to MATLAB II. 1. Vector, Matrix and

4

limit( (1 + x/n)^n,n,inf ) ans = exp(x)

In the case of undefined limits, the Symbolic Math Toolbox returns NaN (not a number). The command

limit(1/x,x,0) or limit(1/x) returns ans = NaN

Observe that the default case ,limit(f) is the same as limit(f,x,0).Explore the options for the limit command in this table. Here, we assume that f is a function of the symbolic object x. Mathmatical Operation MATLAB command

0lim ( )x

f x

Limit(f)

lim ( )x a

f x

Limit(f,x,a)

lim ( )x a

f x

Limit(f,x,a,’left’)

lim ( )x a

f x

Limit(f, x,a,’right’)

3.3 Integration

If f is a symbolic expression, then the integration of f int(f)

We can do this in (at least) three different ways. The shortest is:

>>int(’xˆ2’) ans = 1/3*xˆ3

Alternatively, we can define x symbolically first, and then leave off the single quotes in theint statement.

>>syms x >>int(xˆ2) ans = 1/3*xˆ3

Page 5: Experiment 2: Introduction to MATLAB IIsite.iugaza.edu.ps/mmashharawi/files/2015/09/Experiment... · 2015-09-27 · 1 Experiment 2: Introduction to MATLAB II. 1. Vector, Matrix and

5

Mathematical Operation MATLAB Command

int(x^n) or int(x^n,x)

>>int(sin(2*x),x,0,pi/2) ans = 1

>> g = 'cos(a*t + b)' g = cos(a*t + b) >>int(g) ans = sin(b + a*t)/a

4.Solving Equations

Solving Algebraic Equations If S is a symbolic expression, solve(S) attempts to find values of the symbolic variable in S (as determined byfindsym) for which S is zero. For example,

>>syms a b c x S = a*x^2 + b*x + c; solve(S) ans = -(b + (b^2 - 4*a*c)^(1/2))/(2*a) -(b - (b^2 - 4*a*c)^(1/2))/(2*a)

This is a symbolic vector whose elements are the two solutions. If you want to solve for aspecific variable, you must specify that variable as an additional argument. For example, if you want to solve S for b, use thecommand

>> b = solve(S,b) b = -(a*x^2 + c)/x

Note that these examples assume equations of the form f(x) = 0. If you need to solve equations of the form f(x)=q(x) you must use quoted strings. In particular, the command

s = solve('cos(2*x)+sin(x)=1') s = 0 pi/6 (5*pi)/6

Page 6: Experiment 2: Introduction to MATLAB IIsite.iugaza.edu.ps/mmashharawi/files/2015/09/Experiment... · 2015-09-27 · 1 Experiment 2: Introduction to MATLAB II. 1. Vector, Matrix and

6

4.1 Several Algebraic Equations Now let’s look at systems of equations. Suppose we have the system

and we want to solve for x and y. First create the necessary symbolic objects. There are several ways to address the output of solve. One is to use a two-output call

>>syms x y alpha >> [x,y] = solve(x^2*y^2, x-y/2-alpha) x = alpha 0 y = 0 (-2)*alpha

5: Single Differential Equation

The function dsolve computes symbolic solutions to ordinary differential equations. The equations are specified by symbolic expressions containing the letter D to denote differentiation. The symbols D2, D3, ... DN, correspond to the second, third, ..., Nth

derivative, respectively. Thus, D2y is the Symbolic Mathof

The dependent variables are those preceded by D and the default independent variable is t. Note that names of symbolic variables should not contain D.The independent variable can be changed from t to some other symbolic variable by including that variable as the last input argument. Initial conditions can be specified by additional equations. If initial conditions are not specified, the solutions contain constants of integration, C1, C2, etc. The output from dsolve parallels the output from solve. That is, you can call D solve with the number of output variables equal to the number of dependent variables or place the output in a structure whose fields contain the solutions of the differential equations. Example 1 The following call to dsolve dsolve('Dy=1+y^2')

uses y as the dependent variable and t as the default independent variable. The output of this command is

>>dsolve('Dy=1+y^2') ans = i -i tan(C4 + t)

Page 7: Experiment 2: Introduction to MATLAB IIsite.iugaza.edu.ps/mmashharawi/files/2015/09/Experiment... · 2015-09-27 · 1 Experiment 2: Introduction to MATLAB II. 1. Vector, Matrix and

7

To specify an initial condition, use

y = dsolve('Dy=1+y^2','y(0)=1') y = tan(t+1/4*pi)

Notice that y is in the MATLAB workspace, but the independent variable t is not. Thus, the command diff(y,t) returns an error. To place t in the workspace, type syms t. 6 .Linear algebra in MATLAB

Solving Equation

One of the most important problems in technical computing is the solution of simultaneous linear equations. In matrix notation, this problem can be stated as follows.

we may need to find x1, x2, and x3 so that

133

2462

12

321

321

321

XXX

XXX

XXX

The problem can be rewritten in matrix-vector notation. We introduce a matrix A and a vector b by

1

2

1

;

331

462

121

bA

Now we want to find the solution vector 321 XXXX so that

bAX

A = [ 1 2 -1; -2 -6 4 ; -1 -3 3 ] b = [ 1; -2; 1 ] X = A\b

Matlab should give the solution

2

2

1

3

2

1

X

X

X

Page 8: Experiment 2: Introduction to MATLAB IIsite.iugaza.edu.ps/mmashharawi/files/2015/09/Experiment... · 2015-09-27 · 1 Experiment 2: Introduction to MATLAB II. 1. Vector, Matrix and

8

7. Polynomial Roots and Characteristic Polynomial

If p is a row vector containing the coefficients of a polynomial, roots(p) returns a column vector whose elements are the roots of the polynomial. If r is a column vector containing the roots of a polynomial, poly(r) returns a row vector whose elements are the coefficients of the polynomial. To find the roots of following polynomial

1575.1475.6725.6125.319 23456 SSSSSS The polynomial coefficients are entered in a row vector in descending powers. The roots are found using roots.

p = [ 1 9 31.25 61.25 67.75 14.75 15 ] r = roots(p)

The polynomial roots are obtained in column vector

r = -4.0000 -3.0000 -1.0000 + 2.0000i -1.0000 - 2.0000i 0.0000 + 0.5000i 0.0000 - 0.5000i

If we want to find the coefficient of polynomial that has the roots-1, -2, -3 j4. We write this

r = [-1 -2 -3+4i -3-4i ] p = poly(r)

The coefficients of the polynomial equation are obtained in a row vector. p = 1 9 45 87 50

Therefore, the polynomial equation is

05087459 234 SSSS

Page 9: Experiment 2: Introduction to MATLAB IIsite.iugaza.edu.ps/mmashharawi/files/2015/09/Experiment... · 2015-09-27 · 1 Experiment 2: Introduction to MATLAB II. 1. Vector, Matrix and

9

Polynomial Evaluation

If c is a vector whose elements are the coefficients of a polynomial in descending powers, the polyval(c, x) is the value of the polynomial evaluated at x. For example, to evaluate the above polynomial at points 0, 1, 2, 3, and 4, use the commands

>> c = [1 2 3 1]; x = 0:1:4; y = polyval(c, x) y = 1 7 23 55 109

You may use the following functions; try to find out its function

Polyval,polyvalm

8. Laplace Transformation The laplace Transformation in MATLAB is very easy way. MATLAB has a function called (laplace) which transfer a function from time-domain to S-Domain. Befor you use this function you must declare the variable by syms function, see the below examble >>syms t >>laplace(t^5) ans = 120/s^6 To get the laplace inverse it is very easy also, only use ilaplace after declare the variable >>syms s ilaplace(1/(s-1)) ans = exp(t) Or by using Partial-Faction Expansion with MatLab as below example

Page 10: Experiment 2: Introduction to MATLAB IIsite.iugaza.edu.ps/mmashharawi/files/2015/09/Experiment... · 2015-09-27 · 1 Experiment 2: Introduction to MATLAB II. 1. Vector, Matrix and

10

Consider the following transfer function:

6116

635223

23

SSS

SSS

we write num=[2 5 3 6] den=[1 6 11 6] [r,p,k]=residue(num,den) r -6 -4 3 p -3 -2 -1 k 2 Which mean

21

3

2

4

3

6

SSS