4.3 Fixed Point Iteration
109
0.01
0.008
0.006
0.004
0.002
0
0.002
0.004
0.006
0.008
0.01
1.16
1.165
1.17
1.175
1.18
1.185
1.19
after successive clicks of the left mouse button. This allows us to obtain a better
estimate of the root, namely 1.175. You sho
96
3. Loops and Conditional Statements
The command input allows us to input a matrix, which would cause our
program to fail (in fact the routine factor issues an error message). We could
incorporate a check to ensure that what is entered is a scalar. This
94
3. Loops and Conditional Statements
9
6
x = (1:40)/10;
g = (x-1).*(x-2).*(x-3);
izero = find(g=0);
ii = find(g=0);
f(izero) = NaN;
f(ii) = 1./g(ii);
8
7
This allows us to plot the function whilst missing out the innite parts. This
would be accomplished
3.7 Error Checking
95
allow us to either warn the user of a problem or actually stop the code because of an irretrievable problem, respectively. Both the commands warning
and error are used with an argument, which is displayed when the command
is encounte
3.6 MATLAB Specic Commands
93
We briey dissect this code.
In the rst line set up a vector which runs from one to twenty;
the second line calculates the sine of the elements in vector ii and stores the
result in the vector f;
in the third line we use fi
92
3. Loops and Conditional Statements
This checks to see if xn+1 = xn to within a certain tolerance. This procedure
gives a reasonable approximation to 3 and would improve if the tolerance
(1e-5) was reduced.
3.5.1 The break Command
A command which is of
44
2. Writing Scripts and Functions
> y = evaluate_poly(x);
> plot(x,y)
which gives
90
80
70
60
50
40
30
20
10
0
5
4
3
2
1
0
1
2
3
4
5
2.2.2 More on Plotting
At this point we briey return to the problem of generating plots using MATLAB. The simple code gi
2.2 Plotting Simple Functions
45
third argument of the plot command, such as in plot(x,y,r.). The string
which is passed as the third argument conveys information as to how the plot
should be presented: here the rst character r is the colour (red) and the
2.2 Plotting Simple Functions
43
This general recursive form goes under the name of Horners method. With
the function evaluated the answer is displayed, using the function num2str
(number-to-string) which takes as input a number and returns a character
st
98
3. Loops and Conditional Statements
Task 3.4 Construct a program to display the values of the function f (x) =
x2 + 1 for x = 0 to x = in steps of /4. The key here is to set up an array
of points from a to b in steps of h (you can either use the colon
3.8 Tasks
97
Another very useful command in this context is exist. This can be used to
check whether the requisite variables exist. This is particularly useful if input
is required from another code.
Example 3.22 Let us consider this header for a code:
5
4.2 Initial Estimates
107
6
4
2
0
2
4
6
8
10
0
1
2
3
4
5
6
7
8
9
10
We can see that there are zeros at 0 and near 1.2, 2.5, 6.7 and 9.3. We
could use the above data set but it would seem sensible to consider each point
separately. For this purpose we use
106
4. Root Finding
9
6
% Modified testplotm.m
x = -2.0:0.01:2.0;
y = feval(userfn,x,2,2);
disp(Click the mouse near the zero)
disp(and when you have finished press)
disp(the return key)
plot(x,y)
grid on
[xvalues,yvalues] = ginput
[yy,ii] = min(abs(yvalu
104
4. Root Finding
4.2 Initial Estimates
In order to determine a root it is usually essential to have an initial estimate
of its value. In some cases you may have more than one root (or none) and
you wish to identify which one you are concerned with. The
4.2 Initial Estimates
105
4
3
2
1
0
1
2
3
4
2
1.5
1
0.5
0
0.5
1
1.5
2
From this initial gure it is not that clear where the zeros lie. There appears
to be one at the origin, which we can see straight away from consideration of
the function, f (x) = x 2 si
4
Root Finding
4.1 Introduction
In many problems we are required to determine when a function is zero. It
should have become clear even for relatively simple examples there were many
subcases. It is our intention to discuss methods which are in some sense
100
3. Loops and Conditional Statements
Task 3.15 (*) Write a code which allows the user to input a two character
string, the rst being a letter and the second being a digit. (Note that to check
if a character is a letter we can use inequalities on string
3.8 Tasks
99
Task 3.10 Write down a logical expression which is true for even values of n.
Extend this to only be true for even values of n greater than 20. (Hint: Try
using the MATLAB command mod: for information on the command type help
mod.)
Task 3.11
3.8 Tasks
101
Correct the code so that it accomplishes this. This can be checked by using the
command plot(x,f) and comparing the gure to what you expect the function
to look like.
42
2. Writing Scripts and Functions
Comments at the start of a code have a special signicance in that they are
used by MATLAB to provide the entry for the help manual for that particular
script. The manual entry can then be accessed by typing help quadrat
2.2 Plotting Simple Functions
41
meshgrid sets up a grid of points by generating copies of the x and y values;
see help meshgrid for details of this command. To further explore the potential
of MATLABs plotting facilities see the MATLAB demo topics Visual
2
Writing Scripts and Functions
2.1 Creating Scripts and Functions
With the preliminaries out of the way we now turn our attention to actually
using MATLAB by writing a short piece of code. Most of the commands in
this section have purposely been written
2.1 Creating Scripts and Functions
29
For instance myfile.1.m and 2power.m are not viable lenames (good alternatives would be myfile 1.m and twopower.m respectively). Filenames also
have the same restrictions which we met earlier for variable names (see p
26
1. Simple Calculations with MATLAB
for x [0, 1] (using 200 steps). Correct the code and check this by evaluating
the function at x = 1 using f(200) which should be 1/6.
9
6
x = linspace(0,1);
clear all
g = x3+1;
H = x+2;
z = x.2;
y = cos xpi;
f = y*z/g
1.8 Tasks
25
of rationals you may well have left the format as rat, so go back to the default
by typing format).
Task 1.4 Explore the use of the functions round, ceil, floor and fix for the
values x = 0.3, x = 1/3, x = 0.5, x = 1/2, x = 1.65 and x = 1.34.
24
1. Simple Calculations with MATLAB
The expression end is very useful at this point, since it can be used to refer
to the nal element within an array. In the previous example f(end) gives the
value of f(21) since the length of f is 21.
Example 1.21 We n
1.7 Accessing Elements of Arrays
23
in this case [1 20]; this will be particularly useful when we consider matrices
in due course.
1.7 Accessing Elements of Arrays
This is one of the most important ideas in MATLAB and other programming
languages which is
~ d rh vhd
feWppYW
Iwi i Qb
epjhRjIYuh g heeYV
~|z y ud h
PYBcfw_veYhPspeQYV x
V r u d rh Q
qwudvvYHttupte
tBphxwvtsYVqph"xeQpeQe6h g ph g dwsrvqdPQWphRexc`
Q r i g f rhh fQ p Q Q tV r p r tIi vd
Qb nV r
eesV omutsV
r i fQd i hfQd
7jXi g lVkXRejbp g 7Re