% Problem 1.5
clear all, clc, close all

H0=10; % initial tank level
A=5; % cross sectional area of tank
R=1; % fluid resistance of tank
tau=A*R; % time constant of linear tank
tfinal=5*tau; % final simulation time

% Part a)
dt=2.5; % time step size
nfinal=round(tfinal/dt); % number of iterations in loop to compute approximate tank level for tfinal min
HA(1:nfinal+1)=0; % pre-allocate components of aproximate tank level array HA
t(1:nfinal+1)=0; % pre-allocate components of time array t
HA(1)=H0; % first component of approximate tank level array HA
t(1)=0; % first component of time array t

for n=1:nfinal % begin for loop for computing components of time array t and approximate tank level array HA
    t(n+1)=n*dt; % compute (n+1)st component of time array t
    HA(n+1)=(1-(dt/(A*R)))*HA(n); % compute (n+1)st component of approximate tank level array HA
end % for n
plot(t,HA,'b.:') % open Figure window(1) and plot HA vs t as blue dots connected by dashed lines using default axis scales
hold on % keep HA vs t plot in current Figure window for next plot
xlabel('t  (min)')
ylabel('H_{A}  (ft)')
title('Simulated Tank Level vs Time')

% Part b)
clear t HA % clear t and HA arrays
dt=1; % reset time step size
nfinal=round(tfinal/dt); % number of iterations in loop to compute approximate tank level for tfinal min
HA(1:nfinal+1)=0; % pre-allocate components of aproximate tank level array HA
t(1:nfinal+1)=0; % pre-allocate components of time array t
HA(1)=H0;  % first component of approximate tank level array HA (required because HA array was cleared)
t(1)=0; % first component of time array t (required because t array was cleared)
for n=1:nfinal % begin for loop for computing components of time array t and approximate tank level array HA
    t(n+1)=n*dt; % compute (n+1)st component of time array t
    HA(n+1)=(1-(dt/(A*R)))*HA(n); % compute (n+1)st component of approximate tank level array HA
end % for n
plot(t,HA,'r.:') % plot HA vs t as red dots connected by dashed lines

% Part c)
clear t HA % clear t and HA arrays
dt=0.25; % reset time step size
nfinal=round(tfinal/dt); % number of iterations in loop to compute approximate tank level for tfinal min
HA(1:nfinal+1)=0; % pre-allocate components of aproximate tank level array HA
t(1:nfinal+1)=0; % pre-allocate components of time array t
HA(1)=H0; % first component of approximate tank level array HA (required because HA array was cleared)
t(1)=0; % first component of time array t (required because t array was cleared)
for n=1:nfinal % begin for loop for computing components of time array t and approximate tank level array HA
    t(n+1)=n*dt; % compute (n+1)st component of time array t
    HA(n+1)=(1-(dt/(A*R)))*HA(n); % compute (n+1)st component of approximate tank level array HA
end % for n
plot([t(1) t(3:2:end)],[HA(1) HA(3:2:end)],'g.:') % plot every second point of HA vs t as green dots connected by dashed lines
% Note, t(1),HA(1) is initial point t=0, HA(0)=H0 which corresponds to
% discrete-time n=0
% Note, every 2nd point of HA vs t is plotted starting with t(3),HA(3)
% which corresponds to discrete-time n=2                                         
                                       
legend('H_{A}(n), n=0,1,2,...,10','H_{A}(n), n=0,1,2,...,25', 'H_{A}(n), n=0,2,4,...,100','Location','east')

text(3.5,1.25,'\Deltat=2.5') % place string text starting at pt (3.5,1.25) 
plot([6 7.5],[1.25 1.25],'b') % plot blue line connecting points (6,1.25) and (7.5,1.25)

text(8,2.62,'\Deltat=1') % place string text starting at pt (8,2.62)
plot([6 7.5],[2.62 2.62],'r') % plot red line connecting points (6,2.62) and (7.5,2.62)

text(12,1.28,'\Deltat=0.25') % place string text starting at pt (12,1.28)
plot([10 11.5],[1.28 1.28],'g') % plot green line connecting points (10,1.28) and (11.5,1.28)

text(8,9,'EXERCISE problem 1.5')




