Skip to content

Instantly share code, notes, and snippets.

@ibeckermayer
Created August 22, 2023 05:18
Show Gist options
  • Save ibeckermayer/0b27020cf6373d3ef38607fc47ff5923 to your computer and use it in GitHub Desktop.
Save ibeckermayer/0b27020cf6373d3ef38607fc47ff5923 to your computer and use it in GitHub Desktop.
Fix the water level in Udemy Applied Control Systems
diff --git a/calculus_sim_waterTanks_Kp_controller.py b/calculus_sim_waterTanks_Kp_controller.py
index d8365f6..06477b1 100644
--- a/calculus_sim_waterTanks_Kp_controller.py
+++ b/calculus_sim_waterTanks_Kp_controller.py
@@ -20,11 +20,9 @@ import matplotlib.gridspec as gridspec
import matplotlib.animation as animation
import numpy as np
-radius=5 # Radius of the tank - The tank is round.
bottom=0 # Initial volume of the tank
final_volume=100 # Final volume of the tank
dVol=10 # The change of volume on the vertical scale.
-width_ratio=1 #Necessary for the horizontal axis
dt=0.04 # Time interval
t0=0 # Initial time of the simulation
t_end=50 # Final time of the simulation
@@ -108,22 +106,26 @@ vol_r3_2=vol_r3
def update_plot(num):
+ global tank_12, tank_22, tank_32
if num>=len(volume_Tank1):
num=len(volume_Tank1)-1
- tank_12.set_data([0,0],[-63,volume_Tank1[num]-63])
+ tank_12.remove()
+ tank_12=ax0.fill_between([-1, 1], 0, volume_Tank1[num], color='royalblue')
tnk_1.set_data(t[0:num],volume_Tank1[0:num])
- vol_r1.set_data([-radius*width_ratio,radius*width_ratio],[vol_r1_2[num],vol_r1_2[num]])
+ vol_r1.set_data([-1,1],[vol_r1_2[num],vol_r1_2[num]])
vol_r1_line.set_data([t0,t_end],[vol_r1_2[num],vol_r1_2[num]])
- tank_22.set_data([0,0],[-63,volume_Tank2[num]-63])
+ tank_22.remove()
+ tank_22=ax1.fill_between([-1, 1], 0, volume_Tank2[num], color='royalblue')
tnk_2.set_data(t[0:num],volume_Tank2[0:num])
- vol_r2.set_data([-radius*width_ratio,radius*width_ratio],[vol_r2_2[num],vol_r2_2[num]])
+ vol_r2.set_data([-1,1],[vol_r2_2[num],vol_r2_2[num]])
vol_r2_line.set_data([t0,t_end],[vol_r2_2[num],vol_r2_2[num]])
- tank_32.set_data([0,0],[-63,volume_Tank3[num]-63])
+ tank_32.remove()
+ tank_32=ax2.fill_between([-1, 1], 0, volume_Tank3[num], color='royalblue')
tnk_3.set_data(t[0:num],volume_Tank3[0:num])
- vol_r3.set_data([-radius*width_ratio,radius*width_ratio],[vol_r3_2[num],vol_r3_2[num]])
+ vol_r3.set_data([-1,1],[vol_r3_2[num],vol_r3_2[num]])
vol_r3_line.set_data([t0,t_end],[vol_r3_2[num],vol_r3_2[num]])
@@ -140,22 +142,22 @@ gs=gridspec.GridSpec(2,3)
# Create object for Tank1
ax0=fig.add_subplot(gs[0,0],facecolor=(0.9,0.9,0.9))
vol_r1,=ax0.plot([],[],'r',linewidth=2)
-tank_12,=ax0.plot([],[],'royalblue',linewidth=260,zorder=0)
-plt.xlim(-radius*width_ratio,radius*width_ratio)
+tank_12=ax0.fill_between([-1, 1], 0, volume_Tank1[0], color='royalblue')
+plt.xlim(-1,1)
plt.ylim(bottom,final_volume)
-plt.xticks(np.arange(-radius,radius+1,radius))
+plt.xticks(np.arange(-1,2,1))
plt.yticks(np.arange(bottom,final_volume+dVol,dVol))
plt.ylabel('tank volume [m^3]')
plt.title('Tank 1')
-copyright=ax0.text(-radius*width_ratio,(final_volume+10)*3.2/3,'© Mark Misin Engineering',size=12)
+copyright=ax0.text(-1,(final_volume+10)*3.2/3,'© Mark Misin Engineering',size=12)
# Create object for Tank2
ax1=fig.add_subplot(gs[0,1],facecolor=(0.9,0.9,0.9))
vol_r2,=ax1.plot([],[],'r',linewidth=2)
-tank_22,=ax1.plot([],[],'royalblue',linewidth=260,zorder=0)
-plt.xlim(-radius*width_ratio,radius*width_ratio)
+tank_22=ax1.fill_between([-1, 1], 0, volume_Tank2[0], color='royalblue')
+plt.xlim(-1,1)
plt.ylim(bottom,final_volume)
-plt.xticks(np.arange(-radius,radius+1,radius))
+plt.xticks(np.arange(-1,2,1))
plt.yticks(np.arange(bottom,final_volume+dVol,dVol))
plt.title('Tank 2')
@@ -163,10 +165,10 @@ plt.title('Tank 2')
# Create object for Tank3
ax2=fig.add_subplot(gs[0,2],facecolor=(0.9,0.9,0.9))
vol_r3,=ax2.plot([],[],'r',linewidth=2)
-tank_32,=ax2.plot([],[],'royalblue',linewidth=260,zorder=0)
-plt.xlim(-radius*width_ratio,radius*width_ratio)
+tank_32=ax2.fill_between([-1, 1], 0, volume_Tank3[0], color='royalblue')
+plt.xlim(-1,1)
plt.ylim(bottom,final_volume)
-plt.xticks(np.arange(-radius,radius+1,radius))
+plt.xticks(np.arange(-1,2,1))
plt.yticks(np.arange(bottom,final_volume+dVol,dVol))
plt.title('Tank 3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment