Introduction To Polymath For Workshop PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Introduction to POLYMATH based on Polymath and Excel Tutorial for Process Fluid

Transport

POLYMATH tutorial Objectives: A student will be able to

1. Enter and solve 3 differential equations for a batch chemical reactor problem .

2. Use the built-in dialog box buttons to enter a differential equation and its initial
condition. This form is useful since you will not forget to enter the initial condition.

3. Prepare a word document that contains all required information for homework solution.
Students must do more than just turn in a polymath program file. They must show how
the model equations were derived, answer the questions, produce graphs and sample
calculations.

4. How to copy POLYMATH output into an excel spreadsheet such that the produced
output has headers in the first row.

5. use the trouble shooting DEQ Message list to determine that a variable has been defined
more than once or has not been defined

6. Identify problems that cause a program to stop running such as a divide by zero error

7. Use the comment feature in the polymath program (#)

8. How to use an if - then - else statement

1
1. Open Polymath:
Polymath Short Cut

2. We suggest that you open two windows so that you can see this pdf file and the polymath
program as shown below.

2
3. Start the Differential Equations Solver by selecting Program, DEQ Differential Equations
and keep the help window open
If help is needed click on Question Mark
or go to Help menu.

Figure 1: Blank screen of Differential Equation Solver

3
Example of using the Differential Equation Solver in Reaction Engineering

4. Now you will create a simple POLYMATH file: Series Reactions Example.pol. We will
enter the 3 differential Equations and supporting explicit algebraic equations similar to that
given in the POLYMATH help example. (The only difference is that concentrations are defined
as CA, CB and CC).

This example is based on a batch reactor with 2 simultaneous chemical reactions in series.
𝑘𝑘1 𝑘𝑘2
𝐴𝐴 → 𝐵𝐵 → 𝐶𝐶
A component mole balance is constructed for each chemical species. Since this is a batch
reactor, then the mole balances are differential equations. If this problem was an assigned
homework problem then the first page of the problem would be a hand written setup of the
problem on green engineering paper. This page would contain:

a) Setup of the component species mole balances including a diagram of the process
(process flow diagram, pfd)

b) Initial conditions

c) Sample calculations showing that the correct units have been used and an order of
magnitude estimate of the results.

For example the mole balances for A, B and C are given by

𝑑𝑑 (𝐶𝐶𝐴𝐴 )
= −𝑘𝑘1 𝐶𝐶𝐴𝐴 (1)
𝑑𝑑𝑑𝑑

𝑑𝑑 (𝐶𝐶𝐵𝐵 )
= 𝑘𝑘1 𝐶𝐶𝐴𝐴 − 𝑘𝑘2 𝐶𝐶𝐵𝐵 (2)
𝑑𝑑𝑑𝑑

𝑑𝑑 (𝐶𝐶𝐶𝐶 )
= 𝑘𝑘2 𝐶𝐶𝐵𝐵 (3)
𝑑𝑑𝑑𝑑

The initial conditions in the batch reactor at t=0 min are 𝐶𝐶𝐴𝐴 = 1 kmol/L, 𝐶𝐶𝐵𝐵 = 0 kmol/L and
𝐶𝐶𝐶𝐶 = 0 kmol/L. These are known as initial values. The integration will proceed from 0 min to
t=3 min. The rate constants are 𝑘𝑘1 = 1 min−1 and 𝑘𝑘2 = 2 min−1 .

Sample calculations of all equations are required to be submitted on green engineering paper.
These calculations will help you to troubleshoot your program. Sample calculations for explicit
equations should be straight forward always showing the number and units. For differential
equations I suggest that you show an order of magnitude estimate as the sample calculation.

4
For this example for the batch reactor mole balance using the initial conditions the initial change
in concentration of A with time is:

𝑑𝑑 (𝐶𝐶𝐴𝐴 ) 𝑘𝑘𝑘𝑘𝑘𝑘𝑘𝑘
� = −1 𝑚𝑚𝑚𝑚𝑚𝑚−1 �1 � = −1 𝑘𝑘𝑘𝑘𝑘𝑘𝑘𝑘/(𝐿𝐿 min) (4)
𝑑𝑑𝑑𝑑 𝑡𝑡=0 𝐿𝐿

