Part A. System of Linear Equations: Computer Lab # 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

CHEN2004 2020

Name: Click here to enter text. Student ID: Click here to enter text.

Date: Click here to enter text. Time: Click here to enter text.

Computer lab # 3
Instructions:

 Download the Excel template from BB.


 Save it as IC-3-yourstudentID.xlsm
 Enter your answers into a separate spreadsheet according to the spreadsheet name.
 For Part A, you can copy the code (page 3) and extend it
 For Part B, you can modify the previous VBA code (IC-2), using delx as an input.
 After working in Excel, write down your answers in the space provided below.
 You need to submit both your paper and Excel file for marking.

Part A. System of linear equations


(1+1.5+0.5 mark)
Question A1. Solve the system of four equations in the template by minimizing the sum of the
square of the differences. To understand the relationship between the template and reactors
system, please review the lecture note, homework and recordings for lab 3. An Excel template is
provided to help you formulate the problems.

Explanation of the template: the system can be simplified to 4 equations as below

U 0  k1V1  C1 U 0C0


U1C1    U r3  U 0   k 2V2  C2  U r 3C3  0

 U r3  U 0  C2    U r3  U 0  U r 4   k3V3  C3  U r 4 C4  0

  U 0  U r 4  C3   U 0  U r 4  k4V4  C4  0

The four equations can be written matrix form. The cells from M12 to P16 represent the 4x4
coefficients, cells Q12 to Q16 contain C1 to C4. The values of U3R and U4R are in cell J18 and J19.

In question A1, you solve the system of four equation by Solver:

a- Formulate the four squares of differences in cells


b- Formulate the sum of squares in cell
c- Use Solver to minimize cell by changing cells (use your homework answers to check)
CHEN2004 2020

Answers: C1 = Click here to enter text. C2 = Click here to enter text.


C3 = Click here to enter text. C4 = Click here to enter text.

Question A2. You need to save your Excel in the macro-enabled format. Go to sheet A2. Write
a VBA code to solve the system using Gaussian-Seidel iterative method (see the hints for VBA
code). What are the solutions?

Answers: C1 = Click here to enter text. C2 = Click here to enter text.


C3 = Click here to enter text. C4 = Click here to enter text.

Question A3. If U3R = 80 L/hr, how much should be recycled from 4 th reactor (i.e. U4R) to achieve
the final concentration C4A = 0.5 mol/L?

Answer: U4R = Click here to enter text. L/hr

Hints
You can use the following structure for the Gauss-Seidel method. The inputs are A – matrix of
coefficients (n rows by n columns), B – an array (n element), Cguess – an array (n element), tol –
err. The output is Cin – an array of n elements.

Function GS(a, b, Cguess, tol)


Dim err(10), row, col, n, sum, Cin(10), cnew(10), MaxErr, k
MaxErr = 1: k = 0: n = a.Rows.Count 'n = number of rows in matrix “a”
For row = 1 To n
Cin(row) = Cguess(row)
Next row
While MaxErr > tol And k < 15
k = k + 1: MaxErr = 0 ’ increase k after each iteration
For row = 1 To n
sum = 0 'reset sum
For col = 1 To n
sum = sum + a(row, col) * Cin(col) ‘ sum is “a21*x1+a22*x2+a23*x3”
Next col

cnew(row) = (b(row) - sum + Cin(row) * a(row, row)) / a(row, row) 'calculate Cnew
err(row) = Abs(cnew(row) - Cin(row)) 'there will be n errors, the code stops when ALL errors <
tol, or sum of all errors < tol....
………………….’ updating values, cin(1,2,3,4) will be the input at start of next loop
…………………………………………..’calculate MaxErr for each iteration
Next row
Debug.Print k, Cin(1), Cin(2), Cin(3), Cin(4), MaxErr ‘showing these values in the Intermediate
Window
CHEN2004 2020

Wend ‘ next iteration


GS = Cin ‘oupout as array (cells are in horizontal …)
End Function

Notes:

For G-S methodology (see picture below), you can either use

For col=1 to n
Sum=Sum + a(row,col)*cin(col)
Next col
Cnew(row)=(b(row)-sum+a(row,row)*cin(row))/ a(row,row)

OR

For col=1 to n
If col<>row then
Sum=Sum + a(row,col)*cin(col)
End if
Next col
Cnew(row)=(b(row)-Sum)/a(row,row)

Iterative methods: (a) Gauss-Seidel, (b) Jacobi


CHEN2004 2020

Part B. Runge-Kutta methods for ODEs


(0.25 + 0.25 + 0.5 + 0.5 + 0.5 mark)
Add a new sheet to your Excel file and name it “Part B”.

The concentration of fatty acids (A) in a biodiesel reactor is given as a function of time by a function
of A is given by:
dA
=40−3 A
dt
At t=0, the fatty acid concentration is 50 g/L.
Solve numerically for fatty acid concentration after 1 minute:
1. Euler method with a step size of 0.5

B1. Answer: Click here to enter text. 0.25 mark


2. Euler method using a step size of 0.05

Answer: Click here to enter text. 0.25 mark


3. 4th Runge-Kutta method with a step size of 0.5

Answer: Click here to enter text. 0.5 mark


4. Plot the three results and the exact answer:
110 −3 t 40
A(t )= e +
3 3
Insert your graph in to box below (1 mark)

5. Compare the numerical solutions in terms of the number of calculations, accuracy at t=1 and
overall accuracy.

Discussion: Click here to enter text. 0.25 mark

You might also like