%-----------------------------------------------------------
% This script generates an array x, does simple arithmetic
% and makes some plots.  Run this script by placing it in your working
% directory and typing "example1".  Alternatively you can copy
% and paste any single (multiple) line(s) to execute that command.  
%-------------------------------------------------------------

clear; 			%clears any arrays currently in Matlab memory

x=[0.1:0.1:1];   	%an array with 101 columns 1 x 10, the ";" tells
			%matlab not to print the array to the screen
x2=[1:1:10]';  		% the ' means transpose so this is a 10 x 1 array
			%"size(x2)" gives you the dimensions of x2

y=5.*x+1;		%make a line the "." means element-by-element
                 	%math as opposed to matrix math.  
%-----------------------------------------------------------
% Generate plot 1
%-------------------------------------------------------------
figure(1); clf;		%make a figure window, "clf" clear plot window
plot(x,y,'r-')   	% plot the line with a red line, type "help plot" for 
			%more documentation
xx=zeros(10,1);		%make an array of zeros with 10 rows and 1 column
xx(1)=0.1;
xx(2:10)=1.2*x(2:10);

hold on;		        %hold current plot so we can add more to it
plot(xx,y,'k--','Linewidth',2); %this just shows that you can plot any array
			        %versus another as long as they are the same length
			
xlabel('x and xx'); 
ylabel('y');	 	%label each axis
axis([0 1.5 0 7]);  	%set bounds of axis
grid on			%draw grid lines
%-----------------------------------------------------------
% Load and plot data from another file
%-----------------------------------------------------------
load data.lab1_example
xdata=data(:,1);        %create a data array of x values
ydata=data(:,2);        %"                 "    y values
plot(xdata,ydata,'ro','Markersize',10,'MarkerFaceColor','y')
%-----------------------------------------------------------
% Generate plot 2
%-------------------------------------------------------------
a=5; c=1;		%can make "scalar" variable "a" and "c" to be used
			% in equations below
y2=x2.^2+a.*x';		%make a binomial.  In this case
		 	%square each number in the array.  Also, we take
			% the transpose of x because it must be the same
			%shape as x2 and y2 
			
y3=x2.*x'+a;		%multiply each element of x2 by x
y4=x2./x'+c;		%or divide each element of x2 by x

figure(2); clf
subplot(211);		%break figure into two rows with one column and identify
			% the top plot
plot(x2,y3,'go--',x2,y4,'k*',x2,y2,'b-');
xlabel('x_2'); 
ylabel('y_2, y_3 and y_4'); 
grid on;
axis([0 10 0 50]);
legend('y_3','y_4','y_2','Location','NorthWest');
y5=sqrt(x2.^2+10);

subplot(212);
semilogy(x2,y5,'k--'); 
hold on;		%hold plot to add more stuff
plot(x2,y2,'r');
xlabel('x_2','FontSize',14); 
ylabel('y5=sqrt(x2^2+10) and y2','FontSize',14); 
grid on;

%-----------------------------------------------------------
% output
%-------------------------------------------------------------
iout=0;			%to write an output to a file called "example.out"
			%set iout=1
if (iout==1);		%This is an "if" statement, which runs the following 
  out=[x2 y5];		%commands before "end" if the statement in parentheses is true
  save -ascii example out;
end

	
figure(2);		%to create a postscript file of figure (2)
orient tall;		%just make plot bigger on page 
subplot(211); 
title('Example plot 1'); %a title for the top plot
print -dps example1	 %