An estimate of the value of concentration after 1 minute would be


(NOTICE that this is not a correct integration of the differential equation. This is ONLY an
ESTIMATE and a check on the units. To check for order of magnitude changes an assumption is
made that the rate is constant.)

𝐶𝐶𝐴𝐴 1 𝑚𝑚𝑚𝑚𝑚𝑚
� 𝑑𝑑 (𝐶𝐶𝐴𝐴 ) ~ � −1 𝑘𝑘𝑘𝑘𝑘𝑘𝑘𝑘/(𝐿𝐿 min)𝑑𝑑𝑑𝑑 = 𝐶𝐶𝐴𝐴 − 1𝑘𝑘𝑘𝑘𝑘𝑘𝑘𝑘/𝐿𝐿 = −1 𝑘𝑘𝑘𝑘𝑘𝑘𝑘𝑘/𝐿𝐿 (5)
𝐶𝐶𝐴𝐴 =1 𝑘𝑘𝑘𝑘𝑘𝑘𝑘𝑘/𝐿𝐿 𝑡𝑡=0𝑚𝑚𝑚𝑚𝑚𝑚

The above result gives the final value of the concentration of A to be zero. In other words if the
reaction rate was at 1 kmol/(L min) for 1 minute, then there would be no reactant A left. It is
then up to the student doing the problem to evaluate if this reaction rate is what was specified or
should the rate be 10 times lower. The above equations do not need to be typed, and can be
written by hand and then scanned in B&W using your phone with an free App such as
CamScanner. This scan will then be inserted into the word document that will be submitted on
Blackboard.

5. Of special note is the usefulness of the wizard menu’s in entering a differential equation. This
will write the equation using the proper syntax. For example to enter the first differential
equation click on the d(x)+ button. d(CA) / d(t) = -k1*CA #Concentration of component A and
then the initial condition that concentration of A at t=0 is 1.

Click on the d(x)+ button to add a


differential equation.

Figure 2: Add a differential Equation

5
6. Fill out the form shown below and then select done.

Figure 3: Fill out the menu screen as shown

Notice that the comment is entered with


a # sign in front of the text.

The initial condition is This message box let’s you know what is
converted to this line with the missing and other errors.
t=0 symbolized within the
parenthesis.

Figure 4: Result of filling out the menu for one differential equation

6
7. Notice that an error has appeared stating that there is an
undefined variable, k1. To remove this error you can add
the explicit equation k1=1 min-1. Open up the wizard for an
explicit equation

Click on the x=+


button.

Figure 5: Explicit Equation Entry Form

7
Figure 6: Result of entering explicit equation

8. Enter the second differential equation using the wizard. Notice that the second time that
you open this wizard the independent variable of time is already entered.

Time already entered

Figure 7: Second use of ODE wizard

8
Figure 8: Second ODE

9. Finally enter the 3rd ODE

9
10. The result of entering the 3 ODE’s is

second message.

11. To remove the error marked above add the value of the second rate constant

Third message. You need to tell


the program the starting time and
the final time for the integration.

10
12. To remove the final marked error go to the ini-finl button to enter the initial and final values for
the integration of the time variable. In this case t=0 to t=3 min.

Enter initial and final condition of t=0 and t=3 min using a set
of forms by pressing the ini-finl button.

13. Now the program can be run since the purple arrow appears, but I recommend that you use the
Arrange equation feature to order your equations. This will be easier for your professor to
troubleshoot and/or grade when put in this order.

11
ODE’s are at the top

Explicit Equations

Initial conditions: VariableName(0)

Final condition for independent variable VariableName(f)

Figure 9: Result of the arrangement of equations (the ordering of the initial conditions may be different
to that shown above)

12
14. Now enter a problem title by selecting Edit, Enter Problem Title…

