1.1 Numerical Computation with MATLAB
You can use MATLAB as a powerful numerical computer. While most calculators handle numbers only to a preset degree of precision, MATLAB performs exact calculations to any desired degree of precision. In addition, unlike calculators, we can perform operations not only with individual numbers, but also with objects such as arrays.
Most of the topics of classical numerical analysis are treated by this software. It supports matrix calculus, statistics, interpolation, least squares fitting, numerical integration, minimization of functions, linear programming, numerical and algebraic solutions of differential equations and a long list of further methods that well meet as this book progresses.
Here are some examples of numerical calculations with MATLAB. (To obtain the results simply press Enter once the desired command has been entered after the prompt >> .)
We calculate 4 + 3 to obtain the result 7. To do this, just type 4 + 3, and then Enter .
>> 4 + 3
ans =
We find the value of 3 to the power of 100, without having previously set the precision. To do this we simply enter 3 ^ 100.
>> 3 ^ 100
ans =
5. 1538e + 047
We can use the command format long e to obtain results to 15 digits (floating-point).
>> format long e
>> 3^100
ans =
5.153775207320115e + 047
We can also work with complex numbers. We find the result of the operation raising (2 + 3i) to the power 10 by typing the expression (2 + 3i) ^ 10.
>> (2 + 3i) ^ 10
ans =
-1 415249999999998e + 005 - 1. 456680000000000e + 005i
The previous result is also available in short format, using the format short command.
>> format short
>> (2 + 3i)^10
ans =
-1.4152e + 005- 1.4567e + 005i
We can calculate the value of the Bessel function J 0 at 11.5. To do this we type besselj(0,11.5).
>> besselj(0,11.5)
ans =
-0.0677
We can also perform numerical integration. To calculate the integral of sin(sin( x )) between 0 and we type int(sin((sin(x)), 0, pi).
>> int ('sin(sin(x))', 0, pi)
ans =
1235191162052677/2251799813685248 * pi
These ideas will be treated more thoroughly later in the book.
1.2 Symbolic Computation with MATLAB
MATLAB perfectly handles symbolic mathematical computations, manipulating and performing operations on formulae and algebraic expressions with ease. You can expand, factor and simplify polynomials and rational and trigonometric expressions, find algebraic solutions of polynomial equations and systems of equations, evaluate derivatives and integrals symbolically, find solutions of differential equations, manipulate powers, and investigate limits and many other features of algebraic series.
To perform these tasks, MATLAB first requires all the variables (or algebraic expressions) to be written between single quotes. When MATLAB receives a variable or expression in quotes, it is interpreted as symbolic.
Here are some examples of symbolic computations with MATLAB.
We can expand the following algebraic expression: (( x + 1)( x + 2) - ( x + 2) ^ 2)^3. This is done by typing: expand(((x + 1)(x + 2) - (x + 2) ^ 2) ^ 3). The result will be another algebraic expression:
>> syms x; expand(((x + 1) *(x + 2)-(x + 2) ^ 2) ^ 3)
ans =
-x ^ 3-6 * x ^ 2-12 * x-8
We can factor the result of the calculation in the above example by typing: factor(((x + 1) *(x + 2) - (x + 2) ^ 2) ^ 3)
>> syms x; factor(((x + 1)*(x + 2)-(x + 2)^2)^3)
ans =
-(x+2)^3
We can find the indefinite integral of the function ( x ^ 2) sin( x ) ^ 2 by typing: int(x ^ 2 * sin(x) ^ 2, x)
>> int('x^2*sin(x)^2', 'x')
ans =
x ^ 2 *(-1/2 * cos(x) * sin(x) + 1/2 * x)-1/2 * x * cos(x) ^ 2 + 1/4 * cos(x) * sin(x) + 1/4 * 1/x-3 * x ^ 3
We can simplify the previous result:
>> syms x; simplify(int(x^2*sin(x)^2, x))
ans =
sin(2*x)/8 -(x*cos(2*x))/4 -(x^2*sin(2*x))/4 + x^3/6
We can present the previous result using a more elegant mathematical notation:
>> syms x; pretty(simplify(int(x^2*sin(x)^2, x)))
ans =
2 3
sin(2 x) x cos(2 x) x sin(2 x) x
-------- - ---------- - ----------- + --
8 4 4 6
We can find the series expansion up to order 12 of the function x ^ 2 * sin (x) ^ 2, presenting the result in elegant form:
>> pretty(taylor('x^2*sin(x)^2',12))
4 6 8 10 12
x - 1/3 x + 2/45 x - 1/315 x + o(x)
We can solve the equation 3 ax - 7 x ^ 2 + x ^ 3 = 0 (where a is a parameter):
>> solve('3*a*x-7*x^2 + x^3 = 0', 'x')
ans =
[ 0]
[7/2 + 1/2 *(49-12*a) ^(1/2)]
[7/2-1/2 *(49-12*a) ^(1/2)]
We can find the five solutions of the equation x ^ 5 + 2 x + 1 = 0:
>> solve('x^5+2*x+1','x')
ans =
RootOf(_Z^5+2*_Z+1)
As the result does not explicitly give five solutions, we apply the allvalues command:
>> allvalues(solve('x^5+2*x+1','x'))
ans =
[-.7018735688558619-. 8796971979298240 * i]
[-. 7018735688558619 +. 8796971979298240 * i]
[-. 4863890359345430]
[.9450680868231334-. 8545175144390459 * i]
[. 9450680868231334 +. 8545175144390459 * i]
On the other hand, MATLAB can use the Maple program libraries to work with symbolic math, and can thus extend its field of action. In this way, MATLAB can be used to work on such topics as differential forms, Euclidean geometry, projective geometry, statistics, etc.
At the same time, Maple can also benefit from MATLABs powers of numerical calculation, which might be used, for example, in combination with the Maple libraries (combinatorics, optimization, number theory, etc.)
1.3 MATLAB and Maple
Provided the Extended Symbolic Math Toolbox is installed then MATLAB can extend its symbolic calculation abilities by making use of the Maple libraries. To use a Maple command from MATLAB, use the command maple followed by the corresponding Maple syntax.
To use a Maple command from Matlab, the syntax is as follows:
maple ('Maple_command_syntax')
or alternatively:
maple 'Maple_command_syntax'
To use a Maple command with N arguments from Matlab, the syntax is as follows:
maple('Maple_command_syntax', argument1,
argument2,..., argumentN)
Here are some examples.
We can calculate the limit of the function (x ^ 3-1) / (x-1) as x tends to 1:
>> maple('limit((x^3-1)/(x-1),x=1)')
ans =
We could also have used the following syntax:
>> maple limit('(x^3-1)/(x-1),x=1)';
ans =
We can calculate the greatest common divisor of 10,000 and 5,000:
>> maple('gcd', 10000, 5000)
ans =
5000