Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
506 views

Tutorial 1 Getting Started With MATLAB

This tutorial provides an introduction to basic MATLAB concepts including starting MATLAB, using the command window, performing arithmetic operations and saving variables. It discusses data types in MATLAB including integers, real and complex numbers. It also covers saving and reloading workspaces, using the help system, and MATLAB resources available online.

Uploaded by

Soavan Sahu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
506 views

Tutorial 1 Getting Started With MATLAB

This tutorial provides an introduction to basic MATLAB concepts including starting MATLAB, using the command window, performing arithmetic operations and saving variables. It discusses data types in MATLAB including integers, real and complex numbers. It also covers saving and reloading workspaces, using the help system, and MATLAB resources available online.

Uploaded by

Soavan Sahu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 142



     

Edward Neuman
Department of Mathematics
Southern Illinois University at Carbondale
edneuman@siu.edu

The purpose of this tutorial is to present basics of MATLAB. We do not assume any prior
knowledge of this package. This tutorial is intended for users running a professional version of
MATLAB 5.3, Release 11 under Windows 95. Topics discussed in this tutorial include the
Command Window, numbers and arithmetic operations, saving and reloading a work, using
help, MATLAB demos, interrupting a running program, long command lines, and MATLAB
resources on the Internet.


    

You can start MATLAB by double clicking on the MATLAB icon that should be on the desktop
of your computer. This brings up the window called the Command Window. This window
allows a user to enter simple commands. To clear the Command Window type clc and next press
the Enter or Return key. To perform a simple computations type a command and next press the
Enter or Return key. For instance,

s = 1 + 2

s =
3

fun = sin(pi/4)

fun =
0.7071

In the second example the trigonometric function sine and the constant  are used. In MATLAB
they are named sin and pi, respectively.

Note that the results of these computations are saved in variables whose names are chosen by the
user. If they will be needed during your current MATLAB session, then you can obtain their
values typing their names and pressing the Enter or Return key. For instance,
2

s =
3

Variable name begins with a letter, followed by letters, numbers or underscores. MATLAB
recognizes only the first 31 characters of a variable name.

To change a format of numbers displayed in the Command Window you can use one of the
several formats that are available in MATLAB. The default format is called short (four digits
after the decimal point.) In order to display more digits click on File, select Preferences…, and
next select a format you wish to use. They are listed below the Numeric Format. Next click on
Apply and OK and close the current window. You can also select a new format from within the
Command Window. For instance, the following command

format long

changes a current format to the format long. To display more digits of the variable fun type

fun

fun =
0.70710678118655

To change a current format to the default one type

format short

fun

fun =
0.7071

To close MATLAB type exit in the Command Window and next press Enter or Return key. A
second way to close your current MATLAB session is to select File in the MATLAB's toolbar
and next click on Exit MATLAB option. All unsaved information residing in the MATLAB
Workspace will be lost.

           

There are three kinds of numbers used in MATLAB:

• integers
• real numbers
• complex numbers

Integers are enterd without the decimal point


3

xi = 10

xi =
10

However, the following number

xr = 10.01

xr =
10.0100

is saved as the real number. It is not our intention to discuss here machine representation of
numbers. This topic is usually included in the numerical analysis courses.
Variables realmin and realmax denote the smallest and the largest positive real numbers in
MATLAB. For instance,

realmin

ans =
2.2251e-308

Complex numbers in MATLAB are represented in rectangular form. The imaginary unit − 1 is
denoted either by i or j

ans =
0 + 1.0000i

In addition to classes of numbers mentioned above, MATLAB has three variables representing
the nonnumbers:

• -Inf
• Inf
• NaN

The –Inf and Inf are the IEEE representations for the negative and positive infinity, respectively.
Infinity is generated by overflow or by the operation of dividing by zero. The NaN stands for the
not-a-number and is obtained as a result of the mathematically undefined operations such as
0.0/0.0 or ∞ − ∞ .

List of basic arithmetic operations in MATLAB include six operations


4

Operation Symbol
addition +
subtraction -
multiplication *
division / or \
exponentiation ^

MATLAB has two division operators / - the right division and \ - the left division. They do not
produce the same results

rd = 47/3

rd =
15.6667

ld = 47\3

ld =
0.0638

! "       # $

All variables used in the current MATLAB session are saved in the Workspace. You can view
the content of the Workspace by clicking on File in the toolbar and next selecting Show
Workspace from the pull-down menu. You can also check contents of the Workspace typing
whos in the Command Window. For instance,

whos

Name Size Bytes Class

ans 1x1 16 double array (complex)


fun 1x1 8 double array
ld 1x1 8 double array
rd 1x1 8 double array
s 1x1 8 double array
xi 1x1 8 double array
xr 1x1 8 double array

Grand total is 7 elements using 64 bytes

shows all variables used in current session. You can also use command who to generate a list of
variables used in current session

who
5

Your variables are:

ans ld s xr
fun rd xi

To save your current workspace select Save Workspace as… from the File menu. Chose a name
for your file, e.g. filename.mat and next click on Save. Remember that the file you just created
must be located in MATLAB's search path. Another way of saving your workspace is to type
save filename in the Command Window. The following command save filename s saves only
the variable s.

Another way to save your workspace is to type the command diary filename in the Command
Window. All commands and variables created from now will be saved in your file. The following
command: diary off will close the file and save it as the text file. You can open this file in a text
editor, by double clicking on the name of your file, and modify its contents if you wish to do so.

To load contents of the file named filename into MATLAB's workspace type load filename in
the Command Window.

More advanced computations often require execution of several lines of computer code. Rather
than typing those commands in the Command Window you should create a file. Each time you
will need to repeat computations just invoke your file. Another advantage of using files is the
ease to modify its contents. To learn more about files, see [1], pp. 67-75 and also Section 2.2 of
Tutorial 2.

% & 

One of the nice features of MATLAB is its help system. To learn more about a function you are
to use, say rref, type in the Command Window

help svd

SVD Singular value decomposition.


[U,S,V] = SVD(X) produces a diagonal matrix S, of the same
dimension as X and with nonnegative diagonal elements in
decreasing order, and unitary matrices U and V so that
X = U*S*V'.
S = SVD(X) returns a vector containing the singular values.
[U,S,V] = SVD(X,0) produces the "economy size"
decomposition. If X is m-by-n with m > n, then only the
first n columns of U are computed and S is n-by-n.

See also SVDS, GSVD.

Overloaded methods
help sym/svd.m

If you do not remember the exact name of a function you want to learn more about use command
lookfor followed by the incomplete name of a function in the Command Window. In the
following example we use a "word" sv
6

lookfor sv

ISVMS True for the VMS version of MATLAB.


HSV2RGB Convert hue-saturation-value colors to red-green-blue.
RGB2HSV Convert red-green-blue colors to hue-saturation-value.
GSVD Generalized Singular Value Decomposition.
SVD Singular value decomposition.
SVDS Find a few singular values and vectors.
HSV Hue-saturation-value color map.
JET Variant of HSV.
CSVREAD Read a comma separated value file.
CSVWRITE Write a comma separated value file.
ISVARNAME Check for a valid variable name.
RANDSVD Random matrix with pre-assigned singular values.
Trusvibs.m: % Example: trusvibs
SVD Symbolic singular value decomposition.
RANDSVD Random matrix with pre-assigned singular values.

The helpwin command, invoked without arguments, opens a new window on the screen. To find
an information you need double click on the name of the subdirectory and next double click on a
function to see the help text for that function. You can go directly to the help text of your function
invoking helpwin command followed by an argument. For instance, executing the following
command

helpwin zeros

ZEROS Zeros array.


ZEROS(N) is an N-by-N matrix of zeros.
ZEROS(M,N) or ZEROS([M,N]) is an M-by-N matrix of zeros.
ZEROS(M,N,P,...) or ZEROS([M N P ...]) is an M-by-N-by-P-by-...
array of zeros.
ZEROS(SIZE(A)) is the same size as A and all zeros.

See also ONES.

generates an information about MATLAB's function zeros.

MATLAB also provides the browser-based help. In order to access these help files click on Help
and next select Help Desk (HTML). This will launch your Web browser. To access an
information you need click on a highlighted link or type a name of a function in the text box. In
order for the Help Desk to work properly on your computer the appropriate help files, in the
HTML or PDF format, must be installed on your computer. You should be aware that these files
require a significant amount of the disk space.

' ( 

To learn more about MATLAB capabilities you can execute the demo command in the
Command Window or click on Help and next select Examples and Demos from the pull-down
menu. Some of the MATLAB demos use both the Command and the Figure windows.
7

To learn about matrices in MATLAB open the demo window using one of the methods described
above. In the left pane select Matrices and in the right pane select Basic matrix operations then
click on Run Basic matrix … . Click on the Start >> button to begin the show.

If you are familiar with functions of a complex variable I recommend another demo. Select
Visualization and next 3-D Plots of complex functions. You can generate graphs of simple
power functions by selecting an appropriate button in the current window.

) *         

To interrupt a running program press simultaneously the Ctrl-c keys. Sometimes you have to
repeat pressing these keys a couple of times to halt execution of your program. This is not a
recommended way to exit a program, however, in certain circumstances it is a necessity. For
instance, a poorly written computer code can put MATLAB in the infinite loop and this would be
the only option you will have left.

+      

To enter a statement that is too long to be typed in one line, use three periods, … , followed by
Enter or Return. For instance,

x = sin(1) - sin(2) + sin(3) - sin(4) + sin(5) -...


sin(6) + sin(7) - sin(8) + sin(9) - sin(10)

x =
0.7744

You can suppress output to the screen by adding a semicolon after the statement

u = 2 + 3;

,       *   

If your computer has an access to the Internet you can learn more about MATLAB and also
download user supplied files posted in the public domain. We provide below some pointers to
information related to MATLAB.

• The MathWorks Web site: http://www.mathworks.com/

The MathWorks, the makers of MATLAB, maintains an important Web site. Here you can
find information about new products, MATLAB related books, user supplied files and much
more.
• The MATLAB newsgroup: news://saluki-news.siu.edu/comp.soft-sys.matlab/

If you have an access to the Internet News, you can read messages posted in this newsgroup.
Also, you can post your own messages. The link shown above would work only for those
who have access to the news server in Southern Illinois University at Carbondale.
8

• http://dir.yahoo.com/science/mathematics/software/matlab/

A useful source of information about MATLAB and good starting point to other Web sites.

• http://www.cse.uiuc.edu/cse301/matlab.html

Thus Web site, maintained by the University of Illinois at Champaign-Urbana, provides


several links to MATLAB resources on the Internet.

• The Mastering Matlab Web site: http://www.eece.maine.edu/mm

Recommended link for those who are familiar with the book Mastering Matlab 5.
A Comprehensive Tutorial and Reference, by D. Hanselman and B. Littlefield (see [2].)
9

- .   

[1] Getting Started with MATLAB, Version 5, The MathWorks, Inc., 1996.

[2] D. Hanselman and B. Littlefield, Mastering MATLAB 5. A Comprehensive Tutorial and


Reference, Prentice Hall, Upper Saddle River, NJ, 1998.

[3] K. Sigmon, MATLAB Primer, Fifth edition, CRC Press, Boca Raton, 1998.

[4] Using MATLAB, Version 5, The MathWorks, Inc., 1996.




    

Edward Neuman
Department of Mathematics
Southern Illinois University at Carbondale
edneuman@siu.edu

This tutorial is intended for those who want to learn basics of MATLAB programming language.
Even with a limited knowledge of this language a beginning programmer can write his/her own
computer code for solving problems that are complex enough to be solved by other means.
Numerous examples included in this text should help a reader to learn quickly basic programming
tools of this language. Topics discussed include the m-files, inline functions, control flow,
relational and logical operators, strings, cell arrays, rounding numbers to integers and MATLAB
graphics.

  

Files that contain a computer code are called the m-files. There are two kinds of m-files: the script
files and the function files. Script files do not take the input arguments or return the output
arguments. The function files may take input arguments or return output arguments.

To make the m-file click on File next select New and click on M-File from the pull-down menu.
You will be presented with the MATLAB Editor/Debugger screen. Here you will type your
code, can make changes, etc. Once you are done with typing, click on File, in the MATLAB
Editor/Debugger screen and select Save As… . Chose a name for your file, e.g., firstgraph.m
and click on Save. Make sure that your file is saved in the directory that is in MATLAB's search
path.

If you have at least two files with duplicated names, then the one that occurs first in MATLAB's
search path will be executed.

To open the m-file from within the Command Window type edit firstgraph and then press
Enter or Return key.

Here is an example of a small script file

% Script file firstgraph.

x = pi/100:pi/100:10*pi;
y = sin(x)./x;
plot(x,y)
grid
2

Let us analyze contents of this file. First line begins with the percentage sign %. This is a
comment. All comments are ignored by MATLAB. They are added to improve readability of the
code. In the next two lines arrays x and y are created. Note that the semicolon follows both
commands. This suppresses display of the content of both vectors to the screen (see Tutorial 1,
page 5 for more details). Array x holds 1000 evenly spaced numbers in the interval [/100 10]
while the array y holds the values of the sinc function y = sin(x)/x at these points. Note use of the
dot operator . before the right division operator /. This tells MATLAB to perform the
componentwise division of two arrays sin(x) and x. Special operators in MATLAB and operations
on one- and two dimensional arrays are discussed in detail in Tutorial 3, Section 3.2. The
command plot creates the graph of the sinc function using the points generated in two previous
lines. For more details about command plot see Section 2.8.1 of this tutorial. Finally, the
command grid is executed. This adds a grid to the graph. We invoke this file by typing its name
in the Command Window and next pressing the Enter or Return key

firstgraph

0.8

0.6

0.4

0.2

-0.2

-0.4
0 5 10 15 20 25 30 35

Here is an example of the function file

function [b, j] = descsort(a)

% Function descsort sorts, in the descending order, a real array a.


% Second output parameter j holds a permutation used to obtain
% array b from the array a.
3

[b ,j] = sort(-a);
b = -b;

This function takes one input argument, the array of real numbers, and returns a sorted array
together with a permutation used to obtain array b from the array a. MATLAB built-in function
sort is used here. Recall that this function sort numbers in the ascending order. A simple trick
used here allows us to sort an array of numbers in the descending order.

To demonstrate functionality of the function under discussion let

a = [pi –10 35 0.15];

[b, j] = descsort(a)

b =
35.0000 3.1416 0.1500 -10.0000
j =
3 1 4 2

You can execute function descsort without output arguments. In this case an information about a
permutation used will be lost

descsort(a)

ans =
35.0000 3.1416 0.1500 -10.0000

Since no output argument was used in the call to function descorder a sorted array a is assigned
to the default variable ans.


      

Sometimes it is handy to define a function that will be used during the current MATLAB session
only. MATLAB has a command inline used to define the so-called inline functions in the
Command Window.

Let

f = inline('sqrt(x.^2+y.^2)','x','y')

f =
Inline function:
f(x,y) = sqrt(x.^2+y.^2)

You can evaluate this function in a usual way

f(3,4)

ans =
5
4

Note that this function also works with arrays. Let

A = [1 2;3 4]

A =
1 2
3 4

and

B = ones(2)

B =
1 1
1 1

Then

C = f(A, B)

C =
1.4142 2.2361
3.1623 4.1231

For the later use let us mention briefly a concept of the string in MATLAB. The character string
is a text surrounded by single quotes. For instance,

str = 'programming in MATLAB is fun'

str =
programming in MATLAB is fun

is an example of the string. Strings are discussed in Section 2.5 of this tutorial.

In the previous section you have learned how to create the function files. Some functions take as
the input argument a name of another function, which is specified as a string. In order to execute
function specified by string you should use the command feval as shown below

feval('functname', input parameters of function functname)

Consider the problem of computing the least common multiple of two integers. MATLAB has a
built-in function lcm that computes the number in question. Recall that the least common
multiple and the greatest common divisor (gcd) satisfy the following equation

ab = lcm(a, b)gcd(a, b)

MATLAB has its own function, named gcd, for computing the greatest common divisor.
5

To illustrate the use of the command feval let us take a closer look at the code in the m-file
mylcm

function c = mylcm(a, b)

% The least common multiple c of two integers a and b.

if feval('isint',a) & feval('isint',b)


c = a.*b./gcd(a,b);
else
error('Input arguments must be integral numbers')
end

Command feval is used twice in line two (I do do not count the comment lines and the blank
lines). It checks whether or not both input arguments are integers. The logical and operator &
used here is discussed in Section 2.4. If this condition is satisfied, then the least common multiple
is computed using the formula mentioned earlier, otherwise the error message is generated. Note
use of the command error, which takes as the argument a string. The conditional if - else - end
used here is discussed in Section 2.4 of this tutorial. Function that is executed twice in the body of
the function mylcm is named isint

function k = isint(x);

% Check whether or not x is an integer number.


% If it is, function isint returns 1 otherwise it returns 0.

if abs(x - round(x)) < realmin


k = 1;
else
k = 0;
end

New functions used here are the absolute value function (abs) and the round function (round).
The former is the classical math function while the latter takes a number and rounds it to the
closest integer. Other functions used to round real numbers to integers are discussed in Section
2.7. Finally, realmin is the smallest positive real number on your computer

format long

realmin

ans =
2.225073858507201e-308

format short

The Trapezoidal Rule with the correction term is often used to numerical integration of functions
that are differentiable on the interval of integration
6

b
h h2

a
f ( x )dx  [ f ( a )  f (b)] 
2 12
[ f ' (a )  f ' (b)]

where h = b – a. This formula is easy to implement in MATLAB

function y = corrtrap(fname, fpname, a, b)

% Corrected trapezoidal rule y.


% fname - the m-file used to evaluate the integrand,
% fpname - the m-file used to evaluate the first derivative
% of the integrand,
% a,b - endpoinds of the interval of integration.

h = b - a;
y = (h/2).*(feval(fname,a) + feval(fname,b))+ (h.^2)/12.*( ...
feval(fpname,a) - feval(fpname,b));

The input parameters a and b can be arrays of the same dimension. This is possible because the
dot operator proceeds certain arithmetic operations in the command that defines the variable y.

In this example we will integrate the sine function over two intervals whose end points are stored
in the arrays a and b, where

a = [0 0.1];
b = [pi/2 pi/2 + 0.1];

y = corrtrap('sin', 'cos', a, b)

y =
0.9910 1.0850

Since the integrand and its first order derivative are both the built-in functions, there is no need to
define these functions in the m-files.

  

To control the flow of commands, the makers of MATLAB supplied four devices a programmer
can use while writing his/her computer code

 the for loops


 the while loops
 the if-else-end constructions
 the switch-case constructions
7

 

  
Syntax of the for loop is shown below

for k = array
commands
end

The commands between the for and end statements are executed for all values stored in the
array.

Suppose that one-need values of the sine function at eleven evenly spaced points n/10, for
n = 0, 1, …, 10. To generate the numbers in question one can use the for loop

for n=0:10
x(n+1) = sin(pi*n/10);
end

x =
Columns 1 through 7
0 0.3090 0.5878 0.8090 0.9511 1.0000 0.9511
Columns 8 through 11
0.8090 0.5878 0.3090 0.0000

The for loops can be nested

H = zeros(5);
for k=1:5
for l=1:5
H(k,l) = 1/(k+l-1);
end
end

H =
1.0000 0.5000 0.3333 0.2500 0.2000
0.5000 0.3333 0.2500 0.2000 0.1667
0.3333 0.2500 0.2000 0.1667 0.1429
0.2500 0.2000 0.1667 0.1429 0.1250
0.2000 0.1667 0.1429 0.1250 0.1111

Matrix H created here is called the Hilbert matrix. First command assigns a space in computer's
memory for the matrix to be generated. This is added here to reduce the overhead that is required
by loops in MATLAB.

The for loop should be used only when other methods cannot be applied. Consider the following
problem. Generate a 10-by-10 matrix A = [akl], where akl = sin(k)cos(l). Using nested loops one
can compute entries of the matrix A using the following code
8

A = zeros(10);
for k=1:10
for l=1:10
A(k,l) = sin(k)*cos(l);
end
end

A loop free version might look like this

k = 1:10;
A = sin(k)'*cos(k);

First command generates a row array k consisting of integers 1, 2, … , 10. The command sin(k)'
creates a column vector while cos(k) is the row vector. Components of both vectors are the values
of the two trig functions evaluated at k. Code presented above illustrates a powerful feature of
MATLAB called vectorization. This technique should be used whenever it is possible.

 

 
 
Syntax of the while loop is

while expression
statements
end

This loop is used when the programmer does not know the number of repetitions a priori.

Here is an almost trivial problem that requires a use of this loop. Suppose that the number  is
divided by 2. The resulting quotient is divided by 2 again. This process is continued till the
current quotient is less than or equal to 0.01. What is the largest quotient that is greater than 0.01?
To answer this question we write a few lines of code

q = pi;
while q > 0.01
q = q/2;
end

q =
0.0061

 
    
 
Syntax of the simplest form of the construction under discussion is

if expression
commands
end
9

This construction is used if there is one alternative only. Two alternatives require the construction

if expression
commands (evaluated if expression is true)
else
commands (evaluated if expression is false)
end

Construction of this form is used in functions mylcm and isint (see Section 2.3).

If there are several alternatives one should use the following construction

if expression1
commands (evaluated if expression 1 is true)
elseif expression 2
commands (evaluated if expression 2 is true)
elseif …
.
.
.
else
commands (executed if all previous expressions evaluate to false)
end

Chebyshev polynomials Tn(x), n = 0, 1, … of the first kind are of great importance in numerical
analysis. They are defined recursively as follows

Tn(x) = 2xTn – 1(x) – Tn – 2(x), n = 2, 3, … , T0(x) = 1, T1(x) = x.

Implementation of this definition is easy

function T = ChebT(n)

% Coefficients T of the nth Chebyshev polynomial of the first kind.


% They are stored in the descending order of powers.

t0 = 1;
t1 = [1 0];
if n == 0
T = t0;
elseif n == 1;
T = t1;
else
for k=2:n
T = [2*t1 0] - [0 0 t0];
t0 = t1;
t1 = T;
end
end
10

Coefficients of the cubic Chebyshev polynomial of the first kind are

coeff = ChebT(3)

coeff =
4 0 -3 0

Thus T3(x) = 4x3 – 3x.

  
   

Syntax of the switch-case construction is

switch expression (scalar or string)


case value1 (executes if expression evaluates to value1)
commands
case value2 (executes if expression evaluates to value2)
commands
.
.
.
otherwise
statements
end

Switch compares the input expression to each case value. Once the match is found it executes the
associated commands.

In the following example a random integer number x from the set {1, 2, … , 10} is generated. If
x = 1 or x = 2, then the message Probability = 20% is displayed to the screen. If x = 3 or 4 or 5,
then the message Probability = 30% is displayed, otherwise the message Probability = 50% is
generated. The script file fswitch utilizes a switch as a tool for handling all cases mentioned
above

% Script file fswitch.