15. Next save the program with a file name and then run the
program by pressing on the pink arrow. The default
output is the POLYMATH Report.

Run program

Figure 10: Default output from POLYMATH

13
16. Polymath has a program for graphs but you can also produce a graph in excel. In this
case you should do the following

1. Select the Table output button


Table Output
button

2. Run the program again

3. Select the table window

4. Click on the upper left corner of the table (similar to excel)

Copy with Headers

Upper left Corner


Selects all

5. Then select Edit, Copy With Headers. (This will copy the names of the variables
as well as the numbers)

6. Paste this into an excel spreadsheet and produce a graph with all titles given and
labels.

For homework assignments with POLYMATH I have students paste a pdf of the handwritten
derivation of equations, the POLYMATH Report and the graph into a word document.

14
POLYMATH Report Series Reactions
Ordinary Differential Equations 26-May-2017

Calculated values of DEQ variables


Variable Initial value Minimal value Maximal value Final value
1 CA 1. 0.0497871 1. 0.0497871
2 CB 0 0 0.2499865 0.0473083
3 CC 0 0 0.9029046 0.9029046
4 k1 1. 1. 1. 1.
5 k2 2. 2. 2. 2.
6 t 0 0 3. 3.

Differential equations
1 d(CC)/d(t) = k2*CB
Concentration of component C
This is what the electronic part of
your homework should look like! But
2 d(CB)/d(t) = k1*CA-k2*CB
remember to also submit the hand
concentration of component B
derivation of the equations used in
3 d(CA)/d(t) = -k1*CA
the POLYMATH model and answer
Concentration of component A
any questions that were asked. For
example the concentration of A, B,
Explicit equations
and C at t=3 s.
1 k2 = 2
units of min^-1

2 k1 = 1
Units of min^-1

General
Total number of equations 5
Number of differential equations 3
Number of explicit equations 2
Elapsed time 0.000 sec
Solution method RKF_45
Step size guess. h 0.000001
Truncation error tolerance. eps 0.000001

Data file: h:\documents\cache\aseeworkshop\series reactions example.pol

15
Concentration (mol/m3)

CC
CB
CA

Time (minutes)

Figure 11: Concentration Profiles of a Series Reaction

16
Troubleshooting Example

As you can see POLYMATH is a very easy program to use. In the previous batch reactor example you
probably produced a POLYMATH Report in less than 5 minutes (or if you did it a second time it would be
even shorter). Some of the problems that you solve will be more complex than this and it is useful to
see how to avoid errors in placing your model equations into polymath. Of course one of the biggest
errors that students make is that they don’t write out the equations. In addition they don’t place
numbers with units in them to make sure that all the units are consistent. This is probably the biggest
error done by students. Numbers and units!!! Unfortunately POLYMATH can not help the student with
errors in units other than putting the units in comment fields. What follows is the errors that
POLYMATH can identify.

17. Common errors by students in using Polymath software:

1. What the user thinks are the same variables, but the computer uses them as different
variables. This happens when you incorrectly spell a variable name or you do not match
upper or lower cases. (e.g. 𝑇𝑇𝑇𝑇𝑇𝑇 ≠ 𝑡𝑡𝑡𝑡𝑡𝑡)

2. Defining a variable more than once. Once a variable is on the left hand side of the equals
sign then it is considered defined by the program.

3. Dividing by zero

4. Using too many parenthesis. POLYMATH uses the standard order of operators which is
exponent, multiplication/division, addition/subtraction: ^, (* or /), (+ or -) which is
invoked working from left to right in an expression.

Cut and paste the following program in the POLYMATH ODE Solver or load the program
troubleshooting errors example.pol. Examine each of these errors and then correct them as directed in the
error explanation below:
d(vtheta) / d(r) = vtheta/r-tau/mu
d(gamma) / d(r) = 0
tau = Gamma/r^2
tau=2
vtheta =omega*R1
r1=0.1
r(0) =0.1
r(f) = 0.12
vtheta(0) =0
gamma(0) =-6.5455E-06
error = (0.012-vtheta)/.012*100

