Engineering With Python Rotating Header Image

Solving Differential Algebraic Equations – Programming Approach

Plan your solution:

  • Draw a picture, in this case, list all of your data and equations (see the first post of this series)
  • Remember the fundamentals (unsteady state material balance) and apply
  1. Draw your material or energy balance envelope (batch distillation at wikipedia)
  2. Remember [Accumulation = In – Out + Source/Sink]
  • Think about what you need to do and the answer you want
  • You need to solve an ordinary differential and associated algebraic equation, so you need an ODE solver
  • You’ll want to plot your results, so need a plotting function

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

Define the initial conditions and time interval:

Note that that instead of range(), I used arange(), any ideas why?  I’ve mentioned it in some of the earlier posts…

Define the equations for solution: (Note: There are several errors in this program listing, I point them out in the ‘solution’ post for this problem.  Sorry, I explain it all in the following post…)

 You see that I have two parameters for the function f, and x2.  Can you see how I may have made this more efficient?  I was a little lazy and just entered the parameters directly instead of setting them up as a set of constants.  For example, I should have set A_toluene =, B_toluene = and so on.  That way, all I needed to do to study another binary system would have been to read in the coefficients and run.  This was quick to set up, but not so great for user friendliness.  Perhaps you can think of some ways to streamline this?  For example, could you set up a way for the user to input a set coefficients with a GUI and then run?

Use odeint and print the final solution:

Note how to call the ‘odeint’ and then label the parts of the answer array to get a plot.  You see that odeint uses the function call, initial conditions and a time interval for the solver.  Then, you can ‘slice’ different portions of the answer array for the plot. 

Full program listing for your viewing pleasure:

In the next post, we’ll show the final solutions.  Stay tuned.

Leave a Reply