Matlab Book
Matlab Book
Matlab Book
Of
MATLAB
Second Edition
Sagren Munsamy
For all those students who bothered me with
M ATLAB questions. . . and those that hate the
inventors of M ATLAB.
The Mesmerizing World of M ATLAB:
An Introduction
Second Edition
Sagren Munsamy
c
Copyright °Sagren Munsamy 2005
All rights reserved. No part of this publication may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, electronic, mechanical, photocopy-
ing, recording or otherwise, without the prior written permission of the author.
2
Contents
I Introducing M ATLAB 9
1 The Essentials of m-file Programming 13
1.1 The Basic Structure of an m-file . . . . . . . . . . . . . . . . . . . . . . . . 13
1.2 Variable Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.3 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.4 Assigning Values to Variables . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.5 Matrix, Scalar and Array Operations . . . . . . . . . . . . . . . . . . . . . 16
1.6 Flow Control: Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.7 Flow Control: Decision Structures . . . . . . . . . . . . . . . . . . . . . . . 18
1.8 Essential M ATLAB Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 19
II Intermediate Programming 39
4 Cell Arrays and Structures 43
4.1 Structure or Cell Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.2 Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3
4.2.1 Creating Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.2.2 Referencing Cell Array Items . . . . . . . . . . . . . . . . . . . . . 44
4.2.3 Removing and Clearing Contents of Cell Arrays . . . . . . . . . . . 45
4.2.4 Reshaping Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.2.5 Nested Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.2.6 Visualising Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . 47
4.3 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4.3.1 Creating Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4.3.2 Types of Arrangements . . . . . . . . . . . . . . . . . . . . . . . . . 48
4.3.3 Dynamic Fields: A Way to Search for Data . . . . . . . . . . . . . . 49
4.3.4 Setting Up a Searchable Structure . . . . . . . . . . . . . . . . . . . 49
4.3.5 Removing Fields from Structures . . . . . . . . . . . . . . . . . . . 50
4.3.6 Operations on Structures . . . . . . . . . . . . . . . . . . . . . . . . 50
4.3.7 Nested Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5 Logical Subscripting 53
5.1 Logical Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
5.2 Applications of Logical Vectors . . . . . . . . . . . . . . . . . . . . . . . . . 54
5.2.1 The find command . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
5.2.2 Discontinuous Graphs . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.2.3 Avoiding Division by Zero . . . . . . . . . . . . . . . . . . . . . . . 56
5.2.4 Avoiding Infinity . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.2.5 Counting Random Numbers . . . . . . . . . . . . . . . . . . . . . . 57
5.3 Replacing Stacked elseif Ladders . . . . . . . . . . . . . . . . . . . . . . 57
4
IV Numerical Methods 87
8 Basic Integration and Differentiation, and Data Regression 91
8.1 Why Numerical Methods? . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
8.2 Basic Numerical Integration . . . . . . . . . . . . . . . . . . . . . . . . . . 92
8.3 Basic Numerical Differentiation . . . . . . . . . . . . . . . . . . . . . . . . 94
8.4 Data Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
8.5 When the Derivative Does Not Exist at a Point. . . . . . . . . . . . . . . . . 96
5
Acknowledgements
6
Preface to the Second Edition
This book was made available to the students of the University of KwaZulu-Natal
during the first semester of 2005. Some students found the book useful, while others
were not aware that the book was available. After having had a chance to gain some
feedback on the material in the book, I decided to review some parts of the book. The
changes in the book are minor. Typographical errors were corrected and so were some
of the programs. A new cover was also designed.
The intended audience for the book remains the same . . . those that are beginning
M ATLAB, and intermediate M ATLAB programmers.
I sincerely hope that this book proves to be useful.
– Sagren Munsamy
[email protected]
BScEng(Chemical), 3rd Year
July 2005
7
Preface to the First Edition
This book took just under one and a half months to complete. My vision was to
write a book that would serve as a simple introduction to using M ATLAB as a computa-
tional environment. The end-result and my initial vision are totally different. This book
quickly covers the basics of m-file programming, and then goes on to introduce more
advanced topics such as cell arrays and structures. I found myself including information
that was not so basic, very early in the writing of this book. I have tried to keep the
language and explanations as simple as possible. I intended including a section with
examples and exercises at the end of this book, but time constraints have not made that
possible. In a future edition, it shall be included.
This book is not a true introductory guide to using M ATLAB. The aim of this book
is to provide you with sufficient information, tools and explanations to broaden your
knowledge of M ATLAB programming. Some experience of using M ATLAB is thus re-
quired.
In my opinion, this book and Brian D. Hahn’s book Essential Matlab for Scientists
and Engineers complement each other quite well. His book assumes no knowledge of
M ATLAB programming and also has a greater volume of information regarding basic
m-file programming. Graphics is not covered in detail in this book, but it is in Hahn’s.
The very interesting topic of logical vectors is presented very differently here, albeit that
the same examples have been used for that section. Some of the topics that have been
omitted will be posted as separate articles on the Engineering Server at UKZN. The path
on the server is S:\COURSES\mecheng\matlab book.
I have attempted to explain numerical methods and the use of ODE solvers in more
detail than that covered in Hahn’s book. I think you will find it easier to understand the
sections in this book. GUIs, structures and cell arrays have also been covered in greater
detail in this book.
This book alone will not be sufficient to develop your M ATLAB programming skills.
You need to practice, and to practice you need examples. There are many tutorials
available on the Internet and on the engineering server, and great questions in Hahn’s
book.
I do not by any means consider myself to be a great M ATLAB programmer, or some-
one that knows a lot of M ATLAB. I have just tried to write down some of the stuff that
I think will prove useful to undergraduate engineering students. It is my sincere hope
that this book will help unleash your broaden your repertoire of programming skills
and aid in the development of structured thinking and prove useful to you.
– Sagren Munsamy
[email protected]
BScEng(Chemical), 3rd Year
December 2004
8
Part I
Introducing M ATLAB
9
This part introduces the basics of m-file programming and also includes some ad-
vanced programming techniques and information. Input, output and displaying of data
(including graphics) is covered.
11
12
Chapter 1
Those that find this introductory chapter useful have KS(see the preface) to thank,
as this individual told me in a very nice manner that this book would not be complete
without this chapter. There are many great introductory M ATLAB tutorials around and
that is why I did not initially want to include this. I will do my best to introduce the
basics of M ATLAB in this chapter, but this introduction is by no means a definitive intro-
duction. I still assume that you have had some experience using the M ATLAB command
window.
called the primary function and the rest are called subfunctions. This matter is dealt with in an-
other chapter.
13
function outputargs = function_name(inputargs)
FUNCTION BODY
outputargs is the variable via which the function returns results, and inputargs
is the variable via which the function receives inputs. For those that have done some
other programming language–inputargs is also known as a parameter. You can have
multiple input and/or output arguments in the function definition line. Examples of
function definition lines are:
function meanvalue=mv(x,y)
function [meanvalue,sum]=mv(x,y)
As you can see, multiple output arguments are enclosed by square brackets and
multiple input arguments are enclosed by round brackets. In both cases, multiple ar-
guments are separated by commas. Also note that the m-file name should be the same
as the name of the function in the case of a function m-file. For the first example of a
function definition line, the name of the m-file should be mv.m
Of course, if one has many arguments, then it becomes an irritation to use the above
notation for multiple arguments. In that case, one can resort to cell arrays or structures.
Since it is quite an advanced topic, it is presented later on in this book.
To access the function mv, we need only type, provided that a and b are defined in
the base workspace
As you can see, the arguments passed to the function need not have the same name
as the input arguments of the function.
x=[0 1 2 3];
y=[0 2 4 6];
plot(x,y);
close all;
plot=6;
plot(x,y);
14
This time, you will not get a plot of a straight line.M ATLAB has assigned the numeric
value of 6 to the function name(which it now thinks is a variable!). So what does one
do if one accidently assigns a value to an already defined function? All you need to do
is type clear all to clear all variables in the workspace and restore everything to its
normal state. Variable naming problems can be sorted out by,
1. Typing the proposed variable name in the command window. If M ATLAB returns
where proposed name represents the proposed variable name; and step 2 re-
turns the value one, then the variable name can be used.
2. type isvarname proposed name
• logical arrays
• character arrays3
• numeric data type double
• cell arrays
• structures
• function handles
In this introductory chapter we will restrict ourselves to numeric data type double.
EXAMPLES
To prevent the assignment result from being echoed in the command window, state-
ments should be terminated with semicolons.
2 M ATLAB refers to vectors, matrices and n-dimensional matrices as arrays.
3 Character arrays are dealt with in the chapter on string handling
15
1.5 Matrix, Scalar and Array Operations
Matrix Operations M ATLAB interprets all operators in a matrix-sense unless oth-
erwise specified by the user. In other words, * and / mean multiply and divide in the
matrix sense. This of course means that the dimensions of the matrices that are involved
must agree. Matrix operations are covered in Applied Math I(a), so I will just present
matrix multiplication and matrix division.
TOOL 1.1 Let A ∗ B = C where A, B and C are matrices with dimensions m-by-n, n-by-m
and m-by-m respectively. The matrix product of A and B is defined by the matrix C where,
n X
X n
x = A−1 b 6= bA−1
Scalar Operations Scalar operations are the same as scalar operations in the matrix
sense i.e., each element is scaled by the scalar. An example would be x=3*A
TOOL 1.3 Let C = A ∗ B. If this is an array operation, then the dimension of A, B and C is
m-by-n. Then,
Cij = Aij Bij
This can be represented in M ATLAB as C=A.*B
Element-wise division is defined analogously.
4 The latter is often said as “A under b”, to distinguish it from “A over b”. Remember that b is
16
1.6 Flow Control: Loops
Horror Story I remember the first experience I had with loops in M ATLAB. It was
quite funny, but at the time it did not seem amusing. We had an assignment to hand
in and I had finished the assignment and was confident that everything was logically
correct, and that the syntax was also correct. But, when I ran my program I got the error
??? Attempt to execute SCRIPT for as a function.
I was horrified. I went over the code a million times and could find nothing wrong.
I went to one of the lecturers (I won’t say who–it was not Dr. Rawatlal) and he said that
there was nothing wrong. I did not know what to do. I retyped the code and it worked.
I compared the two m-files and saw that the only difference was one letter—the “f” in
“for” was capitalized. It was supposed to be a small letter! That was what resulted in
the error. From then on, I never used capital letters in programming. I use underscores
if required e.g., rather than MeanValue I use mean value, for purposes of readability.
Anyway. . .
Loop Structures M ATLAB has two loop structures, for and while. The syntax for
for is
for loop_counter=start_val:increment:end_val
STATEMENTS
end;
It is rather similar to PASCAL in that the loop is terminated with an end, but dis-
similar in the sense that it does not have a do. Note that the increment is optional. If it
is not specified, then it is assumed to be one. For example to run a loop from 1 to 10 in
increments of 2, we would say,
for j=1:2:10
end;
17
while decision
STATEMENTS
end
while n˜=4
disp(’hello’)
end;
The boolean operators not, and and or are represented by ˜, & and | respectively.
The first symbol is called a tilde and the last one is usually shift+backslash (for each |)
on computer keyboards. Note that these are element-wise operators, and are the ones
that are usually intended when thinking in terms of Boolean algebra.
if decision
STATEMENTS
else
end
The else part is optional, but each if needs a corresponding end. The syntax for
switch is,
switch switch_expr
case case_expr,
STATEMENTS
STATEMENTS
otherwise
STATEMENTS
end
The otherwise part is optional. Note that case expr and switch expr can be
either a string or scalar, making switch quite restrictive. Another important thing to
18
note is that switch short circuits. In other words, if one of the case expressions is true,
then those corresponding statements are executed, and the remainder of the switch
structure is bypassed. If none of the case expressions are true but the otherwise is
true, then the statements for the otherwise part are executed and program execution
is resumed after the end of the switch structure.
Implications of Stacking and Nesting If you have stacked if’s then it could end
up looking like,
if expr1
STATEMENTS
end
if expr2
STATEMENTS
end
if expr3
STATEMENTS
end
This is termed a stacked if ladder and implies that the decisions are independent of
each other. This sort of code can be replaced by,
if expr1
STATEMENTS
elseif expr2
STATEMENTS
elseif expr3
STATEMENTS
end
It looks neater and still implies that the decisions are independent of one another. The
else part can be incorporated into the code with elseif, by putting the else in its
usual place—before the end.
Note that the primary if of nested if’s cannot be turned into elseif’s as the
nested structure implies dependence of the decisions.
19
transpose A matrix can be transposed by using the ’ operator, or the transpose
function. transpose(a) is the same as a’, provided that a is non-complex. transpose(a)
is the pure transpose of a and is always the same as a.’.
linspace This function is used to generate linearly spaced points over an interval.
The general syntax is var=linspace(start val,end val,numpoints). Note that
the number of points is specified, not the step size.
Vector initialization Here you specify the step size and the start and end values. The
general syntax is var=start val:step size:end val. Let a represent the starting
value, b the end value, h the step size and n the number of points. The relationship
amongst these variables is h = (b − a)/(n − 1). It is quite useful to keep this in mind.
repmat This function replicates and tiles a matrix or vector. For example let’s say
that x=[1 2 3]. If we say,
newx=repmat(x,[4 1])
1 2 3
1 2 3
1 2 3
1 2 3
2. For the variable that has been set up in rows, replicate in columns i.e., the first
argument of the square bracket in repmat should be 1 and the other should be
n, where n is the number of elements in the other vector. This is done to make
sure that the matrix dimensions agree. For the variable that has been set up in
columns replicate in rows.
newx=repmat(x,[1 60])
newy=repmat(y,[100 1])
20
MESHGRID X and Y arrays for 3-D plots.
[X,Y] = MESHGRID(x,y) transforms the domain specified by vectors
x and y into arrays X and Y that can be used for the evaluation
of functions of two variables and 3-D surface plots.
The rows of the output array X are copies of the vector x and
the columns of the output array Y are copies of the vector y.
Note that shading interp is not required, and that the number of points have been
specified in meshgrid. As you can see, the code is much simpler, and you don’t have to
worry about the dimensions of x and y. A word of warning . . . round–off errors can lead
to the end point not being included, if the number of points in an interval are specified,
but you decide to specify the interval, as in the meshgrid statement above. This issue
is not as serious as I make it sound, though.
size This function returns the number of rows and columns of an one-dimensional
or two dimensional array, as a matrix of size 1-by-2. If you want to know the number of
columns there are in a matrix then you can execute size(m,2). Alternatively, to find
the number of rows, you can execute size(m,1). To find the number of elements in a
one-dimensional array (let’s say x), you can also use length(x). length is useful in
string handling.
21
22
Chapter 2
Most people will be happy knowing how to output and input data using disp and
input, respectively. This chapter will deal with the use of the basic input and output
commands and the more versatile(thus, more complicated) fprintf command. You
may consider Graphical User Interfaces as being advanced input/output structures1 , but
since there is so much of information that one needs to know about GUIs, a separate
chapter is devoted to the creation and use of GUIs.
⊲ E XAMPLE
Let x=[1 2 3 4]. Use disp to display,
x(1) is 1
1 In this instance structures do not refer to M ATLAB structures, although structures are used
23
Solution
Since disp only accepts a single argument, an appropriate argument needs to be cre-
ated. The code is,
x=[1 2 3 4];
s=[’x(1) is ’, num2str(x(1))]
disp(s);
Note that when the optional argument s is used, the user does not have to enclose a
string input within quotes. Quotes are necessary when the optional argument is omit-
ted.
Enter k:
k?
Now if k was, say, thermal conductivity in units of W/mK, we could have some-
thing like,
Long prompts irritate people as most people do not like reading much! I know that
I have found it irritating to debug a program that requires more than 5 inputs, with
prompts that are a mile long.
24
2.2 Advanced Input and Output
2.2.1 Basic Uses of fprintf
fprintf allows exact control over command window output. It can be used to:
1. control command window output
2. write tables of data to text files
fprintf(’format_string’,list_of_variables);
The basic procedure to be followed for displaying data as columns with headings
is,
1. The column headings must be set up as rows i.e., a column vector.
2. The data to be displayed must be set up in rows. Each column of data must be
represented by a separate row.
3. Apply fprintf to the headings then the data.
Format Strings
An example of a format string is,
%-10.2e\n
If you have done some C programming, then this will look very familiar. The format
string always starts with a %. The minus is called a flag and indicates that the data should
be left justified. In output of tables, headings and data should be right justified. To right
justify, omit the minus. The next number specifies the width of the field. It is analogous
to a column width in spreadsheets. The number after the decimal point indicates the
number of decimal places to be included in the output. The e, tells M ATLAB to output
the data in exponential (scientific) notation. g specifies that M ATLAB should choose the
more compact of fixed point and scientific notation. To output a sting using fprintf,
the e is replaced by an s. The \n is called an escape code. The n indicates that the cursor
should be moved to the next line.
⊲ E XAMPLE
Lets say we have the following data and want to output only the run number and
height:
Run Time/s Height/m
1 3 4
2 8 8
3 6 6
4 10 9
We can use the code,
25
r=1:4;
h=[4 8 6 9];
fprintf(’%10s %10s\n’,’Run’,’Height/m’);
fprintf(’%10.2g %10.2g\n’,[r;h]);
Run Height/m
1 4
2 8
3 6
4 9
Note that each format string is coupled with a corresponding row in the second
fprintf statement i.e., the first format string is coupled with the first row of the matrix
etc.
When using M ATLAB to output tables, it is easy to output only the data that you
want to see. The command window output can then be copied and pasted into EXCEL.
You can then click on the clipboard that appears and select Use Text Import Wizard2 . You
can then set up the data as required. Thereafter, you can make the spreadsheet look
pretty!
fprintf(file_id,’format_string’,list_of_variables)
fileid is an integer value returned by the function fopen. The procedure to write
data to a text file is:
1. Arrange the data as for output of formatted data to the command window.
2. Use fopen to open a file, and assign the file ID to a variable.
3. Use fprintf to write the data to the file.
4. Use fclose to close the file.
This is best explained with an example.
⊲ E XAMPLE
Let us take the same data used in the previous example and keep the specification the
same. This time we will output the data to a file data.txt3 using fprintf. The code
is,
2 This applies to Office XP and later. If you are using some other version of Office then type Text
26
r=1:4;
h=[4 8 6 9];
file_id=fopen(’data.txt’,’w’);
fprintf(file_id,’%10s %10s\n’,’Run’,’Height/m’);
fprintf(file_id,’%10.2g %10.2g\n’,[r;h]);
fclose(file_id);
fopen(’file_name’,’access_mode’);
[run,data]=textread(’data.txt’,’%s %s’)
This imports the entire columns i.e., header and data as strings, and stores them in
variables run and data. The output is,
run =
’Run’
’1’
’2’
’3’
’4’
data =
’Height/m’
’4’
’8’
’6’
’9’
The headers and data can be retrieved via normal matrix operations, and the strings
can be converted to numerical values by using char and then str2num i.e.,
27
r=run(2:end,1);
d=data(2:end,1);
r=char(r);
d=char(d);
r=str2num(r);
d=str2num(d);
If you are going to use this sort of importing a lot or require automated importing of
data for say, a presentation or post practical interview, I suggest that you write up a
function m-file that can do this for you. It is an interesting task! Of course, you can just
use the import wizard to do this for you.
An easier alternative to textread is importdata. All you need to do in this case
is to specify the file name. The conversion to numerical values is not required. The code
is,
expdata=importdata(’data.txt’);
expdata =
expdata.data
which returns,
ans =
1 4
2 8
3 6
4 9
This is probably the preferred way of programatically import this sort of text file
into M ATLAB.There are tonnes of other functions that are available for the import of
data. Type help fileformats in the command window to see a list.
28
x y
10 40
30 200
80 400
90 350
Now assume that the first column is stored in a variable x, and the second column
stored in the variable y(these must be set up as column vectors for the code below to
work!). To export these two columns to the file mydata.txt, we first need to set up a
single variable that has the data in the arrangement that we want, and then export the
data i.e.,
data=[x y]
save mydata d -ascii -double -tab
-ascii tells M ATLAB to save the data in ASCII format, -double causes the data
to be save with 16 bit precision, and -tab indicates that the data should be tabulator
delimited. The contents of the file should be, (you may view the file with a text-editor)
1.0000000000000000e+001 4.0000000000000000e+001
3.0000000000000000e+001 2.0000000000000000e+002
8.0000000000000000e+001 4.0000000000000000e+002
9.0000000000000000e+001 3.5000000000000000e+002
The drawback of using save is that you cannot have column headings exported
with the numeric data. You will have to add these in if you require them. Oh yes, the
data can be read into M ATLAB by using load.
29
30
Chapter 3
M ATLAB has many functions that allow excellent visualisation of data. There are far
too many functions, so only those necessary and useful to undergraduate students will
be covered in this chapter. One of the great things about using the graphics functions of
M ATLAB is that the output is computed extremely quickly. It is quite an amazing thing.
Various line types, plot symbols and colors may be obtained with
PLOT(X,Y,S) where S is a character string made from one element
from any or all the following 3 columns:
31
b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star
y yellow s square
k black d diamond
v triangle (down)
ˆ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram
If we had, say,
x=[1 2 3 4];
y=[5 6 7 8];
plot(x,y);
32
If we want a green line with the points plotted as circles1 , we could use,
plot(x,y,’go’)
or alternatively,
plot(x,y,’og’)
plot(x,y,’linestyle’,’none’,’color’,’g’,’marker’,’o’);
Other properties can be set in this manner. See line in the search tab of help. Of
course, one can also use handles, but that topic is a bit too much for this chapter.
plot3 is the three-dimensional version of plot. It is much the same as plot.
M ATLAB also has easy-to-use plotting functions e.g., ezplot and ezsurf. Look
them up in M ATLAB help. They are easy to use!
semilogy(x_values,y_values);
semilogx(x_values,y_values);
loglog(x_values,y_values);
3.3 surf
surf is used to plot surfaces. The general syntax is,
surf(x_values,y_values,z_values);
The arguments are usually two-dimensional arrays. See help for more details.
1 When points are emphasized by using circles, dots etc., these circles, dots etc., are referred to
33
3.4 Multiple Plots on a Figure
If you want to plot more than one set of data on the same set of axes, you can use hold.
If we use,
hold on
The current figure is not cleared and further plots are appended to the current figure.
To replace the plots on the current figure, use hold off. You may think of hold as
being an instruction to hold all current plots on the current figure.
When using hold, the axis limits are automatically redefined to fit the data. You
can also have multiple axes on the same figure. This is accomplished by using subplot.
The syntax is,
subplot(r,c,p)
This creates r by c axes on the current figure and makes axes with index p the current
axis. M ATLAB counts cumulatively. Rows are counted, and then M ATLAB moves to the
next column. See find in the index. Indexing is done the same way.
x=[1 2 3 4];
y=[5 6 7 8];
plot(x,y,’r’);
plot(x,y,’go’);
legend(’data points’,’line plot’);
This gives,
34
8
data points
line plot
7.5
6.5
5.5
5
1 1.5 2 2.5 3 3.5 4
How does legend know whether the green plot with markers takes the string data
points or whether the red plot does? We did get the correct result though. It’s not
magic, unfortunately! If we change the code to,
we get,
35
8
line plot
data points
7.5
6.5
5.5
5
1 1.5 2 2.5 3 3.5 4
This may seem confusing at first, but it isn’t after you try it on a few plots.
α = 1/3β 2/3 y2
How to do this in M ATLAB? Answer:
36
figure; %generate a blank figure
xlabel(’\alpha=1/3\betaˆ{2/3} y_2’,’interpreter’,’tex’)
In M ATLAB Greek letters can be generated by prefixing the English spelling with
a backslash. Other symbols can also be generated. Examples can be found in M ATLAB
help. Other useful things are:
Note that when using super- and subscripts, all the text that you want to appear
as super- or subscript needs to be surrounded by curly braces. You can also select the
fontsize and fontname for the text labels. For example,
Now I don’t really like typing out the TEX commands by hand, so I use MathType
5.0 to generate the TEX commands that result in the label that I require. It is much easier.
Of course, you could spend a day learning some LATEX 2ε , if you really want to.
You can also place text at any point on an axis by specifying the x and y coordinates
and the string. For example,
text(3,7,’\alpha=1/3\betaˆ{2/3} y_2’,’interpreter’,’tex’)
puts the formula above at coordinate (3,7) on the axis. This of course means that
you need to know how the graph is going to look before issuing such a command, or
at least have some way of calculating the coordinate that you want to place the text at.
Note that the default alignment is center. You can change it. . . and I am sure that you
know how to do that by now!
You can place the same formula interactively at a point by using the mouse. To do
this, use
gtext(’\alpha=1/3\betaˆ{2/3} y_2’,’interpreter’,’tex’)
Try it.
By the way, you can use ginput to get the x and y values of the points you click on
using the mouse. You can do this by,
[x,y]=ginput
37
38
Part II
Intermediate Programming
39
This part covers the very interesting topics of
1. cell arrays
2. structures
3. logical vectors and logical subscripting
4. function handles
These topics are not essential topics, but familiarity with these topics will make some
programming tasks easier. Function handles are required for the chapter on using M AT-
LAB ODE solvers and structures are useful for designing GUIs.
41
42
Chapter 4
Cell arrays and structures can be considered to be conceptually similar. Both these
data types allow one to store variables of different data types in a single variable. This
cannot be done in matrices—all the elements of the matrix need to be of the same data
type.
Another use of cell arrays is in the programming of function m-files. Sometimes, it is
cumbersome to specify all the input arguments to a function in the function definition
line. A structure can be used to simplify the situation where there are many input
arguments to a function or where there are a variable number of input arguments to a
function.1
programs function m-files. They have provided us with the reserved variables varargin and
varargout, which are to be used in this situation. This shall be presented later in this chapter.
43
• You want to access subsets of the data as comma-separated variable lists.
• You don’t have a fixed set of field names.
• You routinely remove fields from the structure.
Preallocation Just as there are functions to preallocate normal arrays, the cell func-
tion is used to preallocate cells. It’s use is analogous to the usual preallocation functions
zeros and ones. For example, to preallocate a cell array of dimension 3-by-4, one need
only say, c=cell(3,4).
44
4.2.3 Removing and Clearing Contents of Cell Arrays
You should have guessed by now that to clear an element of the cell array, you would
do the same thing as you would with a numeric array, except that you would use the
curly bracket notation. For example, assume that a=’what’,’is’;1, [1 2; 3 4].
To clear the contents of the cell that contains what, we would say,
a{1,1}={}
If you are curious as to the accuracy of the information presented in this book, then you
probably have looked at the online M ATLAB help and seen that it says,
You can delete an entire dimension of cells using a single statement. Like
standard array deletion, use vector subscripting when deleting a row or
column of cells and assign the empty matrix to the dimension.
A(cell_subscripts) = []
When deleting cells, curly braces do not appear in the assignment state-
ment at all.
This is also a valid command, but also causes some peculiar things to occur. Let’s say
that a=cell(3,4);a{3,4}=[1 2; 3 4]. M ATLAB outputs,
a =
[] [] [] []
[] [] [2x2 double] []
[] [] [] []
Let’s say that we wanted to delete the first element of the cell. One may be inclined
to say (after reading the M ATLAB help quite quickly and not carefully enough!) that,
a(1,1)=[]
If you reread the quote from M ATLAB help, you will notice that it mentions entire di-
mension. When we speak of dimension in matrix terms, we are referring to rows or
columns. Then, entire dimension refers to an entire row or entire column. The error
returned should now make sense—if you specify the row or column number you are
specifying the position of the entire row or column. Okay, now we know that we cannot
use this command as we only want to remove a single element, not an entire dimension.
But what if we said,
a(1)=[]
This does not give an error, but M ATLAB does the following:
1. M ATLAB counts cumulatively starting at one at position (1,1) and removes the
element with the specified index. In other words, for the cell array above, the
counting(count is represented in open-close chevrons) is as follows:
45
a =
<1> [] <4> [] <7> [] <10> []
<2> [] <5> [] <8> [2x2 double] <11> []
<3> [] <6> [] <9> [] <12> []
2. Since the structure cannot exist as a matrix, M ATLAB reshapes the cell array into
a one dimensional array with the element at count position 1 removed.
Note the subtle distinction between clearing the contents of a cell and deleting a cell.
It is often quite easy to confuse the two. So, be careful.
So then, how do we clear the contents of the first element without reshaping the
array? We can say,
a{1,1}=[]
or alternatively,
a{1,1}={}
The above two statements seem to accomplish the same thing. They do not, tech-
nically. One returns an empty cell in the position (1,1) and the other returns an empty
matrix in position (1,1) i.e., they return two different data types. Another subtlety. I
cannot think of any situations where using one of the above methods would cause a
problem. So so not worry too much about this.
reshape(matrix_to_reshape,size_1st_dim, size_2nd_dim,...)
Note that the reshaped matrix can only have the same number of elements as the
matrix to reshape i.e., the product of the size of the matrix to reshape in each dimension
must be the same as the number of elements in the new matrix. The matrix that is
returned by reshape is of dimension
a=cell(4,3);
a{2,3}=cell(1,2);
46
The cell array of dimension 1-by-2 is a nested cell array. We may assign a value of 3
to the element in position (1,2) of the nested cell array by saying,
a{2,3}{1,2}=3;
We could also assign a numeric array to position (1,1) of the nested cell array. Let’s
assume that we want to create a matrix with zeros except for the element in position
(1,3), which will be 100. Further, the size of the numeric array is 10-by-3. This can be
accomplished by,
a{2,3}{1,1}=zeros(10,3);
a{2,3}{1,1}(1,3)=100;
4.3 Structures
Structures are particularly useful in cases where you have a set of fixed field names. An
example of such a case is in the creation of a database. In doing calculations for practi-
cals (labs) such as the one in various courses in Chemical Engineering, using M ATLAB
with structures in an appropriate manner will lead to a very elegant, easily editable set
of calculations5 .
Good practices In creating structures, it is good to sit down and think about how
you want to arrange the structure and its fields. It is good practice to draw a rough
diagram of the intended structure.
Assignment statements To store the character string Jeffry in the field name of
the parent structure Characters, we would say,
Characters.name=’Jeffry’
5 I have also written an article Practical Applications of Cell Arrays and Strucures: Spreadsheeting
that explains how to go about using M ATLAB to do such calculations, and also includes an actual
calculation done by me.
47
This is implicitly interpreted by M ATLAB as being,
Characters(1).name=’Jeffry’
Of course we may add to the structure. Say we wanted to store the name Kitana
in the appropriate field. We would then just say,
Characters(2).name=’Kitana’
⊲ E XAMPLE
For example, say we have a database, debtors, which we shall assume to be the name
of the parent structure. We wish to have the name and outstanding balance of the
debtor. We could arrange the data in two ways viz.,
1. We could store all the names in a single field and all the balances in a single field.
Related data in fields share common indices, usually. This is an example of plane-
field arrangement.
2. We could treat debtors as an array, where the first debtor’s data is represented
by debtors(1) and so forth. We could then store the name and outstanding
balance of each debtor as a unit, as an item of the array. For example, we could
say, for the first two debtors,
48
debtors.name = ’Mike Boswell’
debtors.balance = 100000
debtors(2).name = ’Charlie Chaplin’
debtors(2).balance = 4000
I think that the easiest way to remember the two basic arrangement types is to think
of the database here. In plane-field arrangement, names are stored together as a single
field, and so are account balances. In distributed arrangement, all information regarding
a specific person (say) is stored together.
Name Age
Sagren 21
Preshodin 17
Keith 20
student.Sagren.age=21
student.Preshodin.age=17
student.Keith.age=20
We may now retrieve any of the above student’s age by using a single function m-
file that has the name of the student as a dynamic field.
The m-file would be something like,
49
function getage(studentstr,name)
disp([name, ’: ’, num2str(studentstr.(name).age), ’ years.’])
To retrieve the age of student Keith, we would say,
getage(student,’Keith’)
which returns
Keith: 20 years.
Note that in database management, the dynamic field is usually unique to each
record. The reason for this is obvious.
⊲ E XAMPLE
Assume that some sort of experiment was conducted, where values of discharge
coefficients we calculated for a Venturi Meter, and we want to find the average discharge
coefficient for each run. The data is,
Run 1 Run 2 Run 3
0.93 0.86 0.96
0.89 0.84 0.94
0.91 0.91 0.93
We could set up the structure by,
exp.run.cd(1)=0.93;
exp.run.cd(2)=0.89;
exp.run.cd(3)=0.91;
exp.run(2).cd(1)=0.86;
exp.run(2).cd(2)=0.84;
exp.run(2).cd(3)=0.91;
exp.run(3).cd(1)=0.96;
exp.run(2).cd(2)=0.94;
exp.run(2).cd(3)=0.93;
50
This sort of set up is not an efficient way of doing this. A better way would be,
cd=[0.93,0.89,0.91;0.86,0.84,0.91;0.96,0.94,0.93]
for r=1:3
for c=1:3
exp.run(r).cd(c)=cd(r,c);
end
end
This is easier to type, and lends itself to easy editing of the data.
Let us find the average for each run. We could say,
avgcd=zeros(1,3);
for r=1:3
avgcd(1,r)= mean([exp.run(r).cd])
end
avgcd=zeros(1,3);
for r=1:3
avgcd(1,r)= mean([exp.run(r).cd(1), exp.run(r).cd(2),exp.run(r).cd(3)])
end
avgcd =
51
52
Chapter 5
Logical Subscripting
a=logical([1 0 1 1 0]);
53
p=logical([1 0 1 0]);
a=[1 2 3 4];
Now typing,
a(p)
returns the elements of a that correspond to the ones in p. Note that p and a do not
have to be the same dimension.
Relational operators can also be used in combination with logical vectors e.g.,
a>2
returns
ans =
0 0 1 1
So, typing,
a(a>2)
should return the elements of a that are greater than two. The result returned is,
ans =
3 4
The majority of the chapter in Hahn’s book presents various uses of this sort of
strategy.
a=[1 2 3 4];
find(a>2)
returns
ans =
3 4
54
Now say, we had
returns
ans =
2
4
5
6
a =
1 2 3
4 5 6
we see that find counts cumulatively in rows and then moves to the next column i.e.,
the item numbers for the matrix a are,
1 3 5
2 4 6
In the case of a one-dimensional array, item numbers like this are fine. When dealing
with matrices, we usually want row and column indices. This can be accomplished by,
[r,c]=find(a>2);
which returns the row index in variable r and column index in variable c i.e.,
r =
2
2
1
2
c =
1
2
3
3
The first element the find comes across that is greater than two is a(r(1),c(1)), and
so forth.
That’s all you need to know about find.
55
5.2.2 Discontinuous Graphs
Plot the following graph,
½
sin(x), sin(x) > 0
y(x) =
0, sin(x) ≤ 0
56
2. x=linspace(-3/2*pi,3/2*pi,100);
y=tan(x);
y(find(abs(y)>=1e10))=0;
plot(x,y);
Note that the logical statement was reversed in order to use my approach. Also, I
would prefer replacing,
y(find(abs(y)>=1e10))=0;
with
y(find(abs(y)>=1e10))=NaN;
NaN stands for not a number, and is useful in the cropping of plots. M ATLAB ignores
NaNs, and if plotting ordered pairs, ignores the element at the corresponding position
in the other vector. Surfaces may also be cropped using NaN.
57
elseif inc(j)>10000 && inc(j)<=20000
tax(j)=1000+0.20*(inc(j)-10000);
else
tax(j)=3000 + 0.50*(inc(j)-20000);
end;
end;
which returns,
tax =
What did not make sense at first was why tax was added to itself in the third and
fourth lines.
My approach is to use,
tax(intersect(find(inc>10000),find(inc<=20000)))=1000 + ...
0.2.*(inc(intersect(find(inc>10000),find(inc<=20000)))-10000);
tax(find(inc>20000))=3000 + 0.5.*(inc(find(inc>20000))-20000);
Once again, a dirty trick is used. intersect finds the common elements of two
vectors. Can you figure out why this needs to be done. What would happen if you had
three logical statements joined by &?
After looking at this bit of code you are probably thinking that logical vectors are
not such a good idea. My advice is that you should use logical vectors where their use
is obvious to you. . . otherwise stick to elseif ladders!
58
Chapter 6
The chapter introduces function handles. Some individuals consider function han-
dles as being something quite abstract, when in fact they are quite simple and find use
in many areas of M ATLAB programming.
If you have a copy of Essential M ATLAB for Scientists and Engineers, you will notice
that the solution of differential a system of differential equations over a finite time inter-
val is accomplished by the use of an m-file that contains the definition of the system of
DEs. Also, in the section that provides the coding for Newton’s Method, two function
m-files are used viz., one for the function definition and one for the definition of the
derivative of the function. The use of inline functions reduces the number of m-files
required in Newton’s Method, as both the definition of the function and its derivative
can appear in the same m-file. For a system of FODEs1 , a single m-file can be used,
where an inline function represents the FODE system, and also contains the commands
that actually initialize solution of the system.
1 There are cases where it is easier to give function definitions in seperate m-files such as in
solving a system of FODEs with a constraint equation. See the section on First Order Differential
Equations and Runge-Kutta Methods for information.
59
The question does then arise — how does one choose whether to use m-files or
inline functions to solve systems of DEs? As it turns out, one cannot use inline func-
tions when the system has non-constant variable coefficients. In simpler terms, one
may only use inline functions if the system has explicitly specified numeric coefficients (i.e.,
numbers must appear as coefficients) for the variables.
⊲ E XAMPLE
Generate an inline function f (x, y) = x2 + y 2 and find f (x, y) for x = [1, 2, 3, 4] and
y = [5, 6, 7, 8].
Of course the task can be solved very easily using the following code:
Okay, I know that you’re probably thinking that if this problem can be solved so
easily without inline functions, why should we use inline functions? Let’s say that the
problem was modified to read,
Given f (x, y) = x2 +y 2 . Find f (x, y) for x1 = [1, 2, 3, 4] and y1 = [5, 6, 7, 8];
and x2 = [10, 12, 13, 14] and y2 = [15, 26, 57, 78].
x=input(’x ? ’);
y=input(’y ? ’);
disp([’xˆ2+yˆ2= ’, ’ ’, num2str(x.ˆ2+y.ˆ2)]);
The first approach is not the cleverest as if one had 100 sets of data, then it would
take a million years to generate the required output. The second method is good, as it
can be used for any number of data sets —it is probably the method that you thought
of. The problem with this is that you have to create an m-file. Another way of solving
the problem is to use the inline function. We could simply say, (for the first set of
data)
60
f=inline(’x.ˆ2+y.ˆ2’,’x’,’y’);
x=[1 2 3 4];
y=[5 6 7 8];
f(x,y);
TOOL 6.1 Let f (x) be any mathematical expression where x is a vector of variables, and
x = [x1 , x2 , . . . , xn ]. A substitutable mathematical function f (x) may be created by the
use of, f (x1 , x2 , . . . , xn )=inline(f (x, y), x1 , x2 , . . . , xn )
I have found symbolic variables very useful. You can do many things with sym-
bolic expressions e.g. differentiate, integrate and plot symbolic expressions using
ezplot. Look up the relevant sections in M ATLAB help. The only downside of using
symbolic variables is that the Symbolic Math Toolbox is not part of the standard M AT-
LAB package. The University of Kwazulu Natal, for example, does not have the toolbox
installed on the computers in the student LANs. Also, sometimes lecturers will insist
that you not use the Symbolic Math Toolbox in your assignments. The most important
drawback of the Symbolic Math Toolbox is that it cannot be used to generate the SMFs
that the solvers in M ATLAB require. This is because feval cannot be used on symbolic
variables—only eval is valid. I guess that you’re now wondering what feval does.
The next section explains function handles and feval.
61
that an in depth knowledge of function handles is not required at this stage, I will not
present much in this section. By the way, function handles are created with the @ oper-
ator.
⊲ E XAMPLE
Let’s say that you wanted to calculate the square root of a number. Let the number be
x. You could say sqrt(x). The more complex way of doing this is to say,
s=@sqrt;
feval(s,x);
If you know the function that you want to create a handle to, then you can just use the
name of the function prefixed with @, together with feval. An alternative to the code
above is,
feval(@sqrt,x);
Note that if sqrt required two input arguments, then you would have to say some-
thing like feval(s,x,y), where x and y are the two input arguments.
I know that it looks kind of stupid, but function handles are quite useful as ex-
plained next. You probably never will use them, but it is good to know.
⊲ E XAMPLE
In the current directory, create isodd.m as follows,
62
function msg=isodd(x)
if mod(x,2)˜=0
msg=’odd’;
else
msg=’even’;
end;
function msg=isodd(x)
if mod(x,2)˜=0
msg=[num2str(x), ’ is odd’ ];
else
msg=[num2str(x), ’is even’];
end;
Lastly,
• type isodd(3) in the command window. Take careful note of the result.
• type feval(f,3) in the command window. Is this result what you expect? Can
you explain what M ATLAB has done?
nmean can only be called by nsum according to rules of scope. To make nmean
accessible from outside the function m-file, we edit it as follows,
2 This term is used as using function handles in this way does not make the subfunction global—
the subfunction is merely accessible beyond its usual scope. In other words, function handles allow
subfunctions to supersede their usual scope.
63
function sumnums = nsum(x,y)
sumnums=x+y;
nmean=@nmean;
nmean can now be called from anywhere within M ATLAB, provided that the function han-
dle is passed as a parameter to the function or script file in which you want to make use of the
subfunction or declared as global3 , while the subfunction is in focus. We have effectively pro-
moted a subfunction to pseudo-global scope. This technique is useful if you have many
function m-files for a particular task. They can be condensed into fewer m-files. Let’s
say that we wanted to know if the mean of x and y is even or not. Now assume we have
another m-file that checks if the mean is even or odd, and is as follows:
function ismeaneven(mhandle,x,y)
if mod(feval(mhandle,x,y),2)==0
disp(’mean is even’)
else
disp(’mean is odd’);
end
3 The function handle will need to be declared as global from within the function m-file and in
64
Part III
Creating GUIs
65
This part covers the creation of GUIs in M ATLAB. Two examples (one easy and
the other slightly more involved) show how you can quickly create GUIs in M ATLAB.
Makes for good reading, for those that have always wanted to know how to create GUIs
in M ATLAB.
67
68
Chapter 7
I remember the first time that I wanted to use GUIs in M ATLAB . . . it was when I
had to the Oil and Mineral Processing assignment in 2003. The calculations had been
completed in M ATLAB, and the output in the command window was acceptable, but
my friend FJ Nadaraju suggested that we make our solutions to the assignment more
interesting by the using GUIs. So off we went to Venter LAN, where I spent five hours
trying to figure out how to create a GUI in M ATLAB. I knew that GUIDE existed, and
initialised GUIDE. The first thing that I tried to do was to create a command button1
with text on it and then change the text when the user clicked on another command
button that was on the GUI. What a task it proved to be. I eventually managed to do
it, but then I realised that it would probably take me the entire semester to create a GUI
for the assignment. I asked lots of students and a couple of the lecturers to explain how
to create GUIs. Some did not know how to explain, and some simply said that it is
not really necessary to know how to create GUIs. The problem at that time was that I
did not understand function handles and structures. Anyway, we eventually handed our
assignments in without the GUI. Not even the final year students who are excellent at
programming in M ATLAB, knew how to create a GUI application in M ATLAB. Is there
hope for us students that know just a little2 M ATLAB programming? It is my hope that
this chapter helps students to create GUIs in M ATLAB.
1 I will sometimes refer to the M ATLAB uicontrols by their Visual Basic equivalents. The corre-
spondences between VB controls and M ATLAB uicontrols are presented later in this chapter.
2 Even though I have written this book, I still consider myself as being a beginner to program-
ming in M ATLAB. There are billions of things that one can do in M ATLAB, and I don’t think that
anyone can actually become a M ATLAB M ASTER!
69
7.1 M ATLAB GUI Components
M ATLAB has all the usual components that are required for the creation of GUIs. The
axes component is unique to M ATLAB 3 . In M ATLAB, GUI components are referred to
as uicontrols, which is short for user-interface controls.
M ATLAB has the following uicontrols4 :
Push Button The M ATLAB equivalent of a command button in VB.
Toggle Button A push button that looks as if it is pressed when it is selected, and looks
like a normal push button when it is not selected.
Radio Button Note that these are mutually exclusive i.e., only one from a group can be
selected at any one time.
Check Box Not mutually exclusive.
Static Text The equivalent of a label.
Edit Text equivalent of a textbox. Note that scrollable textboxes are not available in
M ATLAB.
Slider
Frame Note that axes cannot be framed with other uicontrols.
List Box
Pop Up Menu
Axes Used to plot data, or to load images
I assume that you have some idea of the uses and the common uses of the above uicon-
trols. If not, then ask someone or take a look at a text book on Visual Basic.
text1.text="Someting"
3 When programming in Visual Basic, one can insert an Excel Chart Control into a GUI. I sup-
pose that this could be regarded as the VB equivalent of the axes component of M ATLAB.
4 A description will follow where necessary. If a description does not follow, then assume that
the operation of the uicontrol is the same as the corresponding control in Visual Basic.
70
to change the text in text box. In this case, the structure is text1 and the field is text.
In M ATLAB, however, the automatically generated structure handles contains links
to all information associated with the GUI e.g., the text that appears in a textbox. It
is therefore, what I like to call a global structure. Note that although I said global, the
structure and its fields are restricted to the GUI i.e., the fields cannot be accessed from
outside the GUI. It therefore makes sense to store variables that we want to keep global
(with respect to the GUI) in the handles structure. By doing this, we are able to share
information between callbacks. Note that properties (e.g., the text in a textbox) is set
using set, and the current state of the property can be retrieved using get. The usage
of these functions shall become apparent via the examples in this chapter.
⊲ E XAMPLE
Design a GUI in M ATLAB, that accepts two numbers and finds the sum of the numbers.
The input must be via edit text uicontrols and the output should appear in an edit text
uicontrol. A push button should be used to initialise the calculation.
Solution The GUI was constructed as follows,
Note that
1. The properties of each UIC is edited via the Property Editor (it’s in the Tools
menu). Note that the properties of the currently selected UIC is displayed in
the property editor.
2. The edit text UIC for the first number has its tag value set to txtnum1, in keeping
with what one would do in Visual Basic. The edit text UIC for the 2nd number
has a tag of txtnum2
3. The edit text UIC for the output of the sum has a tag value of txtsum
4. The push button has a tag value of cmdsum.
5. All edit text UICs have the string value set to blank i.e., nothing
71
In the examples that follow, I will not explicitly state what the property values for
each uicontrol on the GUI are. These shall be quite obvious. The corresponding m-file
for the callbacks is as follows,
72
function varargout = eg1(varargin)
% EG1 M-file for eg1.fig
% EG1, by itself, creates a new EG1 or raises the existing
% singleton*.
%
% H = EG1 returns the handle to a new EG1 or the handle to
% the existing singleton*.
%
% EG1(’CALLBACK’,hObject,eventData,handles,...) calls the local
% function named CALLBACK in EG1.M with the given input arguments.
%
% EG1(’Property’,’Value’,...) creates a new EG1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before eg1_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to eg1_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE’s Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
73
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to eg1 (see VARARGIN)
% --- Outputs from this function are returned to the command line.
function varargout = eg1_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
74
% --- Executes during object creation, after setting all properties.
function txtNum2_CreateFcn(hObject, eventdata, handles)
% hObject handle to txtNum2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
%%%%%% THE LINES THAT ARE DELIMITED IS WHAT HAS BEEN ENTERED IN THE M-FILE%%%%%%%%%%
x=get(handles.txtnum1,’string’);
x=str2double(x);
y=get(handles.txtnum2,’string’);
y=str2double(y);
s=x+y;
set(handles.txtsum,’string’,s);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75
% handles structure with handles and user data (see GUIDATA)
Everything not delimited has been automatically generated by GUIDE. As you can
see, everything is accessed and set using the handles structure {handles. I suppose that
this example is too simple. . . so on to something more interesting. By the way can you
make the push button disappear when it is clicked? All you have to do is say,
set(handles.cmdsum,’visible’,’off’);
In essence, to retrieve the current state of a property of a component5 , all one needs
to say is,
get(handles.uicontrol,’property_to_retrieve’);
76
7.4 UIControls: Useful Properties and Related Stuff
I have already mentioned the UICs that are available in M ATLAB. Most people will only
need to know about a few properties for each UIC. The aim of this section is to mention
the properties that are most commonly used.
get(handles.radio_button_tag,’value’)
which returns either a zero(not selected) or one. A similar thing can be done for check
boxes.
7.4.3 Sliders
The maximum and minimum value of the slider can be set by saying,
Since sliders can be either vertical or horizontal, the increment for both these cases
can be set. When the triangles on the slider control is clicked, it increments or decre-
ments by a fraction of the range defined by the maximum and minimum slider values.
This increment or decrement is known as the slider step As an example,
will cause the slider value to increment or decrement by 0.1 × 10 = 1 each time the
arrow on the slider is clicked, if the slider is horizontal. A increment/decrement of 2 for
a vertical slider is defined by the above commands. It is useful to have a edit text UIC
linked to the slider that indicates the current slider value.
set(handles.list_box_tag,’max’,2);
set(handles.list_box_tag,’min’,0);
77
The issuing of the above commands also defines that the user may select 0, 1 or 2
of the options in the list box. This is fairly obvious as we are setting the maximum and
minimum values.
Data may be output to the list box UIC by,
set(handles.list_box_tag,’string’,output_string,’value’,1)
Note that ’value’,1 must always appear when you want to output to a list box.
This clears the list box and populates the list box with the string that you wish to output.
The items indices of the items currently selected can be retrieved during the list box call
back by,
get(gcbo,’value’);
get(handles.list_box_tag,’value’)
gcbo means get current callback object, which from within the callback for the list box
refers to the list box! A trick to get the actual item that has been selected is to get the
items as a cell array and to then reference the correct position in the cell array6 i.e.,
7.4.6 Axes
If you have more than one axes7 on a GUI, you need to specify the axes on which you
wish to plot data. This can be done by,
Axes(handles.axes_tag)
them from numeric and string arrays. Just as a review, recall that cell arrays allow one to store
mixed data types in a single structure i.e., you can store numerics and strings together without any
problem. Cell arrays can also be used to display multiple lines of text in edit text boxes, and to
display multiple items in pop up menus(drop down menus). Note that the cell arrays must be row
vectors i.e., set up in columns to force multiple lines.
7 This is not a grammatical error. A single UIC of this type is referred to as axes in M ATLAB .
78
7.4.7 Accessing Workspace Variables
You will at some point want to access variables that are in the main (base) M ATLAB
workspace i.e., variables that are visible from the command window scope. You can do
this by using evalin8 . Let’s say that we have variables x and y in the workspace and
want to plot these on an axes(with tag set to axes1) on a GUI. This can be accomplished
by,
GUIx=evalin(’base’,’x’);
GUIy=evalin(’base’,’y’);
axes(handles.axes1); %make sure its the right axes
plot(GUIx,GUIy);
This function is very useful as you can do calculations based on variables that are
in the base workspace from within the scope of the GUI and push the results into the
main workspace so that you can access these results later from the command window.
For example, say we wanted to calculate z where z = x2 + y 2 and store the result in
the main workspace. It is assumed that x and y are defined in the main workspace. We
would need to say,
evalin(’base’,’z=x.ˆ2+y.ˆ2’);
Now, if you have variables within the GUI and want to make these accessible from
the workspace, then you will have to,
1. declare them as global9 from within the GUI.
2. declare them as global from the command window.
Say we had a variable, x, only visible from inside the GUI. To accomplish making x
visible from the command window, we could say,
global x;
evalin(’base’,’global x’);
⊲ E XAMPLE
Design a GUI that,
8 Note that evalin cannot have nested arguments. Also, the expression must be defined in the
scope defined by the first argument i.e., if the expression contains x, y and z and the first argument
is ’base’, then these variables need to be already defined in the base workspace.
9 If you are within a function or GUI and want to make the variable visible from the command
line, then you need to declare the variable as global both in the function or GUI and in the com-
mand window. Note that once a variable is global its value can be changed from within any scope.
This can lead to unexpected errors. If you must use global variables, be very careful.
79
• accepts a vector of x values and a vector of y values
• allows the user to choose the order of polynomial fit via a list box
• plots the input data and shows the fit graphically on an axes UIC on the GUI
Solution
The GUI layout and sample run is as follows,
It should be fairly obvious from the m-file for the GUI, what property values have been
set. The m-file is,
80
%
% *See GUI Options on GUIDE’s Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
81
% --- Outputs from this function are returned to the command line.
function varargout = eg2_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
82
function txty_Callback(hObject, eventdata, handles)
% hObject handle to txty (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
83
n=str2double(itemsel);
p=polyfit(handles.fitdata.x,handles.fitdata.y,n);
handles.fitdata.xi=linspace(min(handles.fitdata.x), ...
max(handles.fitdata.x),100);
handles.fitdata.yi=polyval(p,handles.fitdata.xi);
cla;
hold on;
axes(handles.axes);
plot(handles.fitdata.xi,handles.fitdata.yi,’g-’);
plot(handles.fitdata.x,handles.fitdata.y,’ro’);
hold off;
legend(’fitted data’,’original data’,4);
fresult=cell(4,1);
for j=1:n
fresult{j,1}=num2str(p(j));
end
set(handles.lstp,’string’,fresult,’value’,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc
set(hObject,’BackgroundColor’,’white’);
else
set(hObject,’BackgroundColor’,get(0,’defaultUicontrolBackgroundColor’));
end
Comments on Code
The following should be noted:
1. The items in the popup menu have been created in the opening function. This
function is called just before the GUI is made visible. See comments in the m-file.
This could have been done a bit differently—you could have used the property
editor to set the string of the popup menu.
2. All variables are stored in the handles structure. This is not required in this
case, as the variables are only used within a single function. However, when data
needs to be made available to all functions in the scope of the GUI, then the data
must be stored in the handles structure. You could have also more simply said
something like,
handles.xi=str2num(get(handles.txtx,’string’));
instead of the more elaborate structure presented in the example.
3. By using num2str, the user can input the x and y vectors as comma-separated
lists of space-delimited lists. If you use str2double the code would not work as
expected. See M ATLAB help on these two functions, and as to why they are used
here.
4. cla clears the current axes.
85
86
Part IV
Numerical Methods
87
This part of introduces some basic numerical methods and the use of M ATLAB as
a tool in numeric calculations. Topics such as integration, differentiation, solution of
FODEs are covered. ODE solvers are covered in detail.10
10 Students undertaking the course Applied Computer Methods at The University of Kwazulu
89
90
Chapter 8
91
8.2 Basic Numerical Integration
The integral of a function can be evaluated if we have points over a specified do-
main and the corresponding values in the range of the function. Say we have x =
[1, 2, 3, 4] and y = [2, 4, 6, 8]. It is clear in this case that the relationship between x and y
is y = 2x. Let us evaluate the integral of this function analytically and then numerically.
We shall at the end compare the two results. Although this example is quite trivial, it is
important that you understand the process involved in evaluating the numerical solu-
tion, and also how we link up the algorithm for the solution to its equivalent M ATLAB
coding, as we proceed. Before we proceed, we need to establish how to estimate the
integral of a function given pairs of points 1
TOOL 8.1 Let x and y be abscissa and ordinate vectors,respectively, each with length n. The
integral of the function y = f (x) may be approximated by the Trapezoidal Rule,
n−1
X (yi+1 − yi )
(xi+1 − xi ) (8.1)
2
i=1
or,
n
X (yi − yi−1 )
(xi − xi−1 ) (8.2)
2
i=2
Analytic Solution
• Clearly, x varies from 1 to 4. In M ATLAB, to access the first and last elements
of a 1 dimensional array, we could say x(1) and x(end), respectively. Note
that saying x(end) is interpreted as x(end,1) i.e. the last element in the first
dimension (rows). This gives us the limits of integration.
• We evaluate,
Z4
2x dx
1
x=[1 2 3 4];
y=[2 4 6 8];
npoints=size(x,2) %number of points
differences. The central difference method requires an odd number of points and thus is restrictive.
For this reason it shall not be presented here.
92
yav=0.5.*(y(i+1)+y(i)) %average y value
dA=deltax.*yav;
A=A+dA;
end;
I think that I should mention at this point, that it is very easy to fall into the trap of
writing code such as that above, that does not harness the matrix capabilities of M AT-
LAB . Also, it is possible to vectorize loops. Vectorization of loops is considered an
artform by many as it is sometimes quite difficult to spot how one goes about vector-
izing a particular loop. Say, we wanted to find the sum of the squares of the first 50
counting numbers. We could write,
summ =0;
for num=1:50
sum =summ+numˆ2;
end;
A more elegant way of writing the same thing is using “matrix thinking”,
summ =[];
for num=1:50
summ =[summ numˆ2];
end;
sumsq=sum(summ)
num=1:50
sumsq=sum(n.ˆ2)
There are obviously a few other ways of rewriting the above code. You are probably
wondering why you need to 3 different ways of writing the same bit of code. In my
opinion, the advantage(s) of each of the above versions is, respectively,
• if you have programmed in another language, then the first version is probably
the one that popped up in your mind. I think of this type of programming as be-
ing linear thinking. The advantage of this method is that the solution to a problem
is very apparent, albeit (usually) the version that will take the longest to run.
• matrix thinking is useful in iterative procedures. You shall see this later on. Also,
it sometimes simplifies the solution as M ATLAB has many functions for dealing
with matrices. It is also easier to visualize this solution, in some cases. Note that
this option can consume a large amount of memory.
93
• if you want a program that runs as quickly as possible, then you will want to
vectorize your code. Note that sometimes it is impossible to vectorize code, and
that the increase in performance sometimes adds too much complexity actual
code. Beware, that for loops can only be vectorized if the order of evaluation of
the operations in the loop is irrelevant. Anyway, since the release of M ATLAB 6.5
the gain in performance by vectorization of code has become negligible, as the
looping structures have been optimized greatly.
Another popular method of quadrature is Simpson’s One Third Rule. It is a combination
of the Trapezoidal and Midpoint rules for quadrature. The integral over an interval is
defined as, (where a and b are the end-points of the interval)
N
P xi −xi−1 ¡ ¡ xi +xi−1 ¢¢
6
f (xi ) + f (xi−1 ) + 4f 2
i=2
(8.3)
N −1
P xi+1 −xi ¡ ¡ xi+1 +xi ¢¢
6
f (xi+1 ) + f (xi ) + 4f 2
i=1
The writing of the M ATLAB code shall be left as exercise for the reader.
TOOL 8.2 Given abscissa and ordinate vectors x and y both with n elements, we define the
derivative at xi using left–hand differences as,
yi − yi−1
f ′ (xi ) = (8.4)
xi − xi−1
We are of course aware that the derivative is defined with regard to a particular
point. Sometimes we may wish to evaluate the average gradient over an interval. This
is achieved by extending the above definitions a little.
TOOL 8.3 Given abscissa and ordinate vectors x and y both with n elements, we define the
average derivative over x ∈ (x(1), x(n)) using left–hand differences as,
n
£ ¤ 1 X yi − yi−1
f ′ (x) = (8.6)
av n xi − xi−1
i=2
94
and using right–hand differences as,
n−1
£ ¤ 1 X yi+1 − yi
f ′ (x) = (8.7)
av n xi+1 − xi
i=1
It is important to note that for left–hand difference the summation starts at 2 and
ends at n. If the indexing started at 1, we would have x(0) and y(0), for i = 1. Unlike
a language like, say, Visual Basic, M ATLAB does not allow an Option Base 0 option.
For the right–hand difference, if the indexing ended at n, we would have x(n+1) and
y(n+1), which clearly do not exist, as x and y are of length n.
To end of this chapter, here is the M ATLAB code for finding the derivative vectors
using left–hand differences. The code for right–hand differences is similar and the onus
is on the you to write the code as an exercise.
There are just 2 other items that deserve mention in this chapter viz., fitting polyno-
mials to data and the use of the polyval function. The former will be presented as a
tool.
Lastly, say we wanted to show graphically, the fit achieved by the use of the polyfit
function. We could do this by the following code (note the use of polyval)
95
%assuming that x and y are defined,and that we want a fit of order n
p=polyfit(x,y,n);
%generate a high resolution of x points within the domain of x values
xi=linspace(min(x,2),max(x,2),100);
yi=polyval(p,xi)
plot(x,y,’r’) %generate a red plot
hold(’on’); %do the next plot on the same figure
plot(x,y,’b’);
In the notes by Dr. Rawatlal, mention is made of the function interp1. interp1
performs one-dimensional interpolation and the general form of the function is,
interp1(known\_x,known\_y,’method’)
Note that the method is an optional argument. Linear interpolation is assumed if this
argument is omitted. I like to think of interp1 as being a special case of polyfit i.e.,
we are fitting a polynomial of order one to the given xy data. Thus, it is not a necessity
to use the interp function—it is worth your while to know of it, as it decreases the
number of lines that you have to type. Thus far, I think I have found use for using the
interp in the generation of a smooth curve that passes through a given set of data
points. An example would be the generation of a titration curve, where you do not
know the order of fit. The syntax I used in that case was,
xi=linspace(min(x,2),max(x,2),1000)
yi=interp1(x,y,xi,’splines’)
plot(x,y,xi,yi)
An example of using spline fitting will be presented in the exercises for this chapter.
TOOL 8.5 To calculate an approximation of f ′ (xi ) where f ′ (xi ) does is not defined (but
f ′ (xi+1 ) and f ′ (xi−1 ) are defined), central differences may be applied i.e.,
96
As you can see, the central difference technique is quite restrictive. If the central
difference technique of calculating the derivative at a point fails, then one may use the
definition of the limit. Since this requires choosing a successively smaller value for h, it
requires an iterative approach, where either the number of iterations is constrained or a
tolerance criterion is used. Due to the iterative nature of the process, this method shall
be discussed later.
97
98
Chapter 9
It is a certainty that you will encounter differential equations during your Engineer-
ing course. A famous example in Chemical Engineering is the stirred tank problem,
where one has to determine the mass fraction of salt in a continuously stirred tank as
a function of time, given an initial condition. In applied math, many systems are mod-
elled by differential equations e.g., the differential equation that models a simple har-
monic oscillator (ẍ(t) = ẋ(t)). Of course, one needs to be able to find solutions to such
systems. Sometimes, the analytic solutions can be exceedingly difficult to find, and in
some cases may not even exist. It is, then, imperative to develop numerical methods of
solving such problems.
Since this is not a book on numerical methods, the main focus of this chapter will
be on the use of M ATLAB in the finding numerical solutions to differential equations.
1 Named in honour of two German applied mathematicians, Runga-Kutta Methods refer to mul-
tistage, single-step methods for solving differential equations where y(tn+1 ) is based on a linear
combination of slopes calculated previously. Single-step methods only require y(tn−1 ) i.e. the so-
lution at the previous point to calculate y(tn ). Refer to a book on numerical methods if you want
to see how to use Runga-Kutta Methods to solve differential equations by hand.
99
9.1 Euler’s Method
Euler’s Method may be used to solve First Order Differential Equations(FODEs) where
the initial condition is known. Note that this can be regarded to be a very crude method
of solving FODEs.
dy
TOOL 9.1 Let dx
= f (x, y) where y(0) = y1 is known. The solution of the FODE may be
found by,
yi+1 = y1 + hf (xi , yi )
⊲ E XAMPLE
Given,
dy
= 4x2 , y(0) = 3
dx
Use Euler’s Method to find y(10). Show the solution graphically. Also find the analytic
solution for y(10) and compare the results obtained.
In order to apply Euler’s Method we require h. Let’s assume that n = 40. We also
know that the interval of interest is [0, 10]. The corresponding code is,
f=inline(’4.*x.ˆ2’,’x’,’y’);
nintervals=40;
a=0;
b=10;
h=(b-a)/nintervals;
100
y=zeros(1,nintervals+1);
x=a:h:b;
y0=3;
y(1)=y0;
for i=1:nintervals %perform over each interval
y(i+1)=y(i)+h.*f(x(i),y(i));
end
Comments on Code
1. Note that the y has n + 1 elements. All you have to do is remember that the
additional element in the array is the initial condition.
2. x=a:h:b could be replaced by x=linspace(a,b,n+1). It is a matter of pref-
erence. I prefer the latter method as the dimension of the arrays created can be
seen quite easily
3. Rather than using an inline function, you could have a function m-file that has
the definition for the function. I prefer using inline functions as the the resulting
program can be debugged more easily. Once again, it is a matter of preference.
4. Looking at the code, one gets a sense that the above program would be nicer if
you could replace y=zeros(1,n+1) by y=zeros(1,n). This can be done, but
I advise against it, as it just makes the code more difficult to follow and debug.
All you have to remember is that the number of points equals the number of in-
tervals plus one.
The analytic solution plotted on the same set of axes as the numeric solution is,
Solution of dy/dx=4*x2
1400
1200
1000
800
y
600
400
200
0
0 1 2 3 4 5 6 7 8 9 10
x
101
The analytic solution is 1336 and the numeric solution is 1286. The accuracy of Eu-
ler’s Method can be improved by choosing a higher resolution of points i.e., increasing
the value of n. With n = 400, the numeric solution is 1333—much better than the pre-
vious result. However, it ca be seen that in order to get an acceptable numeric solution,
the value of n needs to be very high. The other downside of Euler’s Method is that we
do not have a way of assessing the accuracy of our result.
TOOL 9.2 3 Given a system of differential equations, with v distinct variables, where the
system of DEs is of mixed order. Let the highest order of differentiation of vi be ni . We may
generate a vector-valued function y(t) with v-by-n elements as follows,
y(t) may then be used to express the mixed-order system as a system of FODEs by differen-
tiating y(t) and making the necessary substitutions from the original system, and substituting
for all vi from 9.1 i.e.,
⊲ E XAMPLE
Given,
ü(t) = u(t)
v̈(t) = v(t)
Express the mixed-order system as a system of first-order differential equations.
Solution We note that there are 2 distinct variables, u and v. We expect y(t) to have
4 entries as both have a highest order of differentiation of two. The system may be
transformed as follows,
2 Stiffness refers to a type of differential equation or system of differential equations where the
solution of interest varies slightly, but at the same time, nearby solutions vary rapidly. In these
cases, small steps need to be taken so that the solution of interest is found.
3 It is customary to represent differential equations with the independent variable as being time,
hence it is so presented here. The presentation can of course be adapted to situations where the
independent variable is not time.
102
y1 (t) u̇(t) ẏ1 (t) u(t) y2 (t)
y2 (t) u(t) ẏ2 (t) u̇(t) y1 (t)
y (t) = v̇(t) ⇒ ẏ (t) = v(t) = y (t)
3 3 4
y4 (t) v(t) ẏ4 (t) v̇(t) y3 (t)
y2 (t)
y1 (t)
ẏ(t) =
y4 (t)
y3 (t)
⊲ E XAMPLE
Given,
ẏ1 = y2 y3
ẏ2 = −y1 y3
ẏ3 = −0.51y1 y2
Solve the system of 1st order differential equations and show the solution graphically.
103
ODE45 solution
1
0.8
0.6
0.4
0.2
0
y
−0.2
−0.4
−0.6
−0.8
−1
0 2 4 6 8 10 12
time
Comments
1. The order of the variables specified for the inline function (and the m-file, see be-
low) needs to be t then y. The ODE solver will not return the correct solution if
the order is swapped. Also, note that t needs to included as a variable (parame-
ter) even though it is not explicitly part of the right hand sides of the differential
equation system.
2. If you do not specify return arguments for the ODE solvers, the results will be
automatically plotted.
3. The return arguments [t y] could have been any other variable names e.g., [a
b]
4. Once again, the inline function need not be used if you have a preference for
creating a seperate m-file that has the definition of the function dydt. You could
have something like,
function f=dydx(t,y)
dydx=[y(2)*y(3);-y(1)*y(3);-0.51*y(1)*y(2)];
The code would have to change accordingly i.e., you will have to use a function
handle. The change would be to remove the line that defines the inline function
and to change the following line to,
[t y]=ode45(@dydx,[0 12],[0 1 1]) % return t and y
Note that you would not have had to have define the function handle, as the
function handle is the same as the name of the function m-file. This is the default
behaviour of M ATLAB.
104
This chapter has dealt with systems where the time interval is known. In many real-
life applications, one needs to find tf inal for a system. This can be done using events
in M ATLAB. It is an option of the ODE solvers. My initial opinion, was that this is too
advanced an issue to be included here, but I think it will come in useful in your final
year of Chemical Engineering. So get ready. . .
⊲ E XAMPLE
Given,
ẍ = ẋ + 2, x(0) = 1 and ẋ(0) = 0
When is x(t) = 10?
Solution
The constraint equation is x(t) − 10 = 0. Transforming to a system of FODEs,
· ¸
y1 (t) + 2
ẏ(t) = , y2 (0) = 1; y1 (0) = 0
y1 (t)
function ydot=f(t,y)
ydot=[y(1)+2;y(1)];
And the function m-file, c.m for the constraint equation is,
function [constrstop,isterminal,direction]=c(t,y)
constrstop=y(2)-10;
isterminal=1;
direction=[];
The m-file that contains the necessary commands to initialize the solution to the prob-
lem is,
opts = odeset(’events’,@c);
y0 = [0; 1];
[t,y,tfinal] = ode45(@f,[0 Inf],y0,opts);
tfinal %displays tfinal in cmd window
4 If you recall, I mentioned that subfunctions could be made global by the use of function han-
dles. Since I have not presented this use of function handles as yet, it is better to use a seperate
m-file for the constraint equation.
105
plot(t,y(:,2),’-’); %plots the solution NB. x(t)=y(2)
xlabel(’t’)
ylabel(’y’)
title(’Event Handling with ODE45’)
text(1.2, 2, [’tfinal = ’ num2str(tfinal)]) %print tfinal on graph
The result returned is 2.01716306153964 which is very close to the result that one gets
after evaluating the analytical solution. I used dsolve to check whether the answer
is acceptable. For t = 2.01716306153964, x(t) gives 9.99961282914699, which is fairly
close to 10. The plot is of the solution is,
6
y
2 tfinal = 2.0172
1
0 0.5 1 1.5 2 2.5
t
Comments
1. For c.m, the output argument variable names isterminal and direction are
fixed i.e., they cannot be named differently. constrstop could be named dif-
ferently. If isterminal is 1 then, the ODE solver stops when the a (i.e., any)
zero of the constraint equation has been found. If isterminal is 0, then the
ODE solver does not stop when a zero has been reached. with direction=[]
or 0, all zeros are computed. If direction=1, then zeros where the constraint
function increases are computed and if If direction=-1, the zeros where the
constraint function decreases are computed.
2. Note that the time interval input for the ODE solver is from 0 to ∞.
The rest of the code is quite self-explanatory. As you can see using M ATLAB to solve
differential equations is not that difficult. . . it is setting up the system of equations from
the description of a situation that usually poses a problem!
106
Bibliography
[1] Hahn BD, Essential M ATLAB for Scientists and Engineers, 3rd ed.,Maskew
Miller Longman: 2002
[2] Rawatlal R, An Introduction to Programming in M ATLAB With Applications in
Chemical Engineering, University of Kwazulu-Natal: April 2003
[3] Online M ATLAB help for M ATLAB version 6.5 release 13
107
Index
, 62 fopen, 26, 27
’, 20 for, 17, 46, 50, 94
*, 16 fprintf, 23, 25, 26
-ascii, 29 gcbo, 78
-double, 29 get, 71, 77
-tab, 29 ginput, 37
.m, 13 guide, 70
/, 16 hold, 34
GUIDE, 69, 70, 76 if, 18, 19, 57
NaN, 57 importdata, 28
ODE45, 99, 103 inline, 59–61
&, 18, 58 inputargs, 14
˜, 18 input, 23, 24
and, 18 interp1, 91, 96
axis, 34 interp, 96
break, 17 intersect, 58
celldisp, 47 int, 61
cellplot, 47 isterminal, 106
cell, 44, 46 i, 17
char, 27 j, 17
children, 36 legend, 35, 36
cla, 85 length, 21
clear all, 15 linspace, 19, 20, 100
continue, 17 load, 26, 29
diff, 61 logical, 53
disp, 23, 24 max, 77
do, 17 meshgrid, 20, 21
dsolve, 106 min, 77
elseif, 19, 53, 58 not, 18
else, 18, 19 num2str, 85
end, 17–19 odeset, 103
eps, 56 ones, 44
evalin, 64, 79 or, 18
eval, 61 otherwise, 18, 19
events, 105 outputargs, 14
ezplot, 33, 61 plot3, 33
ezsurf, 33 plot, 31, 33
fclose, 26 polyfit(x,y,n), 95
feval, 59, 61, 62 polyfit, 91, 95, 96
find, 34, 53–55 polyval, 91, 95
108
repmat, 19, 20
reshape, 46
return, 17
rmfield, 50
save, 26, 29
set, 71, 77
size(m,1), 21
size(m,2), 21
size, 19, 21
sqrt, 62
str2double, 85
str2num, 27
struct, 48
subplot, 34
surf, 33
switch, 18, 19
textread, 28
title, 36
transpose, 19, 20
varargin, 43
varargout, 43
while, 17
xlabel, 36
ylabel, 36
zeros, 44
zlabel, 36
109