17
18. To see the errors in your program look in the message area or alternatively to see a full
list: Select Problem from the Polymath menu and then Show Variables… to see the
following errors in this program. Alternatively you could just press on the information
button

Message Area

To see more than one


message go to Problem,
Show Variables or click or
select the button

Figure 12: Original Code

Figure 13: Request to show all of the errors by selecting Problem and then Show Variables

18
Figure 14: Original listing of variables and errors

Explanation of Errors in Message:

Differential variables (2); vtheta, gamma There are three ordinary differential equations, but one
of the equations has a syntax error and is not recognized. See below.

Explicit variables (5); tau, tau, vtheta, r1, error There are 5 explicit (variable = ) equations.
Notice that it has tau twice which means that it was defined more than once as an explicit
equation. This is an error that is also mentioned in the “Multiple (This is a spelling error!)
defined variables”

Independent variable = r The ODE’s are with respect to only one variable and that variable is r.
This is OK

19
19. Now for the RED X’s X Error in Line #2, Syntax Error. Please enter a valid
implicit/explicit equation or boundary condition. d(omega*R1) / d(r) = vtheta/r-tau/mu The
error in this equation is the use of an operator on the left hand side which is not allowed.
If this equation was required you would need to define a new variable: junk=omega*R1.
The correct form is using the line #1 ODE so to correct this error we will delete line 2.

There should not be a multiplication operator between omega


and R1. If this equation was required you would need to
define a new variable: junk=omega*R1.
But for this example we will just Delete line #2

Figure 15: Error in line #2

20
Figure 16: Resulting code after deleting line #2 from above. To see this code you need to click on
another line and the message box will update to the next error.

20. X Undefined variables : mu, Gamma, omega, R1 The variable mu is used in line 1, but
not defined. You need to add an explicit equation mu=0.001. The reason that Gamma
is undefined is that it never appears on the left hand side of the equal sign, but the
variable gamma does! To correct this error change the case of the g in Gamma. The
remaining undefined variables we will correct in the next error.

Change the case of G to g. Variables names are case sensitive

Add mu=0.001 to define the variable mu

21
Figure 17: Result after adding definition of mu and change case of G to g

