%Simple plots with MATLAB
%when plotting a curve, it is good practice to have a lot of sample points
x=linspace(0,4*pi,10); %10 equally spaced points
y=exp(-0.4*x).*sin(x); %this is a decaying sine curve
plot(x,y) %hard to tell what's really going on...
x=linspace(0,4*pi,50); %50 points should be better
y=exp(-0.4*x).*sin(x); %remember to recalculate y as well
plot(x,y) %better, but still kinda jagged
plot(x,y,'o') %will display data points as circles
z=cos(x); %we'll display multiple curves on the same plot
plot(x,y,'o',x,z,'--') %this does both curves, the latter with dashes
text(2,.5,'local maximum here') %include text at given location
%-------------------------------------------------------------
%color options: yellow, magenta, cyan, red, green, blue, white, black (k)
%line style options: - (solid), --(dashed), :(dotted), -. (dash-dot)
%marker style options: +,o,*,x,.,^,

%--------------------------------------------------------------
%--------------------------------------------------------------

%you can also overlay plots even if you don't plot all at once
x=linspace(0,2*pi,100); %generate x
y1=sin(x); %generate sine curve
plot(x,y1,'-')
hold on %hold command will keep current plots
y2=x; plot(x,y2,'r--') %plot linear appoximation using red dashed line
y3=x-(x.^3)/6+(x.^5)/120; plot(x,y3,'ko') %plot 5th order Taylor poly with black o's
axis([0 2*pi -1 3]) %define axis as you like: xmin xmax ymin ymax
%use of -inf and inf will let the lower and upper bounds be set automatically
xlabel('x')
ylabel('Approximations of sin(x)')
title('Fun with sin(x)')
gtext('sin(x)') %gtext allows you to click where you want the text
gtext('lin approx')
gtext('5th order Taylor poly')
legend('sin(x)','LIN approx', '5OTP') %if you prefer a legend, use legend command
figure(2) %opens a new figure
%subplots - it might be preferred to display curves in subplots
subplot(2,2,1), plot(x,y1) %sets up a m by n grid for subplots, plot will go in A(1,1)
subplot(2,2,2), plot(x,y2) %last argument determines the placement within array, counted rowwise
subplot(2,2,3), plot(x,y3) %since the subplot only has two columns, this will go into the next row
%anonymous 2d plots
subplot(2,2,4) fplot(@(x) x.*sin(x), [0 10*pi]) %automatically plots function w/o defining x and y vectors
%polar plots
figure(3)
t=linspace(0,2*pi,200); r=cos(5*t); polar(t,r) %plot is a 5-petaled flower
%moving into 3D - contour map
r=-2:.2:2; %set up a parameter range
[X,Y]=meshgrid(r,r); %meshgrid creates a grid for us to plot on
Z=X.^2+Y.^2-5*sin(X.*Y); %compute the surface
cs=contour(X,Y,Z); %displays contour map
clabel(cs); %labels contours
%quiver plots vector fields
[dx,dy]=gradient(Z,.2,.2); %built-in gradient to find gradient at regular intervals
quiver(X,Y,dx,dy); %plots X,Y and vector field


%-------------------------------------------------------------------------
%-------------------------------------------------------------------------
%3D Plotting
theta = linspace(0,2*pi,100); %initialize parameter
x=cos(theta);
y=sin(theta);
plot3(x,y,theta); %plots curves in 3D space
z=theta.^3; 
plot3(x,y,z); %can also plot with a third variable other than parameter
xlabel('x(t)=cos(t)')
ylabel('y(t)=sin(t)')
zlabel('z(t)=t^3') %zlabel is obviously introduced for the third dimesion, other commands still apply
%Main tool - meshgrid
%for the x and y axes, you create vectors that define the domain, to plot
%you'll want a matrix that corresponds to the cartesian product of these
%vectors.  That is precisely what meshgrid does!
x=linspace(-3,3,50); y=x; %this creates the vectors x and y
[X,Y]=meshgrid(x,y); %this creates the grid
Z=X.*Y.*(X.^2-Y.^2)./(X.^2+Y.^2); %compute the value at each grid point
mesh(X,Y,Z)
title('Plot created by mesh')
figure(2) %this will create a second figure
meshc(X,Y,Z) %plots the same surface with contours on the xy plane
title('Plot created by meshc')
%can also do meshz, waterfall
figure(3) %new figure
surf(X,Y,Z) %this looks more like a true surface
shading flat %gets rid of wireframe
shading interp %smoother change with interpolated values
shading faceted %shows wireframe
%surfc will do the surface with contour lines
%colormap can be used to changed the colors
colormap(cool)
colormap(copper)
contour3(X,Y,Z) %shows contour lines for 3D figure

%put an image on a surface
%first define a surface
[T,Phi]=meshgrid(0:pi/300:2*pi,0:pi/500:pi);
X=sin(Phi).*cos(T); Y=sin(Phi).*sin(T); Z=cos(Phi);
[I,map]=imread('Shakespeare.jpg'); %use imread to read an image in your directory
warp(X,Y,Z,I)
figure(2)
[X,Y]=meshgrid(-5:.1:5);
Z=X.^2-Y.^2;
[I,map]=imread('pringles2.jpg');
warp(X,Y,Z,I)
