% Problem 1.6
clear all, clc, close all

H0=10; % initial tank level
A=5; % cross sectional area of tank
c=3; % nonlinear tank outflow constant
tfinal=25; % 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 approximate 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)=HA(n)-(c*dt/A)*sqrt(HA(n)); % compute (n+1)st component of approximate tank level array HA
end % for n

subplot(2,2,1) % Open Figure window(1) for 2x2 subplots with first plot going in 1st row, 1st column
plot(t,HA,'b.:') % plot HA vs t as blue dots connected by dashed lines
xmin=-2; xmax=27; ymin=-1; ymax=H0+1; % set x-axis and y-axis limits
v=[xmin xmax ymin ymax]; % set vector defining x-axis and y-axis limits
axis(v) % implement defined axis limits 
hold on % keep plot in Figure window when drawing next plot
plot([xmin xmax],[0 0],'k') % plot solid black line connecting points (xmin,0) and (xmax,0)
plot([0 0],[ymin ymax],'k') % plot solid black line connecting points (0,ymin) and (0,ymax)
xlabel('t  (min)')
ylabel('H_{A}  (ft)')
title('Simulated Tank Level vs Time')
text(10,5,'\Deltat=2.5') % insert string text starting at (10,5)
text(3,9,'EXERCISE problem 1.6') % insert string text starting at (3,9)

% 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 approximate 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 and approximate tank level array HA
    t(n+1)=n*dt; % compute (n+1)st component of time array
    HA(n+1)=HA(n)-(c*dt/A)*sqrt(HA(n)); % compute (n+1)st component of approximate tank level array HA
end % for n

subplot(2,2,2) % next plot goes in 1st row, 2nd column
plot(t,HA,'b.:') % plot HA vs t as blue dots connected by dashed lines
axis(v) % implement defined axis limits 
hold on % keep plot in Figure window when drawing next plot
plot([xmin xmax],[0 0],'k') % plot solid black line connecting points (xmin,0) and (xmax,0)
plot([0 0],[ymin ymax],'k') % plot solid black line connecting points (0,ymin) and (0,ymax)
xlabel('t  (min)')
ylabel('H_{A}  (ft)')
title('Simulated Tank Level vs Time')
text(10,5,'\Deltat=1') % insert string text starting at (10,5)
text(3,9,'EXERCISE Problem 1.6') % insert string text starting at (3,9)

% 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 approximate 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 and approximate tank level array HA
    t(n+1)=n*dt; % compute (n+1)st component of time array t
    HA(n+1)=HA(n)-(c*dt/A)*sqrt(HA(n)); % compute (n+1)st component of approximate tank level array HA
end % for n

subplot(2,2,3) % next plot goes in 2nd row, 1st column
plot(t(1:5:end),HA(1:5:end),'b.:') % plot every fifth point of HA vs t as blue dots connected by dashed lines
axis(v) % implement defined axis limits 
hold on % keep plot in Figure window when drawing next plot
plot([xmin xmax],[0 0],'k') % plot solid black line connecting points (xmin,0) and (xmax,0)
plot([0 0],[ymin ymax],'k') % plot solid black line connecting points (0,ymin) and (0,ymax)
xlabel('t  (min)')
ylabel('H_{A}  (ft)')
title('Simulated Tank Level vs Time')
text(10,5,'\Deltat=0.25') % insert string text starting at (10,5)
text(3,9,'EXERCISE Problem 1.6') % insert string text starting at (3,9)

% Part d) - new part added to problem
clear t HA % clear t and HA arrays
dt=0.025; % 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 approximate 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 and approximate tank level array HA
    t(n+1)=n*dt; % compute (n+1)st component of time array t
    HA(n+1)=HA(n)-(c*dt/A)*sqrt(HA(n)); % compute (n+1)st component of approximate tank level array
end % for n

subplot(2,2,4) % next plot goes in 2nd row, 2nd column
plot(t(1:50:end),HA(1:50:end),'b.:') % plot every 50th point of HA vs t as blue dots connected by dashed lines
axis(v) % implement defined axis limits 
hold on % keep plot in Figure window when drawing next plot
plot([xmin xmax],[0 0],'k') % plot solid black line connecting points (xmin,0) and (xmax,0)
plot([0 0],[ymin ymax],'k') % plot solid black line connecting points (0,ymin) and (0,ymax)
xlabel('t  (min)')
ylabel('H_{A}  (ft)')
title('Simulated Tank Level vs Time')
text(10,5,'\Deltat=0.025') % insert string text starting at (10,5)
text(3,9,'EXERCISE problem 1.6') % insert string text starting at (3,9)








