Professional Documents
Culture Documents
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
>>
2. Matrix Functions
Much of MATLAB’s power comes from its matrix functions. Some useful ones are:
[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)
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
lim f ( x ) Limit(f,x,a)
x a
lim f ( x ) Limit(f,x,a,’left’)
x a
3.3 Integration
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
4.Solving Equations
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
Example 1
The following call to dsolve
dsolve('Dy=1+y^2')
6
To specify an initial condition, use
y = dsolve('Dy=1+y^2','y(0)=1')
y=
tan(t+1/4*pi)
Solving Equation
X1 2X 2 X 3 1
2 X 1 6 X 2 4 X 3 2
X 1 3X 2 3X 3 1
1 2 1 1
A 2 6 4 ; b 2
1 3 3 1
AX b
A = [ 1 2 -1; -2 -6 4 ; -1 -3 3 ]
b = [ 1; -2; 1 ]
X = A\b
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.
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)
S 4 9S 3 45S 2 87S 50 0
8
Polynomial Evaluation
>> 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
>>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)
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
(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).
12S
a. ( S 9)
2 2
4- solve
7- solve
8-solve ( ) ( )
11