Академический Документы
Профессиональный Документы
Культура Документы
You can think of an array as a two-dimensional grid of data. A single number (or symbolic
expression) is regarded by MATLAB as a 1 × 1 array, sometimes called a scalar. A 1 × n
array is called a row vector, and an m × 1 array is called a column vector. (A string is actually
a row vector of characters.) An m × n array of numbers is called a matrix. You can see the
class and array size of every variable you have defined by looking in the Workspace Browser
or typing whos. The set of variable definitions shown by whos is called your Workspace.
In order to use MATLAB commands effectively, you must pay close attention to the class of
data each command accepts as input and returns as output. The input to a command consists
of one or more arguments separated by commas; some arguments are optional. Some
commands, like whos, do not require any input. The help text for each command usually tells
what classes of inputs the command expects as well as what class of output it returns.
Many commands allow more than one class of input, though sometimes only one data class is
mentioned in the online help. This flexibility can be a convenience in some cases and a pitfall
in others. For example, the integration command, int, accepts strings as well as symbolic
input, though its help text mentions only symbolic input.
Sometimes you need to convert one data class into another in order to prepare the output of
one command to serve as the input for another. For example, we have used double to convert
symbolic expressions to floating-point numbers and sym to convert numbers or strings to
symbolic expressions. The commands num2str and str2num convert between numbers and
strings, while char converts a symbolic expression to a string. You can also use vectorize to
convert a symbolic expression to a vectorized string; it adds a . before every *, /, and ˆ in the
expression.
MATLAB 28 CHAPTER 4
4.1.1 String Manipulation
Often it is useful to concatenate two or more strings together. The simplest way to do this is
to use MATLAB’s vector notation, keeping in mind that a string is a “row vector” of
characters. For example, typing [string1, string2] combines string1 and string2 into one
string.
Here is a useful application of string concatenation. You may need to define a string variable
containing an expression that takes more than one line to type. (In most circumstances you
can continue your MATLAB input onto the next line by typing ... followed by ENTER or
RETURN, but this is not allowed in the middle of a string.) The solution is to break the
expression into smaller parts and concatenate them, as in:
>> eqn = ['left hand side of equation = ', ...
'right hand side of equation']
eqn =
left hand side of equation = right hand side of equation
MATLAB was designed so that some floating-point numbers are restored to their exact
values when converted into symbolic numbers. Integers, rational numbers with small
numerators and denominators, square roots of small integers, the number π, and certain
combinations of these numbers are so restored. For example:
>> d=1/sqrt(5)
d =
0.4472
>> sym(d)
ans =
sqrt(1/5)
MATLAB 29 CHAPTER 4
Since it is difficult to predict when MATLAB will preserve exact values, it is best to suppress
the floating-point evaluation of a numerical argument to sym by enclosing it in single quotes
to make it a string, e.g., sym('2 + sqrt(5)’).
ans =
-1+i
-1-i
Both roots of this quadratic equation are complex numbers, expressed in terms of the number
i. Some common functions also return complex values for certain values of the argument.
>> log(-5)
ans =
1.6094 + 3.1416i
You can use MATLAB to do computations involving complex numbers by entering numbers
in the form a + b*i.
>> (4+5*i)/(2+i)
ans =
2.6000 + 1.2000i
Complex arithmetic is a powerful and valuable feature. Even if you don’t intend to use
complex numbers, you should be alert to the possibility of complex-valued answers when
evaluating MATLAB expressions.
A(2,3) represents the 2, 3 element of A, i.e., the element in the second row and third column.
You can specify submatrices in a similar way. Typing A(2,[2 4]) yields the second and fourth
elements of the second row of A. To select the second, third, and fourth elements of this row,
MATLAB 30 CHAPTER 4
type A(2,2:4). The submatrix consisting of the elements in rows 2 and 3 and in columns 2, 3,
and 4 is generated by A(2:3,2:4). A colon by itself denotes an entire row or column. For
example, A(:,2) denotes the second column of A, and A(3,:) yields the third row of A.
4.4.1 Differentiation
You can use diff to differentiate symbolic expressions, and also to approximate the derivative
of a function given numerically (say by an M-file).
> syms x
>> diff(x^3+2*x^2-x)
ans =
3*x^2+4*x-1
f = @(x) x^3
f =
@(x)x^3
>> diff(f(x))
ans =
3*x^2
The syntax for second derivatives is diff(f(x), 2), and for nth derivatives, diff(f(x), n).
>> diff(f(x),2)
ans =
6*x
The command diff can also compute partial derivatives of expressions involving several
variables, as in diff(xˆ2*y, y),
>> diff(x^2*y, y)
ans =
x^2
To perform multiple partials with respect to mixed variables you must use diff repeatedly, as
in diff(diff(sin(x*y/z), x), z)).
>> diff(diff(sin(x*y/z),x),z)
ans =
sin(x*y/z)*x*y^2/z^3-cos(x*y/z)*y/z^2
MATLAB 31 CHAPTER 4
4.4.2 Integration
MATLAB can compute definite and indefinite integrals. Here is an indefinite integral:
>> int('3*x^2+4*x-1','x')
ans =
x^3+2*x^2-x
As with diff, you can declare x to be symbolic and dispense with the character string quotes.
Note that MATLAB does not include a constant of integration; the output is a single
antiderivative of the integrand. Now here is a definite integral:
>> int('x+sin(x)',0,pi)
ans =
2+1/2*pi^2
>> eval(ans)
ans =
6.9348
You are undoubtedly aware that not every function that appears in calculus can be
symbolically integrated, and so numerical integration is sometimes necessary. MATLAB has
two commands for numerical integration of a function f(x): quad and quadl.
The commands quad and quadl will not accept Inf or -Inf as a limit of integration (though int
will). The best way to handle a numerical improper integral over an infinite interval is to
evaluate it over intervals of increasing length until the result stabilizes.
MATLAB can also do multiple integrals. The following command computes the double
integral
𝜋 𝑠𝑖𝑛𝑥
∫ ∫ (𝑥 2 + 𝑦 2 )𝑑𝑦𝑑𝑥
0 0
>> syms x y
>> int(int(x^2 + y^2, y, 0, sin(x)), 0, pi)
ans =
MATLAB 32 CHAPTER 4
-32/9+pi^2
>> eval(ans)
ans =
6.3140
Note that MATLAB presumes that the variable of integration in int is x unless you prescribe
otherwise. Note also that the order of integration is as in calculus, from the “inside out.”
There is a numerical double-integral command dblquad.
4.4.3 Limits
You can use limit to compute right- and left-handed limits and limits at infinity. For example,
here is
𝑠𝑖𝑛(𝑥)
lim
𝑥→0 𝑥
>> limit(sin(x)/x, x, 0)
ans =
1
To compute one-sided limits, use the 'right' and 'left' options. For example:
>> limit(abs(x)/x, x, 0, 'left')
ans =
-1
ans =
1/3
ans =
1-1/2*x^2+1/24*x^4-1/720*x^6+1/40320*x^8
You can also compute a Taylor polynomial at a point other than the origin. For example:
>> taylor(exp(x), 4, 1)
ans =
MATLAB 33 CHAPTER 4
exp(1)+exp(1)*(x-1)+1/2*exp(1)*(x-1)^2+1/6*exp(1)*(x-1)^3
computes a Taylor polynomial of ex centered at the point x = 1.
You can do finite and infinite symbolic sums using the command symsum. To illustrate, here
is the telescoping sum
𝑛
1 1
∑( − )
𝑘 1+𝑘
𝑘=1
>> syms k n
>> symsum(1/k-1/(k+1),1,n)
ans =
-1/(n+1)+1
MATLAB 34 CHAPTER 4
EXERCISE
1. Find the derivatives of the following and if possible, simplify each answer:
(a) f(x) = 6x3 − 5x2 + 2x – 3
2𝑥 − 1
(𝒃) 𝑓(𝑥) =
𝑥2 + 1
(c) f(x) = sin(3x2 + 2)
(d) f(x) = arcsin(2x + 3)
(e) 𝑓(𝑥) = √1 + 𝑥 4
(f) f(x) = xr
(g) f(x) = arctan(x2 + 1).
2. See whether MATLAB can do the following integrals symbolically, and for the indefinite
integrals, check the results by differentiating:
𝜋⁄
(a) ∫0 2 𝑐𝑜𝑠𝑥 𝑑𝑥
(b) ∫ 𝑥 𝑠𝑖𝑛(𝑥 2 )𝑑𝑥
(c) ∫ sin(3𝑥)√1 − cos(3𝑥)𝑑𝑥
(d) ∫ 𝑥 2 √𝑥 + 4 𝑑𝑥
∞ 2
(e) ∫−∞ 𝑒 −𝑥 𝑑𝑥
1 + 𝑐𝑜𝑠𝑥
(𝐛) lim
𝑥→−𝜋 𝑥 + 𝜋
(𝐜) lim 𝑥 2 𝑒 −𝑥
𝑥→∞
1
(𝐝) lim−
𝑥→1 𝑥 − 1
1
(𝐞) lim+ 𝑠𝑖𝑛 ( )
𝑥→0 𝑥
MATLAB 35 CHAPTER 4
5. Find the Taylor polynomial of the indicated degree n at the indicated point c for the
following functions:
(a) f(x) = ex, n = 6, c = 0
(b) f(x) = sinx, n = 4 and n = 6, c = 0
(c) f(x) = sinx, n = 5, c = 2
(d) f(x) = tanx, n = 6, c = 0
(e) f(x) = lnx, n = 4, c = 1
(f) f(x) = erfx, n = 8, c = 0
(𝐚) ∑ 𝑘 2
𝑘=1
𝑛
(𝐛) ∑ 𝑟 𝑘
𝑘=0
∞
𝑥𝑘
(𝐜) ∑
𝑘!
𝑘=0
∞
1
(𝐝) ∑
(𝑧 − 𝑘)2
𝑘=−∞
MATLAB 36 CHAPTER 4