form , solving the equation
2.
and finding
a zero
of f(x)
are equivalent .
Write a function that computes f (x) : The function should return
ven x .
3 . Use the built-in function
value of
f (x)
the
at any gi
to fi n d the solution: f z e r o requires
an i

There are two ways a function can be executed, whether it is built-in or user-written:
1. With explicit output : This is the full syntax of calling a function. Both
the output and input list are specified in the call. For example, if the function
definiti

iting formatted binary and text files. The functions tmpported include
open
fclose
f r e ad
fwr i t e
f s c anf
fpr int f
s s c anf
sprint
fgets
fgetl
frewind
wr
f
f s e ek
ftell
f errer
opens an existing file or creates
closes an open file ,
a new
file,

Temperature . t abl e ,
The output file,
is shown here. Note that the data
matrix
t has two rows, whereas the output file writes the matrix in two columns. This is
because t is read columnwise and then written in the format specified (two values
in a row)

the decreasing order of the powers of x, i .e ., a [an an-1 a1 ao] .
Given a data vector x and the coefficients of a polynomial in a row
vector a , the command ypolyval ( a , x) evaluates the polynomial at the data
points
Yi
=
Xi
and generates the values

appropriate to compute y and err, depending
on
what the user specifies
in options. Implement this call sequence with swit ch using
opt ions
as
.
the switch
Program the output y and err to be two column arrays if the user asks
for 'both ' in options or as

We have already discussed numerous features of MATLAB 's language through many E!P
lang
examples
in
the previous sections. You are advised to pay special attention to
proper usage of punctuation marks and different
and operators
delimiters (Appendix A)
)

0
Here V is a matrix containing the
eigenvectors of A as its columns.
For example, the first column of V
- .
is the first eigenvector of
0
5375
A.
0.8429
-0.0247
0
0
9 . 7 96 0
D is a matrix that contains the
eigenvalues of A on its diagonal.
Figure 5 .

doub l e , char , struct ,
cell,
you whether the contents are
or
of what dimension.
If you want to access the contents of a container, you have to use the special
cell- content-indexing-indices enclosed within curly braces. Thus, to see the 3 x 3
random m

t i t le ( ' A funct ion plotted with fplot ' )
In MATLAB , there is a suite of easy function plotters (their names begin with
the prefix ez) that produce 2-D and 3-D plots and contour plots. These functions
require the user to specify the function to be

a nonlinear function. See on-line help for more details.
5.7
Advanced Topics
The topics covered in the preceding sections of this chapter are far from exhaustive
in what you can do readily with MATLAB . These topics have been selected carefully
to introdu

% add legend
6.3.
hold Lhe mou on the legend e
l
The output generated by the preceding script file is shown in Fig.
plot , click and
se
r
you can drag the legend to some other position. Alternatively, you could specify
an option in the legend command to p

however, a major difference;
array to a variable with colon
you cannot assign
range specifier .
all values of
Thus,
a
field
across a
structmn
if FalLSem is a 3 x 1 strncture
array, then
Fall_Sem ( 1 ) . s c ore ( 2 )
Fall_Sem ( 1 ) . s core ( : )
a = FalL

Figure 4.3: Cells
can be displayed schematically with
the function
cellplot.
4.5
Publishing Reports
4.5 Publishing Reports
You can create a fancy, formatted report in HTML , XML , WIE;X, MS Word, or
MS werPoint (MS stuff only on PCs) using MATLAB 's built

want to sort according to the sorted order of
the second column of
that you
B.
You can do this simply with the commands:
[z , j ]
= s ort (B ( : , 2 ) ) ; Bnew B ( j , : )
=
(A,1)
t
For a matrix A, use s ort
Lu ort A by rows and :s ort (A , 2) o :5ort by

double-click (or right-click) on any object in the current plot to edit it . Doubleclicking
on the selected object brings up a property editor window where you can
select and modify the current properties of the
object . Other tools in the menu bar ,
e.g.

