Engineering With Python Rotating Header Image

Dynamics of a Heated Tank with Proportional/Integral (PI) Control – Programming Approach

Plan your solution:

  • Draw a picture, in this case, list all of your data and equations
  • Remember the fundamentals (material and energy balances, equations of state) and apply
  1. Draw your material or energy balance envelope (Proportional/Integral Contoller)
  2. Remember [Accumulation = In – Out + Source/Sink]
  • Think about what you need to do and the answer you want
  • You need to solve a set of ordinary differentials equations with supporting algebraic equations
  • You’ll want to plot your results

How to start your program (we do this every time…):

  • With Ipython open, open your editor
  • Label your program (you’ll never remember it, go ahead and label it!)
  • Import the packages you’ll need for solving

Import the packages you need for solution:


I like to import everything I think I’ll need.  It is annoying enough sometimes working out programming problems.  So, I try to catch it all up front.

Define your constants, your initial conditions and the time interval for your ODE solver:


This time, I put in all of the parameters, initial conditions and the time start and end (I wasn’t lazy like I have been in past posts).  Also, notice that three empty lists t_plot, q_plot and qlim_plot in the constants list.  Later you will see that we define those items inside the function for our ODE solver.  However, remember that variables defined inside a function are only available inside that function.  So, this is one way to bring the information out so that it can be plotted later.  There are other ways, such as declaring a global variable, but this works and it never hurts to learn to work with lists.  Also, note that I put in an ‘on’ and an ‘off’.  This allows to you to turn the proportional and integral parts of your controller ‘on’ and ‘off’ as you need.

Define the function for the ODE solver:


This is a pretty long function.  There are four ODE’s and then the ‘step’ function and the heater output limits.  Also, we have the commands to ‘append’ the data to the lists to allow us to plot the heater output data for the last question.  Look at how I put the ‘on’ and ‘off’ on the different portions of the controller equation.  That way you can turn them on and off for the different questions.  You can change the parameters (Kc for example) in the parameter list to see how that affects the outcome.  I used the ‘on’ and ‘off’ for the Ti and Tr as well since each will be used to calculate the ‘step’ change in temperature on the system.  Ignore the off*sin(0.1*t)*(-20).  I was playing with how periodically changing input conditions affected the system.  Play with it.  Also, Tstep should have been used instead of -20.  I forgot to update.

Set up the odeint solver and plotting:


Whew, that was a lot.  About 101 lines, pretty long.  I’ll show the plots in the next post.  Come check them out, they’re pretty cool.  This example may need a GUI, hmm, maybe that’s an idea for a new set of posts….

Leave a Reply