%This script demonstrates plotting 
%P. Chidyagwai
%MA 302

clf
clear 

%%
%Plotting single variable functions
%STEP 1: define discrete points in the plotting domain
%Here it is good practice to use a resonably large number of points to correctly capture the behavior of this continuous function.
x=linspace(-2*pi,2*pi,100);

%Step2: To plot any function f, we evaluate each point in the vector x, for
%example to plot f(x) = x^2, evaluate each element of the vector x  component-wise as:
yvals = x.^2;

%%Call the MATLAB plot function, here with an option to specify the width of the line.

plot(x,yvals,'LineWidth',3);
title('$y=x^2$ \quad ($n=100$)', 'Interpreter','latex');
xlabel('x'); ylabel('y');
print -djpeg plot1.jpg
%%
%We can also make the axis more consistent with the plotting data
clf
plot(x,yvals,'LineWidth',3);
title('$y=x^2$ \quad ($n=100$)', 'Interpreter','latex');
xlabel('x'); ylabel('y');
axis tight
print -djpeg plot2.jpg;
%%
%NOTE: if the value of n, in the call to linspace(-5,5,n) is small, then the quality of the plot is poor, e.g.

clf
x_coarse = linspace(-1,1,10);
yvals_coarse = x_coarse.^2;
plot(x_coarse,yvals_coarse,'LineWidth',3);
title('$y=x^2$ \quad ($n=10$)', 'Interpreter','latex');
xlabel('x'); ylabel('y');
print -djpeg plot3.jpg;
%%
%Multiple plots
clf
y1 = sin(x);
y2 = 2*cos(x);
y3 = 3*cos(2*x);
%pass the "domain" and "range" for each function to plot
plot(x,y1,x,y2,x,y3,'LineWidth',2)
title('Mutiple plots')
xlabel('x')
ylabel('y')
%add legend 
legend('sin(x)','2cos(x)','3cos(2x)')
axis tight
print -djpeg plot4.jpg

%% 
%Multiple plots - hold
%We can create multiple plots using the hold command. We can add more functions to our initial plot using the hold command
clf
y1 = sin(x);
y2 = 2*cos(x);
y3 = 3*cos(2*x);
plot(x,y1,'LineWidth',2) %first plot
hold on % retains the current axis and to add more plots
plot(x,y2,'LineWidth',2) % second plot
plot(x,y3,'LineWidth',2) %third plot
title('Mutiple plots')
xlabel('x')
ylabel('y')
%add legend with option to specify its location
legend('sin(x)','2cos(x)','3cos(2x)', 'Location','SouthWest')
%make the plot box fit tightly around the data
axis tight 
hold off
print -djpeg plot5.jpg
%%
%In addition to the size of the lines, we can also specify the line style 
%and color in plots
clf
y1 = sin(x);
y2 = 2*cos(x);
y3 = 3*cos(2*x);
plot(x,y1,'rv','LineWidth',2); %red triangles (no line)
hold on % retains the current axis and to add more plots
plot(x,y2,'g--*','LineWidth',2); % second plot green dashed line
plot(x,y3,'b:d','LineWidth',2); %third plot blue dotted
title('Mutiple plots');
xlabel('x');
ylabel('y');
%add legend with option to specify its location
legend('sin(x)','2cos(x)','3cos(2x)', 'Location','SouthWest');
%make the plot box fit tightly around the data
axis tight 
hold off 
print -djpeg plot6.jpg
%%
%subplot(m,n,k) will create an array of subplots within the same window
%Example, suppose we want to visualize function transpormations of f(x) =sin(x)

clf

% define a discrete domain vector
x=linspace(-5*pi,5*pi,200); 

% Evaluate your functions on x
y1 = sin(x);
y2 = 2*sin(x);
y3 = sin(x/2);
y4 = sin(2*x);

% We want to plot 4 functions on a 2 by 2 grid

subplot(2,2,1) % subplot 1
plot(x,y1,'b','LineWidth',2) 
title('Subplot 1: sin(x)');

subplot(2,2,2)
plot(x,y2,'g','LineWidth',2) % subplot2
title('Subplot 2: 2sin(x)');

subplot(2,2,3) % subplot 3
plot(x,y3,'k','LineWidth',2) 
title('Subplot 3: sin(x/2)');

subplot(2,2,4) %subplot 4
plot(x,y4,'r','LineWidth',2) 
title('Subplot 4: sin(2x)');
print -djpeg plot7.jpg;
%%
%We can also specify common axis properties as follows:
clf
%store axis objects
ax1=subplot(2,2,1); % subplot 1
plot(x,y1,'b','LineWidth',2) 
title('Subplot 1: sin(x)');

ax2=subplot(2,2,2);
plot(x,y2,'g','LineWidth',2) % subplot2
title('Subplot 2: 2sin(x)');

ax3=subplot(2,2,3); % subplot 3
plot(x,y3,'k','LineWidth',2) 
title('Subplot 3: sin(x/2)');

ax4=subplot(2,2,4); %subplot 4
plot(x,y4,'r','LineWidth',2) 
title('Subplot 4: sin(2x)');

%Specify common axis properties 
axis([ax1 ax2 ax3 ax4], [-5*pi, 5*pi, -2, 2])
%axis([ax1 ax2 ax3 ax4],'tight')

print -djpeg plot8.jpg;

%%
%Parametric plots

clf
t = linspace(0,2*pi);
x = 5*cos(t);
y = 3*sin(t);
ax=gca; % returns current axis
plot(x,y,'LineWidth',2)
xlabel('x')
ylabel('y')
title('Parametric plot');
axis equal % use the same length for the data units along each axis
axis([-6 6 -6 6])
print -djpeg plot9.jpg;

%set other axis properties
ax.FontSize =15;
ax.LineWidth =2;
ax.XTick =-6:2:6;
ax.YTick = -6:2:6;
ax.XMinorTick='on';
ax.YMinorTick='on';
print -djpeg plot10.jpg;