x = ceil(10*rand); % Generate a random integer in {1, 2, ... , 10}


switch x
case {1,2}
disp('Probability = 20%');
case {3,4,5}
disp('Probability = 30%');
otherwise
disp('Probability = 50%');
end

Note use of the curly braces after the word case. This creates the so-called cell array rather than
the one-dimensional array, which requires use of the square brackets.
11

Here are new MATLAB functions that are used in file fswitch

rand – uniformly distributed random numbers in the interval (0, 1)


ceil – round towards plus infinity infinity (see Section 2.5 for more details)
disp – display string/array to the screen

Let us test this code ten times

for k = 1:10
fswitch
end

Probability = 50%
Probability = 30%
Probability = 50%
Probability = 50%
Probability = 50%
Probability = 30%
Probability = 20%
Probability = 50%
Probability = 30%
Probability = 50%

! "    #

Comparisons in MATLAB are performed with the aid of the following operators

Operator Description
< Less than
<= Less than or equal to
> Greater
>= Greater or equal to
== Equal to
~= Not equal to

Operator == compares two variables and returns ones when they are equal and zeros otherwise.

Let

a = [1 1 3 4 1]

a =
1 1 3 4 1

Then
12

ind = (a == 1)

ind =
1 1 0 0 1

You can extract all entries in the array a that are equal to 1 using

b = a(ind)

b =
1 1 1

This is an example of so-called logical addressing in MATLAB. You can obtain the same result
using function find

ind = find(a == 1)

ind =
1 2 5

Variable ind now holds indices of those entries that satisfy the imposed condition. To extract all
ones from the array a use

b = a(ind)

b =
1 1 1

There are three logical operators available in MATLAB

Logical operator Description


| And
& Or
~ Not

Suppose that one wants to select all entries x that satisfy the inequalities x  1 or x < -0.2 where

x = randn(1,7)

x =
-0.4326 -1.6656 0.1253 0.2877 -1.1465 1.1909 1.1892

is the array of normally distributed random numbers. We can solve easily this problem using
operators discussed in this section

ind = (x >= 1) | (x < -0.2)

ind =
1 1 0 0 1 1 1

y = x(ind)
13

y =
-0.4326 -1.6656 -1.1465 1.1909 1.1892

Solve the last problem without using the logical addressing.

In addition to relational and logical operators MATLAB has several logical functions designed
for performing similar tasks. These functions return 1 (true) if a specific condition is satisfied and
0 (false) otherwise. A list of these functions is too long to be included here. The interested reader
is referred to [1], pp. 85-86 and [4], Chapter 10, pp. 26-27. Names of the most of these functions
begin with the prefix is. For instance, the following command

isempty(y)

ans =
0

returns 0 because the array y of the last example is not empty. However, this command

isempty([ ])

ans =
1

returns 1 because the argument of the function used is the empty array [ ].

Here is another example that requires use of the isempty command

function dp = derp(p)

% Derivative dp of an algebraic polynomial that is


% represented by its coefficients p. They must be stored
% in the descending order of powers.

n = length(p) - 1;
p = p(:)'; % Make sure p is a row array.
dp = p(1:n).*(n:-1:1); % Apply the Power Rule.
k = find(dp ~= 0);
if ~isempty(k)
dp = dp(k(1):end); % Delete leading zeros if any.
else
dp = 0;
end

In this example p(x) = x3 + 2x2 + 4. Using a convention for representing polynomials in


MATLAB as the array of their coefficients that are stored in the descending order of powers, we
obtain

dp = derp([1 2 0 4])

dp =
3 4 0
14

$ % 

String is an array of characters. Each character is represented internally by its ASCII value.

This is an example of a string

str = 'I am learning MATLAB this semester.'

str =
I am learning MATLAB this semester.

To see its ASCII representation use function double

str1 = double(str)

str1 =
Columns 1 through 12
73 32 97 109 32 108 101 97 114 110 105
110
Columns 13 through 24
103 32 77 65 84 76 65 66 32 116 104
105
Columns 25 through 35
115 32 115 101 109 101 115 116 101 114 46

You can convert array str1 to its character form using function char

str2 = char(str1)

str2 =
I am learning MATLAB this semester.

Application of the string conversion is used in Tutorial 3, Section 3.11 to uncode and decode
messages.

To compare two strings for equality use function strcmp

iseq = strcmp(str, str2)

iseq =
1

Two strings can be concatenated using function ctrcat

strcat(str,str2)

ans =
I am learning MATLAB this semester.I am learning MATLAB this semester.

Note that the concatenated strings are not separated by the blank space.
15

You can create two-dimensional array of strings. To this aim the cell array rather than the two-
dimensional array must be used. This is due to the fact that numeric array must have the same
number of columns in each row.

This is an example of the cell array

carr = {'first name'; 'last name'; 'hometown'}

carr =
'first name'
'last name'
'hometown'

Note use of the curly braces instead of the square brackets. Cell arrays are discussed in detail in
the next section of this tutorial.

MATLAB has two functions to categorize characters: isletter and isspace. We will run both
functions on the string str

isletter(str)

ans =
Columns 1 through 12
1 0 1 1 0 1 1 1 1 1 1
1
Columns 13 through 24
1 0 1 1 1 1 1 1 0 1 1
1
Columns 25 through 35
1 0 1 1 1 1 1 1 1 1 0

isspace(str)

ans =
Columns 1 through 12
0 1 0 0 1 0 0 0 0 0 0
0
Columns 13 through 24
0 1 0 0 0 0 0 0 1 0 0
0
Columns 25 through 35
0 1 0 0 0 0 0 0 0 0 0

The former function returns 1 if a character is a letter and 0 otherwise while the latter returns 1 if
a character is whitespace (blank, tab, or new line) and 0 otherwise.

We close this section with two important functions that are intended for conversion of numbers to
strings. Functions in question are named int2str and num2str. Function int2str rounds its
argument (matrix) to integers and converts the result into a string matrix.
16

Let

A = randn(3)

A =
-0.4326 0.2877 1.1892
-1.6656 -1.1465 -0.0376
0.1253 1.1909 0.3273

Then

B = int2str(A)

B =
0 0 1
-2 -1 0
0 1 0

Function num2str takes an array and converts it to the array string. Running this function on the
matrix A defined earlier, we obtain

C = num2str(A)

C =
-0.43256 0.28768 1.1892
-1.6656 -1.1465 -0.037633
0.12533 1.1909 0.32729

Function under discussion takes a second optional argument - a number of decimal digits. This
feature allows a user to display digits that are far to the right of the decimal point. Using matrix A
again, we get

D = num2str(A, 18)

D =
-0.43256481152822068 0.28767642035854885 1.1891642016521031
-1.665584378238097 -1.1464713506814637 -0.037633276593317645
0.12533230647483068 1.1909154656429988 0.32729236140865414

For comparison, changing format to long, we obtain

format long

A =
-0.43256481152822 0.28767642035855 1.18916420165210
-1.66558437823810 -1.14647135068146 -0.03763327659332
0.12533230647483 1.19091546564300 0.32729236140865

format short
17

Function num2str his is often used for labeling plots with the title, xlabel, ylabel, and text
commands.

&  '

Two data types the cell arrays and structures make MATLAB a powerful tool for applications.
They hold other MATLAB arrays. In this section we discuss the cell arrays only. To learn about
structures the interested reader is referred to [4], Chapter 13 and [1], Chapter 12.

To create the cell array one can use one of the two techniques called the cell indexing and the
content indexing. The following example reveals differences between these two techniques.
Suppose one want to save the string 'John Brown' and his SSN 123-45-6789 (without dashes) in
the cell array.

1. Cell indexing

A(1,1) = {'John Brown'};

A(1,2) = {[1 2 3 4 5 6 7 8 9]};

2. Content indexing

B{1,1} = 'John Brown';

B{1,2} = [1 2 3 4 5 6 7 8 9];

A condensed form of the cell array A is

A =
'John Brown' [1x9 double]

To display its full form use function celldisp

celldisp(A)

A{1} =
John Brown
A{2} =
1 2 3 4 5 6 7 8 9

To access data in a particular cell use content indexing on the right-hand side. For instance,

B{1,1}

ans =
John Brown

To delete a cell use the empty matrix operator [ ]. For instance, this operation
18

B(1) = []

B =
[1x9 double]

deletes cell B(1, 1) of the cell array B.

This command

C = {A B}

C =
{1x2 cell} {1x1 cell}

creates a new cell array

celldisp(C)

C{1}{1} =
John Brown
C{1}{2} =
1 2 3 4 5 6 7 8 9
C{2}{1} =
1 2 3 4 5 6 7 8 9

How would you delete cell C(2,1)?

