%%%%----- Use lines after here ....  line=306 part of \section{Using a script file --- the create, edit, run cycle} starting on page 4


format compact
A=[4, 1, 1; 1, 4, 1; 1, 1, 4]
y=[1; 2; 3]
b=A*y
x=A\b


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=380 part of \section{Scalars, vectors and matrices of type double} starting on page 6


clear;
p=2*pi;
r=[1, 2];
x=r';
A=[1, 2, 3; 4, 5, 6; 7, 8, 9];
whos


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=532 part of \subsection{{\tt if} and {\tt if--else} statements} starting on page 8


if t<0 || t>=2*pi
    t=mod(t, 2*pi);
end


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=571 part of \subsection{{\tt if} and {\tt if--else} statements} starting on page 8


d=b^2-4*a*c;
sd=sqrt(abs(d));
if d>=0
    x1=(-b-sd)/(2*a);
    x2=(-b+sd)/(2*a);
else
    disp('quadratic has complex roots')
end


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=633 part of \subsection{{\tt for}--loops} starting on page 10


for k=1:8
  fprintf('%d, %2d, %3d\n', k, k^2, k^3);
end


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=668 part of \subsection{{\tt for}--loops} starting on page 10


for k=1:2:8
   fprintf('%d, %2d, %3d\n', k, k^2, k^3);
end
fprintf('\n')
for k=8:-3:1
   fprintf('%d, %2d, %3d\n', k, k^2, k^3);
end


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=701 part of \subsection{{\tt for}--loops} starting on page 10


s=0;
for p=[2, 3, 5, 7, 11, 13, 17, 19];
  s=s+p^2;
end


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=774 part of \subsection{{\tt break} and {\tt continue} statements within loops} starting on page 12


format long
x=1;
for n=1:20
  d=(x^2-2)/(2*x);
  x=x-d;
  fprintf('n=%2d, x=%21.18f, d=%10.2e\n', n, x, d);
  if abs(d)<1e-12
      break
  end
end


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=820 part of \subsection{{\tt break} and {\tt continue} statements within loops} starting on page 12




%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=872 part of \subsection{{\tt break} and {\tt continue} statements within loops} starting on page 12


rand('seed', 1);
count=0;
for k=1:500
  % generate the random matrix and get the eigenvalues
  A=randi([-5, 5], 2, 2);  
  mu=eig(A);
  
  % do not consider further if the required conditions 
  % are not met
  if abs(imag(mu(1)))>0 || abs(imag(mu(2)))>0 ...
     || mu(1)<0 || mu(2)<0
      continue;
  end
  
  % display what has been found and stop when we have 
  % 3 matrices
  fprintf('The next matrix has positive eigenvalues.\n');
  A
  fprintf('The eigenvalues are %e and %e\n', mu(1), mu(2));
  count=count+1;
  if count==3
      break;
  end
end
fprintf('count=%d after %d attempts\n', count, k);


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=986 part of \subsection{Matrix operations}


A=[1 2 3
   3 2 1
   4 0 2];
x=[1; 1; 1];
b=A*x
r=(x'*b)/(x'*x)
C=A^2


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1030 part of \subsection{Matrix operations}


A=[4, 1, 1;
   1, 4, 1;
   1, 1, 4];
p=poly(A)
E=p(1)*A^3+p(2)*A^2+p(3)*A+p(4)*eye(3)


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1081 part of \subsection{Matrix operations}


format compact
A=[4, 1, 1;
   1, 4, 1;
   1, 1, 4];
B=A-6
C=A-6*eye(3)


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1227 part of \subsection{Entry-wise operations} starting on page 18


t=linspace(0, 2*pi, 500);
y=sin(t)+0.3*exp(-0.1*t.^2).*sin(10*t);
figure(2)
plot(t, y)


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1247 part of \subsection{Entry-wise operations} starting on page 18


z=zeros(1, 500);
for k=1:500
  z(k)=sin(t(k))+0.3*exp(-0.1*t(k)^2)*sin(10*t(k));
end


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1344 part of \section{Function files and anonymous (1-line) functions} starting on page 20


function y=f7(x)
y=sin(x)+sin(3*x)/3+sin(5*x)/5+sin(7*x)/7;


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1355 part of \section{Function files and anonymous (1-line) functions} starting on page 20


x=linspace(0, 3*pi, 300);
figure(3)
plot(x, f7(x));


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1372 part of \section{Function files and anonymous (1-line) functions} starting on page 20


f7 =@(x) sin(x)+sin(3*x)/3+sin(5*x)/5+sin(7*x)/7;
x=linspace(0, 3*pi, 300);
figure(4)
plot(x, f7(x));


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1387 part of \section{Function files and anonymous (1-line) functions} starting on page 20


function [x1, x2]=solve_quad(a, b, c)
% [x1, x2]=solve_quad(a, b, c) 
% Output arguments x1 and x2 solve a*x^2+b*x+c=0

d=b^2-4*a*c;
sd=sqrt(abs(d));
if d>=0
    x1=(-b-sd)/(2*a);
    x2=(-b+sd)/(2*a);
else
    x1=(-b-1i*sd)/(2*a);
    x2=(-b+1i*sd)/(2*a);
end


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1409 part of \section{Function files and anonymous (1-line) functions} starting on page 20


[x1a, x2a]=solve_quad(1, 3, 2)
[x1b, x2b]=solve_quad(1, 1, 1)
[x1c, x2c]=solve_quad(1, 4, 1)


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1462 part of \subsection{The {\tt plot} and the {\tt figure} commands} starting on page 22


x=linspace(0, 2*pi, 200);
figure(20)
plot(x, sin(2*x));

figure(21)
plot(x, sin(4*x));

