experiment 2: introduction to matlab 2015-09-27¢  1 experiment 2: introduction to matlab...

Download Experiment 2: Introduction to MATLAB 2015-09-27¢  1 Experiment 2: Introduction to MATLAB II. 1. Vector,

Post on 01-Aug-2020

9 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • 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

  • 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

  • 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

  • 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

    0 lim ( ) 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

  • 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

  • 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)

  • 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 ma

View more >