( "    ) * * +  

We have already used two MATLAB functions round and ceil to round real numbers to integers.
They are briefly described in the previous sections of this tutorial. A full list of functions
designed for rounding numbers is provided below

Function Description
floor Round towards minus infinity
ceil Round towards plus infinity
fix Round towards zero
round Round towards nearest integer

To illustrate differences between these functions let us create first a two-dimensional array of
random numbers that are normally distributed (mean = 0, variance = 1) using another MATLAB
function randn

randn('seed', 0) % This sets the seed of the random numbers generator to zero

T = randn(5)
19

T =
1.1650 1.6961 -1.4462 -0.3600 -0.0449
0.6268 0.0591 -0.7012 -0.1356 -0.7989
0.0751 1.7971 1.2460 -1.3493 -0.7652
0.3516 0.2641 -0.6390 -1.2704 0.8617
-0.6965 0.8717 0.5774 0.9846 -0.0562

A = floor(T)

A =
1 1 -2 -1 -1
0 0 -1 -1 -1
0 1 1 -2 -1
0 0 -1 -2 0
-1 0 0 0 -1

B = ceil(T)

B =
2 2 -1 0 0
1 1 0 0 0
1 2 2 -1 0
1 1 0 -1 1
0 1 1 1 0

C = fix(T)

C =
1 1 -1 0 0
0 0 0 0 0
0 1 1 -1 0
0 0 0 -1 0
0 0 0 0 0

D = round(T)

D =
1 2 -1 0 0
1 0 -1 0 -1
0 2 1 -1 -1
0 0 -1 -1 1
-1 1 1 1 0

It is worth mentioning that the following identities

floor(x) = fix(x) for x  0


and
ceil(x) = fix(x) for x  0
20

hold true.

In the following m-file functions floor and ceil are used to obtain a certain representation of a
nonnegative real number

function [m, r] = rep4(x)

% Given a nonnegative number x, function rep4 computes an integer m


% and a real number r, where 0.25 <= r < 1, such that x = (4^m)*r.

if x == 0
m = 0;
r = 0;
return
end
u = log10(x)/log10(4);
if u < 0
m = floor(u)
else
m = ceil(u);
end
r = x/4^m;

Command return causes a return to the invoking function or to the keyboard. Function log10 is
the decimal logarithm.

[m, r] = rep4(pi)

m =
1
r =
0.7854

We check this result

format long

(4^m)*r

ans =
3.14159265358979

format short

,  #

MATLAB has several high-level graphical routines. They allow a user to create various graphical
objects including two- and three-dimensional graphs, graphical user interfaces (GUIs), movies, to
mention the most important ones. For the comprehensive presentation of the MATLAB graphics
the interested reader is referred to [2].
21

Before we begin discussion of graphical tools that are available in MATLAB I recommend that
you will run a couple of demos that come with MATLAB. In the Command Window click on
Help and next select Examples and Demos. ChoseVisualization, and next select 2-D Plots. You
will be presented with several buttons. Select Line and examine the m-file below the graph. It
should give you some idea about computer code needed for creating a simple graph. It is
recommended that you examine carefully contents of all m-files that generate the graphs in this
demo.

  



Basic function used to create 2-D graphs is the plot function. This function takes a variable
number of input arguments. For the full definition of this function type help plot in the
Command Window.

x
In this example the graph of the rational function f ( x )  , -2  x  2, will be plotted
1 x2
using a variable number of points on the graph of f(x)

% Script file graph1.

% Graph of the rational function y = x/(1+x^2).

for n=1:2:5
n10 = 10*n;
x = linspace(-2,2,n10);
y = x./(1+x.^2);
plot(x,y,'r')
title(sprintf('Graph %g. Plot based upon n = %g points.' ...
, (n+1)/2, n10))
axis([-2,2,-.8,.8])
xlabel('x')
ylabel('y')
grid
pause(3)
end

Let us analyze contents of this file. The loop for is executed three times. Therefore, three graphs
of the same function will be displayed in the Figure Window. A MATLAB function
linspace(a, b, n) generates a one-dimensional array of n evenly spaced numbers in the interval
[a b]. The y-ordinates of the points to be plotted are stored in the array y. Command plot is
called with three arguments: two arrays holding the x- and the y-coordinates and the string 'r',
which describes the color (red) to be used to paint a plotted curve. You should notice a difference
between three graphs created by this file. There is a significant difference between smoothness of
graphs 1 and 3. Based on your visual observation you should be able to reach the following
conclusion: "more points you supply the smoother graph is generated by the function plot".
Function title adds a descriptive information to the graphs generated by this m-file and is
followed by the command sprintf. Note that sprintf takes here three arguments: the string and
names of two variables printed in the title of each graph. To specify format of printed numbers we
use here the construction %g, which is recommended for printing integers. The command axis
tells MATLAB what the dimensions of the box holding the plot are. To add more information to
22

the graphs created here, we label the x- and the y-axes using commands xlabel and the ylabel,
respectively. Each of these commands takes a string as the input argument. Function grid adds
the grid lines to the graph. The last command used before the closing end is the pause command.
The command pause(n) holds on the current graph for n seconds before continuing, where n can
also be a fraction. If pause is called without the input argument, then the computer waits to user
response. For instance, pressing the Enter key will resume execution of a program.

Function subplot is used to plot of several graphs in the same Figure Window. Here is a slight
modification of the m-file graph1

% Script file graph2.

% Several plots of the rational function y = x/(1+x^2)


% in the same window.

k = 0;
for n=1:3:10
n10 = 10*n;
x = linspace(-2,2,n10);
y = x./(1+x.^2);
k = k+1;
subplot(2,2,k)
plot(x,y,'r')
title(sprintf('Graph %g. Plot based upon n = %g points.' ...
, k, n10))
xlabel('x')
ylabel('y')
axis([-2,2,-.8,.8])
grid
pause(3);
end

The command subplot is called here with three arguments. The first two tell MATLAB that a
2-by-2 array consisting of four plots will be created. The third parameter is the running index
telling MATLAB which subplot is currently generated.

graph2
23

Graph 1. Plot based upon n = 10 points. Graph 2. Plot based upon n = 40 points.

0.5 0.5

0 0

y
-0.5 -0.5

-2 -1 0 1 2 -2 -1 0 1 2
x x
Graph 3. Plot based upon n = 70 points. Graph 4. Plot based upon n = 100 points.

0.5 0.5

0 0
y

y
-0.5 -0.5

-2 -1 0 1 2 -2 -1 0 1 2
x x

Using command plot you can display several curves in the same Figure Window.

We will plot two ellipses

( x  3) 2 ( y  2) 2 ( x  7) 2 ( y  8) 2
  1 and  1
36 81 4 36

using command plot

% Script file graph3.

% Graphs of two ellipses

% x(t) = 3 + 6cos(t), y(t) = -2 + 9sin(t)

% and

% x(t) = 7 + 2cos(t), y(t) = 8 + 6sin(t).

t = 0:pi/100:2*pi;
x1 = 3 + 6*cos(t);
y1 = -2 + 9*sin(t);
x2 = 7 + 2*cos(t);
y2 = 8 + 6*sin(t);
h1 = plot(x1,y1,'r',x2,y2,'b');
set(h1,'LineWidth',1.25)
axis('square')
xlabel('x')
24

h = get(gca,'xlabel');
set(h,'FontSize',12)
set(gca,'XTick',-4:10)
ylabel('y')
h = get(gca,'ylabel');
set(h,'FontSize',12)
set(gca,'YTick',-12:2:14)
title('Graphs of (x-3)^2/36+(y+2)^2/81 = 1 and (x-7)^2/4+(y-8)^2/36 =
1.')
h = get(gca,'Title');
set(h,'FontSize',12)
grid

In this file we use several new MATLAB commands. They are used here to enhance the
readability of the graph. Let us now analyze the computer code contained in the m-file graph3.
First of all, the equations of ellipses in rectangular coordinates are transformed to parametric
equations. This is a convenient way to plot graphs of equations in the implicit form. The points to
be plotted, and smoothed by function plot, are defined in the first five lines of the file. I do not
count here the comment lines and the blank lines. You can plot both curves using a single plot
command. Moreover, you can select colors of the curves. They are specified as strings
(see line 6). MATLAB has several colors you can use to plot graphs:

y yellow
m magenta
c cyan
r red
g green
b blue
w white
k black

Note that the command in line 6 begins with h1 = plot… Variable h1 holds an information about
the graph you generate and is called the handle graphics. Command set used in the next line
allows a user to manipulate a plot. Note that this command takes as the input parameter the
variable h1. We change thickness of the plotted curves from the default value to a width of our
choice, namely 1.25. In the next line we use command axis to customize plot. We chose option
'square' to force axes to have square dimensions. Other available options are:
'equal', 'normal', 'ij', 'xy', and 'tight'. To learn more about these options use MATLAB's help.

If function axis is not used, then the circular curves are not necessarily circular. To justify this let
us plot a graph of the unit circle of radius 1 with center at the origin

t = 0:pi/100:2*pi;
x = cos(t);
y = sin(t);
plot(x,y)
25

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
-1 -0.5 0 0.5 1

Another important MATLAB function used in the file under discussion is named get
(see line 10). It takes as the first input parameter a variable named gca = get current axis. It
should be obvious to you, that the axis targeted by this function is the x-axis. Variable
h = get(gca, … ) is the graphics handle of this axis. With the information stored in variable h,
we change the font size associated with the x-axis using the 'FontSize' string followed by a size
of the font we wish to use. Invoking function set in line 12, we will change the tick marks along
the x-axis using the 'XTick' string followed by the array describing distribution of marks. You
can comment out temporarily line 12 by adding the percent sign % before the word set to see the
difference between the default tick marks and the marks generated by the command in line 12.
When you are done delete the percent sign you typed in line 12 and click on Save from the File
menu in the MATLAB Editor/Debugger. Finally, you can also make changes in the title of your
plot. For instance, you can choose the font size used in the title. This is accomplished here by
using function set. It should be obvious from the short discussion presented here that two
MATLAB functions get and set are of great importance in manipulating graphs.

Graphs of the ellipses in question are shown on the next page

graph3
26

Graphs of (x-3)2/36+(y+2)2/81 = 1 and (x-7)2/4+(y-8)2/36 = 1.


14
12
10
8
6
4
2
0
y -2
-4
-6
-8
-10
-12

-4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10
x

MATLAB has several functions designed for plotting specialized 2-D graphs. A partial list of
these functions is included here fill, polar, bar, barh, pie, hist, compass, errorbar, stem, and
feather.

In this example function fill is used to create a well-known object

n = -6:6;
x = sin(n*pi/6);
y = cos(n*pi/6);
fill(x, y, 'r')
axis('square')
title('Graph of the n-gone')
text(-0.45,0,'What is a name of this object?')

Function in question takes three input parameters - two arrays, named here x and y. They hold the
x- and y-coordinates of vertices of the polygon to be filled. Third parameter is the user-selected
color to be used to paint the object. A new command that appears in this short code is the text
command. It is used to annotate a text. First two input parameters specify text location. Third
input parameter is a text, which will be added to the plot.

Graph of the filled object that is generated by this code is displayed below
27

Graph of the n-gone


1

0.8

0.6

0.4

0.2

0 What is a name of this object?

-0.2

-0.4

-0.6

-0.8

-1
-1 -0.5 0 0.5 1

  



MATLAB has several built-in functions for plotting three-dimensional objects. In this subsection
we will deal mostly with functions used to plot curves in space (plot3), mesh surfaces (mesh),
surfaces (surf) and contour plots (contour). Also, two functions for plotting special surfaces,
sphere and cylinder will be discussed briefly. I recommend that any time you need help with the
3-D graphics you should type help graph3d in the Command Window to learn more about
various functions that are available for plotting three-dimensional objects.

Let r(t) = < t cos(t), t sin(t), t >, -10  t  10, be the space curve. We plot its graph over the
indicated interval using function plot3

% Script file graph4.

% Curve r(t) = < t*cos(t), t*sin(t), t >.

t = -10*pi:pi/100:10*pi;
x = t.*cos(t);
y = t.*sin(t);
h = plot3(x,y,t);
set(h,'LineWidth',1.25)
title('Curve u(t) = < t*cos(t), t*sin(t), t >')
h = get(gca,'Title');
set(h,'FontSize',12)
xlabel('x')
h = get(gca,'xlabel');
set(h,'FontSize',12)
ylabel('y')
h = get(gca,'ylabel');
28

set(h,'FontSize',12)
zlabel('z')
h = get(gca,'zlabel');
set(h,'FontSize',12)
grid

Function plot3 is used in line 4. It takes three input parameters – arrays holding coordinates of
points on the curve to be plotted. Another new command in this code is the zlabel command
(see line 4 from the bottom). Its meaning is self-explanatory.

graph4

Curve u(t) = < t*cos(t), t*sin(t), t >

40

20

0
z

-20

-40
40
20 40
0 20
0
-20 -20
-40 -40
y x

Function mesh is intended for plotting graphs of the 3-D mesh surfaces. Before we begin to work
with this function, another function meshgrid should be introduced. This function generates two
two-dimensional arrays for 3-D plots. Suppose that one wants to plot a mesh surface over the grid
that is defined as the Cartesian product of two sets

x = [0 1 2];
y = [10 12 14];

The meshgrid command applied to the arrays x and y creates two matrices

[xi, yi] = meshgrid(x,y)


29

xi =
0 1 2
0 1 2
0 1 2
yi =
10 10 10
12 12 12
14 14 14

Note that the matrix xi contains replicated rows of the array x while yi contains replicated
columns of y. The z-values of a function to be plotted are computed from arrays xi and yi.

In this example we will plot the hyperbolic paraboloid z = y2 – x2 over the square –1  x  1,
-1  y  1

x = -1:0.05:1;
y = x;
[xi, yi] = meshgrid(x,y);
zi = yi.^2 – xi.^2;
mesh(xi, yi, zi)
axis off

To plot the graph of the mesh surface together with the contour plot beneath the plotted surface
use function meshc

meshc(xi, yi, zi)


axis off
30

Function surf is used to visualize data as a shaded surface.

Computer code in the m-file graph5 should help you to learn some finer points of the 3-D
graphics in MATLAB

% Script file graph5.

% Surface plot of the hyperbolic paraboloid z = y^2 - x^2


% and its level curves.

x = -1:.05:1;
y = x;
[xi,yi] = meshgrid(x,y);
zi = yi.^2 - xi.^2;
surfc(xi,yi,zi)
colormap copper
shading interp
view([25,15,20])
grid off
title('Hyperbolic paraboloid z = y^2 – x^2')
h = get(gca,'Title');
set(h,'FontSize',12)
xlabel('x')
h = get(gca,'xlabel');
set(h,'FontSize',12)
ylabel('y')
h = get(gca,'ylabel');
set(h,'FontSize',12)
zlabel('z')
h = get(gca,'zlabel');
set(h,'FontSize',12)
pause(5)
figure
contourf(zi), hold on, shading flat
[c,h] = contour(zi,'k-'); clabel(c,h)
title('The level curves of z = y^2 - x^2.')
h = get(gca,'Title');
set(h,'FontSize',12)
31

xlabel('x')
h = get(gca,'xlabel');
set(h,'FontSize',12)
ylabel('y')
h = get(gca,'ylabel');
set(h,'FontSize',12)

graph5

A second graph is shown on the next page.


32

The level curves of z = y2 - x2.


40 0.8
0.4 0.8
0.6 0.6 0.4
0.2 0.2 0 -0.
2
35

-0

.4
.4
0

-0
0.4

-0
.2
30 0.2

-0.6

-0.6
25 0

-0.2
0

-0.4

-0.4
y
20

-0.8
-0.2
-0.8
15 0
-0.6

0
0.2

-0.
10

-0

6
.2
0.4

-0
0.2
.4

0.2

.4
2
-0

5 -0. 0.6 0
0.4
0 0.6 0.8 0.4
0.8
5 10 15 20 25 30 35 40
x

There are several new commands used in this file. On line 5 (again, I do not count the blank lines
and the comment lines) a command surfc is used. It plots a surface together with the level lines
beneath. Unlike the command surfc the command surf plots a surface only without the level
curves. Command colormap is used in line 6 to paint the surface using a user-supplied colors. If
the command colormap is not added, MATLAB uses default colors. Here is a list of color maps
that are available in MATLAB

hsv - hue-saturation-value color map


hot - black-red-yellow-white color map
gray - linear gray-scale color map
bone - gray-scale with tinge of blue color map
copper - linear copper-tone color map
pink - pastel shades of pink color map
white - all white color map
flag - alternating red, white, blue, and black color map
lines - color map with the line colors
colorcube - enhanced color-cube color map
vga - windows colormap for 16 colors
jet - variant of HSV
prism - prism color map
cool - shades of cyan and magenta color map
autumn - shades of red and yellow color map
spring - shades of magenta and yellow color map
winter - shades of blue and green color map
summer - shades of green and yellow color map

Command shading (see line 7) controls the color shading used to paint the surface. Command in
question takes one argument. The following
33

shading flat sets the shading of the current graph to flat


shading interp sets the shading to interpolated
shading faceted sets the shading to faceted, which is the default.

are the shading options that are available in MATLAB.

Command view (see line 8) is the 3-D graph viewpoint specification. It takes a three-dimensional
vector, which sets the view angle in Cartesian coordinates.

We will now focus attention on commands on lines 23 through 25. Command figure prompts
MATLAB to create a new Figure Window in which the level lines will be plotted. In order to
enhance the graph, we use command contourf instead of contour. The former plots filled contour
lines while the latter doesn't. On the same line we use command hold on to hold the current plot
and all axis properties so that subsequent graphing commands add to the existing graph. First
command on line 25 returns matrix c and graphics handle h that are used as the input parameters
for the function clabel, which adds height labels to the current contour plot.

Due to the space limitation we cannot address here other issues that are of interest for
programmers dealing with the 3-D graphics in MATLAB. To learn more on this subject the
interested reader is referred to [1-3] and [5].

 


In addition to static graphs discussed so far one can put a sequence of graphs in motion. In other
words, you can make a movie using MATLAB graphics tools. To learn how to create a movie, let
us analyze the m-file firstmovie

% Script file firstmovie.

% Graphs of y = sin(kx) over the interval [0, pi],


% where k = 1, 2, 3, 4, 5.

m = moviein(5);
x = 0:pi/100:pi;
for i=1:5
h1_line = plot(x,sin(i*x));
set(h1_line,'LineWidth',1.5,'Color','m')
grid
title('Sine functions sin(kx), k = 1, 2, 3, 4, 5')
h = get(gca,'Title');
set(h,'FontSize',12)
xlabel('x')
k = num2str(i);
if i > 1
s = strcat('sin(',k,'x)');
else
s = 'sin(x)';
end
ylabel(s)
h = get(gca,'ylabel');
set(h,'FontSize',12)
m(:,i) = getframe;
pause(2)
34

end
movie(m)

I suggest that you will play this movie first. To this aim type firstmovie in the Command
Window and press the Enter or Return key. You should notice that five frames are displayed
and at the end of the "show" frames are played again at a different speed.

There are very few new commands one has to learn in order to animate graphics in MATLAB.
We will use the m-file firstmovie as a starting point to our discussion. Command moviein, on
line 1, with an integral parameter, tells MATLAB that a movie consisting of five frames is
created in the body of this file. Consecutive frames are generated inside the loop for. Almost all
of the commands used there should be familiar to you. The only new one inside the loop is
getframe command. Each frame of the movie is stored in the column of the matrix m. With this
remark a role of this command should be clear. The last command in this file is movie(m). This
tells MATLAB to play the movie just created and saved in columns of the matrix m.

Warning. File firstmovie cannot be used with the Student Edition of MATLAB, version 4.2.
This is due to the matrix size limitation in this edition of MATLAB. Future release of the Student
Edition of MATLAB, version 5.3 will allow large size matrices. According to MathWorks, Inc.,
the makers of MATLAB, this product will be released in September 1999.

       



MATLAB has some functions for generating special surfaces. We will be concerned mostly with
two functions- sphere and cylinder.

The command sphere(n) generates a unit sphere with center at the origin using (n+1)2 points. If
function sphere is called without the input parameter, MATLAB uses the default value n = 20.
You can translate the center of the sphere easily. In the following example we will plot graph of
the unit sphere with center at (2, -1, 1)

[x,y,z] = sphere(30);
surf(x+2, y-1, z+1)

Function sphere together with function surf or mesh can be used to plot graphs of spheres of
arbitrary radii. Also, they can be used to plot graphs of ellipsoids. See Problems 25 and 26.
35

Function cylinder is used for plotting a surface of revolution. It takes two (optional) input
parameters. In the following command cylinder(r, n) parameter r stands for the vector that
defines the radius of cylinder along the z-axis and n specifies a number of points used to define
circumference of the cylinder. Default values of these parameters are r = [1 1] and n = 20. A
generated cylinder has a unit height.

The following command

cylinder([1 0])
title('Unit cone')

Unit cone

0.8

0.6

0.4

0.2

0
1
0.5 1
0 0.5
0
-0.5 -0.5
-1 -1

plots a cone with the base radius equal to one and the unit height.

In this example we will plot a graph of the surface of revolution obtained by rotating the curve
r(t) = < sin(t), t >, 0  t   about the y-axis. Graphs of the generating curve and the surface of
revolution are created using a few lines of the computer code

t = 0:pi/100:pi;
r = sin(t);
plot(r,t)
36

3.5

2.5

1.5

0.5

0
0 0.2 0.4 0.6 0.8 1

cylinder(r,15)
shading interp

! "

#$% 

In this section we deal with printing MATLAB graphics. To send a current graph to the printer
click on File and next select Print from the pull down menu. Once this menu is open you may
37

wish to preview a graph to be printed be selecting the option PrintPreview… first. You can also
send your graph to the printer using the print command as shown below

x = 0:0.01:1;
plot(x, x.^2)
print

You can print your graphics to an m- file using built-in device drivers. A fairly incomplete list of
these drivers is included here:

-depsc Level 1 color Encapsulated PostScript


-deps2 Level 2 black and white Encapsulated PostScript
-depsc2 Level 2 color Encapsulated PostScript

For a complete list of available device drivers see [5], Chapter 7, pp. 8-9.

Suppose that one wants to print a current graph to the m-file Figure1 using level 2 color
Encapsulated PostScript. This can be accomplished by executing the following command

print –depsc2 Figure1

You can put this command either inside your m-file or execute it from within the Command
Window.
38

"

[1] D. Hanselman and B. Littlefield, Mastering MATLAB 5. A Comprehensive Tutorial and


Reference, Prentice Hall, Upper Saddle River, NJ, 1998.

[2] P. Marchand, Graphics and GUIs with MATLAB, Second edition, CRC Press, Boca Raton,
1999.

[3] K. Sigmon, MATLAB Primer, Fifth edition, CRC Press, Boca Raton, 1998.

[4] Using MATLAB, Version 5, The MathWorks, Inc., 1996.

[5] Using MATLAB Graphics, Version 5, The MathWorks, Inc., 1996.


39

- 

In Problems 1- 4 you cannot use loops for or while.

1. Write MATLAB function sigma = ascsum(x) that takes a one-dimensional array x of real
numbers and computes their sum sigma in the ascending order of magnitudes.
Hint: You may wish to use MATLAB functions sort, sum, and abs.

2. In this exercise you are to write MATLAB function d = dsc(c) that takes a one-dimensional
array of numbers c and returns an array d consisting of all numbers in the array c with all
neighboring duplicated numbers being removed. For instance, if c = [1 2 2 2 3 1], then
d = [1 2 3 1].

3. Write MATLAB function p = fact(n) that takes a nonnegative integer n and returns value of
the factorial function n! = 1*2* … *n. Add an error message to your code that will be
executed when the input parameter is a negative number.

4. Write MATLAB function [in, fr] = infr(x) that takes an array x of real numbers and returns
arrays in and fr holding the integral and fractional parts, respectively, of all numbers in the
array x.

5. Given an array b and a positive integer m create an array d whose entries are those in the
array b each replicated m-times. Write MATLAB function d = repel(b, m) that generates
array d as described in this problem.

6. In this exercise you are to write MATLAB function d = rep(b, m) that has more
functionality than the function repel of Problem 5. It takes an array of numbers b and the
array m of positive integers and returns an array d whose each entry is taken from the array b
and is duplicated according to the corresponding value in the array m. For instance, if
b = [ 1 2] and m = [2 3], then d = [1 1 2 2 2].

7. A checkerboard matrix is a square block diagonal matrix, i.e., the only nonzero entries are in
the square blocks along the main diagonal. In this exercise you are to write MATLAB
function A = mysparse(n) that takes an odd number n and returns a checkerboard matrix
as shown below

A = mysparse(3)

A =
1 0 0
0 1 2
0 3 4

A = mysparse(5)
40

A =
1 0 0 0 0
0 1 2 0 0
0 3 4 0 0
0 0 0 2 3
0 0 0 4 5

A = mysparse(7)

A =
1 0 0 0 0 0 0
0 1 2 0 0 0 0
0 3 4 0 0 0 0
0 0 0 2 3 0 0
0 0 0 4 5 0 0
0 0 0 0 0 3 4
0 0 0 0 0 5 6

First block in the upper-left corner is the 1-by-1 matrix while the remaining blocks are all
2-by-2.

8. The Legendre polynomials Pn(x), n = 0, 1, … are defined recursively as follows

nPn(x) = (2n-1)xPn -1 – (n-1)Pn-2(x), n = 2, 3, … , P0(x) = 1, P1(x) = x.

Write MATLAB function P = LegendP(n) that takes an integer n – the degree of Pn(x) and
returns its coefficient stored in the descending order of powers.

9. In this exercise you are to implement Euclid's Algorithm for computing the greatest common
divisor (gcd) of two integer numbers a and b:

gcd(a, 0) = a, gcd(a, b) = gcd(b, rem(a, b)).

Here rem(a, b) stands for the remainder in dividing a by b. MATLAB has function rem.
Write MATLAB function gcd = mygcd(a,b) that implements Euclid's Algorithm.

10. The Pascale triangle holds coefficients in the series exapansion of (1 + x)n, where
n = 0, 1, 2, … . The top of this triangle, for n = 0, 1, 2, is shown here

1
11
1 2 1

Write MATLAB function t = pasctri(n) that generates the Pascal triangle t up to the level n.
Remark. Two-dimensional arrays in MATLAB must have the same number of columns in
each row. In order to aviod error messages you have to add a certain number of zero entries
to the right of last nonzero entry in each row of t but one. This

t = pasctri(2)
41

t =
1 0 0
1 1 0
1 2 1

is an example of the array t for n = 2.

11. This is a continuation of Problem 10. Write MATLAB function t = binexp(n) that
computes an array t with row k+1 holding coefficients in the series expansion of (1-x)^k,
k = 0, 1, ... , n, in the ascending order of powers. You may wish to make a call from within
your function to the function pasctri of Problem 10. Your output sholud look like this (case
n = 3)

t = binexp(3)

t =
1 0 0 0
1 -1 0 0
1 -2 1 0
1 -3 3 -1

12. MATLAB come with the built-in function mean for computing the unweighted arithmetic
mean of real numbers. Let x = {x1, x2, … , xn} be an array of n real numbers. Then

1 n
mean ( x )   x n
n k 1

In some problems that arise in mathematical statistics one has to compute the weighted
arithmetic mean of numbers in the array x. The latter, abbreviated here as wam, is defined as
follows

 w k xk
wam( x, w)  k 1
n

 w
k 1
k

Here w = {w1, w2, … , wn} is the array of weights associated with variables x. The weights
are all nonnegative with w1 + w2 + … + wn > 0.
In this exercise you are to write MATLAB function y = wam(x, w) that takes the arrays of
variables and weights and returns the weighted arithmetic mean as defined above. Add three
error messages to terminate prematurely execution of this file in the case when:

 arrays x and w are of different lengths


 at least one number in the array w is negative
 sum of all weights is equal to zero.
42

13. Let w = {w1, w2, … , wn} be an array of positive numbers. The weighted geometric mean,
abbreviated as wgm, of the nonnegative variables x = {x1, x2, … , xn} is defined as follows

wgm( x, w)  x1 1 x 2 2 ... x n
w w wn

Here we assume that the weights w sum up to one.


Write MATLAB function y = wgm(x, w) that takes arrays x and w and returns the weighted
geometric mean y of x with weights stored in the array w. Add three error messages to
terminate prematurely execution of this file in the case when:

 arrays x and w are of different lengths


 at least one variable in the array x is negative
 at least one weight in the array w is less than or equal to zero

Also, normalize the weights w, if necessary, so that they will sum up to one.

14. Write MATLAB function [nonz, mns] = matstat(A) that takes as the input argument a real
matrix A and returns all nonzero entries of A in the column vector nonz. Second output
parameter mns holds values of the unweighted arithmetic means of all columns of A.

15. Solving triangles requires a bit of knowledge of trigonometry. In this exercise


you are to write MATLAB function [a, B, C] = sas(b, A, c) that is intended for solving
triangles given two sides b and c and the angle A between these sides. Your function should
determine remaining two angels and the third side of the triangle to be solved. All angles
should be expressed in the degree measure.

16. Write MATLAB function [A, B, C] = sss(a, b, c) that takes three positive numbers a, b, and
c. If they are sides of a triangle, then your function should return its angles A, B, and C, in
the degree measure, otherwise an error message should be displayed to the screen.

17. In this exercise you are to write MATLAB function dms(x) that takes a nonnegative number
x that represents an angle in the degree measure and converts it to the form
x deg. y min. z sec.. Display a result to the screen using commands disp and sprintf.
Example:

dms(10.2345)

Angle = 10 deg. 14 min. 4 sec.

18. Complete elliptic integral of the first kind in the Legendre form K(k2), 0 < k2 < 1,

 /2
dt
K (k 2 )  
0 1  k 2 sin 2 (t )

cannot be evaluated in terms of the elementary functions. The following algorithm, due to
C. F. Gauss, generates a sequence of the arithmetic means {an} and a sequence of the
geometric means {bn}, where
43

a0 = 1, b0 = 1 k 2

an = (an-1 + bn-1)/2, bn = a n 1 b n 1 n = 1, 2, … .

It is known that both sequences have a common limit g and that an  bn, for all n.
Moreover,


K(k2) =
2g

Write MATLAB function K = compK(k2) which implements this algorithm. The input
parameter k2 stands for k2. Use the loop while to generate consecutive members of both
sequences, but do not save all numbers generated in the course of computations. Continue
execution of the while loop as long as an – bn  eps, where eps is the machine epsilon

eps

ans =
2.2204e-016

Add more functionality to your code by allowing the input parameter k2 to be an array. Test
your m-file and compare your results with those included here

format long

compK([.1 .2 .3 .7 .8 .9])

ans =
1.61244134872022
1.65962359861053
1.71388944817879
2.07536313529247
2.25720532682085
2.57809211334794

format short

19. In this exercise you are to model one of the games in the Illinois State Lottery. Three
numbers, with duplicates allowed, are selected randomly from the set {0,1,2,3,4,5,6,7,8,9}
in the game Pick3 and four numbers are selected in the Pick4 game. Write MATLAB
function winnumbs = lotto(n) that takes an integer n as its input parameter and returns an
array winnumbs consisting of n numbers from the set of integers described in this
problem. Use MATLAB function rand together with other functions to generate a set of
winning numbers. Add an error message that is displayed to the screen when the input
parameter is out of range.
44

20. Write MATLAB function t = isodd(A) that takes an array A of nonzero integers and returns
1 if all entries in the array A are odd numbers and 0 otherwise. You may wish to use
MATLAB function rem in your file.

21. Given two one-dimensional arrays a and b, not necessarily of the same length. Write
MATLAB function c = interleave(a, b) which takes arrays a and b and returns an array c
obtained by interleaving entries in the input arrays. For instance, if a = [1, 3, 5, 7] and
b = [-2, –4], then c = [1, –2, 3, –4, 5, 7]. Your program should work for empty arrays too.
You cannot use loops for or while.

22. Write a script file Problem22 to plot, in the same window, graphs of two parabolas y = x2
and x = y2, where –1  x  1. Label the axes, add a title to your graph and use command
grid. To improve readability of the graphs plotted add a legend. MATLAB has a command
legend. To learn more about this command type help legend in the Command Window and
press Enter or Return key.

23. Write MATLAB function eqtri(a, b) that plots the graph of the equilateral triangle with two
vertices at (a,a) and (b,a). Third vertex lies above the line segment that connects points (a, a)
and (b, a). Use function fill to paint the triangle using a color of your choice.

24. In this exercise you are to plot graphs of the Chebyshev polynomial Tn(x) and its first order
derivative over the interval [-1, 1]. Write MATLAB function plotChT(n) that takes as the
input parameter the degree n of the Chebyshev polynomial. Use functions ChebT and derp,
included in Tutorial 2, to compute coefficients of Tn(x) and T'n(x), respectively. Evaluate
both, the polynomial and its first order derivative at x = linspace(-1, 1) using MATLAB
function polyval. Add a meaningful title to your graph. In order to improve readability of
your graph you may wish to add a descriptive legend. Here is a sample output

plotChT(5)

Chebyshev polynomial T5(x) and its first order derivative


25
polynomial
derivative
20

15

10
y

-5

-10
-1 -0.5 0 0.5 1
x
45

25. Use function sphere to plot the graph of a sphere of radius r with center at (a, b, c). Use
MATLAB function axis with an option 'equal'. Add a title to your graph and save your
computer code as the MATLAB function sph(r, a, b, c).

26. Write MATLAB function ellipsoid(x0, y0, z0, a, b, c) that takes coordinates (x0, y0, z0) of
the center of the ellipsoid with semiaxes (a, b, c) and plots its graph. Use MATLAB
functions sphere and surf. Add a meaningful title to your graph and use function
axis('equal').

27. In this exercise you are to plot a graph of the two-sided cone, with vertex at the origin, and
the-axis as the axis of symmetry. Write MATLAB function cone(a, b), where the input
parameters a and b stand for the radius of the lower and upper base, respectively. Use
MATLAB functions cylinder and surf to plot a cone in question. Add a title to your graph
and use function shading with an argument of your choice. A sample output is shown below

cone(1, 2)

Two-sided cone with the radii of the bases equal to1 and2

0.5

0
z

-0.5
2
1 2
0 1
0
-1 -1
y -2 -2
x

28. The space curve r(t) = < cos(t)sin(4t), sin(t)sin(4t), cos(4t) >, 0  t  2, lies on the surface
of the unit sphere x2 + y2 + z2 = 1. Write MATLAB script file curvsph that plots both the
curve and the sphere in the same window. Add a meaningful title to your graph. Use
MATLAB functions colormap and shading with arguments of your choice. Add the
view([150 125 50]) command.

29. This problem requires that the professional version 5.x of MATLAB is installed.
In this exercise you are to write the m-file secondmovie that crates five frames of the surface
z = sin(kx)cos(ky), where 0  x, y   and k = 1, 2, 3, 4, 5. Make a movie consisting of the
46

frames you generated in your file. Use MATLAB functions colormap and shading with
arguments of your choice. Add a title, which might look like this
Graphs of z = sin(kx)*cos(ky), 0 <= x, y <= , k =1, 2, 3, 4, 5. Greek letters can be
printed in the title of a graph using TeX convention, i.e., the following \pi is used to print
the Greek letter . Similarly, the string \alpha will be printed as .


      

 

Edward Neuman
Department of Mathematics
Southern Illinois University at Carbondale
edneuman@siu.edu

One of the nice features of MATLAB is its ease of computations with vectors and matrices. In
this tutorial the following topics are discussed: vectors and matrices in MATLAB, solving
systems of linear equations, the inverse of a matrix, determinants, vectors in n-dimensional
Euclidean space, linear transformations, real vector spaces and the matrix eigenvalue problem.
Applications of linear algebra to the curve fitting, message coding and computer graphics are also
included.

           

For the reader's convenience we include lists of special characters and MATLAB functions that
are used in this tutorial.

Special characters
; Semicolon operator
' Conjugated transpose
.' Transpose
* Times
. Dot operator
^ Power operator
[] Emty vector operator
: Colon operator
= Assignment
== Equality
\ Backslash or left division
/ Right division
i, j Imaginary unit
~ Logical not
~= Logical not equal
& Logical and
| Logical or
{} Cell
2

Function Description
acos Inverse cosine
axis Control axis scaling and appearance
char Create character array
chol Cholesky factorization
cos Cosine function
cross Vector cross product
det Determinant
diag Diagonal matrices and diagonals of a matrix
double Convert to double precision
eig Eigenvalues and eigenvectors
eye Identity matrix
fill Filled 2-D polygons
fix Round towards zero
fliplr Flip matrix in left/right direction
flops Floating point operation count
grid Grid lines
hadamard Hadamard matrix
hilb Hilbert matrix
hold Hold current graph
inv Matrix inverse
isempty True for empty matrix
legend Graph legend
length Length of vector
linspace Linearly spaced vector
logical Convert numerical values to logical
magic Magic square
max Largest component
min Smallest component
norm Matrix or vector norm
null Null space
num2cell Convert numeric array into cell array
num2str Convert number to string
ones Ones array
pascal Pascal matrix
plot Linear plot
poly Convert roots to polynomial
polyval Evaluate polynomial
rand Uniformly distributed random numbers
randn Normally distributed random numbers
rank Matrix rank
reff Reduced row echelon form
rem Remainder after division
reshape Change size
roots Find polynomial roots
sin Sine function
size Size of matrix
sort Sort in ascending order
3

subs Symbolic substitution


sym Construct symbolic bumbers and variables
tic Start a stopwatch timer
title Graph title
toc Read the stopwatch timer
toeplitz Tioeplitz matrix
tril Extract lower triangular part
triu Extract upper triangular part
vander Vandermonde matrix
varargin Variable length input argument list
zeros Zeros array

      

The purpose of this section is to demonstrate how to create and transform vectors and matrices in
MATLAB.

This command creates a row vector

a = [1 2 3]

a =
1 2 3

Column vectors are inputted in a similar way, however, semicolons must separate the components
of a vector

b = [1;2;3]

b =
1
2
3

The quote operator ' is used to create the conjugate transpose of a vector (matrix) while the dot-
quote operator .' creates the transpose vector (matrix). To illustrate this let us form a complex
vector a + i*b' and next apply these operations to the resulting vector to obtain

(a+i*b')'

ans =
1.0000 - 1.0000i
2.0000 - 2.0000i
3.0000 - 3.0000i

while
4

(a+i*b').'

ans =
1.0000 + 1.0000i
2.0000 + 2.0000i
3.0000 + 3.0000i

Command length returns the number of components of a vector

length(a)

ans =
3

The dot operator. plays a specific role in MATLAB. It is used for the componentwise application
of the operator that follows the dot operator

a.*a

ans =
1 4 9

The same result is obtained by applying the power operator ^ to the vector a

a.^2

ans =
1 4 9

Componentwise division of vectors a and b can be accomplished by using the backslash operator
\ together with the dot operator .

a.\b'

ans =
1 1 1

For the purpose of the next example let us change vector a to the column vector

a = a'

a =
1
2
3

The dot product and the outer product of vectors a and b are calculated as follows

dotprod = a'*b
5

dotprod =
14
outprod = a*b'

outprod =
1 2 3
2 4 6
3 6 9

The cross product of two three-dimensional vectors is calculated using command cross. Let the
vector a be the same as above and let

b = [-2 1 2];

Note that the semicolon after a command avoids display of the result. The cross product of a and
b is

cp = cross(a,b)

cp =
1 -8 5

The cross product vector cp is perpendicular to both a and b

[cp*a cp*b']

ans =
0 0

We will now deal with operations on matrices. Addition, subtraction, and scalar multiplication are
defined in the same way as for the vectors.

This creates a 3-by-3 matrix

A = [1 2 3;4 5 6;7 8 10]

A =
1 2 3
4 5 6
7 8 10

Note that the semicolon operator ; separates the rows. To extract a submatrix B consisting of
rows 1 and 3 and columns 1 and 2 of the matrix A do the following

B = A([1 3], [1 2])

B =
1 2
7 8

To interchange rows 1 and 3 of A use the vector of row indices together with the colon operator

C = A([3 2 1],:)
6

C =
7 8 10
4 5 6
1 2 3

The colon operator : stands for all columns or all rows. For the matrix A from the last example
the following command

A(:)

ans =
1
4
7
2
5
8
3
6
10

creates a vector version of the matrix A. We will use this operator on several occasions.

To delete a row (column) use the empty vector operator [ ]

A(:, 2) = []

A =
1 3
4 6
7 10

Second column of the matrix A is now deleted. To insert a row (column) we use the technique for
creating matrices and vectors

A = [A(:,1) [2 5 8]' A(:,2)]


A =
1 2 3
4 5 6
7 8 10

Matrix A is now restored to its original form.

Using MATLAB commands one can easily extract those entries of a matrix that satisfy an impsed
condition. Suppose that one wants to extract all entries of that are greater than one. First, we
define a new matrix A

A = [-1 2 3;0 5 1]

A =
-1 2 3
0 5 1
7

Command A > 1 creates a matrix of zeros and ones

A > 1

ans =
0 1 1
0 1 0

with ones on these positions where the entries of A satisfy the imposed condition and zeros
everywhere else. This illustrates logical addressing in MATLAB. To extract those entries of the
matrix A that are greater than one we execute the following command

A(A > 1)

ans =
2
5
3

The dot operator . works for matrices too. Let now

A = [1 2 3; 3 2 1] ;

The following command

A.*A

ans =
1 4 9
9 4 1

computes the entry-by-entry product of A with A. However, the following command

A*A

¨??? Error using ==> *


Inner matrix dimensions must agree.

generates an error message.

Function diag will be used on several occasions. This creates a diagonal matrix with the diagonal
entries stored in the vector d

d = [1 2 3];

D = diag(d)

D =
1 0 0
0 2 0
0 0 3
8

To extract the main diagonal of the matrix D we use function diag again to obtain

d = diag(D)

d =
1
2
3

What is the result of executing of the following command?

diag(diag(d));

In some problems that arise in linear algebra one needs to calculate a linear combination of
several matrices of the same dimension. In order to obtain the desired combination both the
coefficients and the matrices must be stored in cells. In MATLAB a cell is inputted using curly
braces{ }. This

c = {1,-2,3}

c =
[1] [-2] [3]

is an example of the cell. Function lincomb will be used later on in this tutorial.

function M = lincomb(v,A)

% Linear combination M of several matrices of the same size.


% Coefficients v = {v1,v2,…,vm} of the linear combination and the
% matrices A = {A1,A2,...,Am} must be inputted as cells.

m = length(v);
[k, l] = size(A{1});
M = zeros(k, l);
for i = 1:m
M = M + v{i}*A{i};
end


   !     "

MATLAB has several tool needed for computing a solution of the system of linear equations.

Let A be an m-by-n matrix and let b be an m-dimensional (column) vector. To solve


the linear system Ax = b one can use the backslash operator \ , which is also called the left
division.
9

1. Case m = n

In this case MATLAB calculates the exact solution (modulo the roundoff errors) to the system in
question.

Let

A = [1 2 3;4 5 6;7 8 10]

A =
1 2 3
4 5 6
7 8 10

and let

b = ones(3,1);

Then

x = A\b

x =
-1.0000
1.0000
0.0000

In order to verify correctness of the computed solution let us compute the residual vector r

r = b - A*x

r =
1.0e-015 *
0.1110
0.6661
0.2220

Entries of the computed residual r theoretically should all be equal to zero. This example
illustrates an effect of the roundoff erros on the computed solution.

2. Case m > n

If m > n, then the system Ax = b is overdetermined and in most cases system is inconsistent. A
solution to the system Ax = b, obtained with the aid of the backslash operator \ , is the least-
squares solution.

Let now

A = [2 –1; 1 10; 1 2];

and let the vector of the right-hand sides will be the same as the one in the last example. Then
10

x = A\b
x =
0.5849
0.0491

The residual r of the computed solution is equal to

r = b - A*x

r =
-0.1208
-0.0755
0.3170

Theoretically the residual r is orthogonal to the column space of A. We have

r'*A

ans =
1.0e-014 *
0.1110
0.6994

3. Case m < n

If the number of unknowns exceeds the number of equations, then the linear system is
underdetermined. In this case MATLAB computes a particular solution provided the system is
consistent. Let now

A = [1 2 3; 4 5 6];
b = ones(2,1);

Then

x = A\b

x =
-0.5000
0
0.5000

A general solution to the given system is obtained by forming a linear combination of x with the
columns of the null space of A. The latter is computed using MATLAB function null

z = null(A)

z =
0.4082
-0.8165
0.4082
11

Suppose that one wants to compute a solution being a linear combination of x and z, with
coefficients 1 and –1. Using function lincomb we obtain
w = lincomb({1,-1},{x,z})

w =
-0.9082
0.8165
0.0918

The residual r is calculated in a usual way

r = b - A*w

r =
1.0e-015 *
-0.4441
0.1110

# $      

The built-in function rref allows a user to solve several problems of linear algebra. In this section
we shall employ this function to compute a solution to the system of linear equations and also to
find the rank of a matrix. Other applications are discussed in the subsequent sections of this
tutorial.

Function rref takes a matrix and returns the reduced row echelon form of its argument. Syntax of
the rref command is

B = rref(A) or [B, pivot] = rref(A)

The second output parameter pivot holds the indices of the pivot columns.

Let

A = magic(3); b = ones(3,1);

A solution x to the linear system Ax = b is obtained in two steps. First the augmented matrix of
the system is transformed to the reduced echelon form and next its last column is extracted

[x, pivot] = rref([A b])

x =
1.0000 0 0 0.0667
0 1.0000 0 0.0667
0 0 1.0000 0.0667
pivot =
1 2 3
12

x = x(:,4)

x =
0.0667
0.0667
0.0667

The residual of the computed solution is

b - A*x

ans =
0
0
0

Information stored in the output parameter pivot can be used to compute the rank of the matrix A

length(pivot)

ans =
3

%       &

MATLAB function inv is used to compute the inverse matrix.

Let the matrix A be defined as follows

A = [1 2 3;4 5 6;7 8 10]

A =
1 2 3
4 5 6
7 8 10

Then

B = inv(A)

B =
-0.6667 -1.3333 1.0000
-0.6667 3.6667 -2.0000
1.0000 -2.0000 1.0000

In order to verify that B is the inverse matrix of A it sufficies to show that A*B = I and
B*A = I, where I is the 3-by-3 identity matrix. We have
13

A*B

ans =
1.0000 0 -0.0000
0 1.0000 0
0 0 1.0000

In a similar way one can check that B*A = I.

The Pascal matrix, named in MATLAB pascal, has several interesting properties. Let

A = pascal(3)

A =
1 1 1
1 2 3
1 3 6

Its inverse B

B = inv(A)

B =
3 -3 1
-3 5 -2
1 -2 1

is the matrix of integers. The Cholesky triangle of the matrix A is

S = chol(A)

S =
1 1 1
0 1 2
0 0 1

Note that the upper triangular part of S holds the binomial coefficients. One can verify easily that
A = S'*S.

Function rref can also be used to compute the inverse matrix. Let A is the same as above. We
create first the augmented matrix B with A being followed by the identity matrix of the same size
as A. Running function rref on the augmented matrix and next extracting columns four through
six of the resulting matrix, we obtain

B = rref([A eye(size(A))]);

B = B(:, 4:6)

B =
3 -3 1
-3 5 -2
1 -2 1
14

To verify this result, we compute first the product A *B

A*B

ans =
1 0 0
0 1 0
0 0 1

and next B*A

B*A

ans =
1 0 0
0 1 0
0 0 1

This shows that B is indeed the inverse matrix of A.

' (  

In some applications of linear algebra knowledge of the determinant of a matrix is required.


MATLAB built-in function det is designed for computing determinants.

Let

A = magic(3);

Determinant of A is equal to

det(A)

ans =
-360

One of the classical methods for computing determinants utilizes a cofactor expansion. For more
details, see e.g., [2], pp. 103-114.

Function ckl = cofact(A, k, l) computes the cofactor ckl of the akl entry of the matrix A

function ckl = cofact(A,k,l)

% Cofactor ckl of the a_kl entry of the matrix A.

[m,n] = size(A);
if m ~= n
error('Matrix must be square')
15

end
B = A([1:k-1,k+1:n],[1:l-1,l+1:n]);
ckl = (-1)^(k+l)*det(B);

Function d = mydet(A) implements the method of cofactor expansion for computing


determinants

function d = mydet(A)

% Determinant d of the matrix A. Function cofact must be


% in MATLAB's search path.

[m,n] = size(A);
if m ~= n
error('Matrix must be square')
end
a = A(1,:);
c = [];
for l=1:n
c1l = cofact(A,1,l);
c = [c;c1l];
end
d = a*c;

Let us note that function mydet uses the cofactor expansion along the row 1 of the matrix A.
Method of cofactors has a high computational complexity. Therefore it is not recommended for
computations with large matrices. Its is included here for pedagogical reasons only. To measure a
computational complexity of two functions det and mydet we will use MATLAB built-in
function flops. It counts the number of floating-point operations (additions, subtractions,
multiplications and divisions). Let

A = rand(25);

be a 25-by-25 matrix of uniformly distributed random numbers in the interval ( 0, 1 ). Using


function det we obtain

flops(0)
det(A)

ans =
-0.1867

flops

ans =
10100

For comparison, a number of flops used by function mydet is

flops(0)
16

mydet(A)

ans =
-0.1867

flops

ans =
223350

The adjoint matrix adj(A) of the matrix A is also of interest in linear algebra (see, e.g., [2],
p.108).

function B = adj(A)

% Adjoint matrix B of the square matrix A.

[m,n] = size(A);
if m ~= n
error('Matrix must be square')
end
B = [];
for k = 1:n
for l=1:n
B = [B;cofact(A,k,l)];
end
end
B = reshape(B,n,n);

The adjoint matrix and the inverse matrix satisfy the equation

A-1 = adj(A)/det(A)

(see [2], p.110 ). Due to the high computational complexity this formula is not recommended for
computing the inverse matrix.

)   

The 2-norm (Euclidean norm) of a vector is computed in MATLAB using function norm.

Let

a = -2:2

a =
-2 -1 0 1 2

The 2-norm of a is equal to

twon = norm(a)
17

twon =
3.1623

With each nonzero vector one can associate a unit vector that is parallel to the given vector. For
instance, for the vector a in the last example its unit vector is

unitv = a /twon

unitv =
-0.6325 -0.3162 0 0.3162 0.6325

The angle θ between two vectors a and b of the same dimension is computed using the formula

 = arccos(a.b/||a|| ||b||),

where a.b stands for the dot product of a and b, ||a|| is the norm of the vector a and arccos is the
inverse cosine function.

Let the vector a be the same as defined above and let

b = (1:5)'

b =
1
2
3
4
5

Then

angle = acos((a*b)/(norm(a)*norm(b)))

angle =
1.1303

Concept of the cross product can be generalized easily to the set consisting of n -1 vectors in the
n-dimensional Euclidean space n. Function crossprod provides a generalization of the
MATLAB function cross.

function cp = crossprod(A)

% Cross product cp of a set of vectors that are stored in columns of A.

[n, m] = size(A);
if n ~= m+1
error('Number of columns of A must be one less than the number of
rows')
18

end
if rank(A) < min(m,n)
cp = zeros(n,1);
else
C = [ones(n,1) A]';
cp = zeros(n,1);
for j=1:n
cp(j) = cofact(C,1,j);
end
end

Let

A = [1 -2 3; 4 5 6; 7 8 9; 1 0 1]

A =
1 -2 3
4 5 6
7 8 9
1 0 1

The cross product of column vectors of A is

cp = crossprod(A)

cp =
-6
20
-14
24

Vector cp is orthogonal to the column space of the matrix A. One can easily verify this by
computing the vector-matrix product

cp'*A

ans =
0 0 0

*        

Let L: n  m be a linear transformation. It is well known that any linear transformation in


question is represented by an m-by-n matrix A, i.e., L(x) = Ax holds true for any x  n.
Matrices of some linear transformations including those of reflections and rotations are discussed
in detail in Tutorial 4, Section 4.3.

With each matrix one can associate four subspaces called the four fundamental subspaces. The
subspaces in question are called the column space, the nullspace, the row space, and the left
19

nullspace. First two subspaces are tied closely to the linear transformations on the finite-
dimensional spaces.

Throughout the sequel the symbols (L) and (L) will stand for the range and the kernel of the
linear transformation L, respectively. Bases of these subspaces can be computed easily. Recall
that (L) = column space of A and (L) = nullspace of A. Thus the problem of computing the
bases of the range and the kernel of a linear transformation L is equivalent to the problem of
finding bases of the column space and the nullspace of a matrix that represents transformation L.

Function fourb uses two MATLAB functions rref and null to campute bases of four fundamental
subspaces associated with a matrix A.

function [cs, ns, rs, lns] = fourb(A)

% Bases of four fundamental vector spaces associated


% with the matrix A.
% cs- basis of the column space of A
% ns- basis of the nullspace of A
% rs- basis of the row space of A
% lns- basis of the left nullspace of A

[V, pivot] = rref(A);


r = length(pivot);
cs = A(:,pivot);
ns = null(A,'r');
rs = V(1:r,:)';
lns = null(A','r');

In this example we will find bases of four fundamental subspaces associated with the random
matrix of zeros and ones.
This set up the seed of the randn function to 0

randn('seed',0)

Recall that this function generates normally distributed random numbers. Next a 3-by-5 random
matrix is generated using function randn

A = randn(3,5)

A =
1.1650 0.3516 0.0591 0.8717 1.2460
0.6268 -0.6965 1.7971 -1.4462 -0.6390
0.0751 1.6961 0.2641 -0.7012 0.5774

The following trick creates a matrix of zeros and ones from the random matrix A

A = A >= 0

A =
1 1 1 1 1
1 0 1 0 0
1 1 1 0 1
20

Bases of four fundamental subspaces of matrix A are now computed using function fourb

[cs, ns, rs, lns] = fourb(A)

cs =
1 1 1
1 0 0
1 1 0
ns =
-1 0
0 -1
1 0
0 0
0 1
rs =
1 0 0
0 1 0
1 0 0
0 0 1
0 1 0
lns =
Empty matrix: 3-by-0

Vectors that form bases of the subspaces under discussion are saved as the column vectors.
The Fundamental Theorem of Linear Algebra states that the row space of A is orthogonal to the
nullspace of A and also that the column space of A is orthogonal to the left nullspace of A
(see [6] ). For the bases of the subspaces in this example we have

rs'*ns

ans =
0 0
0 0
0 0

cs'*lns

ans =
Empty matrix: 3-by-0

+ ,  

In this section we discuss some computational tools that can be used in studies of real vector
spaces. Focus is on linear span, linear independence, transition matrices and the Gram-Schmidt
orthogonalization.
21

Linear span

Concept of the linear span of a set of vectors in a vector space is one of the most important ones
in linear algebra. Using MATLAB one can determine easily whether or not given vector is in the
span of a set of vectors. Function span takes a vector, say v, and an unspecified numbers of
vectors that form a span. All inputted vectors must be of the same size. On the output a message
is displayed to the screen. It says that either v is in the span or that v is not in the span.

function span(v, varargin)

% Test whether or not vector v is in the span of a set


% of vectors.

A = [];
n = length(varargin);
for i=1:n
u = varargin{i};
u = u';
A = [A u(:)];
end
v = v';
v = v(:);
if rank(A) == rank([A v])
disp(' Given vector is in the span.')
else
disp(' Given vector is not in the span.')
end

The key fact used in this function is a well-known result regarding existence of a solution to the
system of linear equations. Recall that the system of linear equations Ax = b possesses a solution
iff rank(A) = rank( [A b] ). MATLAB function varargin used here allows a user to enter a
variable number of vectors of the span.

To test function span we will run this function on matrices. Let

v = ones(3);

and choose matrices

A = pascal(3);

and

B = rand(3);

to determine whether or not v belongs to the span of A and B. Executing function span we obtain

span(v, A, B)

Given vector is not in the span.


22

Linear independence

Suppose that one wants to check whether or not a given set of vectors is linearly independent.
Utilizing some ideas used in function span one can write his/her function that will take an
uspecified number of vectors and return a message regarding linear independence/dependence of
the given set of vectors. We leave this task to the reader (see Problem 32).

Transition matrix

Problem of finding the transition matrix from one vector space to another vector space is interest
in linear algebra. We assume that the ordered bases of these spaces are stored in columns of
matrices T and S, respectively. Function transmat implements a well-known method for finding
the transition matrix.

function V = transmat(T, S)

% Transition matrix V from a vector space having the ordered


% basis T to another vector space having the ordered basis S.
% Bases of the vector spaces are stored in columns of the
% matrices T and S.

[m, n] = size(T);
[p, q] = size(S);
if (m ~= p) | (n ~= q)
error('Matrices must be of the same dimension')
end
V = rref([S T]);
V = V(:,(m + 1):(m + n));

Let

T = [1 2;3 4]; S = [0 1;1 0];

be the ordered bases of two vector spaces. The transition matrix V form a vector space having the
ordered basis T to a vector space whose ordered basis is stored in columns of the matrix S is

V = transmat(T, S)

V =
3 4
1 2

We will use the transition matrix V to compute a coordinate vector in the basis S. Let

1
[x]T =  
1

be the coordinate vector in the basis T. Then the coordinate vector [x]S, is

xs = V*[1;1]
23

xs =
7
3

Gram-Schmidt orthogonalization

Problem discussed in this subsection is formulated as follows. Given a basis A = {u1, u2, … , um}
of a nonzero subspace W of n. Find an orthonormal basis V = {v1, v2, … , vm} for W.
Assume that the basis S of the subspace W is stored in columns of the matrix A, i.e.,
A = [u1; u2; … ; um], where each uk is a column vector. Function gs(A) computes an orthonormal
basis V for W using a classical method of Gram and Schmidt.

function V = gs(A)

% Gram-Schmidt orthogonalization of vectors stored in


% columns of the matrix A. Orthonormalized vectors are
% stored in columns of the matrix V.

[m,n] = size(A);
for k=1:n
V(:,k) = A(:,k);
for j=1:k-1
R(j,k) = V(:,j)'*A(:,k);
V(:,k) = V(:,k) - R(j,k)*V(:,j);
end
R(k,k) = norm(V(:,k));
V(:,k) = V(:,k)/R(k,k);
end

Let W be a subspace of 3 and let the columns of the matrix A, where

1 1
A =  2 1
 3 1

form a basis for W. An orthonormal basis V for W is computed using function gs

V = gs([1 1;2 1;3 1])

V =
0.2673 0.8729
0.5345 0.2182
0.8018 -0.4364

To verify that the columns of V form an orthonormal set it sufficies to check that VTV = I. We
have
24

V'*V

ans =
1.0000 0.0000
0.0000 1.0000

We will now use matrix V to compute the coordinate vector [v]V, where

v = [1 0 1];

We have

v*V

ans =
1.0690 0.4364

-  &   

MATLAB function eig is designed for computing the eigenvalues and the eigenvectors of the
matrix A. Its syntax is shown below

[V, D] = eig(A)

The eigenvalues of A are stored as the diagonal entries of the diagonal matrix D and the
associated eigenvectors are stored in columns of the matrix V.

Let

A = pascal(3);

Then

[V, D] = eig(A)

V =
0.5438 -0.8165 0.1938
-0.7812 -0.4082 0.4722
0.3065 0.4082 0.8599
D =
0.1270 0 0
0 1.0000 0
0 0 7.8730

Clearly, matrix A is diagonalizable. The eigenvalue-eigenvector decomposition A = VDV -1of A


is calculated as follows

V*D/V
25

ans =
1.0000 1.0000 1.0000
1.0000 2.0000 3.0000
1.0000 3.0000 6.0000

Note the use of the right division operator / instead of using the inverse matrix function inv. This
is motivated by the fact that computation of the inverse matrix takes longer than the execution of
the right division operation.

The characteristic polynomial of a matrix is obtained by invoking the function poly.


Let

A = magic(3);

be the magic square. In this example the vector chpol holds the coefficients of the characteristic
polynomial of the matrix A. Recall that a polynomial is represented in MATLAB by its
coefficients that are ordered by descending powers

chpol = poly(A)

chpol =
1.0000 -15.0000 -24.0000 360.0000

The eigenvalues of A can be computed using function roots

eigenvals = roots(chpol)

eigenvals =
15.0000
4.8990
-4.8990

This method, however, is not recommended for numerical computing the eigenvalues of a matrix.
There are several reasons for which this approach is not used in numerical linear algebra. An
interested reader is referred to Tutorial 4.

The Caley-Hamilton Theorem states that each matrix satisfies its characteristic equation, i.e.,
chpol(A) = 0, where the last zero stands for the matrix of zeros of the appropriate dimension. We
use function lincomb to verify this result

Q = lincomb(num2cell(chpol), {A^3, A^2, A, eye(size(A))})

Q =
1.0e-012 *
-0.5684 -0.5542 -0.4832
-0.5258 -0.6253 -0.4547
-0.5116 -0.4547 -0.6821
26

     

List of applications of methods of linear algebra is long and impressive. Areas that relay heavily
on the methods of linear algebra include the data fitting, mathematical statistics, linear
programming, computer graphics, cryptography, and economics, to mention the most important
ones. Applications discussed in this section include the data fitting, coding messages, and
computer graphics.

 
In many problems that arise in science and engineering one wants to fit a discrete set of points in
the plane by a smooth curve or function. A typical choice of a smoothing function is a polynomial
of a certain degree. If the smoothing criterion requires minimization of the 2-norm, then one has
to solve the least-squares approximation problem. Function fit takes three arguments, the degree
of the approximating polynomial, and two vectors holding the x- and the y- coordinates of points
to be approximated. On the output, the coefficients of the least-squares polynomials are returned.
Also, its graph and the plot of the data points are generated.

function c = fit(n, t, y)

% The least-squares approximating polynomial of degree n (n>=0).


% Coordinates of points to be fitted are stored in the column vectors
% t and y. Coefficients of the approximating polynomial are stored in
% the vector c. Graphs of the data points and the least-squares
% approximating polynomial are also generated.

if ( n >= length(t))
error('Degree is too big')
end
v = fliplr(vander(t));
v = v(:,1:(n+1));
c = v\y;
c = fliplr(c');
x = linspace(min(t),max(t));
w = polyval(c, x);
plot(t,y,'ro',x,w);
title(sprintf('The least-squares polynomial of degree n = %2.0f',n))
legend('data points','fitting polynomial')

To demonstrate functionality of this code we generate first a set of points in the plane. Our goal is
to fit ten evenly spaced points with the y-ordinates being the values of the function y = sin(2t) at
these points

t = linspace(0, pi/2, 10); t = t';

y = sin(2*t);

We will fit the data by a polynomial of degree at most three

c = fit(3, t, y)

c =
-0.0000 -1.6156 2.5377 -0.0234
27

Fitting polynomial of degree at most 3


1.2
data points
fitting polynomial
1

0.8

0.6

0.4

0.2

-0.2
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6


 
Some elementary tools of linear algebra can be used to code and decode messages. A typical
message can be represented as a string. The following 'coded message' is an example of the
string in MATLAB. Strings in turn can be converted to a sequence of positive integers using
MATLAB's function double. To code a transformed message multiplication by a nonsingular
matrix is used. Process of decoding messages can be viewed as the inverse process to the one
described earlier. This time multiplication by the inverse of the coding matrix is applied and next
MATLAB's function char is applied to the resulting sequence to recover the original message.
Functions code and decode implement these steps.

function B = code(s, A)

% String s is coded using a nonsingular matrix A.


% A coded message is stored in the vector B.

p = length(s);
[n,n] = size(A);
b = double(s);
r = rem(p,n);
if r ~= 0
b = [b zeros(1,n-r)]';
end
b = reshape(b,n,length(b)/n);
B = A*b;
B = B(:)';
28

function s = dcode(B, A)

% Coded message, stored in the vector B, is


% decoded with the aid of the nonsingular matrix A
% and is stored in the string s.

[n,n]= size(A);
p = length(B);
B = reshape(B,n,p/n);
d = A\B;
s = char(d(:)');

A message to be coded is

s = 'Linear algebra is fun';

As a coding matrix we use the Pascal matrix

A = pascal(4);

This codes the message s

B = code(s,A)

B =
Columns 1 through 6
392 1020 2061 3616 340
809
Columns 7 through 12
1601 2813 410 1009 2003
3490
Columns 13 through 18
348 824 1647 2922 366
953
Columns 19 through 24
1993 3603 110 110 110
110

To decode this message we have to work with the same coding matrix A

dcode(B,A)

ans =
Linear algebra is fun


  
Linear algebra provides many tools that are of interest for computer programmers especially for
those who deal with the computer graphics. Once the graphical object is created one has to
transform it to another object. Certain plane and/or space transformations are linear. Therefore
they can be realized as the matrix-vector multiplication. For instance, the reflections, translations,
29

rotations all belong to this class of transformations. A computer code provided below deals with
the plane rotations in the counterclockwise direction. Function rot2d takes a planar object
represented by two vectors x and y and returns its image. The angle of rotation is supplied in the
degree measure.

function [xt, yt] = rot2d(t, x, y)

% Rotation of a two-dimensional object that is represented by two


% vectors x and y. The angle of rotation t is in the degree measure.
% Transformed vectors x and y are saved in xt and yt, respectively.

t1 = t*pi/180;
r = [cos(t1) -sin(t1);sin(t1) cos(t1)];
x = [x x(1)];
y = [y y(1)];
hold on
grid on
axis equal
fill(x, y,'b')
z = r*[x;y];
xt = z(1,:);
yt = z(2,:);
fill(xt, yt,'r');
title(sprintf('Plane rotation through the angle of %3.2f degrees',t))
hold off

Vectors x and y

x = [1 2 3 2]; y = [3 1 2 4];

are the vertices of the parallelogram. We will test function rot2d on these vectors using as the
angle of rotation t = 75.

[xt, yt] = rot2d(75, x, y)

xt =
-2.6390 -0.4483 -1.1554 -3.3461 -2.6390
yt =
1.7424 2.1907 3.4154 2.9671 1.7424
30

Plane rotation through the angle of 75.00 degrees


5

4.5

3.5

2.5

1.5

0.5

0
-3 -2 -1 0 1 2 3

The right object is the original parallelogram while the left one is its image.
31

, 

[1] B.D. Hahn, Essential MATLAB for Scientists and Engineers, John Wiley & Sons, New
York, NY, 1997.

[2] D.R. Hill and D.E. Zitarelli, Linear Algebra Labs with MATLAB, Second edition, Prentice
Hall, Upper Saddle River, NJ, 1996.

[3] B. Kolman, Introductory Linear Algebra with Applications, Sixth edition, Prentice Hall,
Upper Saddle River, NJ, 1997.

[4] R.E. Larson and B.H. Edwards, Elementary Linear Algebra, Third edition, D.C. Heath and
Company, Lexington, MA, 1996.

[5] S.J. Leon, Linear Algebra with Applications, Fifth edition, Prentice Hall, Upper Saddle
River, NJ, 1998.

[6] G. Strang, Linear Algebra and Its Applications, Second edition, Academic Press, Orlando,
FL, 1980.
32

.

In Problems 1 – 12 you cannot use loops for and/or while.


Problems 40 - 42 involve symbolic computations. In order to do these problems you have to use
the Symbolic Math Toolbox.

1. Create a ten-dimensional row vector whose all components are equal 2. You cannot enter
number 2 more than once.

2. Given a row vector a = [1 2 3 4 5]. Create a column vector b that has the same components as
the vector a but they must bestored in the reversed order.

3. MATLAB built-in function sort(a) sorts components of the vector a in the ascending order.
Use function sort to sort components of the vector a in the descending order.

4. To find the largest (smallest) entry of a vector you can use function max (min). Suppose that
these functions are not available. How would you calculate

(a) the largest entry of a vector ?


(b) the smallest entry of a vector?

5. Suppose that one wants to create a vector a of ones and zeros whose length is equal to 2n
( n = 1, 2, … ). For instance, when n = 3, then a = [1 0 1 0 1 0]. Given value of n create a
vector a with the desired property.

6. Let a be a vector of integers.

(a) Create a vector b whose all components are the even entries of the vector a.
(b) Repeat part (a) where now b consists of all odd entries of the vector a.

Hint: Function logical is often used to logical tests. Another useful function you may
consider to use is rem(x, y) - the remainder after division of x by y.

7. Given two nonempty row vectors a and b and two vectors ind1and ind2 with length(a) =
length(ind1) and length(b) = length(ind2). Components of ind1 and ind2 are positive
integers. Create a vector c whose components are those of vectors a and b. Their indices are
determined by vectors ind1 and ind2, respectively.

8. Using function rand, generate a vector of random integers that are uniformly distributed in
the interval (2, 10). In order to insure that the resulting vector is not empty begin with a
vector that has a sufficient number of components.
Hint: Function fix might be helpful. Type help fix in the Command Window to learn more
about this function.

9. Let A be a square matrix. Create a matrix B whose entries are the same as those of A except
the entries along the main diagonal. The main diagonal of the matrix B should consist entierly
of ones.
33

10. Let A be a square matrix. Create a tridiagonal matrix T whose subdiagonal, main diagonal,
and the superdiagonal are taken from the matrix A.
Hint: You may wish to use MATLAB functions triu and tril. These functions take a second
optional argument. To learn more about these functions use MATLAB's help.

11. In this exercise you are to test a square matrix A for symmetry. Write MATLAB function
s = issymm(A) that takes a matrix A and returns a number s. If A is symmetric, then s = 1,
otherwise s = 0.

12. Let A be an m-by-n and let B be an n-by-p matrices. Computing the product C = AB
requires mnp multiplications. If either A or B has a special structure, then the number of
multiplications can be reduced drastically. Let A be a full matrix of dimension m-by-n and let
B be an upper triangular matrix of dimension n-by-n whose all nonzero entries are equal to
one. The product AB can be calculated without using a single multiplicationa. Write an
algorithm for computing the matrix product C = A*B that does not require multiplications.
Test your code with the following matrices A = pascal(3) and B = triu(ones(3)).

13. Given square invertible matrices A and B and the column vector b. Assume that the matrices
A and B and the vector b have the same number of rows. Suppose that one wants to solve a
linear system of equations ABx = b. Without computing the matrix-matrix product A*B, find
a solution x to to this system using the backslash operator \.

14. Find all solutions to the linear system Ax = b, where the matrix A consists of rows one
through three of the 5-by-5 magic square

A = magic(5);

A = A(1:3,: )

A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22

and b = ones(3; 1).

15. Determine whether or not the system of linear equations Ax = b, where

A = ones(3, 2); b = [1; 2; 3];

possesses an exact solution x.

16. The purpose of this exercise is to demonstrate that for some matrices the computed solution
to Ax = b can be poor. Define

A = hilb(50); b = rand(50,1);

Find the 2-norm of the residual r = A*x – b. How would you explain a fact that the computed
norm is essentially bigger than zero?
34

17. In this exercise you are to compare computational complexity of two methods for finding a
solution to the linear system Ax = b where A is a square matrix. First method utilizes the
backslash operator \ while the second method requires a use of the function rref. Use
MATLAB function flops to compare both methods for various linear systems of your choice.
Which of these methods require, in general, a smaller number of flops?

18. Repeat an experiment described in Problem 17 using as a measure of efficiency a time needed
to compute the solution vector. MATLAB has a pair of functions tic and toc that can be used
in this experiment. This illustrates use of the above mentioned functions
tic; x = A\b; toc. Using linear systems of your choice compare both methods for speed.
Which method is a faster one? Experiment with linear systems having at least ten equations.

19. Let A be a real matrix. Use MATLAB function rref to extract all

(a) columns of A that are linearly independent


(b) rows of A that are linearly independent

20. In this exercise you are to use MATLAB function rref to compute the rank of the following
matrices:

(a) A = magic(3)
(b) A = magic(4)
(c) A = magic(5)
(d) A = magic(6)

Based on the results of your computations what hypotheses would you formulate about
the rank(magic(n)), when n is odd, when n is even?

21. Use MATLAB to demonstrate that det(A + B)  det(A) + det(B) for matrices of your choice.

22. Let A = hilb(5). Hilbert matrix is often used to test computer algorithms for reliability. In this
exercise you will use MATLAB function num2str that converts numbers to strings, to see
that contrary to the well-known theorem of Linear Algebra the computed determinant
det(A*A') is not necessarily the same as det(A)*det(A'). You can notice a difference in
computed quantities by executing the following commands: num2str(det(A*A'), 16) and
num2str(det(A)*det(A'), 16).

23. The inverse matrix of a symmetric nonsingular matrix is a symmetric matrix. Check this
property using function inv and a symmetric nonsingular matrix of your choice.

24. The following matrix

A = ones(5) + eye(5)

A =
2 1 1 1 1
1 2 1 1 1
1 1 2 1 1
1 1 1 2 1
1 1 1 1 2
35

is a special case of the Pei matrix. Normalize columns of the matrix A so that all columns of
the resulting matrix, say B, have the Euclidean norm (2-norm) equal to one.

25. Find the angles between consecutive columns of the matrix B of Problem 24.

26. Find the cross product vector cp that is perpendicular to columns one through four of the Pei
matrix of Problem 24.

27. Let L be a linear transformation from 5 to 5 that is represented by the Pei matrix of
Problem 24. Use MATLAB to determine the range and the kernel of this transformation.

28. Let n denote a space of algebraic polynomials of degree at most n. Transformation L


from n to 3 is defined as follows

1 

 p( t )dt 
0 
L( p ) =  p ( 0 ) 
 
 0 
 
 

(a) Show that L is a linear transformation.


(b) Find a matrix that represents transformation L with respect to the ordered basis
{tn, tn –1, … 1}.
(c) Use MATLAB to compute bases of the range and the kernel of L. Perform your
experiment for the following values of n = 2, 3, 4.

29. Transformation L from n to n –1 is defined as follows L(p) = p'(t). Symbol n, is


introduced in Problem 28. Answer questions (a) through (c) of Problem 28 for the
transformation L of this problem.

30. Given vectors a = [1; 2; 3] and b = [-3; 0; 2]. Determine whether or not vector c = [4; 1;1] is
in the span of vectors a and b.

31. Determine whether or not the Toeplitz matrix

A = toeplitz( [1 0 1 1 1] )

A =
1 0 1 1 1
0 1 0 1 1
1 0 1 0 1
1 1 0 1 0
1 1 1 0 1

is in the span of matrices B = ones(5) and C = magic(5).


36

32. Write MATLAB function linind(varargin) that takes an arbitrary number of vectors
(matrices) of the same dimension and determines whether or not the inputted vectors
(matrices) are linearly independent. You may wish to reuse some lines of code that are
contained in the function span presented in Section 3.9 of this tutorial.

33. Use function linind of Problem 32 to show that the columns of the matrix A of Problem
31 are linearly independent.

34. Let [a]A = ones(5,1) be the coordinate vector with respect to the basis A – columns of the
matrix A of Problem 31. Find the coordinate vector [a]P , where P is the basis of the vector
space spanned by the columns of the matrix pascal(5).

35. Let A be a real symmetric matrix. Use the well-known fact from linear algebra to determine
the interval containing all the eigenvalues of A. Write MATLAB function
[a, b] = interval(A) that takes a symmetric matrix A and returns the endpoints a and b of the
interval that contains all the eigenvalues of A.

36. Without solving the matrix eigenvalue problem find the sum and the product of all
eigenvalues of the following matrices:

(a) P = pascal(30)
(b) M= magic(40)
(c) H = hilb(50)
(d) H = hadamard(64)

37. Find a matrix B that is similar to A = magic(3).

38. In this exercise you are to compute a power of the diagonalizable matrix A. Let
A = pascal(5). Use the eigenvalue decomposition of A to calculate the ninth
power of A. You cannot apply the power operator ^ to the matrix A.

39. Let A be a square matrix. A matrix B is said to be the square root of A if B^2 = A.
In MATLAB the square root of a matrix can be found using the power operator ^. In this
exercise you are to use the eigenvalue-eigenvector decomposition of a matrix find the square
root of A = [3 3;-2 -2].

40. Declare a variable k to be a symbolic variable typing syms k in the Command Window.
Find a value of k for which the following symbolic matrix
A = sym( [1 k^2 2; 1 k -1; 2 –1 0] ) is not invertible.

41. Let the matrix A be the same as in Problem 40.

(a) Without solving the matrix eigenvalue problem, determine a value of k for which all the
eigenvalues of A are real.
(b) Let v be a number you found in part (a). Convert the symbolic matrix A to a numeric
matrix B using the substitution command subs, i.e., B = subs(A, k, v).
(c) Determine whether or not the matrix B is diagonalizable. If so, find a diagonal matrix D
that is similar to B.
37

(d) If matrix B is diagonalizable use the results of part (c) to compute all the eigenvectors of
the matrix B. Do not use MATLAB's function eig.

42. Given a symbolic matrix A = sym( [1 0 k; 2 2 0; 3 3 3]).

(a) Find a nonzero value of k for which all the eigenvalues of A are real.
(b) For what value of k two eigenvalues of A are complex and the remaining one is real?


    

 
 

MATLAB has many tools that make this package well suited for numerical computations. This
tutorial deals with the rootfinding, interpolation, numerical differentiation and integration and
numerical solutions of the ordinary differential equations. Numerical methods of linear algebra
are discussed in Tutorial 4.

     

Function Description
abs Absolute value
dblquad Numerically evaluate double integral
erf Error function
feval Execute function specified by string
fzero Scalar nonlinear zero finding
gamma Gamma function
inline Construct INLINE object
interp1 One-dimensional interpolation
interp2 Two-dimensional interpolation
linspace Evenly spaced vector
meshgrid X and Y arrays for 3-D plots
norm Matrix or vector norm
ode23 Solve non-stiff differential equations
ode45 Solve non-stiff differential equations
ode113 Solve non-stiff differential equations
ode15s Solve stiff differential equations
ode23s Solve stiff differential equations
poly Convert roots to polynomial
polyval Evaluate polynomial
ppval Evaluate piecewise polynomial
quad Numerically evaluate integral, low order method
quad8 Numerically evaluate integral, higher order method
rcond Reciprocal condition estimator
roots Find polynomial roots
spline Cubic spline data interpolation
surf 3-D colored surface
unmkpp Supply details about piecewise polynomial
2

! " #

A central problem discussed in this section is formulated as follows. Given a real-valued function
f: n  n, n  1, find a vector r so that f(r) = 0. Vector r is called the root or zero of f.

5.2.1 Computing roots of the univariate polynomials

Polynomials are represented in MATLAB by their coefficients in the descending order of powers.
For instance, the cubic polynomial p(x) = 3x3 + 2x2 - 1 is represented as

p = [3 2 0 1];

Its roots can be found using function roots

format long

r = roots(p)

r =
-1.00000000000000
0.16666666666667 + 0.55277079839257i
0.16666666666667 - 0.55277079839257i

To check correctness of this result we evaluate p(x) at r using function polyval

err = polyval(p, r)

err =
1.0e-014 *
0.22204460492503
0 + 0.01110223024625i
0 - 0.01110223024625i

To reconstruct a polynomial from its roots one can use function poly. Using the roots r computed
earlier we obtain

poly(r)

ans =
1.00000000000000 0.66666666666667 0.00000000000000
0.33333333333333

Let us note that these are the coefficients of p(x) all divided by 3. The coefficients of p(x) can be
recovered easily

3*ans

ans =
3.00000000000000 2.00000000000000 0.00000000000000
1.00000000000000
3

Numerical computation of roots of a polynomial is the ill-conditioned problem. Consider the fifth
degree polynomial p(x) = x5 – 10x4 + 40x3 – 80x2 + 80x – 32. Let us note that p(x) = (x –2)5.
Using function roots we find

format short

p = [1 –10 40 –80 80 –32];

x = roots(p)

x =
2.0017
2.0005 + 0.0016i
2.0005 - 0.0016i
1.9987 + 0.0010i
1.9987 - 0.0010i

These results are not satisfactory. We will return to the problem of finding the roots of p(x) in the
next section.

5.2.2 Finding zeros of the univariate functions using MATLAB function fzero

Let now f be a transcendental function from  to . MATLAB function fzero computes a zero of
the function f using user supplied initial guess of a zero sought.

In the following example let f(x) = cos(x) – x. First we define a function y = f1(x)

function y = f1(x)

% A univariate function with a simple zero.

y = cos(x) - x;

To compute its zero we use MATLAB function fzero

r = fzero('f1', 0.5)

r =
0.73908513321516

Name of the function whose zero is computed is entered as a string. Second argument of function
fzero is the initial approximation of r. One can check last result using function feval

err = feval('f1', r)

err =
0

In the case when a zero of function is bracketed a user can enter a two-element vector that
designates a starting interval. In our example we choose [ 0 1] as a starting interval to obtain
4

r = fzero('f1', [0 1])

r =
0.73908513321516

However, this choice of the designated interval

fzero('f1', [1 2])

¨??? Error using ==> fzero


The function values at the interval endpoints must differ in sign.

generates the error message.

By adding the third input parameter tol you can force MATLAB to compute the zero of a
function with the relative error tolerance tol. In our example we let tol = 10-3 to obtain

rt = fzero('f1', .5, 1e-3)

rt =
0.73886572291538

A relative error in the computed zero rt is

rel_err = abs(rt-r)/r

rel_err =
2.969162630892787e-004

Function fzero takes fourth optional parameter. If it is set up to 1, then the iteration information is
displayed. Using function f1, with x0 = 0.5, we obtain

format short

rt = fzero('f1', .5, eps, 1)

Func evals x f(x) Procedure


1 0.5 0.377583 initial
2 0.485858 0.398417 search
3 0.514142 0.356573 search
4 0.48 0.406995 search
5 0.52 0.347819 search
6 0.471716 0.419074 search
7 0.528284 0.335389 search
8 0.46 0.436052 search
9 0.54 0.317709 search
10 0.443431 0.459853 search
11 0.556569 0.292504 search
12 0.42 0.493089 search
13 0.58 0.256463 search
14 0.386863 0.539234 search
15 0.613137 0.20471 search
16 0.34 0.602755 search
17 0.66 0.129992 search
18 0.273726 0.689045 search
5

19 0.726274 0.0213797 search


20 0.18 0.803844 search
21 0.82 -0.137779 search

Looking for a zero in the interval [0.18, 0.82]

22 0.726355 0.0212455 interpolation


23 0.738866 0.00036719 interpolation
24 0.739085 -6.04288e-008 interpolation
25 0.739085 2.92788e-012 interpolation
26 0.739085 0 interpolation
rt =
0.7391

We have already seen that MATLAB function roots had faild to produce satisfactory results
when computing roots of the polynomial p(x) = (x – 2)5. This time we will use function fzero to
find a multiple root of p(x). We define a new function named f2

function y = f2(x)

y = (x - 2)^5;

and next change format to

format long

Running function fzero we obtain

rt = fzero('f2', 1.5)

rt =
2.00000000000000

This time the result is as expected.

Finally, we will apply function fzero to compute the multiple root of p(x) using an expanded
form of the polynomial p(x)

function y = f3(x)

y = x^5 - 10*x^4 + 40*x^3 -80*x^2 + 80*x - 32;

rt = fzero('f3', 1.5)

rt =
1.99845515925755

Again, the computed approximation of the root of p(x) has a few correct digits only.
6

5.2.3 The Newton-Raphson method for systems of nonlinear equations

This section deals with the problem of computing zeros of the vector-valued function
f : n  n, n  1. Assume that the first order partial derivatives of f are continuous on an open
domain holding all zeros of f. A method discussed below is called the Newton-Raphson method.
To present details of this method let us introduce more notation. Using MATLAB's convention
for representing vectors we write f as a column vector f = [f1; …;fn], where each fk is a function
from n to . Given an initial approximation x(0)  n of r this method generates a sequence of
vectors {x(k)} using the iteration

x(k+1) = x(k) – Jf (x(k))-1 f(x(k)), k = 0, 1, … .

Here Jf stands for the Jacobian matrix of f, i.e., Jf (x) = [fi(x)/xj], 1  i, j  n. For more details
the reader is referred to [6] and [9].

Function NR computes a zero of the system of nonlinear equations.

function [r, niter] = NR(f, J, x0, tol, rerror, maxiter)

% Zero r of the nonlinear system of equations f(x) = 0.


% Here J is the Jacobian matrix of f and x0 is the initial
% approximation of the zero r.
% Computations are interrupted either if the norm of
% f at current approximation is less (in magnitude)
% than the number tol,or if the relative error of two
% consecutive approximations is smaller than the prescribed
% accuracy rerror, or if the number of allowed iterations
% maxiter is attained.
% The second output parameter niter stands for the number
% of performed iterations.

Jc = rcond(feval(J,x0));
if Jc < 1e-10
error('Try a new initial approximation x0')
end
xold = x0(:);
xnew = xold - feval(J,xold)\feval(f,xold);
for k=1:maxiter
xold = xnew;
niter = k;
xnew = xold - feval(J,xold)\feval(f,xold);
if (norm(feval(f,xnew)) < tol) |...
norm(xold-xnew,'inf')/norm(xnew,'inf') < tol|...
(niter == maxiter)
break
end
end
r = xnew;

The following nonlinear system

f1(x) = x1 + 2x2 – 2,
f2(x) = x12 + 4x22 – 4
7

has the exact zeros r = [0 1]T and r = [2 0]T (see [6], p. 166). Functions fun1 and J1 define the
system of equations and its Jacobian, respectively

function z = fun1(x)

z = zeros(2,1);
z(1) = x(1) + 2*x(2) - 2;
z(2) = x(1)^2 + 4*x(2)^2 - 4;

function s = J1(x)

s = [1 2;2*x(1) 8*x(2)];

Let

x0 = [0 0];

Then

[r, iter] = NR('fun1', 'J1', x0, eps, eps, 10)

¨??? Error using ==> nr


Try a new initial approximation x0

For x0 as chosen above the associated Jacobian is singular. Let's try another initial guess for r

x0 = [1 0];

[r, niter] = NR('fun1', 'J1', x0, eps, eps, 10)

r =
2.00000000000000
-0.00000000000000
niter =
5

Consider another nonlinear system

f1(x) = x1 + x2 –1
f2(x) = sin(x12 + x22) – x1.

The m-files needed for computing its zeros are named fun2 and J2

function w = fun2(x);

w(1) = x(1) + x(2) - 1;


w(2) = sin(x(1)^2 + x(2)^2) - x(1);
w = w(:);
8

function s = J2(x)

s = [1 1;
2*x(1)*cos(x(1)^2 + x(2)^2)-1 2*x(2)*cos(x(1)^2 + x(2)^2)];

With the initial guess

x0 = [0 1];

the zero r is found to be

[r, niter] = NR('fun2', 'J2', x0, eps, eps, 10)

r =
0.48011911689839
0.51988088310161
niter =
5

while the initial guess

x0 = [1 1];

[r, iter] = NR('fun2', 'J2', x0, eps, eps, 10)

r =
-0.85359545600207
1.85359545600207
iter =
10

gives another solution. The value of function fun2 at the computed zero r is

feval('fun2', r)

ans =
1.0e-015 *
0
-0.11102230246252

Implementation of other classical methods for computing the zeros of scalar equations, including
the fixed-point iteration, the secant method and the Schroder method are left to the reader (see
Problems 3, 6, and 12 at the end of this tutorial).

$ % &  ' (

Interpolation of functions is one of the classical problems in numerical analysis. A one


dimensional interpolation problem is formulated as follows.

Given set of n+1 points xk , yk, 0  k  n, with x0 < x1 < … < xn, find a function f(x) whose
graph interpolates the data points, i.e., f(xk) = yk, for k = 0, 1, …, n.
9

In this section we will use as the interpolating functions algebraic polynomials and spline
functions.

5.3.1 MATLAB function interp1

The general form of the function interp1 is yi = interp1(x, y, xi, method), where the vectors x
and y are the vectors holding the x- and the y- coordinates of points to be interpolated,
respectively, xi is a vector holding points of evaluation, i.e., yi = f(xi) and method is an optional
string specifying an interpolation method. The following methods work with the function interp1

• Nearest neighbor interpolation, method = 'nearest'. Produces a locally piecewise constant


interpolant.
• Linear interpolation method = 'linear'. Produces a piecewise linear interpolant.
• Cubic spline interpolation, method = 'spline'. Produces a cubic spline interpolant.
• Cubic interpolation, method = 'cubic'. Produces a piecewise cubic polynomial.

In this example, the following points (xk, yk) = (k/5, sin(2xk)), k = 0, 1, … , 5,

x = 0:pi/5:pi;

y = sin(2.*x);

are interpolated using two methods of interpolation 'nearest' and 'cubic' . The interpolant is
evaluated at the following points

xi = 0:pi/100:pi;

yi = interp1(x, y, xi, 'nearest');

Points of interpolation together with the resulting interpolant are displayed below

plot(x, y, 'o', xi, yi), title('Piecewise constant interpolant of y =


sin(2x)')
10

Piecewise constant interpolant of y = sin(2x)


1

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 0.5 1 1.5 2 2.5 3 3.5

yi = interp1(x, y, xi, 'cubic');

plot(x, y, 'o', xi, yi), title('Cubic interpolant of y = sin(2x)')

Cubic interpolant of y = sin(2x)


1

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 0.5 1 1.5 2 2.5 3 3.5
11

5.3.2 Interpolation by algebraic polynomials

Assume now that the interpolating function is an algebraic polynomial pn(x) of degree at most n,
where n = number of points of interpolation – 1. It is well known that the interpolating
polynomial pn always exists and is unique (see e.g., [6], [9]). To determine the polynomial
interpolant one can use either the Vandermonde's method or Lagrange form or Newton's form or
Aitken's method. We shall describe briefly the Newton's method.

We begin writing p(x) as

(5.3.1) pn(x) = a0 + a1(x – x0) + a2(x – x0)(x – x1) + … + an(x – x0)(x – x1) … (x – xn-1)

Coefficients a0, a1, … , an are called the divided differences and they can be computed
recursively. Representation (5.3.1) of pn(x) is called the Newton's form of the interpolating
polynomial. The k-th order divided difference based on points x0, … xk, denoted by [x0, … , xk],
is defined recursively as

[xm] = ym if k = 0

[x0, … , xk] = ([x1, … , xk] – [x0, … , xk-1])/(xk – x0) if k > 0.

Coefficients {ak} in representation (5.3.1) and the divided differences are related in the following
way
ak = [x0, … , xk].

Function Newtonpol evaluates an interpolating polynomial at the user supplied points.

function [yi, a] = Newtonpol(x, y, xi)

% Values yi of the interpolating polynomial at the points xi.


% Coordinates of the points of interpolation are stored in
% vectors x and y. Horner's method is used to evaluate
% a polynomial. Second output parameter a holds coeeficients
% of the interpolating polynomial in Newton's form.

a = divdiff(x, y);
n = length(a);
val = a(n);
for m = n-1:-1:1
val = (xi - x(m)).*val + a(m);
end
yi = val(:);

function a = divdiff(x, y)

% Divided differences based on points stored in arrays x and y.

n = length(x);
for k=1:n-1
12

y(k+1:n) = (y(k+1:n) - y(k))./(x(k+1:n) - x(k));


end
a = y(:);

For the data of the last example, we will evaluate Newton's interpolating polynomial of degree at
most five, using function Newtonpol. Also its graph together with the points of interpolation will
be plotted.

[yi, a] = Newtonpol(x, y, xi);

plot(x, y, 'o', xi, yi), title('Quintic interpolant of y = sin(2x)')

Quintic interpolant of y = sin(2x)


1.5

0.5

-0.5

-1

-1.5
0 0.5 1 1.5 2 2.5 3 3.5

Interpolation process not always produces a sequence of polynomials that converge uniformly to
the interpolated function as degree of the interpolating polynomial tends to infinity. A famous
example of divergence, due to Runge, illustrates this phenomenon. Let g(x) = 1/(1 + x2),
-5  x  5, be the function that is interpolated at n + 1 evenly spaced points xk = -5 + 10k/n,
k = 0, 1, … , n.

Script file showint creates graphs of both, the function g(x) ant its interpolating polynomial pn(x).

% Script showint.m
% Plot of the function 1/(1 + x^2) and its
% interpolating polynomial of degree n.

m = input('Enter number of interpolating polynomials ');


13

for k=1:m
n = input('Enter degree of the interpolating polynomial ');
hold on
x = linspace(-5,5,n+1);
y = 1./(1 + x.*x);
z = linspace(-5.5,5.5);
t = 1./(1 + z.^2);
h1_line = plot(z,t,'-.');
set(h1_line, 'LineWidth',1.25)
t = Newtonpol(x,y,z);
h2_line = plot(z,t,'r');
set(h2_line,'LineWidth',1.3,'Color',[0 0 0])
axis([-5.5 5.5 -.5 1])
title(sprintf('Example of divergence (n = %2.0f)',n))
xlabel('x')
ylabel('y')
legend('y = 1/(1+x^2)','interpolant')
hold off
end

Typing showint in the Command Window you will be prompted to enter value for the parameter
m = number of interpolating polynomials you wish to generate and also you have to enter
value(s) of the degree of the interpolating polynomial(s). In the following example m = 1 and
n=9

Divergence occurs at points that are close enough to the endpoints of the interval of interpolation
[-5, 5].

We close this section with the two-point Hermite interpolaion problem by cubic polynomials.
Assume that a function y= g(x) is differentiable on the interval [ a, b]. We seek a cubic
polynomial p3(x) that satisfies the following interpolatory conditions
14

(5.3.2) p3(a) = g(a), p3(b) = g(b), p3'(a) = g'(a), p3' (b) = g'(b)

Interpolating polynomial p3(x) always exists and is represented as follows

(5.3.3) p3(x) = (1 + 2t)(1 - t)2g(a) + (3 - 2t)t2g(b) + h[t(1 - t)2g'(a) + t2(t - 1)g'(b)] ,

where t = (x - a)/(b - a) and h = b – a.

Function Hermpol evaluates the Hermite interpolant at the points stored in the vector xi.

function yi = Hermpol(ga, gb, dga, dgb, a, b, xi)

% Two-point cubic Hermite interpolant. Points of interpolation


% are a and b. Values of the interpolant and its first order
% derivatives at a and b are equal to ga, gb, dga and dgb,
% respectively.
% Vector yi holds values of the interpolant at the points xi.

h = b – a;
t = (xi - a)./h;
t1 = 1 - t;
t2 = t1.*t1;
yi = (1 + 2*t).*t2*ga + (3 - 2*t).*(t.*t)*gb +…
h.*(t.*t2*dga + t.^2.**(t - 1)*dgb);

In this example we will interpolate function g(x) = sin(x) using a two-point cubic Hermite
interpolant with a = 0 and b = /2

xi = linspace(0, pi/2);

yi = Hermpol(0, 1, 1, 0, 0, pi/2, xi);

zi = yi – sin(xi);

plot(xi, zi), title('Error in interpolation of sin(x) by a two-point


cubic Hermite polynomial')
15

Error in interpolation of sin(x) by a two-point cubic Hermite polynomial


0

-0.002

-0.004

-0.006

-0.008

-0.01

-0.012
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6

5.3.3 Interpolation by splines

In this section we will deal with interpolation by polynomial splines. In recent decades splines
have attracted attention of both researchers and users who need a versatile approximation tools.
We begin with the definition of the polynomial spline functions and the spline space.

Given an interval [a, b]. A partition  of the interval [a, b] with the breakpoints {xl}1m is defined
as  = {a = x1 < x2 < … < xm = b}, where m > 1. Further, let k and n, k < n, be nonnegative
integers. Function s(x) is said to be a spline function of degree n with smoothness k if the
following conditions are satisfied:

(i) On each subinterval [xl, xl+1] s(x) coincides with an algebraic polynomial of degree at
most n.
(ii) s(x) and its derivatives up to order k are all continuous on the interval [a, b]

Throughout the sequel the symbol Sp(n, k, ) will stand for the space of the polynomial splines
of degree n with smoothness k , and the breakpoints . It is well known that Sp(n, k, ) is a
linear subspace of dimension (n + 1)(m – 1) – (k + 1)(m – 2). In the case when k = n – 1, we will
write Sp(n, ) instead of Sp(n, n – 1, ).

MATLAB function spline is designed for computations with the cubic splines (n = 3) that are
twice continuously differentiable (k = 2) on the interval [x1, xm]. Clearly
dim Sp(3, ) = m + 2. The spline interpolant s(x) is determined uniquely by the interpolatory
conditions s(xl) = yl, l = 1, 2, … , m and two additional boundary conditions, namely that s'''(x)
is continuous at x = x2 and x = xm-1. These conditions are commonly referred to as the not-a-knot
end conditions.
16

MATLAB's command yi = spline(x, y, xi) evaluates cubic spline s(x) at points stored in the array
xi. Vectors x and y hold coordinates of the points to be interpolated. To obtain the piecewise
polynomial representation of the spline interpolant one can execute the command
pp = spline(x, y). Command zi = ppval(pp, xi) evaluates the piecewise polynomial form of the
spline interpolant. Points of evaluation are stored in the array xi. If a spline interpolant has to be
evaluated for several vectors xi, then the use of function ppval is strongly recommended.

In this example we will interpolate Runge's function g(x) = 1/(1 + x2) on the interval [0, 5] using
six evenly spaced breakpoints

x = 0:5;

y = 1./(1 + x.^2);

xi = linspace(0, 5);

yi = spline(x, y, xi);

plot(x, y, 'o', xi, yi), title('Cubic spline interpolant')

Cubic spline interpolant


1

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0
0 1 2 3 4 5

The maximum error on the set xi in approximating Runge's function by the cubic spline we found
is
17

err = norm(abs(yi-1./(1+xi.^2)),'inf')

err =
0.0859

Detailed information about the piecewise polynomial representation of the spline interpolant can
be obtained running function spline with two input parameters x and y

pp = spline(x, y);

and next executing command unmkpp

[brpts, coeffs, npol, ncoeff] = unmkpp(pp)

brpts =
0 1 2 3 4 5
coeffs =
0.0074 0.0777 -0.5852 1.0000
0.0074 0.1000 -0.4074 0.5000
-0.0371 0.1223 -0.1852 0.2000
-0.0002 0.0110 -0.0519 0.1000
-0.0002 0.0104 -0.0306 0.0588
npol =
5
ncoeff =
4

The output parameters brpts, coeffs, npol, and ncoeff represent the breakpoints of the spline
interpolant, coefficients of s(x) on successive subintervals, number of polynomial pieces that
constitute spline function and number of coefficients that represent each polynomial piece,
respectively. On the subinterval [xl, xl+1] the spline interpolant is represented as

s(x) = cl1(x – xl)3 + cl2(x – xl)2 + cl3(x – xl) + cl4

where [cl1 cl2 cl3 cl4] is the lth row of the matrix coeffs. This form is called the piecewise
polynomial form (pp–form) of the spline function.

Differentiation of the spline function s(x) can be accomplished running function splder. In order
for this function to work properly another function pold (see Problem 19) must be in MATLAB's
search path.

function p = splder(k, pp, x)

% Piecewise polynomial representation of the derivative


% of order k (0 <= k <= 3) of a cubic spline function in the
% pp form with the breakpoints stored in the vector x.

m = pp(3);
lx4 = length(x) + 4;
n = pp(lx4);
c = pp(1 + lx4:length(pp))';
c = reshape(c, m, n);
b = pold(c, k);
b = b(:)';
18

p = pp(1:lx4);
p(lx4) = n - k;
p = [p b];

The third order derivative of the spline function of the last example is shown below

p = splder(3, pp, x);

yi = ppval(p, xi);

plot(xi, yi), title('Third order derivative of s(x)')

Third order derivative of s(x)


0.1

0.05

-0.05

-0.1

-0.15

-0.2

-0.25
0 1 2 3 4 5

Note that s'''(x) is continuous at the breakpoints x2 = 1 and x5 = 4. This is due to the fact that the
not-a-knot boundary conditions were imposed on the spline interpolant.

Function evalppf is the utility tool for evaluating the piecewise polynomial function s(x) at the
points stored in the vector xi. The breakpoints x = {x1 < x2 < … < xm} of s(x) and the points of
evaluation xi must be such that x1 = xi1 and xm = xip, where p is the index of the largest number in
xi. Coefficients of the polynomial pieces of s(x) are stored in rows of the matrix A in the
descending order of powers.

function [pts, yi] = evalppf(x, xi, A)

% Values yi of the piecewise polynomial function (pp-function)


% evaluated at the points xi. Vector x holds the breakpoints
% of the pp-function and matrix A holds the coefficients of the
% pp-function. They are stored in the consecutive rows in
19

% the descending order of powers.The output parameter pts holds


% the points of the union of two sets x and xi.

n = length(x);
[p, q] = size(A);
if n-1 ~= p
error('Vector t and matrix A must be "compatible"')
end
yi = [];
pts = union(x, xi);
for m=1:p
l = find(pts == x(m));
r = find(pts == x(m+1));
if m < n-1
yi = [yi polyval(A(m,:), pts(l:r-1))];
else
yi = [yi polyval(A(m,:), pts(l:r))];
end
end

In this example we will evaluate and plot the graph of the piecewise linear function s(x) that is
defined as follows

s(x) = 0, if |x|  1
s(x) = 1 + x, if -1  x  0
s(x) = 1 – x, if 0  x  1

Let

x = -2:2;

xi = linspace(-2, 2);

A = [0 0;1 1;1 –1;0 0];

[pts, yi] = evalppf(x, xi, A);

plot(pts, yi), title('Graph of s(x)'), axis([-2 2 -.25 1.25])


20

Graph of s(x)
1.2

0.8

0.6

0.4

0.2

-0.2
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2

$  &  ' (

The interpolation problem discussed in this section is formulated as follows.

Given a rectangular grid {xk, yl} and the associated set of numbers zkl, 1  k  m, 1  l  n, find
a bivariate function z = f(x, y) that interpolates the data, i.e., f(xk. yl) = zkl for all values of k and l.
The grid points must be sorted monotonically, i.e. x1 < x2 < … < xm with a similar ordering of the
y-ordinates.

MATLAB's built-in function zi = interp2(x, y, z, xi, yi, 'method') generates a bivariate


interpolant on the rectangular grids and evaluates it in the points specified in the arrays xi and yi.
Sixth input parameter 'method' is optional and specifies a method of interpolation. Available
methods are:

• 'nearest' - nearest neighbor interpolation


• 'linear' - bilinear interpolation
• 'cubic' - bicubic interpolation
• 'spline' - spline interpolation

In the following example a bivariate function z = sin(x2 + y2) is interpolated on the square
–1  x  1, -1  y  1 using the 'linear' and the 'cubic' methods.

[x, y] = meshgrid(-1:.25:1);

z = sin(x.^2 + y.^2);

[xi, yi] = meshgrid(-1:.05:1);


21

zi = interp2(x, y, z, xi, yi, 'linear');

surf(xi, yi, zi), title('Bilinear interpolant to sin(x^2 + y^2)')

The bicubic interpolant is obtained in a similar fashion

zi = interp2(x, y, z, xi, yi, 'cubic');


22

   ' #  &  

A classical problem of the numerical integration is formulated as follows.

Given a continuous function f(x), a  x  b, find the coefficients {wk} and the nodes {xk},
1  k  n, so that the quadrature formula

b n
(5.4.1) ∫ f ( x )dx ≈ ∑ w f (x
k =1
k k)
a

is exact for polynomials of a highest possible degree.

For the evenly spaced nodes {xk} the resulting family of the quadrature formulas is called the
Newton-Cotes formulas. If the coefficients {wk} are assumed to be all equal, then the quadrature
formulas are called the Chebyshev quadrature formulas. If both, the coefficients {wk} and the
nodes {xk} are determined by requiring that the formula (5.4.1) is exact for polynomials of the
highest possible degree, then the resulting formulas are called the Gauss quadrature formulas.
23

5.4.1 Numerical integration using MATLAB functions quad and quad8

Two MATLAB functions quad('f ', a, b, tol, trace, p1, p2, …) and
quad8('f ', a, b, tol, trace, p1, p2, …) are designed for numerical integration of the univariate
functions. The input parameter 'f' is a string containing the name of the function to be integrated
from a to b. The fourth input parameter tol is optional and specifies user's chosen relative error in
the computed integral. Parameter tol can hold both the relative and the absolute errors supplied by
the user. In this case a two-dimensional vector tol = [rel_tol, abs_tol] must be included.
Parameter trace is optional and traces the function evaluations with a point plot of the integrand.
To use default values for tol or trace one may pass in the empty matrix [ ]. Parameters p1, p2, …
are also optional and they are supplied only if the integrand depends on p1, p2, … .

In this example a simple rational function


a + bx
f(x) =
1 + cx 2

function y = rfun(x, a, b, c)

% A simple rational function that depends on three


% parameters a, b and c.

y = (a + b.*x)./(1 + c.*x.^2);
y = y';

is integrated numerically from 0 to 1 using both functions quad and quad8. The assumed relative
and absolute errors are stored in the vector tol

tol = [1e-5 1e-3];

format long

[q, nfev] = quad('rfun', 0, 1, tol, [], 1, 2, 1)

q =
1.47856630183943
nfev =
9

Using function quad8 we obtain

[q8,nfev] = quad8('rfun', 0, 1, tol, [], 1, 2, 1)

q8 =
1.47854534395683
nfev =
33

Second output parameter nfev gives an information about the number of function evaluations
needed in the course of computation of the integral.
24

The exact value of the integral in question is

exact = log(2) + pi/4

exact =
1.47854534395739

The relative errors in the computed approximations q and q8 are

rel_errors = [abs(q – exact)/exact; abs(q8 – exact)/exact]

rel_errors =
1.0e-004 *
0.14174663036002
0.00000000380400

5.4.2 Newton – Cotes quadrature formulas

One of the oldest method for computing the approximate value of the definite integral over the
interval [a, b] was proposed by Newton and Cotes. The nodes of the Newton – Cotes formulas
are chosen to be evenly spaced in the interval of integration. There are two types of the Newton –
Cotes formulas the closed and the open formulas. In the first case the endpoints of the interval of
integration are included in the sets of nodes whereas in the open formulas they are not. The
weights {wk} are determined by requiring that the quadrature formula is exact for polynomials of
a highest possible degree.

Let us discuss briefly the Newton – Cotes formulas of the closed type. The nodes of the n – point
formula are defined as follows xk = a + (k – 1)h, k = 1, 2, … , n, where h = (b – a)/(n – 1),
n > 1. The weights of the quadrature formula are determined from the conditions that the
following equations are satisfied for the monomials f(x) = 1, x, … xn - 1

b n

∫ f ( x )dx = ∑ w f (x
k =1
k k)
a

function [s, w, x] = cNCqf(fun, a, b, n, varargin)

% Numerical approximation s of the definite integral of


% f(x). fun is a string containing the name of the integrand f(x).
% Integration is over the interval [a, b].
% Method used:
% n-point closed Newton-Cotes quadrature formula.
% The weights and the nodes of the quadrature formula
% are stored in vectors w and x, respectively.

if n < 2
error(' Number of nodes must be greater than 1')
end
x = (0:n-1)/(n-1);
25

f = 1./(1:n);
V = Vander(x);
V = rot90(V);
w = V\f';
w = (b-a)*w;
x = a + (b-a)*x;
x = x';
s = feval(fun,x,varargin{:});
s = w'*s;

In this example the error function Erf(x) , where

x
2
∫ e − t dt
2
Erf(x) =
π 0

will be approximated at x = 1 using the closed Newton – Cotes quadrature formulas wit n = 2
(Trapezoidal Rule), n = 3 (Simpson's Rule), and n = 4 (Boole's Rule). The integrand of the last
integral is evaluated using function exp2

function w = exp2(x)

% The weight function w of the Gauss-Hermite quadrarure formula.

w = exp(-x.^2);

approx_v = [];

for n =2:4
approx_v = [approx_v; (2/sqrt(pi))*cNCqf('exp2', 0, 1, n)];
end

approx_v

approx_v =
0.77174333225805
0.84310283004298
0.84289057143172

For comparison, using MATLAB's built - in function erf we obtain the following approximate
value of the error function at x = 1

exact_v = erf(1)

exact_v =
0.84270079294971
26

5.4.3 Gauss quadature formulas

This class of numerical integration formulas is constructed by requiring that the formulas are
exact for polynomials of the highest possible degree. The Gauss formulas are of the type

b n

∫ p( x )f ( x )dx ≈ ∑ w f (x
k =1
k k)
a

where p(x) denotes the weight function. Typical choices of the weight functions together with the
associated intervals of integration are listed below

Weight p(x) Interval [a, b] Quadrature name


1 [-1, 1] Gauss-Legendre
1/ 1 − x 2 [-1, 1] Gauss-Chebyshev
e−x [0, ∞ ) Gauss-Laguerre
2 ( −∞ , ∞ ) Gauss-Hermite
e−x

It is well known that the weights of the Gauss formulas are all positive and the nodes are the roots
of the class of polynomials that are orthogonal, with respect to the given weight function p(x), on
the associated interval.

Two functions included below, Gquad1 and Gquad2 are designed for numerical computation of
the definite integrals using Gauss quadrature formulas. A method used here is described in [3],
pp. 93 – 94.

function [s, w, x] = Gquad1(fun, a, b, n, type, varargin)

% Numerical integration using either the Gauss-Legendre (type = 'L')


% or the Gauss-Chebyshev (type = 'C') quadrature with n (n > 0) nodes.
% fun is a string representing the name of the function that is
% integrated from a to b. For the Gauss - Chebyshev quadrature
% it is assumed that a = -1 and b = 1.
% The output parameters s, w, and x hold the computed approximation
% of the integral, list of weights, and the list of nodes,
% respectively.

d = zeros(1,n-1);
if type == 'L'
k = 1:n-1;
d = k./(2*k - 1).*sqrt((2*k - 1)./(2*k + 1));
fc = 2;
J = diag(d,-1) + diag(d,1);
[u,v] = eig(J);
[x,j] = sort(diag(v));
w = (fc*u(1,:).^2)';
w = w(j)';
27

w = 0.5*(b - a)*w;
x = 0.5*((b - a)*x + a + b);
else
x = cos((2*(1:n) - (2*n + 1))*pi/(2*n))';
w(1:n) = pi/n;
end
f = feval(fun,x,varargin{:});
s = w*f(:);
w = w';

In this example we will approximate the error function Erf(1) using Gauss-Legendre formulas
with n = 2, 3, … , 8.

approx_v = [];

for n=2:8
approx_v = [approx_v; (2/sqrt(pi))*Gquad1('exp2', 0, 1, n, 'L')];
end

approx_v

approx_v =
0.84244189252255
0.84269001848451
0.84270117131620
0.84270078612733
0.84270079303742
0.84270079294882
0.84270079294972

Recall that using MATLAB's function erf we have already found that

exact_v = erf(1)

exact_v =
0.84270079294971

If the interval of integration is either semi-infinite or bi-infinite then one may use function
Gquad2. Details of a method used in this function are discussed in [3], pp. 93 – 94.

function [s, w, x] = Gquad2(fun, n, type, varargin)

% Numerical integration using either the Gauss-Laguerre


% (type = 'L') or the Gauss-Hermite (type = 'H') with n (n > 0) nodes.
% fun is a string containing the name of the function that is
% integrated.
% The output parameters s, w, and x hold the computed approximation
% of the integral, list of weights, and the list of nodes,
% respectively.

if type == 'L'
d = -(1:n-1);
28

f = 1:2:2*n-1;
fc = 1;
else
d = sqrt(.5*(1:n-1));
f = zeros(1,n);
fc = sqrt(pi);
end
J = diag(d,-1) + diag (f) + diag(d,1);
[u,v] = eig(J);
[x,j] = sort(diag(v));
w = (fc*u(1,:).^2)';
w = w(j);
f = feval(fun,x,varargin{:});
s = w'*f(:);

The Euler's gamma function


Γ( t ) = e − x x t −1dx
0
( t > -1)

can be approximated using function Gquad2 with type being set to 'L' (Gauss-Laguerre
quadratures). Let us recall that Γ (n) = (n - 1)! for n = 1, 2, … . Function mygamma is designed
for computing numerical approximation of the gamma function using Gauss-Laguerre
quadratures.

function y = mygamma(t)

% Value(s) y of the Euler's gamma function evaluated at t (t > -1).

td = t - fix(t);
if td == 0
n = ceil(t/2);
else
n = ceil(abs(t)) + 10;
end
y = Gquad2('pow',n,'L',t-1);

The following function

function z = pow(x, e)

% Power function z = x^e

z = x.^e;

is called from within function mygamma.

In this example we will approximate the gamma function for t = 1, 1.1, … , 2 and compare the
results with those obtained by using MATLAB's function gamma. A script file testmyg
computes approximate values of the gamma function using two functions mygamma and gamma
29

% Script testmyg.m

format long
disp(' t mygamma gamma')
disp(sprintf('\n
_____________________________________________________'))

for t=1:.1:2
s1 = mygamma(t);
s2 = gamma(t);
disp(sprintf('%1.14f %1.14f %1.14f',t,s1,s2))
end

testmyg

t mygamma gamma

_____________________________________________________
1.00000000000000 1.00000000000000 1.00000000000000
1.10000000000000 0.95470549811706 0.95135076986687
1.20000000000000 0.92244757458893 0.91816874239976
1.30000000000000 0.90150911731168 0.89747069630628
1.40000000000000 0.89058495940663 0.88726381750308
1.50000000000000 0.88871435840715 0.88622692545276
1.60000000000000 0.89522845323377 0.89351534928769
1.70000000000000 0.90971011289336 0.90863873285329
1.80000000000000 0.93196414951082 0.93138377098024
1.90000000000000 0.96199632935381 0.96176583190739
2.00000000000000 1.00000000000000 1.00000000000000

5.4.4 Romberg's method

Two functions, namely quad and qauad8, discussed earlier in this tutorial are based on the
adaptive methods. Romberg (see, e.g., [2] ), proposed another method, which does not belong to
this class of methods. This method is the two-phase method. Phase one generates a sequence of
approximations using the composite trapezoidal rule. Phase two improves approximations found
in phase one using Richardson's extrapolation. This process is a recursive one and the number of
performed iterations depends on the value of the integral parameter n. In many cases a modest
value for n suffices to obtain a satisfactory approximation.

Function Romberg(fun, a, b, n, varargin) implements Romberg's algorithm

function [rn, r1] = Romberg(fun, a, b, n, varargin)

% Numerical approximation rn of the definite integral from a to b


% that is obtained with the aid of Romberg's method with n rows
% and n columns. fun is a string that names the integrand.
% If integrand depends on parameters, say p1, p2, ... , then
30

% they should be supplied just after the parameter n.


% Second output parameter r1 holds approximate values of the
% computed integral obtained with the aid of the composite
% trapezoidal rule using 1, 2, ... ,n subintervals.

h = b - a;
d = 1;
r = zeros(n,1);
r(1) = .5*h*sum(feval(fun,[a b],varargin{:}));
for i=2:n
h = .5*h;
d = 2*d;
t = a + h*(1:2:d);
s = feval(fun, t, varargin{:});
r(i) = .5*r(i-1) + h*sum(s);
end
r1 = r;
d = 4;
for j=2:n
s = zeros(n-j+1,1);
s = r(j:n) + diff(r(j-1:n))/(d - 1);
r(j:n) = s;
d = 4*d;
end
rn = r(n);

We will test function Romberg integrating the rational function introduced earlier in this tutorial
(see the m-file rfun). The interval of integration is [a, b] = [0, 1], n= 10, and the values of the
parameters a, b, and c are set to 1, 2, and 1, respectively.

[rn, r1] = Romberg('rfun', 0 , 1, 10, 1, 2, 1)

rn =
1.47854534395739
r1 =
1.25000000000000
1.42500000000000
1.46544117647059
1.47528502049722
1.47773122353730
1.47834187356141
1.47849448008531
1.47853262822223
1.47854216503816
1.47854454922849

The absolute and relative errors in rn are

[abs(exact - rn); abs(rn - exact)/exact]

ans =
0
31

5.4.4 Numerical integration of the bivariate functions using MATLAB function


dblquad

Function dblquad computes a numerical approximation of the double integral

∫∫ f (x, y )dxdy
D

where D = {(x, y): a  x  b, c  y  d} is the domain of integration. Syntax of the function


dblquad is dblquad (fun, a, b, c, d, tol), where the parameter tol has the same meaning as in the
function quad.

Let f(x, y) = e − xy sin(xy ) , -1  x  1, 0  y  1. The m-file esin is used to evaluate function f

function z = esin(x,y);

z = exp(-x*y).*sin(x*y);

Integrating function f , with the aid of the function dblquad, over the indicated domain we obtain

result = dblquad('esin', -1, 1, 0, 1)

result =
-0.22176646183245

5.4.5 Numerical differentiation

Problem discussed in this section is formulated as follows. Given a univariate function f(x) find
an approximate value of f '(x). The algorithm presented below computes a sequence of the
approximate values to derivative in question using the following finite difference approximation
of f '(x)

f ( x + h) − f ( x − h)
f '(x) ≈
2h

where h is the initial stepsize. Phase one of this method computes a sequence of approximations
to f'(x) using several values of h. When the next approximation is sought the previous value of h
is halved. Phase two utilizes Richardson's extrapolation. For more details the reader is referred to
[2], pp. 171 – 180.

Function numder implements the method introduced in this section.


32

function der = numder(fun, x, h, n, varargin)

% Approximation der of the first order derivative, at the point x,


% of a function named by the string fun. Parameters h and n
% are user supplied values of the initial stepsize and the number
% of performed iterations in the Richardson extrapolation.
% For fuctions that depend on parameters their values must follow
% the parameter n.

d = [];
for i=1:n
s = (feval(fun,x+h,varargin{:})-feval(fun,x-h,varargin{:}))/(2*h);
d = [d;s];
h = .5*h;
end
l = 4;
for j=2:n
s = zeros(n-j+1,1);
s = d(j:n) + diff(d(j-1:n))/(l - 1);
d(j:n) = s;
l = 4*l;
end
der = d(n);

In this example numerical approximations of the first order derivative of the function
f ( x ) = e − x are computed using function numder and they are compared against the exact values
2

of f '(x) at x = 0.1, 0.2, … , 1.0. The values of the input parameters h and n are 0.01 and 10,
respectively.

function testnder(h, n)

% Test file for the function numder. The initial stepsize is h and
% the number of iterations is n. Function to be tested is
% f(x) = exp(-x^2).

format long
disp(' x numder exact')
disp(sprintf('\n
_____________________________________________________'))

for x=.1:.1:1
s1 = numder('exp2', x, h, n);
s2 = derexp2(x);
disp(sprintf('%1.14f %1.14f %1.14f',x,s1,s2))
end

function y = derexp2(x)

% First order derivative of f(x) = exp(-x^2).

y = -2*x.*exp(-x.^2);
33

The following results are obtained with the aid of function testndr

testnder(0.01, 10)

x numder exact

_____________________________________________________
0.10000000000000 -0.19800996675001 -0.19800996674983
0.20000000000000 -0.38431577566308 -0.38431577566093
0.30000000000000 -0.54835871116311 -0.54835871116274
0.40000000000000 -0.68171503117430 -0.68171503117297
0.50000000000000 -0.77880078306967 -0.77880078307140
0.60000000000000 -0.83721159128436 -0.83721159128524
0.70000000000000 -0.85767695185699 -0.85767695185818
0.80000000000000 -0.84366787846708 -0.84366787846888
0.90000000000000 -0.80074451919839 -0.80074451920129


       %  &   )*

Many problems that arise in science and engineering require a knowledge of a function y = y(t)
that satisfies the first order differential equation y' = f(t, y) and the initial condition y(a) = y0,
where a and y0 are given real numbers and f is a bivariate function that satisfies certain
smoothness conditions. A more general problem is formulated as follows. Given function f of n
variables, find a function y = y(t) that satisfies the nth order ordinary differential equation
y( n ) = f(t, y, y', … , y(n – 1)) together with the initial conditions y(a) = y0, y'(a) = y0', … ,
y( n – 1) (a) = y0( n – 1). The latter problem is often transformed into the problem of solving a system
of the first order differential equations. To this end a term "ordinary differential equations" will
be abbreviated as ODEs.

5.5.1 Solving the initial value problems using MATLAB built-in functions

MATLAB has several functions for computing a numerical solution of the initial value problems
for the ODEs. They are listed in the following table

Function Application Method used


ode23 Nonstiff ODEs Explicit Runge-Kutta (2, 3) formula
ode45 Nonstiff ODEs Explicit Runge-Kutta (4, 5) formula
ode113 Nonstiff ODEs Adams-Bashforth-Moulton solver
ode15s Stiff ODEs Solver based on the numerical differentiation
formulas
ode23s Stiff ODEs Solver based on a modified Rosenbrock
formula of order 2

A simplest form of the syntax for the MATLAB ODE solvers is


34

[t, y] = solver(fun, tspan, y0], where fun is a string containing name of the ODE m-file that
describes the differential equation, tspan is the interval of integration, and y0 is the vector
holding the initial value(s). If tspan has more than two elements, then solver returns computed
values of y at these points. The output parameters t and y are the vectors holding the points of
evaluation and the computed values of y at these points.

In the following example we will seek a numerical solution y at t = 0, .25, .5, .75, 1 to the
following initial value problem y' = -2ty2, with the initial condition y(0) = 1. We will use both the
ode23 and the ode45 solvers. The exact solution to this problem is y(t) = 1/(1 + t2) (see, e.g., [6],
p.289). The ODE m-file needed in these computations is named eq1

function dy = eq1(t,y)

% The m-file for the ODE y' = -2ty^2.

dy = -2*t.*y(1).^2;

format long

tspan = [0 .25 .5 .75 1]; y0 = 1;

[t1 y1] = ode23('eq1', tspan, y0);


[t2 y2] = ode45('eq1', tspan, y0);

To compare obtained results let us create a three-column table holding the points of evaluation
and the y-values obtained with the aid of the ode23 and the ode45 solvers

[t1 y1 y2]

ans =
0 1.00000000000000 1.00000000000000
0.25000000000000 0.94118221525751 0.94117646765650
0.50000000000000 0.80002280597122 0.79999999678380
0.75000000000000 0.64001788410487 0.63999998775736
1.00000000000000 0.49999658522366 0.50000000471194

Next example deals with the system of the first order ODEs

y1'(t) = y1(t) – 4y2(t), y2'(t) = -y1(t) + y2(t),


y1(0) = 1; y2(0) = 0.

Instead of writing the ODE m – file for this system, we will use MATLAB inline function

dy = inline('[1 –4;-1 1]*y', 't', 'y')

dy =
Inline function:
dy(t,y) = [1 –4;-1 1]*y
35

The inline functions are created in the Command Window. Interval over wich numerical solution is
computed and the initial values are stored in the vectors tspan and y0, respectively

tspan = [0 1]; y0 = [1 0];

Numerical solution to this system is obtained using the ode23 function

[t,y] = ode23(dy, tspan, y0);

Graphs of y1(t) (solid line) and y2(t) (dashed line) are shown below

plot(t,y(:,1),t,y(:,2),'--'), legend('y1','y2'), xlabel('t'),


ylabel('y(t)'), title('Numerical solutions y_1(t) and y_2(t)')

Numerical solutions y1(t) and y2(t)


12
y1
10 y2

4
y(t)

-2

-4

-6
0 0.2 0.4 0.6 0.8 1
t

The exact solution (y1(t), y2(t)) to this system is

y1, y2

y1 =
1/2*exp(-t)+1/2*exp(3*t)
y2 =
-1/4*exp(3*t)+1/4*exp(-t)

Functions y1 and y2 were found using command dsolve which is available in the Symbolic Math
Toolbox.

Last example in this section deals with the stiff ODE. Consider
36

1
y'(t) = -1000(y – log(1 + t)) + ,
1+ t
y(0) = 1.

dy = inline('-1000*(y – log(1 + t)) + 1/(1 + t)', 't', 'y')

dy =
Inline function:
dy(t,y) = -1000*(y – log(1 + t)) + 1/(1 + t)

Using the ode23s function on the interval

tspan = [0 0.5];

we obtain

[t, y] = ode23s(dy, tspan, 1);

To illustrate the effect of stiffness of the differential equation in question, let us plot the graph of
the computed solution

plot(t, y), axis([-.05 .55 -.05 1] ), xlabel('t'), ylabel('y(t)'),


title('Solution to the stiff ODE')

Solution to the stiff ODE


1

0.9

0.8

0.7

0.6

0.5
y(t)

0.4

0.3

0.2

0.1

0 0.1 0.2 0.3 0.4 0.5


t

The exact solution to this problem is y(t) = log(1+t) + exp(-1000*t). Try to plot this function on
the interval [-0.05, 0.5].
37

5.5.2 The two – point boundary value problem for the second order ODE's

The purpose of this section is to discuss a numerical method for the two – point boundary value
problem for the second order ODE

y''(t) = f(t, y, y')


y(a) = ya, y(b) = yb.

A method in question is the finite difference method. Let us assume that the function f is of the
form f(t, y, y') = g0(t) + g1(t)y + g2(t)y'. Thus the function f is linear in both y and y'. Using
standard second order approximations for y' and y'' one can easily construct a linear system of
equations for computing approximate values of the function y on the set of evenly spaced points.
Function bvp2ode implements this method

function [t, y] = bvp2ode(g0, g1, g2, tspan, bc, n)

% Numerical solution y of the boundary value problem


% y'' = g0(t) + g1(t)*y + g2(t)*y', y(a) = ya, y(b) = yb,
% at n+2 evenly spaced points t in the interval tspan = [a b].
% g0, g1, and g2 are strings representing functions g0(t),
% g1(t), and g2(t), respectively. The boundary values
% ya and yb are stored in the vector bc = [ya yb].

a = tspan(1);
b = tspan(2);
t = linspace(a,b,n+2);
t1 = t(2:n+1);
u = feval(g0, t1);
v = feval(g1, t1);
w = feval(g2, t1);
h = (b-a)/(n+1);
d1 = 1+.5*h*w(1:n-1);
d2 = -(2+v(1:n)*h^2);
d3 = 1-.5*h*w(2:n);
A = diag(d1,-1) + diag(d2) + diag(d3,1);
f = zeros(n,1);
f(1) = h^2*u(1) - (1+.5*h*w(1))*bc(1);
f(n) = h^2*u(n) - (1-.5*h*w(n))*bc(2);
f(2:n-1) = h^2*u(2:n-1)';
s = A\f;
y = [bc(1);s;bc(2)];
t = t';

In this example we will deal with the two-point boundary value problem

y''(t) = 1 +sin(t)y + cos(t)y'


y(0) = y(1) = 1.

We define three inline functions


38

g0 = inline('ones(1, length(t))', 't'), g1 = inline('sin(t)', 't'), g2


= inline('cos(t)', 't')

g0 =
Inline function:
g0(t) = ones(1, length(t))
g1 =
Inline function:
g1(t) = sin(t)
g2 =
Inline function:
g2(t) = cos(t)

and next run function bvp2ode to obtain

[t, y] = bvp2ode(g0, g1, g2, [0 1],[1 1],100);

Graph of a function generated by bvp2ode is shown below

plot(t, y), axis([0 1 0.85 1]), title('Solution to the boundary value


problem'), xlabel('t'), ylabel('y(t)')

Solution to the boundary value problem


1

0.95
y(t)

0.9

0.85
0 0.2 0.4 0.6 0.8 1
t
39

"    

[1] B.C. Carlson, Special Functions of Applied Mathematics, Academic Press, New York, 1977.

[2] W. Cheney and D. Kincaid, Numerical Mathematics and Computing, Fourth edition,
Brooks/Cole Publishing Company, Pacific Grove, 1999.

[3] P.J. Davis and P. Rabinowitz, Methods of Numerical Integration, Academic Press, New
York, 1975.

[4] L.V. Fausett, Applied Numerical Analysis Using MATLAB, Prentice Hall, Upper Saddle
River, NJ, 1999.

[4] D. Hanselman and B. Littlefield, Mastering MATLAB 5. A Comprehensive


Tutorial and Reference, Prentice Hall, Upper Saddle River, NJ, 1998.

[6] M.T. Heath, Scientific Computing: An Introductory Survey, McGraw-Hill, Boston,


MA, 1997.

[7] N.J. Higham, Accuracy and Stability of Numerical Algorithms, SIAM,


Philadelphia, PA, 1996.

[8] G. Lindfield and J. Penny, Numerical Methods Using MATLAB, Ellis Horwood,
New York, 1995.

[9] J.H. Mathews and K.D. Fink, Numerical Methods Using MATLAB, Third edition,
Prentice Hall, Upper Saddle River, NJ, 1999.

[10] MATLAB, The Language of Technical Computing. Using MATLAB, Version 5,


The MathWorks, Inc., 1997.

[11] J.C. Polking, Ordinary Differential Equations using MATLAB, Prentice Hall,
Upper Saddle River, NJ, 1995.

[12] Ch.F. Van Loan, Introduction to Scientific Computing. A Matrix-Vector Approach


Using MATLAB, Prentice Hall, Upper Saddle River, NJ, 1997.

[13] H.B. Wilson and L.H. Turcotte, Advanced Mathematics and Mechanics Applications Using
MATLAB, Second edition, CRC Press, Boca Raton, 1997.
40

+, 

1. Give an example of a polynomial of degree n  3 with real roots only for which function
roots fails to compute a correct type of its roots.

2. All roots of the polynomial p(x) = a0 + a1x + … + an-1xn-1 + xn , with real coefficients
ak ( k = 0, 1, … n - 1), are the eigenvalues of the companion matrix

 0 1 ... 0 0 
 0 0 1 ... 0 
A= 
 . . . . . 
 
 − a0 − a1 − a2 ... − an −1 

Write MATLAB function r = polroots(a) that takes a one-dimensional array a of the


coefficients of the polynomial p(x) in the descending order of powers and returns its roots in
the array r.
Organize your work as follows:

(i) Create a matrix A. You may wish to use MATLAB's built-in function diag to avoid
using loops. Function diag takes a second argument that can be used to put a
superdiagonal in the desired position.
(ii) Use MATLAB's function eig to compute all eigenvalues of the companion matrix A.
See Tutorial 4 for more details about the matrix eigenvalue problem.

3. Write MATLAB function [r, niter] = fpiter(g, x0, maxiter) that computes a zero r of
x = g(x) using the fixed-point iteration xn + 1 = g(xn), n = 0, 1, … with a given initial
approximation x0 of r. The input parameter maxiter is the maximum number of
allowed iterations while the output parameter niter stands for the number of iterations
performed. Use an appropriate stopping criterion to interrupt computations when
current approximation satisfies the exit condition of your choice.

4. In this exercise you are to test function fpiter of Problem 3.


Recall that a convergent sequence {x(k)}, with the limit r, has the order of
convergence  if

|x(k+1) – r|  C|x(k) – r|, for some C > 0.

If  = 1, then C < 1.

(i) Construct at least one equation of the form x = g(x), with at least one real zero, for
which function fpiter computes a sequence of approximations {xn} that converges
to the zero of your function. Print out consecutive approximations of the zero r and
determine the order of convergence.
(ii) Repeat previous part where this time a sequence of approximations generated by
the function fpiter does not converge to the zero r. Explain why a computed
sequence diverges.
41

5. Derive Newton's iteration for a problem of computing the reciprocal of a nonzero


number a.
(i) Does your iteration always converge for any value of the initial guess x0?
(ii) Write MATLAB function r = recp(a, x0) that computes the reciprocal of a using
Newton's method with the initial guess x0.
(iii) Run function recp for the following following values of (a, x0) : (2, 0.3) and
(10, 0.15) and print out consecutive approximations generated by the function recp
and determine the order of convergence.

6. In this exercise you are to write MATLAB function


[r, niter] = Sch(f, derf, x0, m, tol) to compute a multiple root r of the function f(x).
Recall that r is a root of multiplicity m of f(x) if f(x) = (x – r)mg(x), for some
function g(x). Schroder (see [8]) has proposed the following iterative scheme for
computing a multiple root r of f(x)

xk+1 = xk – mf(xk)/f '(xk), k = 0, 1, … .

When m = 1, this method becomes the Newton – Raphson method.


The input parameters: f is the function with a multiple root r, derf is the first
derivative of f, x0 is the initial guess, m stands for the multiplicity of r and tol is the
assumed tolerance for the computed root.
The output parameters: r is the computed root and niter is the number of performed
iterations.

7. In this exercise you are to test function Sch of Problem 6.

(i) Use function f2 defined in Section 5.2 and write function derf2 to compute the first
order derivative of a function in file f2.
(ii) Use unexpanded form for the derivative. Run function Sch with m = 5 then repeat
this experiment letting m = 1. In each case choose x0 = 0. Compare number of
iterations performed in each case.
(iii) Repeat the above experiment using function f3. You will need a function derf3 to
evaluate the first derivative in the expanded form.

8. Let p(x) be a cubic polynomial with three distinct real roots rk , k = 1, 2, 3. Suppose
that the exact values of r1 and r2 are available. To compute the root r3 one wants to use
function Sch of Problem 6 with m = 1 and x0 = (r1 + r2)/2. How many iterations are needed
to compute r3?

9. Based on your observations made during the numerical experiments performed when
solving Problem 8 prove that only one step of the Newton-Raphson method is needed to
compute the third root of p(x).

10. Given a system of nonlinear equations

x2/16 + y2/4 = 1
x2 – y2 = 1

Use function NR to compute all the zeros of this system. Compare your results with the exact
values x = 2 and y =
3. Evaluate function f at the computed zeros and print your results
using format long.
42

11. Using function NR find all the zeros of the system of nonlinear equations

x2/16 + y2/4 = 1
x2 – x – y – 8 = 0
The following graph should help you to choose the initial approximations to the
zeros of this system

2 2 2
Graphs of x /16 + y /4 = 1 and y = x - x - 8
15

10

5
y

-5

-10
-4 -3 -2 -1 0 1 2 3 4
x

Evaluate function f at the computed zeros and print out your results using format long.

12. Another method for computing zeros of the scalar equation f(x) = 0 is the secant
method. Given two initial approximations x0 and x1 of the zero r this method generates a
sequence {xk} using the iterative scheme

x k − x k −1
xk+1 = xk – f(xk) , k = 1, 2, … .
f(x k ) − f(x k −1 )

Write MATLAB function [r, niter] = secm(f, x0, x1, tol, maxiter) that computes the zero r
of f(x) = 0. The input parameters: f is the name of a function whose zero is computed, x0 and
x1 are the initial approximations of r, tol is the prescribed tolerance and maxiter is the
maximum number of the allowed iterations. The output parameters: r is the computed zero of
f(x) and niter is the number of the performed iterations.

13. Use the function secm of Problem 12 to find the smallest positive zero of f(x).

(i) f(x) = sin(tan(x)) – x


(ii) f(x) = sin(x) + 1/(1 + e-x) – 1
(iii) f(x) = cos(x) – e-sin(x)
43

Evaluate each function f at the computed zero and print out results using format long.

14. Another form of the interpolating polynomial is due to Lagrange and uses as the basis
function the so–called fundamental polynomials Lk(x), 0  k  n. The kth fundamental
polynomial Lk is defined as follows: Lk(xk) = 1, Lk(xm) = 0 for k m, and deg(Lk)  n.
Write MATLAB function yi = fundpol(k, x, xi) which evaluates the kth Lagrange
fundamental polynomial at points stored in the array xi.

15. The Lagrange form of the interpolating polynomial pn(x) of degree at most n which
interpolates the data (xk , yk), 0  k  n, is

pn(x) = y0L0(x) + y1L1(x) + … + ynLn(x)

Write MATLAB function yi = Lagrpol(x, y, xi) that evaluates polynomial pn at points stored
in the array xi. You may wish to use function fundpol of Problem 14.

16. In this exercise you are to interpolate function g(x), a  x  b, using functions Newtonpol
(see Section 5.3) and Lagrpol (see Problem 15). Arrays x, y, and xi are defined as follows
xk = a + k(b - a)/10, yk = g(xk), k = 0, 1, … , 10, and xi = linspace(a, b). Run both
functions using the following functions g(x) and the associated intervals [a, b]

(i) g(x) = sin(4x), [a, b] = [0, 1]

(ii) g(x) = J0(x), [a, b] = [2, 3],

where J0 stands for the Bessel function of the first kind of order zero. In MATLAB Bessel
function J0(x) can be evaluated using command besselj(0, x).
In each case find the values yi of the interpolating polynomial at xi and compute the error
maximum err = norm(abs(yi - g(xi)), 'inf '). Compare efficiency of two methods used to
interpolate function g(x). Which method is more efficient? Explain why.

17. Continuation of Problem 16. Using MATLAB's function interp1, with options 'cubic' and
'spline', interpolate both functions g(x) of Problem 16 and answer the same questions as
stated in this problem. Among four method if interpolation you have used to interpolate
functions g(x) which method is the the best one as long as

(i) efficiency is considered?

(ii) accuracy is considered?

18. The Lebesgue function (x) of the interpolating operator is defined as follows

(x) = |Lo(x)| + |L1(x)| + … +|Ln(x)|,

where Lk stands for the kth fundamental polynomial introduced in Problem 14. This function
was investigated in depth by numerous researchers. It's global maximum over the interval of
interpolation provides a useful information about the error of interpolation.
In this exercise you are to graph function (x) for various sets of the interpolating abscissa
{xk}. We will assume that the points of interpolation are symmetric with respect to the
origin, i.e., -xk = xn - k, for k = 0, 1, … , n. Without loss of generality, we may also assume
44

that -x0 = xn = 1. Plot the graph of the Lebesgue function (x) for the following choices of
the points xk

(i) xk = -1 +2k/n, k = 0, 1, … , n

(ii) xk = -cos(k/n), k = 0, 1, … , n

In each case put n = 1, 2, 3 and estimate the global maximum of (x). Which set
of the interpolating abscissa provides a smaller value of Max{ (x) : x0  x  xn}?

19. MATLAB's function polyder computes the first order derivative of an algebraic polynomial
that is represented by its coefficients in the descending order of powers. In this exercise you
are to write MATLAB function B = pold(A, k) that computes the kth order derivative of
several polynomials of the same degree whose coefficients are stored in the consecutive rows
of the matrix A. This utility function is useful in manipulations with splines that are
represented as the piecewise polynomial functions.
Hint: You may wish to use function polyder.

20. The Hermite cubic spline interpolant s(x) with the breakpoints  = {xx < x2 < … < xm} is a
member of Sp(3, 1, ) that is uniquely determined by the interpolatory conditions

(i) s(xl) = yl, l = 1, 2, … , m

(ii) s'(xl) = pl, l = 1, 2, … , m

On the subinterval [xl , xl+1] , l = 1, 2, … , m – 1, s(x) is represented as follows

s(x) = (1 + 2t)(1 – t)2yl + (3 – 2t)t2yl+1 + hl[t(1 – t)2pl + t2(t – 1)pl+1],

where t = (x – xl)/(xl+1 – xl) and hl = xl+1 – xl.


Prove that the Hermite cubic spline interpolant s(x) is convex on the interval [x1, xm] if and
only if the following inequalities

2p l + p l + 1 s l +1 − s l p l + 2p l + 1
≤ ≤
3 hl 3

are satisfied for all l = 1, 2, … , m - 1.

21. Write MATLAB function [pts, yi] = Hermspl(x, y, p) that computes coefficients of the
Hermite cubic spline interpolant s(x) described in Problem 20 and evaluates spline interpolant
at points stored in the array xi. Parameters x, y, and p stand for the breakpoints, values of
s(x), and values of s'(x) at the breakpoints of s(x), respectively. The output parameter yi is the
array of values of s(x) at points stored in the array pts which is defined as the union of the
arrays linspace(x(k), x(k+1)), k = 1, 2, … , n – 1, where n = length(x).
Hint: You may wish to use function Hermpol discussed in Section 5.3.

22. The nodes {xk} of the Newton – Cotes formulas of the open type are defined as follows
xk = a + (k – 1/2)h, k = 1, 2, … , n – 1, where h = (b – a)/(n – 1). Write MATLAB
function [s, w, x] = oNCqf(fun, a, b, n, varargin) that computes an approximate value s of
45

the integral of the function that is represented by the string fun. Interval of integration is
[a, b] and the method used is the n-point open formula whose weights and nodes are
are stored in the arrays w and x, respectively.

23. The Fresnel integral


x
iπt 2
f(x) = ∫0
exp(
2
)dt

is of interest in several areas of applied mathematics. Write MATLAB function [fr1, fr2] =
Fresnel(x, tol, n) which takes a real array x, a two dimensional vector tol holding the relative
and absolute tolerance for the error of the computed integral (see MATLAB help file for the
function quad8), and a positive integer n used in the function Romberg and returns numerical
approximations fr1 and fr2 of the Fresnel integral using each of the following methods

(i) quad8 with tolerance tol = [1e-8 1e-8]


(ii) Romberg with n = 10

Compute Fresnel integrals for the following values of x = 0: 0.1:1.To compare the
approximations fr1 and fr2 calculate the number of decimal places of accuracy
dpa = –log10(norm(fr1 – fr2, 'inf ')). For what choices of the input parameters tol and n the
number dpa is greater than or equal to 13? The last inequality must be satisfied for all values
x as defined earlier.

24. Let us assume that the real-valued function f(x) has a convergent integral

∫ f (x )dx .
0
Explain how would you compute an approximate value of this integral using function
Gquad2 developed earlier in this chapter? Extend your idea to convergent integrals of the
form

−∞
∫ f (x )dx.
25. The following integral is discussed in [3], p. 317

1
dx
J= ∫x
−1
4
+ x 2 + 0.9
.

To compute an approximate value of the integral J use

(i) MATLAB functions quad and quad8 with tolerance tol = [1e-8 1e-8]
(ii) functions Romberg and Gquad1 with n = 8.

Print out numerical results using format long. Which method should be recommended for
numerical integration of the integral J? Justify your answer.
46

26. The arc length s of the ellipse

x2 y 2
+ =1
a2 b2

from (a, 0) to (x, y) in quadrant one is equal to

θ
s=b ∫
0
1 − k 2 sin2 t dt

where k2 = 1 – (a/b)2 , a ≤ b, and θ = arccos( x / a) = arcsin( y / b ).


In this exercise you are to write MATLAB function [sR, sq8] = arcell(a, b, x, n, tol) that
takes as the input parameters the semiaxes a and b and the x – coordinate of the point on the
ellipse in quadrant one and returns an approximate value of the arc of ellipse from (a, 0) to
(x, y) using functions Romberg, described in this chapter, and the MATLAB function
quad8. The fourth input parameter n will be used by the function Romberg. The fifth input
parameter tol is optional and it holds the user supplied tolerances for the relative and absolute
errors in the computed approximation sq8. If tol is not supplied, the default values for
tolerances should be assigned. For more details about using this parameter, type help quad8
in the Command Window. Your program should also work for ellipses whose semiaxes are
not restricted to those in the definition of the parameter k2. Test your function for the
following values of the input parameters

(i) a = 1, b = 2, x = 1: -0.1: 0, n = 10, tol = [ ]


(ii) a = 2, b = 1, x = 2: -0.2: 0, n = 10, tol = [ ]
(iii) a = 2, b = 1, x = 0: -0.2: -2, n = 10, tol = [ ]

Note that the terminal points (x, y) of the third ellipse lie in quadrant two.

27. Many of the most important special functions can be represented as the Dirichlet average F
of a continuous function f (see [1] )

1
1
F(b1, b2 ; a, b) =
B( b1 , b 2 ) 0 ∫
t b1 −1 (1 − t ) b 2 − 1 f [ ta + (1 − t )b]dt,

where B(b1, b2), (b1, b2 > 0) stands for the beta function. Of special interest are the Dirichlet
averages of elementary functions f(t) = t-c and f(t) = et. Former gives raise to the
hypergeometric functions such as a celebrated Gauss hypergeometric function 2F1 while the
latter is used to represent the confluent hypergeometric functions.
In this exercise you are to implement a method for approximating the Dirichlet integral
defined above using f(t) = t-c. Write MATLAB function y = dav(c, b1, b2, a, b) which
computes a numerical approximation of the Dirichlet average of f. Use a method of your
choice to integrate numerically the Dirichlet integral. MATLAB has a function named beta
designed for evaluating the beta function. Test your function for the following values of the
parameter c:

c=0 (exact value of the Dirichlet average F is equal to 1)


47

c = b1 + b2 (exact value of the Dirichlet average is equal to 1/(ab1 bb2).

28. Gauss hypergeometric function 2F1(a, b; c; x) is defined by the infinite power series as
follows


(a, n )( b, n ) n
2F1(a, b; c; x) = x , |x| ≤ 1,
n = 0 ( c, n ) n!

where (a, n) = a(a + 1) … (a + n – 1) is the Appel symbol. Gauss hypergeometric function


can be represented as the Dirichlet average of the power function f(t) = t-a

2F1(a, b; c; x) = F(b, c – b; 1 – x, 1) (c > b > 0, |x| < 1).

Many of the important elementary functions are special cases of this function. For
instance for |x| < 1, the following formulas

arcsin x = 2F1(0.5, 0.5; 1.5; x2)


ln(1 + x) = x2F1(1, 1; 1.5; x2)
arctanh x = x2F1(0.5, 1; 1.5; x2)

hold true. In this exercise you are to use function dav of Problem 27 to evaluate three
functions listed above for x = -0.9 : 0.1 : 0.9. Compare obtained approximate values with
those obtained by using MATLAB functions asin, log, and atanh.

29 Let a and b be positive numbers. In this exercise you will deal with the four formulas for
computing the mean value of a and b. Among the well – known means the arithmetic mean
A(a, b) = (a + b)/2 and the geometric mean G(a, b) = ab are the most frequently used
ones. Two less known means are the logarithmic mean L and the identric mean I

a−b
L(a, b) =
ln a − ln b

I(a, b) = e-1(aa/bb)1/(a – b)

The logarithmic and identric means are of interest in some problems that arise in
economics, electrostatics, to mention a few areas only. All four means described in this
problem can be represented as the Dirichlet averages of some elementary functions. For the
means under discussion their b – parameters are both equal to one. Let M(a, b) stand for any
of these means. Then

M(a, b) = f -1( F(1, 1; a, b) )

where f -1 stands for the inverse function of f and F is the Dirichlet average of f.
In this exercise you will deal with the means described earlier in this problem.
48

(i) Prove that the arithmetic, geometric, logarithmic and identric means can be
represented as the inverse function of the Dirichlet average of the following
functions f(t) = t, f(t) = t-2, f(t) = t-1 and f(t) = ln t, respectively.
(ii) The logarithmic mean can also be represented as


L(a, b ) = a t b1− t dt.
0

Establish this formula.


(iii) Use the integral representations you found in the previous part together with the
midpoint and the trapezoidal quadrature formulas (with the error terms) to establish
the following inequalities: G ≤ L ≤ A and G ≤ I ≤ A. For the sake of brevity the
arguments a and b are omitted in these inequalities.

30. A second order approximation of the second derivative of the function f(x) is

f ( x + h ) − 2f ( x ) + f ( x − h )
f ''(x) = + O( h 2 ) .
h2

Write MATLAB function der2 = numder2(fun, x, h, n, varargin) that computes an


approximate value of the second order derivative of a function named by the string fun at the
point x. Parameters h and n are user-supplied values of the initial step size and the number
of performed iterations in the Richardson extrapolation. For functions that depend on
parameters their values must follow the parameter n.
Test your function for f(x) = tan x with x = /4 and h = 0.01. Experiment with different
values for n and compare your results with the exact value f ''(/4) = 4.

31. Consider the following initial – value problem

y1'''(t) = - y1(t)y1''(t), y1(0) = 1, y1'(0) = -1, y1''(0) = 1.

(i) Replace the differential equation by the system of three differential equations of
order one.
(ii) Write a MATLAB function dy = order3(t, y) that evaluates the right – hand sides of
the equations you found in the previous part.
(iii) Write a script file Problem31 to solve the resulting initial – value problem using
MATLAB solver ode45 on the interval [0 1].
(iv) Plot in the same window graphs of function y1(t) together with its derivatives up to
order two. Add a legend to your graph that clearly describes curves you are plotting.

32. In this exercise you are to deal with the following initial – value problem

x '(t) = -x(t) – y(t), y '(t) = -20x(t) – 2y(t), x(0) = 2, y(0) = 0.

(i) Determine whether or not this system is stiff.


(ii) If it is, use an appropriate MATLAB solver to find a numerical solution on the
interval [0 1].
(iii) Plot the graphs of functions x(t) and y(t) in the same window.
49

33. The Lotka – Volterra equations describe populations of two species

y1'(t) = y1(t) – y1(t)y2(t), y2'(t) = -15y2(t) + y1(t)y2(t).

Write MATLAB function LV(y10, y20, tspan) that takes the initial values y10 = y1(0)
and y20 = y2(0) and plots graphs of the numerical solutions y1 and y2 over the interval
tspan.

34. Numerical evaluation of a definite integral

∫ f ( t )dt
a
can be accomplished by solving the ODE y'(t) = f(t) with the initial condition y(a) = 0. Then
the integral in question is equal to y(b). Write MATLAB function yb = integral(a, b, fun)
which implements this method. The input parameter fun is the string holding the name of the
integrand f(t) and a and b are the limits of integration. To find a numerical solution of the
ODE use the MATLAB solver ode45. Test your function on integrals of your choice.

35. Given the two – point boundary value problem

y'' = -t + t2 + et – ty + ty' , y(0) = 1, y(1) = 1 + e.

(i) Use function bvp2ode included in this tutorial to find the approximate values of the
function y for the following values of n = 8, 18.
(ii) Plot, in the same window, graphs of functions you found in the previous part of the
problem. Also, plot the graph of function y(t) = t + et which is the exact solution to
the boundary value problem in question.

36. Another method for solving the two – point boundary value problem is the collocation
method. Let
y'' = f(t, y, y') , y(a) = ya, y(b) = yb.

This method computes a polynomial p(t) that approximates a solution y(t) using the
following conditions

p(a) = ya, p(b) = yb, p'' (tk) = f(tk, p(tk), p'(tk))

where k = 2, 3, … , n – 1 and a = t1 < t2 < … < tn = b are the collocation points that are
evenly spaced in the given interval.
In this exercise function f is assumed to be of the form f(t, y, y') = g0(t) + g1(t)y + g2(t)y'.

(i) Set up a system of linear equations that follows from the collocation conditions.
(ii) Write MATLAB function p = colloc(g0, g1, g2, a, b, ya, yb, n) which computes
coefficients p of the approximating polynomial. They should be stored in the array p
in the descending order of powers. Note that the approximating polynomial is of
degree n – 1 or less.

37. Test the function colloc of Problem 36 using the two – point boundary value problem of
Problem 35 and plot the graph of the approximating polynomial.
50

You might also like