1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

40
1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2

Transcript of 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

Page 1: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

1

Tips for solving Project 1

Reactor

SO3

SO2 +O2

Page 2: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

2

Reactor

In Project 1 you are asked to develop differential equations to solve for the total pressure, temperature, gas velocity and partial pressures at steady state on a reactor.

Then you need to solve a set of differential equations. How can that be done in Matlab?

v (z)=?P (z)=?T (z)=?Pi

(z)=?

z

Page 3: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

3

Let’s take a look at the following very simple, worked-out example:

( )

( )

( 0) 0

( 0)

0 2

df ag x

dx bdg

abf xdx

f x

g x b

x

Page 4: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

4

Notice that we have a system of 2 ordinary differential equations with its boundary conditions.

The solution is

How’s the script in Matlab?

( ) sin( )

( ) cos( )

f x ax

g x b ax

Page 5: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

5

We start a script on the Matlab Editor and give a name to it

We should save the file with the same name, for example: “project_1_example.m”

If we want to have the results for “f” and “g” available on the workspace, we need to give them as the output on the same line

Page 6: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

6

Next, to avoid problems and a lot of confusion, a good habit when programming in Matlab, is to clear the memory for all variables from previous simulations before we start a new one. So on the top of the program we can use the command “clear all”

Also we can use “clc” to clear the Command Window; and “close all” to close any graphics we might have open

Page 7: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

7

Then we define the parameters that are going to be globally available by declaring them global

Page 8: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

8

Then, we set the values of those parametersA semicolon at the end of the line avoids displaying this line on the screen when the script is run

Page 9: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

9

Next, we specify the integration interval over which Matlab should integrate. For that, we create a vector containing the initial and final values for the independent variable

Page 10: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

10

We can always make comments about our code. This helps remember what we were doing when we see the code in the future.Matlab ignores all the text after a percentage sign (%).

Next, we specify the integration interval over which Matlab should integrate. For that, we create a vector containing the initial and final values for the independent variable

Page 11: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

11

Now we set the boundary conditions on a vector “x0”. First we give the value for “f” and then the one for “g”

If we had, let’s say, 3 differential equations, we would simply give here a vector with 3 columns. We would proceed in a similar fashion if we had only one equation instead of a system

Page 12: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

12

Here we use the function “odeset” which will allow us to specify options to the ODE solver.

For instance “abstol” determines the maximum error for every component of the solution

Page 13: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

13

Other options are available, for example to control the step size and so on.

Page 14: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

14

Here we call the ODE solver “ode15s”

Page 15: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

15

Here, “x” is a vector with the discretization in the independent variable. The number of points will be determined by Matlab, automatically. The number of points will increase as we demand smaller tolerances through the “odeset” function.

Page 16: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

16

“y” is a solution array that contains the solutions, as columns.Each row in the solution array “y” corresponds to a value in the column vector “x”

Page 17: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

17

Here we give the name of the .m file where we have written the equations.(We will see the script in a few slides!)

Page 18: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

18

“xspan” is the vector with the integration interval that we defined previously

Page 19: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

19

“x0” is how we called the initial values vector of our differential equations

Page 20: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

20

“project_1_options” are the options that we defined regarding error tolerances

Page 21: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

21

We have provided the ODE solver with all the information that it needs to deliver the solution to us!

Next on the script we shall plot the results obtained

Page 22: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

22

We have used here “ode15s”, but other solvers are available. Which one is best will depend upon the properties of the system that we are solving, for example, its stiffness

Page 23: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

23

Next we define the vectors “f” and “g” equal to the first and second columns of the solution array “y”

Page 24: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

24

Finally, we plot the functions “f” and “g” on the same figure using the function “subplot”

Page 25: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

25

Now let’s take a look at the script “math_example.m” where we write the equations

Page 26: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

26

Now let’s take a look at the script “math_example.m” where we write the equations

“dydx” is the output (the solution array) that contains the columns with the values for “f” and “g”

Page 27: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

27

Now let’s take a look at the script “math_example.m” where we write the equations

“dydx” is the output (the solution array) that contains the columns with the values for “f” and “g”

“x” is the independent variable and “y” is array that contains both “f” and “g”

Page 28: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

28

Here we make visible the global parameters to the function “math_example”

Page 29: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

29

Next, we identify our functions in the “y” array

Page 30: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

30

Here we write the system of equations

( )

( )

df ag x

dx bdg

abf xdx

Page 31: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

31

And now we put the derivatives togheter in the array “dydx”

It’s done!

Page 32: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

32

Page 33: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

33

The results with A=1 and B=2

Page 34: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

34

Some more tips…In the problem you are asked to find the total pressure.

Using the balance for component j we find that for steady-state and without dispersion, we have:

Using the ideal gas law to replace cj:

Page 35: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

35

Some more tips…Now we need to operate to get an expression for the

derivative of pj with respect to z…

Then solving for the derivative of pressure, we obtain

Page 36: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

36

Some more tips…In the problem you are also asked to find the gas

velocity. For plug-flow the total molar flux is:

Using again the ideal gas law, where the total pressure is P, and differentiating:

Page 37: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

37

Some more tips…Solving for the derivative of velocity, we obtain:

So we have found what Matlab needs as input: an expression for the derivative of velocity.

For the total pressure, we have from plug-flow theory:

Page 38: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

38

Some more tips…But since the mass flux is constant, we know that

So the Ergun equation can be written as

Page 39: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

39

Some more tips…Now let’s look at the temperature. The energy equation

is:

Now taking we get:

Page 40: 1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.

40

Some more tips…Solving for the derivative of temperature, we obtain

where

and