figure(22)
plot(x, sin(2*x), '--', x, sin(4*x))


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1486 part of \subsection{Using {\tt clf}, {\tt hold on} and {\tt hold off}} starting on page 22


% create uniformly spaced points on the unit circle
n=12;
t=1:n;
x=cos(2*pi*t/n);
y=sin(2*pi*t/n);

% join every point with every other point
figure(10)
clf
hold on
for i=1:n
  for j=1:i-1
    plot([x(i), x(j)], [y(i), y(j)]);
  end
end
axis equal
axis off
hold off


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1530 part of \subsection{Using {\tt clf}, {\tt hold on} and {\tt hold off}} starting on page 22


% create the vectors for exp(x)
x1=linspace(-2, 2);
y1=exp(x1);

% create the vectors for log(x)
x2=linspace(exp(-2), exp(2));
y2=log(x2);

% create the data for the y=x line
x3=[-2, exp(2)];

% plot all the curves together with an increased line width
figure(12)
plot(x1, y1, x2, y2, x3, x3, '--', 'LineWidth', 3)

% increase the size of the numbers on the axis
set(gca, 'FontSize', 14);

% add other things at 16pt
text(2.2, exp(2), 'y=exp(x)', 'FontSize', 16)
text(exp(1.6), 1.2, 'y=log(x)', 'FontSize', 16)
xlabel('x', 'FontSize', 16)
ylabel('y', 'FontSize', 16)
title('Plots of exp(x) and log(x)', 'FontSize', 16)


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1580 part of \subsection{Using {\tt clf}, {\tt hold on} and {\tt hold off}} starting on page 22


function p=line_intersect(r1, r2, r3, r4)
%%function p=line_intersect(r1, r2, r3, r4)
% p is the point of interection of the line r1 to r2 with r3 to r4

A=[r2(:)-r1(:), r3(:)-r4(:)];
b=r3(:)-r1(:);
c=A\b;
p=r1(:)+c(1)*(r2(:)-r1(:));


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1669 part of \subsection{Shading in a region, the {\tt fill} function} starting on page 25


t=linspace(0, 2*pi, 201);
x=cos(t);
y=sin(t);
figure(91)
fill(x, y, 'b')
axis equal


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1691 part of \subsection{Shading in a region, the {\tt fill} function} starting on page 25


% create the points for any of the circles
t=linspace(0, 2*pi, 201);
r=1.5;
xc=r*cos(t);
yc=r*sin(t);

% create the points for the outer rectangle
xouter=[-2, 2, 2, -2, -2];
youter=[-2, -2, 12, 12, -2];

% create the points for the positions with 
% xp() for 4 possibilites and yp() for the red, amber, green positions
xp=[0, 6, 12, 18];
yp=[0, 5, 10];

% define the colours in rgb form if necessary
amber=[255, 151, 0]/255;
gray=[17, 17, 17]/255;
col={'g', amber, 'r'};

% start the figure with equal scales and with no axis shown
figure(101)
clf
axis equal
axis off
hold on

% just red case
fill(xp(1)+xouter, youter, gray);
fill(xp(1)+xc, yp(3)+yc, col{3});

% just red and amber together
fill(xp(2)+xouter, youter, gray);
fill(xp(2)+xc, yp(2)+yc, col{2});
fill(xp(2)+xc, yp(3)+yc, col{3});

% just green case
fill(xp(3)+xouter, youter, gray);
fill(xp(3)+xc, yp(1)+yc, col{1});

% just amber case
fill(xp(4)+xouter, youter, gray);
fill(xp(4)+xc, yp(2)+yc, col{2});
hold off


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1823 part of \subsection*{Remember to allocate the space for a vector first}


clear
n=1e6;
% slow or very slow
tic
for k=1:n
  a(k)=1/k;
end
toc

% quicker
clear a;
tic
a=zeros(n, 1);
for k=1:n
  a(k)=1/k;
end
toc

% another quick version
clear a;
tic
a=1./(1:n);
toc


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1861 part of \subsection*{Tri-diagonal matrices --- using {\tt spdiags}}


% small version for display
n=7;
o=ones(n,1);
T=spdiags([o 4*o o], -1:1, n, n);
F=full(T)
whos T F

% larger sparse and full version
n=5000;
o=ones(n, 1);
T=spdiags([o 4*o o], -1:1, n, n);
F=full(T);
whos T F
b=T*o;

% solve using sparse and full version
tic
x=T\b;
toc

tic
x=F\b;
toc


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1901 part of \subsection*{Tri-diagonal matrices --- using {\tt spdiags}}


n=5;
a=(1:n)';
T=spdiags([a, 10*a, -a], -1:1, n, n)
full(T)


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1955 part of \subsection{Accessing fields from a struct in the case of the {\tt whos} statement} starting on page 32


function s=size_ws(t)
% s=size_ws(whos) returns the number of bytes used 
% in the workspace with the details in t

n=size(t, 1);
s=0;
for k=1:n
  s=s+sum( t(k).bytes(:) );
end


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=1998 part of \subsection{An example of creating and using a cell array} starting on page 33


f1 =@(x) sin(x/6)-0.5;
f2 =@(x) tan(x/4)-1;
f3 =@(x) cos(x/3)-0.5;

allf={f1; f2; f3};
m=size(allf, 1);
for k=1:m
  c=fzero(allf{k}, 3.0);
  fprintf('root of function number=%d at %18.15f\n', k, c);
end


%%%%----- end of lines to use



%%%%----- Use lines after here ....  line=2199 part of \subsection{Page~\pageref{prime} exercise involving prime numbers}


fprintf('number of prime numbers in (0,1000], ..., (9000, 10000] is\n');
counts=zeros(11, 1);
for k=0:10
  count(k+1)=length(primes(k*1000));
end
count(2:11)-count(1:10)


%%%%----- end of lines to use