21. X Variables defined more than once: vtheta, tau There are two equations that have tau =
on lines 3 and 4. The variable vtheta has been defined twice; once in line #1 as an
ordinary differential equation and the second time in line #5 shown below. Equation 5
(vtheta =omega*R1) must be deleted in this problem. A variable is defined by having it on
the left hand side of the equal sign. Additionally there can only be one variable on the
left hand side of an equal sign.
Also on line #4 you should delete the equation (tau = 2) since it is already defined in the
equation above it (#3).

Delete the line with tau=2


since tau is defined in line #3
with the equation
tau=gamma/r^2

Delete vtheta=omega*R1 since vtheta is


defined in line #1 using an ODE

Figure 18: tau and vtheta corrections

22
22. Now the program is ready to run:

The purple arrow appears and


the program is ready to run!

Figure 19: Program is ready to run

23. The following errors from the original code were fixed by following the
steps above:

X Multiply (sp Multiple) defined variables : There are 2 variables (tau and vtheta) defined more
than once that are involved in multiplication (This seems to be a strange error code). You
already deleted the equation on line 4 (tau = 2) since you can only define it once.

X Warning: 2 Duplicate explicit variable/s exist/s. Again these are vtheta, tau.

X Warning: 4 Undefined variable/s exist. This is also giving the number of variables undefined
as 4.

X Warning: 2 Multiply defined variable/s exist. Again a repeat of the above.

23
Line 2 you deleted this equation
d(omega*R1) / d(r) = vtheta/r-tau/mu since it contained an
operator on the right hand side

Line 5 you deleted this line tau = 2 since tau was


defined in the line above

Line 6 you deleted this equation vtheta =omega*R1 since it


is defined in the first line which conatins an ODE

Problem Title entered from


Edit, Enter Problem Title

File name

Figure 20: Original program will errors

24. One last step is to have POLYMATH automatically order your equations. This is
optional but very useful for the professor to grade your homework. Go to the menu and
select Problem and then Arrange equations.

Figure 21: Arrange Equations

24
25. The result after arranging equations will put the ODE’s at the top, followed by explicit
eqautions and then initial and final conditions as shown below.

ODE’s are at the top

Explicit Equations

Final condition for independent variable VariableName(f)

Initial conditions: VariableName(0)

26. Now run your corrected program by pressing the Pink arrow

Run Program

27. Your program should run and produce an output page like the following:

25
Problem Title must have been entered from
the menu Edit, Enter Problem Title

For homework assignments you will be required to copy and paste this page into a word
document that will also contain answers to questions, graphs and sample calculations that
will be uploaded to Blackboard.

26
POLYMATH Report Fluid contained between two co-axial cylinders with outer cylinder rotating
Ordinary Differential Equations 21-Jun-2017

Calculated values of DEQ variables


Variable Initial value Minimal value Maximal value Final value
1 error 100. -0.0006944 100. -0.0006944
2 gamma -6.546E-06 -6.546E-06 -6.546E-06 -6.546E-06
3 mu 0.001 0.001 0.001 0.001
4 r 0.1 0.1 0.12 0.12
5 r1 0.1 0.1 0.1 0.1
6 tau -0.0006546 -0.0006546 -0.0004545 -0.0004545
7 vtheta 0 0 0.0120001 0.0120001

Differential equations
1 d(vtheta)/d(r) = vtheta/r-tau/mu
2 d(gamma)/d(r) = 0

Explicit equations
1 tau = gamma/r^2
2 mu = 0.001
3 r1 = 0.1
4 error = (0.012-vtheta)/.012*100

General
Total number of equations 6
Number of differential equations 2
Number of explicit equations 4
Elapsed time 0.000 sec
Solution method RKF_45
Step size guess. h 0.000001
Truncation error tolerance. eps 0.000001

Data file: z:\home\documents\cache\aseeworkshop\troubleshooting errors example.pol

27
Remember to always enter a problem title by selecting Edit, Enter Problem Title…

Figure 22: Always enter a problem title so that the professor will know what problem you solved.

28. Rename and Save this program for submission on blackboard. The new filename should
have your last name as part of the title.

29. Other types of errors cause a program to stop running. This error starts after you press
the Pink arrow to start solving the problem below and the solution stops with an error
message window titled Polymath Guide. Below is a different example to show this error.
Again copy and paste the below code into an ODE solver (or load the program
divideByZeroCode.pol) and then run the code
d(tau_r)/d(r)= delP/L*r
d(vx)/d(r)=-(tau/K)^(1/n)
K=1e-6
delP=100
L=10
tau=tau_r/r
#tau=if(r>0) then (tau_r/r) else(0)
n=2
R=0.009295
r(0)=0
tau_r(0)=0
vx(0)=1.3358812
r(f)=0.009295

28
Figure 23: Divide by zero example after pressing the pink arrow

In line 6, the value of r at the beginning of the integration is zero (r(0)=0) so the program tries to
divide by zero in the expression in line 6 (tau = tau_r/r). This causes the program to stop and this
error code appears. To correct this error, the program needs an If, Then Else statement which
has been commented out using a pound sign. This is shown in line #7: #tau = if (r >0) then
(tau_r/r) else (0). The pound sign # is used as a comment marker. Delete the line: tau=tau_r/r and
then remove the # that is used as a comment marker. Anything after the comment marker is
ignored by the solver. Now the program will run.

30. Save this program for submission on blackboard. Again the filename should have your
last name in it.

Additional tips for troubleshooting are found in the help menu for ODE’s which can be found by
clicking on the yellow question mark or pressing the F1 key.

Click on the
question mark
here for help

29
Now scroll down or click the Collapse All to go to the section marked trouble shooting as shown
below. Of particular interest will be what happens if Integration progresses very slowly and you
need to use a stiff algorithm:

30

You might also like