Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
B.4 General MATLAB Commands
379
n
mean Calculates the mean of a set of data, 1/n
xi .
i=1
mean([1 2 3 4 5 6 7])
This can also be used on matrices:
#
a = [1 2 3; 4 5 6];
mean(a,1)
mean(a,2)
"
!
the former giving the averages of the columns and the latter t
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
C.4 Solutions for Tasks from Chapter 4
409
(
1
b = input(Value of b );
x = 10:0.1:10;
f = x.2+b*x+1;
plot(x,f)
0
)
Notice that we have chosen the range [10, 10] but we could have used knowledge
of the structure of the function to make sure that both root
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
C.3 Solutions for Tasks from Chapter 3
407
4
3
2
1
0
1
2
3
4
3
2
1
0
1
2
3
and the choice of 1015 is in a sense arbitrary, but reects how close we come
to the singularities.
Solution 3.18 The rst line contains a spelling mistake: the command should
be lin
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
C.3 Solutions for Tasks from Chapter 3
9
sumln(1) = 1;
for n = 1:1000
sumln(n+1) = sumln(n)+(1)(n+1)/(n+1);
end
403
6
sum2(1) = 1/2;
for n = 1:1000
sum2(n+1) = sum2(n)+1/(n+1)*(n+2);
end
8
7
where we have used the terms in the summations evaluated at th
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
C.3 Solutions for Tasks from Chapter 3
9
function [val] = isdigit(charac)
lchar = charac(1);
if lchar>=0 & lchar <=9
val = 1;
else
val = 0;
end
8
We can now use the functions:
9
msg = Please enter a letter and a digit ;
405
6
7
6
str = input(msg,s);
while
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
402
C. Solutions to Tasks
cos(v)cosx
ans =
0
0
0
0
This means that the dierence between the MATLAB and the series answers
are less than eps.
Solution 3.6 We can use code which performs the summations separately for
dierent values of N or note that SN +1
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
404
C. Solutions to Tasks
Solution 3.13 We have
1
n = 1:50;
f = n.3n.2+40;
ii = find(f > 1000 & mod(n,3) = 0);
n(ii)
0
(
)
Solution 3.14 The key here is to start with the string of the rst ten letters,
namely abcdefghij. We ask the user to enter the rst
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
410
C. Solutions to Tasks
Considering b > 2 the modulus of this function is greater than one for the
root corresponding to the negative sign and less than one for the root with the
positive sign. For the other option we nd that
g (x) =
b
2
(bx + 1)
.
Wh
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
C.4 Solutions for Tasks from Chapter 4
411
Solution 4.8 The function f (x) = cos 3x has three zeros in the range 0 to .
Using the full range, we encounter the left root /6 (in fact we should check
whether f (b + a)/2) is smaller than the tolerance, which
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
A. A Mathematical Introduction to Matrices
325
Of all the operations which can be performed on matrices one of the simplest
is that of transposition (or taking the transpose of a matrix); this operation is
usually denoted by a subscript T or a prime . If
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
326
A. A Mathematical Introduction to Matrices
The usual arithmetic operations of addition, subtraction and multiplication
also apply to matrices. However, there are now several additional rules (or
constraints) under which these operations can be perform
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
324
A. A Mathematical Introduction to Matrices
one row) then we refer to it as a row vector. A scalar is simply a matrix in which
both n and m are equal to one (that is, a onebyone matrix). Throughout this
text we will adopt the universal convection tha
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
A
A Mathematical Introduction to Matrices
Matrices are objects which have special properties and there are a number of
rules which must be adhered to in order to manipulate them in a consistent
(and correct) manner. A matrix can most readily be dened as a
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
322
9. Simulations and Random Numbers
for a variety of values of using the code:
9
cosa = 0.34;
sina = sqrt(1cosa2);
ii = 0;
for st = 0.5:0.05:0.5
6
x = st; y = st;
for its = 1:1000
ii = ii+1;
xn = cosa*xsina*y+x2*sina;
yn = sina*x+cosa*yx2*cosa;
x =
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
9.5 Tasks
321
Task 9.5 By experimenting with the value of mu in the above code identify a
value for which there is a real period 2 cycle and then show that the points occur
in pairs using the code map.m.
In order to nd roots of higher order it is obviousl
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
408
C. Solutions to Tasks
9
x = linspace(4,4);
N = length(x);
for j = 1:N
if x(j) >= 0 & x(j) <= 1
f(j) = x(j);
elseif x(j) >= 1 & x(j) < 2
f(j) = 2  x(j);
else
f(j) = 0;
end
end
8
6
7
C.4 Solutions for Tasks from Chapter 4
Solution 4.1 The solution to
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
C.3 Solutions for Tasks from Chapter 3
401
5
2
s = 0;
for i = 1:100
s = s+1/i2;
end
disp([Required value is num2str(s)])
4
3
Notice that we have changed the command int2str to num2str: this is because
the answer is no longer an integer.
Solution 3.2 This
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
C.2 Solutions for Tasks from Chapter 2
9
w = ones(1,9);
399
6
w(1) = 1;
for j = 1:4
w(2*j) = 3;
w(2*j+1) = 2*j+1;
end
8
7
Solution 2.11
(a) Here the conversion factor will be worked out by remembering that there are
1760 yards in a mile, a yard is 36 inch
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
B.4 General MATLAB Commands
387
warning Allows codes to issue warnings when there may be problems. Is also
used to aect how the system issues warnings.
which Tells a user where a MATLAB le can be found, and which version is
going to be run.
while Denes th
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
392
C. Solutions to Tasks
9
x = 1:0.1:2;
6
% Part 1
y = x.3+3*x.2+1;
% Part 2
y = sin(x.2);
% Part 3
y = (sin(x).2;
% Part 4
y = sin(2*x)+x.*cos(4*x);
% Part 5
y = x./(x.2+1);
% Part 6
y = cos(x)./(1+sin(x);
% Part 7
y = 1./x+x.3./(x.4+5*x.*sin(x);
8
Solu
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
B.4 General MATLAB Commands
#
x = [1 2 3];
y = [4 5 2];
p = polyfit(x,y,2);
"
383
!
Note that the coecients are returned with the one corresponding to the
largest power rst. This gives the quadratic 4x2 + 13x 5: however
polyfit(x,y,1) on the same data gi
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
B.4 General MATLAB Commands
381
We can set tolerances, amongst other options. Consider the system of differential equations
dx
=ty
dt
dy
= x,
dt
subject to the initial conditions x(0) = y(0) = 0. We introduce the vector
y = (x(t), y(t)T , and as such we m
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
382
B. Glossary of Useful Terms
The upper line is x(t) and the lower one is y(t).
ones Sets up a matrix full of ones. ones(n,m) gives A which is an nbym
matrix for which ai,j = 1. ones(n) gives a square matrix (nbyn).
otherwise If none of the cases co
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
380
B. Glossary of Useful Terms
num2str Converts a number to a string with a specied number of digits;
num2str(pi,4). This can also be used without specifying the number of
digits (which uses the default corresponding to four places after the decimal
poin
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
B.4 General MATLAB Commands
1
s = rand(100,1);
a = reshape(s,10,10);
b = [1 3 4; 2 3 4];
d = reshape(b,1,6);
0
385
(
)
This gives d = [1 2 3 4 4]; the elements of b are read columnwise.
roots This gives the roots of the polynomial which is passed to the
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
384
B. Glossary of Useful Terms
randperm Generates a random permutation of a list of objects: To rearrange
the letters of our names:
9
6
s = otto denier;
for its = 1:20
l = randperm(11);
s(l)
pause
end
8
7
rank This yields the rank of a set of linear equa
Physical Educ Activity Program, Physical Fitness Exercise To Music
764 225

Fall 2014
C
Solutions to Tasks
Please note that these solutions are given for guidance only and are by no means
unique. At the outset we shall give MATLAB output: however subsequently we
shall merely give the commands which can be used to solve the problems.
C.1 So