Sei sulla pagina 1di 9

CHAPTER 4

MATHEMATICS WITH MATLAB

4.1 DATA CLASSES


Every variable you define in MATLAB, as well as every input to, and output from, a
command, is an array of data belonging to a particular class. We can use primarily the
following types of data: floating-point numbers, symbolic expressions, character strings,
unction handles, and inline functions, Type of data, its class (as given by whos) and how you
can create it are listed in Table 4.1.

Table 4.1: MATLAB Data Classes.


Type of data Class Created by
Floating-point double Typing a number
Symbolic sym Using sym or syms
Character string char Typing a string inside single quotes
Function handle function_handle Using @
Inline function inline Using inline

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

The string output, like symbolic output, is not indented.

4.1.2 Symbolic and Floating-Point Numbers


You can convert between symbolic numbers and floating point numbers with double and
sym. Numbers that you type are, by default, floating-point. However, if you mix symbolic
and floating-point numbers in an arithmetic expression, the floating-point numbers are
automatically converted into symbolic numbers. This explains why you can type syms x and
then xˆ2 without having to convert 2 into a symbolic number. Here is another example:
> a=2
a =
2
>> b=3
b =
3
>> c=sym(a/b)
c =
2/3

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)’).

4.2 COMPLEX ARITHMETIC


MATLAB does most of its computations using complex numbers; i.e., numbers of the form a
+ bi, where i = √−1, and a and b are real numbers. The complex number i is represented as i
in MATLAB. Although you may never have occasion to enter a complex number in a
MATLAB session, MATLAB often produces an answer involving complex numbers. For
example, many polynomials with real coefficients have complex roots.
>> solve('x^2+2*x+2=0')

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.

4.3 MORE ON MATRICES


In addition to the usual algebraic methods of combining matrices (e.g., matrix multiplication),
we can also combine them element-wise. Specifically, if A and B are the same size, then
A.*B is the element-by-element product of A and B, i.e., the matrix whose i, j element is the
product of the i, j elements of A and B. Likewise, A./B is the element-by-element quotient of
A and B, and A.ˆc is the matrix formed by raising each of the elements of A to the power c.
More generally, if f is one of the built-in mathematical functions in MATLAB, or is a user-
defined vectorized function, then f(A) is the matrix obtained by applying f element-by-
element to A.

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 CALCULUS WITH MATLAB


MATLAB has in its Symbolic Math Toolbox built-in commands for most of the
computations of basic calculus.

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

Here MATLAB has figured out that the variable is x. Alternatively,

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.

hardintegral = int(log(1 + x^2)*exp(-x^2), x, 0, 1)


Warning: Explicit integral could not be found.
> In sym.int at 58
hardintegral =
int(log(1+x^2)*exp(-x^2),x = 0 .. 1)

>> quadl(@(x) log(1 + x.^2).*exp(-x.^2), 0, 1)


ans =
0.1539

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

Limits at infinity can be computed using the symbol Inf.


>> limit((x^4 + x^2 - 3)/(3*x^4 - log(x)), x, Inf)

ans =
1/3

4.4.4 Taylor Series


You can use taylor to generate Taylor polynomial expansions of a specified degree at a
specified point. For example, to generate the Taylor polynomial up to degree 9 at x = 0 of the
function cos x, we enter
>> taylor(cos(x), x, 10)

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.

The command taylor can also compute Taylor expansions at infinity.


>> taylor(exp(x^-2), 6, Inf)
ans =
1+1/x^2+1/2/x^4

4.5 SUM AND PRODUCTS


Finite numerical sums and products can be computed easily using the vector capabilities of
MATLAB and the commands sum and prod. For example,
>> x=1:6;
>> sum(x)
ans =
21
>> prod(x)
ans =
720

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

Here is the well-known infinite sum



1

𝑛2
𝑛=1
>> symsum(n^-2,1,Inf)
ans =
1/6*pi^2

Another familiar example is the sum of the infinite geometric series:


>> syms a k;
>> symsum(a^k, 0, Inf)
ans =
-1/(a-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) ∫−∞ 𝑒 −𝑥 𝑑𝑥

3. Compute the following integrals numerically using quadl.


𝜋
(a) ∫0 𝑒 𝑠𝑖𝑛𝑥 𝑑𝑥
1
(b) ∫0 √𝑥 3 + 1 𝑑𝑥
∞ 2
(c) ∫−∞ 𝑒 −𝑥 𝑑𝑥 . Also compare with solution of 2(e).

4. Evaluate the following limits:


𝑠𝑖𝑛𝑥
(𝐚) lim
𝑥→0 𝑥

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

6. Compute the following sums:


𝑛

(𝐚) ∑ 𝑘 2
𝑘=1
𝑛

(𝐛) ∑ 𝑟 𝑘
𝑘=0

𝑥𝑘
(𝐜) ∑
𝑘!
𝑘=0

1
(𝐝) ∑
(𝑧 − 𝑘)2
𝑘=−∞

MATLAB 36 CHAPTER 4

Potrebbero piacerti anche