% ---------------------------------------- 
% EPS 131: Intro to Phys. Ocean. & Climate
% Laure Zanna, Feb 7, 2008
% Intro to Matlab, Part III
%-----------------------------------------

% -------------------------
% How to make plots
% -------------------------
% 1. Line Plots in Two Dimensions
% Define x, y then plot 
x = [1 2 3 4 5]
y = [-1 2 10 15 25]
plot(x,y);

% 2. Add labels and title
xlabel('X');
ylabel('Y');
title('Y as fct of X');
 
% 3. Make the x points evenly spaced
x=  [-5:0.1:5];
y= exp(-x);
y2= x.^3;
figure;
plot(x,y,'b');
hold on; 
plot(x,y2,'r');
xlabel('x-axis'); ylabel('y-axis'); 
legend('Exp','Hyp');

% 4. Make it on 2 different plots but on the same figure
figure; 
subplot(2,1,1); 
plot(x,y);
xlabel('x-axis'); ylabel('y-axis'); 
subplot(2,1,2); 
plot(x,y2);
xlabel('x-axis'); ylabel('y-axis'); 

% -----------------------------
% How to save variables
% -----------------------------
clear; close
a = [0:10];
b = [5:0.1:6];
c = a.*b

whos

% You will on the screen:

% Name      Size                   Bytes  Class
%
%  a         1x11                      88  double array
%  b         1x11                      88  double array
%  c         1x11                      88  double array
%
%Grand total is 33 elements using 264 bytes

save all_variables.mat % will save all the variables 
save some_variables.mat a b % will save only the variables a b

% ----------------------------
% Load saved variables 
% ----------------------------
clear

load all_variables.mat 

whos

% Ang again, you should see: 
%   Name      Size                   Bytes  Class
% 
%   a         1x11                      88  double array
%   b         1x11                      88  double array
%   c         1x11                      88  double array
% 
% Grand total is 33 elements using 264 bytes

% You can now use them to plot, etc... 
figure; 
plot(a,c,'r'); 
hold on; 
plot(b,c,'b')
legend('c as fct of a','c as fct of b')

% ----------------------------------------
% More Examples: 
% meshgrid, streamslice, contour, contourf

% 1. Simple Vertical Profile
H=10;  T_0=25;
z=0:100;
T=T_0*exp(-z./H);

% Assume T is the temperature and z is the depth, plot the vertical temperature profile:
figure;
plot(T,z,'linewidth',2); 
% since we want 0 to be the surface, we can reverse the y axis by using: 
set(gca,'YDir','reverse');
xlabel('T'), ylabel('z')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1. Contour lines of T in the X-Y plane using contourf or contour 
clear all; close all;
% Construct a 2D grid between -10 and 10 on both the x and y axis
% using the function meshgrid
[X,Y] = meshgrid(-10:10,-5:5);

% Contruct T=x*exp(-y^2);
T=X.*exp(-Y.^2);

% Plot contour lines of T in the x-y plane using the function contour
figure; 
contourf(X,Y,T); 
% add a colorbar
colorbar
% Not happy with the number of contours, you can change it by adding
% it as the last argument
figure; 
contourf(X,Y,T,60);
colorbar
% if want to get rid of the lines, use:
shading flat

% can use also contour (same than contourf but contour are not filled)
figure; 
[c,h] = contour(X,Y,T,20);
% c is the contour matrix and h is a column vector of handles to line
% You can use these 2 to add labels to the contour lines
clabel(c,h)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2. Plot streamlines (= lines that are 
% parallel to the flow at any given moment)
clear all; close all;

% define some parameters
alpha=2;

% Our grid
[X,Y] = meshgrid(-10:10,-5:5);

% Velocity Components (u,v)
u=-alpha*X; 
v=alpha*Y;

% Plot streamlines + direction of the flow using the function
% streamslice
streamslice(X,Y,u,v);
xlabel('X','fontsize',13); 
ylabel('Y','fontsize',13); 
title('Streamlines','fontsize',14)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 3. Plot Velocity field 
clear all; close all;

% Define parameters
k=2*pi/10;
l=1*pi/20;
U=0.1; V=0.1

% x-y domain btw 0 and 20 evenly spaced (increment of 0.5)
x=0:0.5:20;
y=0:0.5:20;
[X,Y]=meshgrid(x,y); 

% Velocity component: u_0 and v_0 as sine and cosine function
u_0=U*cos(k*X+l*Y);
v_0=V*sin(k*X+l*Y);

% Plot velocity vector using quiver
figure;
quiver(X,Y,u_0,v_0)
% you can set the limits of the axes by using the following:
set(gca,'XLim',[0 20],'YLim',[0 20])
% Put title & labels and change the fontsize
title('Velocity field','fontsize',14)
xlabel('x [m]','fontsize',13); ylabel('y [m]','fontsize',13)
