PSOD Lecture 2 Matrices and vectors in Chemical & Process Engineering Appear in calculations when process is described by the system of equations: – Piping system – Cascade of » Reactors » Heat exchangers » Mixers – System of apparatus and streams in chemical plant Matrices in Chemical & Process Engineering L, cs V L, c 1 V L, c 2 V L, c 3 V L, c4 concentrations give 4-elements vector c To find solution we need system of 4 equations Equation parameters creates square matrix V , c s Matrices in Chemical & Process Engineering V , c 1 V , c 2 V , c 3 V , c 4 input output source(reaction) Lcs Lc1 c1kV Lc1 Lc2 kVc2 Lc2 Lc3 kVc3 Lc3 Lc4 kVc4 c1 L kV 0c2 0c3 0c4 Lcs Lc1 L kV c2 0c3 0c4 0 0c1 Lc2 L kV c3 0c4 0 0c1 0c2 Lc3 L kV c4 0 V , Matrices in Chemical & Process Engineering c s V , c 1 V , c 2 V , c V , c 3 4 L kV L 0 0 0 L kV 0 0 L L kV 0 L 0 0 L kV 0 c1 Lcs c 0 2 c3 0 c 0 4 MathCAD – vectors and matrix MathCAD – vectors and matrix Matrix operations – – – – – Multiply by constant Matrix transpose [ctrl]+[1] Inverse [^][-][1] Matrix multiplying Determinant MathCAD – vectors and matrix To read the matrix elements Ar, k: key [[] rrow nr, k – column nr – e.g. element A1,1 keystrokes: [A][[][1][,][1][=] To chose matrix column: M<col.nr> – First column A( A<0>): keys [A][ctrl]+[6][0] Default first column&row number is 0, – (to change : Math/Options/Array Origin) MathCAD – vectors and matrix Calculations of dot product and cross product of vectors MathCAD – vectors and matrix Special definition of matrix elements as a function of row-column number Mi,j=f(i,j) – E.g. Value of element is equal to product of column and row number Constrain: function arguments have to be integer MathCAD 3D graphs 3D graphs of function on the base of matrix : [ctrl]+[2] [M] – M – matrix defined earlier MathCAD 3D graphs 3D Graphs of function of real type arguments – – – Using procedure: CreateMesh(function, lb_v1, ub_v1, lb_v2, ub_v2, v1grid, v2grid) Assign result to variable Plot of the variable is similar to plot of matrix ([ctrl]+[2]) Boundaries can be the real numbers. (def. –5,5) Grids have to be integer numbers (def. 20) MathCAD 3D graphs MathCAD 3D graphs - formating MathCAD 3D graphs – formatting: fill options MathCAD 3D graphs – formatting: fill options Contours colour filled MathCAD 3D graphs – formatting: line options MathCAD 3D graphs – formatting: Lighting MathCAD 3D graphs – formatting: Fog and perspective MathCAD 3D graphs – formatting: Backplane and Grids MathCAD 3D scatter graphs Data given as three vectors of each point coordinates – Equal vector size – Button on Graph toolbar: 3D Scatter Plot – In the placeholder type in brackets the vectors names separated by comas Predefined constants e = 2,718 – natural logarithm base g = 9,81 m/s2 – acceleration of gravity = 3,142 – circle perimeter/diameter ratio Solving of algebraic equation When equation is implicit When we don’t want to separate variables MathCAD equation solvers Single equation (one unknown value) 1. Given-Find method » » Input start point of variable Type "Given" » » Type equation with using [=] ([ctrl]+[=]) Type Find(variable)= MathCAD equation solving Given-Find – solving methods – – Linear (function of type y=c0x + c1) –starting point choice do not affects on results. Nonlinear – according to nonlinear equation. Obtained result could depend on starting point. Available methods: » » » » Conjugate Gradient Quasi – Newton Levenberg-Marquardt Quadratic The choice of method is automatic by default. User can choose method from the pop-up menu over word Find. MathCAD equation solving Single equation (one unknown value) 2. Root procedure: Root(function, variable, low_limit, up_limit)= – Values of function at the bounds must have different signs or MathCAD equation solving Single equation (one unknown value) 2. Root procedure methods: 1. 2. Secant method Mueller method (2nd order polynomial) y1 x4 x2 y 2 x2 y3 x3 x5 x4 x2 x3 y 2 y3 x1 y2 xi 1 xi 1 yi 1 xi 1 xi yi 1 yi MathCAD equation solving Single equation (one unknown value) 3. Special procedure: polyroots for the polynomials. Argument of procedure is a vector of polynomial coefficients (a0, a1...). The result is a vector too. Methods: 1. Laguerre's method 2. companion matrix Laguerre's method Polynomial p(x) of degree n. Starting from assumed xk. pxk G p xk pxk H G p xk 2 a n G n 1nH G 2 xk 1 xk a MathCAD, the system of equations solving The system of linear equations – Solving on the base of matrix toolbar: » Prepare square matrix of equations coefficients (A) and vector of free terms (B) » Do the operation x:=A-1B and show result: x= Or » Use the procedure LSOLVE: lsolve(A,B)= MathCAD, the system of equations solving MathCAD, the system of equations solving The system of nonlinear equation – Can be solved using given-find method » » Assign starting values to variables Type Given » Type the equations using = sign (bold) » Type Find(var1, var2,...)= MathCAD, the system of equations solving Differential eq. Solvers in MathCAD Ordinary differential equations solving Numerical methods: – Gives only values not function – Engineer usually needs values – There is no need to make complicated transformations (e.g. variables separation) – Basic method implemented in MathCAD is Runge-Kutta 4th order method. Ordinary differential equations solving Numerical methods principle – Calculation involve bounded range of independent variable only – Every point is being calculated on the base of one or few points calculated before or given starting points. – Independent variable is calculated using step: xi+1 = x i + h = xi+Dx – Dependent value is calculated according to the method yi+1 = y i +Dy= y i +Ki Ordinary differential equations solving Runge-Kutta 4th order method principles: – New point of the integral is calculated on the base of one point (given/calculated earlier) and 4 intermediate values k1 hF xi , yi 1 1 k 2 hF xi h, yi k1 2 2 1 1 k3 hF xi h, yi k 2 2 2 k 4 hF xi h, yi k3 1 k1 2k2 k3 k4 6 yi 1 yi K O h 5 K MathCAD differential equations Single, first order differential equation dy f ( x, y ) dx Initial condition x x0 , y x x0 y0 1. Assign the initial value of dependent variable (optionally) 2. Define the derivative function 3. Assign to the new variable the integrating function rkfixed: R:=rkfixed(init_v, low_bound, up_bound, num_seg, function) MathCAD, differential equations 4. Result is matrix (table) of two columns: first contain independent values second dependent ones x0 x1 R x2 ... x N y1, 0 y1,1 y1, 2 ... y1, N 5. To show result as a plot: R<1>@R<0> MathCAD differential equations MathCAD differential equations System of first order differential equations dy0 f x , y , y 0 1 dx dy1 f x, y , y 0 1 dx x x0 y0 xx0 y 0 0 y1xx0 y 0 1 1. Assign the vector of initial conditions of dependent variables (starting vector) 2. Define the vector function of derivatives (right-hand sides of equations) 3. Assign to the variable function rkfixed: R:=rkfixed(init_vect, low_bound, up_bound, num_seg, function) MathCAD differential equations 4. Result is matrix (table) of three columns: first contain independent values, 2nd column contains first dependent variable values, third second ones : x0 y1,0 y2, 0 x1 y1,1 y2,1 R x2 y1, 2 y2, 2 ... ... ... x N y1, N y2, N 5. Results as a plot: R<1>,R<2>@ R<0> MathCAD differential equations x x0 y0 xx0 y00 y1xx0 y10 MathCAD differential equations Single second order equation d y dy f x, y , 2 dx dx 2 Initial condition x x0 , y x x0 y0 dy y0 dx x x0 1. Transform the second order equation to the system of two first order equations: y z0 , dy dz0 z1 , dx dx dz0 dx z1 dz1 f x, z , z 0 1 dx d 2 y dz1 2 dx dx x x0 z0 xx z00 0 z1xx0 z10 MathCAD differential equations Example: Solve the second order differential equation (calculate: values of function and its first derivatives) given by equation: d2y 2 x 3 y y 2 dx While y=10 and y’=-1 for x=0 In the range of x=<0,1> MathCAD differential equations System of equations Starting vector Vectoral function