%% M-file for Chapter 5: Matlab Graphics

%%
clear all
close all
graphicsdefaults

%% Figure 1
T = 0:0.01:1;
plot(cos(2*pi*T), sin(2*pi*T))
axis square
print -deps graphics1.eps

%%
ezplot('cos(t)', 'sin(t)', [0 2*pi]); axis square

%% Figure 2
[X, Y] = meshgrid(-3:0.1:3, -3:0.1:3);
contour(X, Y, X.^2 + Y.^2)
axis square
print -deps graphics2.eps

%%
contour(X, Y, X.^2 + Y.^2, [1 2 3])

%%
contour(X, Y, X.^2 + Y.^2, [1 1])

%%
[X, Y] = meshgrid(-1.1:0.01:1.1, -1.1:0.01:1.1);
contour(X, Y, (X.^2 + Y.^2).^2 - X.^2 + Y.^2, [0 0])
axis square
title('The lemniscate x^2-y^2=(x^2+y^2)^2')
print -deps graphics3.eps

%%
ezcontour('x^2 + y^2', [-3, 3], [-3, 3]); axis square

%%
ezplot('(x^2 + y^2)^2 - x^2 + y^2', ...
[-1.1, 1.1], [-1.1, 1.1]); axis square

%% Figure 4
[x, y] = meshgrid(-1.1:0.2:1.1, -1.1:0.2:1.1);
quiver(x, -y); axis equal; axis off
print -deps graphics4.eps

%% Figure 5
T = -2:0.01:2;
plot3(cos(2*pi*T), sin(2*pi*T), T)
print -deps graphics5.eps

%%
ezplot3('cos(2*pi*t)', 'sin(2*pi*t)', 't', [-2, 2])

%% Figure 6
[X, Y] = meshgrid(-2:0.1:2, -2:0.1:2);
Z = X.^2 - Y.^2;
mesh(X, Y, Z)
print -deps graphics6.eps

%%
ezmesh('x^2 - y^2', [-2, 2, -2, 2])

%% Figure 7
[Z, Theta] = meshgrid(-1:0.1:1, (0:0.1:2)*pi);
X = sqrt(1 - Z.^2).*cos(Theta);
Y = sqrt(1 - Z.^2).*sin(Theta);
surf(X, Y, Z); axis square
print -deps graphics7.eps

%%
ezsurf('sqrt(1-z^2)*cos(t)', 'sqrt(1-z^2)*sin(t)', ...
'z', [0, 2*pi, -1, 1]); axis equal

%% Figure 8
x = 0:0.05:40;
for n = 1:4, subplot(2,2,n)
     plot(x, besselj(n - 1, x))
end
print -deps graphics8.eps
clf

%% Figure 9
[X, Y] = meshgrid(-1.1:0.01:1.1, -1.1:0.01:1.1);
contour(X, Y, (X.^2 + Y.^2).^2 - X.^2 + Y.^2, [0 0])
axis square
title('The lemniscate x^2-y^2=(x^2+y^2)^2', 'FontSize', 20, ...
'FontName', 'Helvetica', 'FontWeight', 'bold')
text(0, 0, ' \leftarrow a node, also an inflection', ...
'FontSize', 12)
text(0.2, -0.1, 'point for each branch', 'FontSize', 12)
xlabel('x'); ylabel('y')
print -deps graphics9.eps

%% Figure 10
hold off
X = (-2:0.02:2)*pi; Y1 = sin(X); Y2 = cos(X);
plot(X, Y1, 'r-', X, Y2, 'b:'); hold on
X1 = [-3*pi/2 pi/2]; Y3 = [1 1]; plot(X1, Y3, 'r+')
X2 = [-2*pi 0 2*pi]; Y4 = [1 1 1]; plot(X2, Y4, 'b*')
axis([-7 7 -1.1 1.1])
set(gca, 'XTick', (-2:2)*pi, 'XTickLabel', '-2pi|-pi|0|pi|2pi', 'FontSize', 16)
print -deps graphics10.eps

%%
set(gca, 'FontName', 'Symbol')
set(gca, 'XTickLabel', '-2p|-p|0|p|2p')

%%
get(gcf)
gca
set(gcf, 'Color', [1 0 0])
set(gca, 'Color', [1 1 0])
findobj('Type', 'Line')

%% Figure 11
white = [1 1 1]; gray = 0.7*white;
a = [0 1 1 0]; b = [0 0 1 1]; c = [1 1 1 1];
figure; hold on
for k = 0:1, for n = 0:2:6
   fill(a'*c + c'*(0:2:6) + k, b'*c + n + k, gray)
end, end
plot(8*a', 8*b', 'k')
set(gca, 'XTickLabel', [], 'YTickLabel', [])
set(gcf, 'Color', white); axis square
print -deps graphics11.eps

%% Comet
T = (0:0.01:2)*pi;
figure; axis equal; axis([-1 1 -1 1]); hold on
comet(cos(T), sin(T))

%% Movie
close all
X = 0:0.01:1; 
for n = 0:50
   plot(X, sin(n*pi/5)*sin(pi*X)), axis([0, 1, -2, 2]);
   M(n+1) = getframe;
end
movie(M)
movie2avi(M, 'string.avi')

%% Sound
x=0:0.1*pi:250*pi; y=zeros(1,200); z=0:0.1*pi:1000*pi;
w=[sin(x),y,sin(x),y,sin(x),y,sin(z*4/5),y,...
sin(8/9*x),y,sin(8/9*x),y,sin(8/9*x),y,sin(z*3/4)];
sound(w);
audiowrite('beethoven.wav',w,8192)

