You are on page 1of 11

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

1
>> 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

2
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“getscloseto”orapproachesacertainvalue. Recallthatthedefinition
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

3
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


lim f ( x ) Limit(f)
x 0

lim f ( x ) Limit(f,x,a)
x a

lim f ( x ) Limit(f,x,a,’left’)
x a 

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


x a 

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

4
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.


Ifyouwanttosolveforaspecificvariable,youmustspecifythatvariableasanadditional
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 needto solve
equations of the form f(x)=q(x) you must use quoted strings. Inparticular, the
command
s = solve('cos(2*x)+sin(x)=1')
s=
0
pi/6
(5*pi)/6

5
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 differentialequations.


The equations are specified by symbolic expressions containing theletter D to denote
differentiation. The symbols D2, D3, ... DN, correspond to thesecond, third, ..., Nth
derivative, respectively. Thus, D2y is the Symbolic Mathof
The dependent variables are those preceded byD and the default independent variable
is t. Note that names of
symbolicvariablesshouldnotcontainD.Theindependentvariablecanbechangedfromt to
some other symbolic variable by including that variable as the last inputargument.
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 out put 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)

6
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

X1  2X 2  X 3  1
 2 X 1  6 X 2  4 X 3  2
 X 1  3X 2  3X 3  1

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


a vector b by

1 2  1 1 
A   2 6 4  ; b   2
 1 3 3  1 

Now we want to find the solution vector X  X 1 X2 X 3  so that

AX  b

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

Matlab should give the solution

X1   1
 X   2 
 2  
 X 3  2 

7
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


S 6  9S 5  31.25S 4  61.25S 3  67.75S 2  14.75S  15
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

S 4  9S 3  45S 2  87S  50  0

8
 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
symsfunction,see the below examble

>>syms t
>>laplace(t^5)
ans =
120/s^6

To get the laplaceinverse it is very easy also, only use ilaplace after decler the
variable

>>syms s
ilaplace(1/(s-1))
ans =
exp(t)

Or by using Partial-Faction Expansion with MatLab as below example

9
Consider the following transfer function:
2S 3  5S 2  3S  6
S 3  6S 2  11S  6
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

6 4 3
  2
S  3 S  2 S 1

10
Exercise

1. Write an M-file to code the function


g ( x)  (1  2 x 2 ) exp(  x 2 )

(a.) Test your function by evaluating it at x  1 and x  2 .

(b.) Test that your function works correctly when x is a vector by evaluating

X =[2 3 4 5 0 ]

(c.) Plot your function for  3  x  3 using both fplot and the plot command
(learn more about fplot).

2. Find the inverse laplace transforms and the partial fraction of

12S
a. ( S  9)
2 2

 (3S 2  34S  108)


b. S ( S  6) 2

3- Use symbolic integration to fined ∫

4- solve

5- find the second derivative of f(x)=sin(ex).

6- find the derivative of f(x,y)=x3y4+ysinx. ( )

7- solve

8-solve ( ) ( )

11