376
B. Glossary of Useful Terms
isempty Checks whether a variable is empty.
#
if isempty(x)
disp(x is empty)
end
"
!
This means that the array has either zero rows or zero columns but still
exists; to check the existence of a variable use the exist comman
5.2 Saving and Reading Data
135
Grand total is 16 elements using 128 bytes
or if we just wanted the names of the variables we could use:
who
Your variables are:
a
a1
b
c
z
If we only wanted information about one variable we could just use the construction
132
4. Root Finding
Task 4.12 Using the command roots calculate the solutions of the equations
x3 + x2 + x + 1 = 0 and 2x + x5 + x2 = 4.
Here you need to be careful that the coecients of the polynomials are entered
correctly as vectors.
Task 4.13 Using th
4.9 Tasks
131
Task 4.5 Modify the code eqn.m (page 112) to determine the zeros of the function f (x) = 2 cos x sin x between 2 and 2 using the xed point method.
(You may need to use a plot of the function to identify initial guesses for the
values.)
Task
5
Interpolation and Extrapolation
5.1 Introduction
We shall now consider the problem where we know the values of a function at a
certain set of predetermined points, (x1 , f1 ), (x2 , f2 ), through to (xN , fN ).
The question to be answered in this chapte
134
5. Interpolation and Extrapolation
If we presume we are given the original data set, shown here as circles, we
might join them together with straight lines (as shown). In order to work out
the value at x = 3.5 say, we would draw a vertical line and se
130
4. Root Finding
function [val] = myfunc(x)
val = x-x.2.*sin(x);
and then use the inline command fzero(myfunc,1). This gives
> fzero(myfunc,1)
Zero found in the interval: [0.84, 1.16].
ans =
1.1142
4.9 Tasks
Task 4.1 Modify the codes userfn.m and testp
128
4. Root Finding
4.8 MATLAB Routines for Finding Zeros
4.8.1 Roots of a Polynomial
As we have seen MATLAB has a specic command for nding the roots of a
polynomial, namely roots. The coecients of the polynomial are placed in a
vector c and the routine r
126
4. Root Finding
Example 4.5 Determine the zeros of the function f : R2 R2 such that
f (x) = (f, g) = (x2 y 2 + 3, (x + 2)2 y)
where we have used (x1 , x2 ) = (x, y). In this case the matrix A is
f
x
g
x
f
y
g
y
=
2x
2y
2(x + 2) 1
.
The code for solvin
4.7 Zeros of Higher-Dimensional Functions(*)
127
> y
y =
4.4623
-2.1546 + 1.1417i
-2.1546 - 1.1417i
1.8469
Notice we nd another real root and pairs of complex answers.
In this example we have just performed 10 iterations: however in reality
we would wish
4.7 Zeros of Higher-Dimensional Functions(*)
125
which calls
(
1
function [f,fp,fpp] = fun2(x);
f = x.4+3*x.3-12*x.2-20*x+48;
fp = 4*x.3+9*x.2-24*x-20;
fpp = 12*x.2+18*x-24;
0
)
We note that the above code is not limited to real numbers. In this example
w
136
5. Interpolation and Extrapolation
9
% Save the variables a & a1 to the
% file session_vars.mat
save session_vars a a1
6
% Save all variables starting with b
% to session_vars.mat
save session_vars b*
% Append all variables starting with ca
% to the f
5.2 Saving and Reading Data
1.0000000e+000
1.0000000e+000
2.0000000e+000
4.0000000e+000
137
3.0000000e+000
9.0000000e+000
4.0000000e+000
1.6000000e+001
(this was created using the code: x = 1:4; y = x.2; save bob1.dat x
y -ascii). This contains two rows v
378
B. Glossary of Useful Terms
U =
1
0
0
2
5
0
3
5
2
To obtain the pivoting information we would have used [L,U,P] = lu(A).
In this case P is the three-by-three identity.
magic Returns a magic square:
> magic(4)
ans =
16
5
9
4
2
11
7
14
3
10
6
15
13
8
12
B.4 General MATLAB Commands
375
helpdesk Provides access to the web-based help facility.
hex2dec Converts a hexadecimal number to a decimal: the input needs to be
a string; a=FF0; hex2dec(a). See also dec2hex.
hilb(n) This sets up the n-by-n Hilbert matri
B.4 General MATLAB Commands
377
load Reads in data, either directly into variable load data (which loads
data.mat) or load data.dat which returns a matrix data.
log Natural logarithms.
x = [1 exp(1) exp(2)]
y = log(x)
This would be written mathematically
374
B. Glossary of Useful Terms
> B
B =
(1,1)
(3,1)
(2,2)
(1,3)
1
-1
-2
2
and C is back to A again.
function Occurs at the start of a function denition;
function [v1,v2]=testfn(in1,in2,in3).
fzero Determines one zero of the function passed as the rst argu
372
B. Glossary of Useful Terms
end This command ends all of the loop structures and for each starting argument there must be a corresponding end (used with for, if, switch and
while).
eps The distance from 1.0 to the next largest oating point number. So
B.4 General MATLAB Commands
x = transpose(1:6);
y = flipud(x)
373
sets y to be [6; 5; 4; 3; 2; 1]. This also works with matrices.
floor Rounds down to the integer below, also works for matrices.
fmins This uses the NelderMead simplex (direct search) metho
B.4 General MATLAB Commands
369
#
x = 0:pi/20:pi;
y = cos(x)
z = acos(y)
"
!
cosh Hyperbolic cosine, equal to (ex + ex )/2.
cov Gives the covariance of two sets of data.
> x = [ 1 2 3 4 5 6];
> y = [ 3 4 2 1 4 5];
> cov(x,y)
ans =
3.5000
0.9000
0.9000
2.1
370
B. Glossary of Useful Terms
diag Sets one of the diagonals of a matrix. The diagonals are referred to as: 0
the leading diagonal, which runs from top left to bottom right. The superdiagonals 1, 2, etc are above the leading diagonal and the sub-diagona
B.4 General MATLAB Commands
367
The parameter is set to be 1/2 here and in fact J 1 (x) = sin x/ x.
2
There are other Bessel functions: bessely(nu,x), besseli(nu,x) and
besselk(nu,x).
break Stop current level of execution and go back to the previous level
368
B. Glossary of Useful Terms
cond Gives the condition number of a matrix, that is the ratio of its largest
and smallest eigenvalues. This reects the ease with which the matrix can
be inverted, amongst other things.
A = [100 0; 0 0.1];
cond(A)
This matr
124
4. Root Finding
where q() = 0 (since the root is of order n). Let us now consider the derivative
of p(x) at x = , so that
dp
n1
n dq
= n (x )
q(x) + (x )
dx
dx
so evaluating at x = , we nd
dp
dx
= 0.
x=
This means the methods which use the derivative
4.6 Repeated Roots of Functions
123
Example 4.4 Using the NewtonRaphson routine determine the zero of the
function f (x) = ex e2x + 1. This can be done by setting up the functions
function [value] = func(x)
value = exp(x)-exp(-2*x)+1;
function [value] = f
Solution Assignment: Due Date:
Dung Nguyen 10 November 04, 2009
Class diagrams
Day
- name : String - number : int <Constructors> + Day() + Day(name: String, number : int) <Get Methods> + getName() : String + getNumber() : int <Set methods> + setName( name
Solution Assignment: Due Date:
Dung Nguyen 08 October 21, 2009
Analysis
Inputs: - Type of pyramid. There are two types of pyramid: have blank space or not - Number of lines - Set of characters Output: Print a pyramid of characters. These characters are pr