want to
keep
adding plots as the data Lecumet:>
available. For example, i
f
a set
of
calculations done in a for loop generll.tP.R vectorR x and y at the end of each loop
and you would like to plot them on the same graph,
1 79
hold
is the way to do it .
18

xlabel ( ' Time ( s e c ) ' ) , ylabel ( ' Pres sure ( t orr ) ' )
% (b) Plot the new curve and the dat a on semilog s cale
lpnew = exp (polyval ( a , tnew) ) ;
semilogy (t , p , ' o ' , tnew , lpnew) , grid
xlabel ( ' Time ( s e c ) ' ) , ylabe l ( ' Pre

fitted line as well as its equation appear in the
figure window. You are now
done. The result is shown in Fig. 5 . 3 .
5 . 2 Curve Fitting and Interpolat ion
350,-,-,-,-.-,-.-.-,-=,
300 y = 3*x - 2.4
'"'
2<)0
150
so L-L-L-L
0 1 0 20 30 40 50 60 70 80 90 1

After a ing two more records,
becomes an array, and
MATLAB now
gives structural information about
the structure.
ex on the structure to
access its elements.
You can use index notation for
the structure as well as its fields.
When no index is sp cifie for

funct i on
[value , i sterminal , dircn] = proj ( t , z , f lag) ;
% PRO J : ODE f o r proj e c t i l e mot i on with event det ect i on
g=
spe c i f y constant
if nargin<3 s e pty f a ) i no
value =
-g] ;
else
swit ch f lag
case ' event s '
value = z ( 3

two in the nput , opt i ons and p l ,
i
p2 , , pn. Here is a brief description
of these
arguments.
It is a six-element-long vector that lists the
performance statistics of
the solven;. The first three elements are (i) number
of successful steps, (ii)
numb

displays the error 11:1essage
message,
and returns the control to the keyboard.
Example:
funct i on c = c r o s sprod ( a , b ) ;
% cros sprod ( a , b ) calculat e s the cro s s product axb .
if nargin =2 % if not two input argument s
error ( ' Sorry , ne

znew
=
int erp2 ( x , y , z , xnew , ynew , me t h o d) .
lfiiiiJ
Three-dimensional analogue
finds Vj at desired (xj , yj , Zj )
vnew
=
of int e rp 1 , i
.e .
,
given Vi at (xi, Yi, zi),
int e rp3 (x , y , z , v , xnew , ynew , znew , me t h o d) .
In ad

Pseudocode: Before you begin writing a function, write a pseudocode. It is
essentially the entire function coded in English
)
(as opposed to the programming
language with clear control flow. Think about the logical structure and
sequence of computations,

matlab.mat.
ASCII datafles can also be loaded into the MATLAB
workspace with the l o ad
command provided the datafle contains only a rectangular matrix of numbers. For
more information, see the on-line help on l o ad. To read and write ASCII fles with
spe

shows some intermediate calculations (see on-line help) a t each step. The
optional arguments p 1 , p 2 , etc. , are simply passed on to the user-defined
x.
function as input arguments in addition to
The steps involved i n numerical integration using thes

funct i on d t ode l ( t , x) ;
funct i on
x
o
% ODE l :
=
to c ompute the derivat ive xdot
% at given t and
x.
% Call synt ax : xdot = ode 1 ( t , x) ;
% - - -
xdot k*x c * s in (t ) ;
+
=
xdo t ,
This , however, won't work. In order for ode ! to compute

A
structure is
like a record. One record (structure) can contain information
(data) about various things under different heads
(fields) .
For example, you could
maintain a record book with one page devoted to each of your relatives. You
could list informa

x
[ yJ In fact , MATLAB uses a combination of both the relative tolerance and the
absolute tolerance. For example, in ODE solvers, the estimated local error
solution component
ei
Yi at any step is required to satisfy the relationship
[ Yi [ , A bs Tol) .