%    MATLAB file illustrating the 2D plotting capabilities 
%    of MATLAB
%    AUTHOR: Balu Santhanam
%    DATE  : 01/22/01
%             Concepts involved: 
%             1) 2D plot environment
%             2) Saving eps files   
%             3) Subplot environment 
%             4) Tabulating data 
%             5) Array/Matrix Commands
%******************************************************************
%  Remember to clear the workspace and clear all  
%  figure windows before running program so that 
%  variables stored during one run of the program 
%  do not affect the next run
% 
x = [-2:0.25:2];
f_2 = ones(1,length(x)) ./ (ones(size(x)) + exp(-x));
f_1 = ones(1,length(x)) ./ (ones(size(x)) + x.^2);
f_3 = f_1.*f_2;
f_4 = f_1 ./ f_2;  
%  
%**************************************************************** 
%     Plot the three functions on the same graph:
%     without using hold on 
%****************************************************************
plot(x,f_1,'rx-',x,f_2,'go-',x,f_3,'b*-',x,f_4,'md--')
xlabel('Domain of the functions (x)')
ylabel('f_(x) , f_2(x) , f_3(x) , f_4(x)')
title('Plot of the functions f_1(x) , f_2(x) , f_3(x) , f_4(x)  vs x')
grid on
orient tall
legend(gca,'f_1(x)','f_2(x)','f_3(x)','f_4(x)',2)
print -deps plot.eps
pause(3)
clf
%******************************************************************* 
%  Plot the three functions on the same graph using 
%  hold on (Option 2 for first part):
%  Remember to switch the hold off at the end
%*******************************************************************
plot(x,f_1,'rx-')
hold on
plot(x,f_2,'go-')
plot(x,f_3,'b*-')
plot(x,f_4,'md-')
xlabel('Domain of Funcions (x)')
ylabel('f_(x) , f_2(x) , f_3(x) , f_4(x)')
title('Plot of the functions f_1(x) , f_2(x) , f_3(x) , f_4(x) vs x')
grid on
orient tall 
legend(gca,'f_1(x)','f_2(x)','f_3(x)','f_4(x)',2)
print -deps hold.eps
pause(3)
hold off
clf
%*******************************************************************
%  Plot the functions on the same graph using the 
%  subplot command in 2 x 2 matrix format:
%******************************************************************* 
subplot(2,2,1),plot(x,f_1,'bx-')         
xlabel('Domain of Function (x)')
ylabel('f_1(x) = 1/(1+x^{2})')
title('Plot of function f_1(x) vs x')
grid on 
subplot(2,2,2),plot(x,f_2,'go-')
xlabel('Domain of Function (x)')
ylabel('f_2(x) = 1/(1+exp(-x))')
title('Plot of function f_2(x) vs x')
grid
subplot(2,2,3),plot(x,f_3,'r*-')
xlabel('Domain of function (x)')
ylabel('f_3(x) = f_1(x)f_2(x)')
title('Plot of function f_3(x) vs x')
grid on
subplot(2,2,4),plot(x,f_4,'md-')
xlabel('Domain of function (x)')
ylabel('f_3(x) = f_1(x) / f_2(x)')
title('Plot of function f_4(x) vs x')
grid on
orient tall 
print -deps subp.eps
%*****************************************************************
%   Display the values of the f_1 , f_2 and f_3 as
%   a table for x = [-2:0.5:2] :
%******************************************************************
%
x = [-2:0.5:2];
f_1 = f_1(1:2:length(f_1)); f_2 = f_2(1:2:length(f_2));
f_3 = f_3(1:2:length(f_3)); f_4 = f_4(1:2:length(f_4));
diary funcplot
fprintf('Table of values for f_1 , f_2 , f_3 , f_4:\n\n')
fprintf('------------------------------------------------------------------\n')
S = '%s%s\t%s\t%s\t%s\n';
fprintf(S,'x       |','f_1(x)    |','f_2(x)    |','f_3(x)    |','f_4(x)')
fprintf('------------------------------------------------------------------\n')
fprintf('%0.2f  \t|%f  |\t%f  |\t%f  |\t%f  |\n',[x;f_1;f_2;f_3;f_4])
fprintf('------------------------------------------------------------------\n')


