https://learnche.org/wiki_3E4/api.php?action=feedcontributions&user=Kevindunn&feedformat=atom Process Model Formulation and Solution: 3E4 - User contributions [en] 2019-11-22T10:40:04Z User contributions MediaWiki 1.28.0 https://learnche.org/wiki_3E4/index.php?title=Modelling_and_scientific_computing&diff=1119 Modelling and scientific computing 2019-01-14T21:31:51Z <p>Kevindunn: /* Approximation and computer representation */</p> <hr /> <div>== Process modelling slides ==<br /> <br /> Please [[Media:A-Modelling-20-Sept-2010.pdf|download the lecture slides]]. 13 September 2010 (slides 1 to 8)&lt;br&gt;15 September 2010 (slides 9 to 15)&lt;br&gt;16 September 2010 (slides 16 to 19)&lt;br&gt;20 September 2010 (slides 20 to the end)<br /> <br /> <br /> <br /> == Approximation and computer representation ==<br /> <br /> Please [[Media:A-Approximation-and-roundoff-23-Sept-2010.pdf|download the lecture slides]]. 22 and 23 September (updated)<br /> <br /> * More about [http://www.around.com/ariane.html the Ariane 5 rocket] 16-bit overflow<br /> * Python code used in class for ...<br /> <br /> '''Calculating relative error'''<br /> &lt;html&gt;&lt;div data-datacamp-exercise data-lang=&quot;python&quot; data-height=&quot;auto&quot;&gt;<br /> &lt;code data-type=&quot;sample-code&quot;&gt;<br /> import numpy as np<br /> y = 13.0<br /> n = 3 # number of significant figures<br /> rel_error = 0.5 * 10 ** (2-n) # relative error calculation<br /> x = y / 2.0<br /> x_prev = 0.0<br /> iter = 0<br /> while abs(x - x_prev)/x &gt; rel_error:<br /> x_prev = x<br /> x = (x + y/x) / 2.0<br /> print(abs(x - x_prev)/x)<br /> iter += 1<br /> <br /> print('Used %d iterations to calculate sqrt(%f) = %.20f; '<br /> 'true value = %.20f\n ' % (iter, y, x, np.sqrt(y)))<br /> &lt;/code&gt;<br /> &lt;/div&gt;&lt;/html&gt;<br /> <br /> <br /> '''Working with integers'''<br /> &lt;html&gt;&lt;div data-datacamp-exercise data-lang=&quot;python&quot; data-height=&quot;auto&quot;&gt;<br /> &lt;code data-type=&quot;sample-code&quot;&gt;<br /> import numpy as np<br /> <br /> print(np.int16(32767))<br /> print(np.int16(32767+1))<br /> print(np.int16(32767+2))<br /> <br /> # Smallest and largest 16-bit integer<br /> print(np.iinfo(np.int16).min, np.iinfo(np.int16).max)<br /> <br /> # Smallest and largest 32-bit integer<br /> print(np.iinfo(np.int32).min, np.iinfo(np.int32).max)<br /> &lt;/code&gt;<br /> &lt;/div&gt;&lt;/html&gt;<br /> <br /> '''Working with floats'''<br /> &lt;html&gt;&lt;div data-datacamp-exercise data-lang=&quot;python&quot; data-height=&quot;auto&quot;&gt;<br /> &lt;code data-type=&quot;sample-code&quot;&gt;<br /> import numpy as np<br /> <br /> help(np.finfo) # Read what the np.finfo function does<br /> <br /> f = np.float32 # single precision, 32-bit float, 4 bytes<br /> f = np.float64 # double precision, 64-bit float, 8 bytes<br /> <br /> print('machine precision = eps = %.10g' % np.finfo(f).eps)<br /> print('number of exponent bits = %.10g' % np.finfo(f).iexp)<br /> print('number of significand bits = %.10g' % np.finfo(f).nmant)<br /> print('smallest floating point value = %.10g' % np.finfo(f).min)<br /> print('largest floating point value = %.10g' % np.finfo(f).max)<br /> <br /> # Approximate number of decimal digits to which this kind<br /> # of float is precise.<br /> print('decimal precision = %.10g' % np.finfo(f).precision) <br /> &lt;/code&gt;<br /> &lt;/div&gt;&lt;/html&gt;<br /> <br /> '''Special numbers'''<br /> &lt;html&gt;&lt;div data-datacamp-exercise data-lang=&quot;python&quot; data-height=&quot;auto&quot;&gt;<br /> &lt;code data-type=&quot;sample-code&quot;&gt;<br /> import numpy as np<br /> <br /> # Infinities<br /> print(np.inf, -np.inf)<br /> <br /> # inf, number exceeds maximum value that<br /> # is possible with a 64-bit float: overflow<br /> print(np.float(1E400)) <br /> <br /> print(np.inf * -4.0) # -inf<br /> print(np.divide(2.4, 0.0)) # inf<br /> <br /> # NaN's<br /> a = np.float(-2.3)<br /> print(np.sqrt(a)) # nan<br /> print(np.log(a)) # nan<br /> <br /> # Negative zeros<br /> a = np.float(0.0)<br /> b = np.float(-4.0)<br /> c = a/b<br /> print(c) # -0.0<br /> print(c * c) # 0.0<br /> <br /> eps = np.finfo(np.float).eps<br /> <br /> # Create a number smaller than machine precision<br /> e = eps/3.0 <br /> # Question: why can we create a number smaller than eps?<br /> print(e)<br /> <br /> # Interesting property: non-commutative operations can occur<br /> # when working with values smaller than eps. Why?<br /> # The print out here should &quot;True&quot;, but it prints &quot;False&quot;<br /> print((1.0 + (e + e)) == (1.0 + e + e))<br /> &lt;/code&gt;<br /> &lt;/div&gt;&lt;/html&gt;<br /> <br /> ==Practice questions==<br /> <br /> &lt;ol&gt;<br /> &lt;li&gt;From the [[Suggested readings | Hangos and Cameron]] reference, ([http://macc.mcmaster.ca/sites/default/files/theses/Hangos_Cameron_2001_pp1-161.pdf available here]] - accessible from McMaster computers only)<br /> <br /> * Work through example 2.4.1 on page 33<br /> * Exercise A 2.1 and A 2.2 on page 37<br /> * Exercise A 2.4: which controlling mechanisms would you consider?<br /> <br /> &lt;li&gt;Homework problem, similar to the case presented on slide 18, except<br /> * Use two inlet streams $$\sf F_1$$ and $$\sf F_2$$, and assume they are volumetric flow rates<br /> * An irreversible reaction occurs, $$\sf A + 3B \stackrel{r}{\rightarrow} 2C$$<br /> * The reaction rate for A = $$\sf -r_A = -kC_\text{A} C_\text{B}^3$$<br /> # Derive the time-varying component mass balance for species B.<br /> #* $$V\frac{dC_B}{dt} = F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} C_{\sf B} + 0 - 3 kC_{\sf A} C_{\sf B}^3 V$$<br /> # What is the steady state value of $$\sf C_B$$? Can it be calculated without knowing the steady state value of $$\sf C_A$$?<br /> #* $$F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} \overline{C}_{\sf B} - 3 k \overline{C}_{\sf A} \overline{C}^3_{\sf B} V$$ - we require the steady state value of $$C_{\sf A}$$, denoted as $$\overline{C}_{\sf A}$$, to calculate $$\overline{C}_{\sf B}$$.<br /> &lt;/ol&gt;<br /> <br /> [[Practice questions | More exercises available here]]</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Modelling_and_scientific_computing&diff=1118 Modelling and scientific computing 2019-01-14T21:30:40Z <p>Kevindunn: /* Approximation and computer representation */</p> <hr /> <div>== Process modelling slides ==<br /> <br /> Please [[Media:A-Modelling-20-Sept-2010.pdf|download the lecture slides]]. 13 September 2010 (slides 1 to 8)&lt;br&gt;15 September 2010 (slides 9 to 15)&lt;br&gt;16 September 2010 (slides 16 to 19)&lt;br&gt;20 September 2010 (slides 20 to the end)<br /> <br /> <br /> <br /> == Approximation and computer representation ==<br /> <br /> Please [[Media:A-Approximation-and-roundoff-23-Sept-2010.pdf|download the lecture slides]]. 22 and 23 September (updated)<br /> <br /> * More about [http://www.around.com/ariane.html the Ariane 5 rocket] 16-bit overflow<br /> * Python code used in class for ...<br /> <br /> '''Calculating relative error'''<br /> &lt;html&gt;&lt;div data-datacamp-exercise data-lang=&quot;python&quot; data-height=&quot;auto&quot;&gt;<br /> &lt;code data-type=&quot;sample-code&quot;&gt;<br /> import numpy as np<br /> y = 13.0<br /> n = 3 # number of significant figures<br /> rel_error = 0.5 * 10 ** (2-n) # relative error calculation<br /> x = y / 2.0<br /> x_prev = 0.0<br /> iter = 0<br /> while abs(x - x_prev)/x &gt; rel_error:<br /> x_prev = x<br /> x = (x + y/x) / 2.0<br /> print(abs(x - x_prev)/x)<br /> iter += 1<br /> <br /> print('Used %d iterations to calculate sqrt(%f) = %.20f; '<br /> 'true value = %.20f\n ' % (iter, y, x, np.sqrt(y)))<br /> &lt;/code&gt;<br /> &lt;/div&gt;&lt;/html&gt;<br /> <br /> <br /> '''Working with integers'''<br /> &lt;html&gt;&lt;div data-datacamp-exercise data-lang=&quot;python&quot; data-height=&quot;auto&quot;&gt;<br /> &lt;code data-type=&quot;sample-code&quot;&gt;<br /> import numpy as np<br /> <br /> print(np.int16(32767))<br /> print(np.int16(32767+1))<br /> print(np.int16(32767+2))<br /> <br /> # Smallest and largest 16-bit integer<br /> print(np.iinfo(np.int16).min, np.iinfo(np.int16).max)<br /> <br /> # Smallest and largest 32-bit integer<br /> print(np.iinfo(np.int32).min, np.iinfo(np.int32).max)<br /> &lt;/code&gt;<br /> &lt;/div&gt;&lt;/html&gt;<br /> <br /> '''Working with floats'''<br /> &lt;html&gt;&lt;div data-datacamp-exercise data-lang=&quot;python&quot; data-height=&quot;auto&quot;&gt;<br /> &lt;code data-type=&quot;sample-code&quot;&gt;<br /> import numpy as np<br /> <br /> help(np.finfo) # Read what the np.finfo function does<br /> <br /> f = np.float32 # single precision, 32-bit float, 4 bytes<br /> f = np.float64 # double precision, 64-bit float, 8 bytes<br /> <br /> print('machine precision = eps = %.10g' % np.finfo(f).eps)<br /> print('number of exponent bits = %.10g' % np.finfo(f).iexp)<br /> print('number of significand bits = %.10g' % np.finfo(f).nmant)<br /> print('smallest floating point value = %.10g' % np.finfo(f).min)<br /> print('largest floating point value = %.10g' % np.finfo(f).max)<br /> <br /> # Approximate number of decimal digits to which this kind<br /> # of float is precise.<br /> print('decimal precision = %.10g' % np.finfo(f).precision) <br /> &lt;/code&gt;<br /> &lt;/div&gt;&lt;/html&gt;<br /> <br /> '''Special numbers'''<br /> &lt;html&gt;&lt;div data-datacamp-exercise data-lang=&quot;python&quot; data-height=&quot;auto&quot;&gt;<br /> &lt;code data-type=&quot;sample-code&quot;&gt;<br /> import numpy as np<br /> <br /> # Infinities<br /> print(np.inf, -np.inf)<br /> print(np.float(1E400)) # inf, number exceeds maximum value that<br /> # is possible with a 64-bit float: overflow<br /> print(np.inf * -4.0) # -inf<br /> print(np.divide(2.4, 0.0)) # inf<br /> <br /> # NaN's<br /> a = np.float(-2.3)<br /> print(np.sqrt(a)) # nan<br /> print(np.log(a)) # nan<br /> <br /> # Negative zeros<br /> a = np.float(0.0)<br /> b = np.float(-4.0)<br /> c = a/b<br /> print(c) # -0.0<br /> print(c * c) # 0.0<br /> <br /> eps = np.finfo(np.float).eps<br /> e = eps/3.0 # create a number smaller than machine precision<br /> # Question: why can we create a number smaller than eps?<br /> print(e)<br /> <br /> # Interesting property: non-commutative operations can occur<br /> # when working with values smaller than eps. Why?<br /> # The print out here should &quot;True&quot;, but it prints &quot;False&quot;<br /> print((1.0 + (e + e)) == (1.0 + e + e)) <br /> &lt;/code&gt;<br /> &lt;/div&gt;&lt;/html&gt;<br /> <br /> ==Practice questions==<br /> <br /> &lt;ol&gt;<br /> &lt;li&gt;From the [[Suggested readings | Hangos and Cameron]] reference, ([http://macc.mcmaster.ca/sites/default/files/theses/Hangos_Cameron_2001_pp1-161.pdf available here]] - accessible from McMaster computers only)<br /> <br /> * Work through example 2.4.1 on page 33<br /> * Exercise A 2.1 and A 2.2 on page 37<br /> * Exercise A 2.4: which controlling mechanisms would you consider?<br /> <br /> &lt;li&gt;Homework problem, similar to the case presented on slide 18, except<br /> * Use two inlet streams $$\sf F_1$$ and $$\sf F_2$$, and assume they are volumetric flow rates<br /> * An irreversible reaction occurs, $$\sf A + 3B \stackrel{r}{\rightarrow} 2C$$<br /> * The reaction rate for A = $$\sf -r_A = -kC_\text{A} C_\text{B}^3$$<br /> # Derive the time-varying component mass balance for species B.<br /> #* $$V\frac{dC_B}{dt} = F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} C_{\sf B} + 0 - 3 kC_{\sf A} C_{\sf B}^3 V$$<br /> # What is the steady state value of $$\sf C_B$$? Can it be calculated without knowing the steady state value of $$\sf C_A$$?<br /> #* $$F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} \overline{C}_{\sf B} - 3 k \overline{C}_{\sf A} \overline{C}^3_{\sf B} V$$ - we require the steady state value of $$C_{\sf A}$$, denoted as $$\overline{C}_{\sf A}$$, to calculate $$\overline{C}_{\sf B}$$.<br /> &lt;/ol&gt;<br /> <br /> [[Practice questions | More exercises available here]]</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Modelling_and_scientific_computing&diff=1117 Modelling and scientific computing 2019-01-14T21:29:27Z <p>Kevindunn: /* Approximation and computer representation */</p> <hr /> <div>== Process modelling slides ==<br /> <br /> Please [[Media:A-Modelling-20-Sept-2010.pdf|download the lecture slides]]. 13 September 2010 (slides 1 to 8)&lt;br&gt;15 September 2010 (slides 9 to 15)&lt;br&gt;16 September 2010 (slides 16 to 19)&lt;br&gt;20 September 2010 (slides 20 to the end)<br /> <br /> <br /> <br /> == Approximation and computer representation ==<br /> <br /> Please [[Media:A-Approximation-and-roundoff-23-Sept-2010.pdf|download the lecture slides]]. 22 and 23 September (updated)<br /> <br /> * More about [http://www.around.com/ariane.html the Ariane 5 rocket] 16-bit overflow<br /> * Python code used in class for ...<br /> <br /> '''Calculating relative error'''<br /> &lt;html&gt;&lt;div data-datacamp-exercise data-lang=&quot;python&quot; data-height=&quot;auto&quot;&gt;<br /> &lt;code data-type=&quot;sample-code&quot;&gt;<br /> import numpy as np<br /> y = 13.0<br /> n = 3 # number of significant figures<br /> rel_error = 0.5 * 10 ** (2-n) # relative error calculation<br /> x = y / 2.0<br /> x_prev = 0.0<br /> iter = 0<br /> while abs(x - x_prev)/x &gt; rel_error:<br /> x_prev = x<br /> x = (x + y/x) / 2.0<br /> print(abs(x - x_prev)/x)<br /> iter += 1<br /> <br /> print('Used %d iterations to calculate sqrt(%f) = %.20f; '<br /> 'true value = %.20f\n ' % (iter, y, x, np.sqrt(y)))<br /> &lt;/code&gt;<br /> &lt;/div&gt;&lt;/html&gt;<br /> <br /> <br /> '''Working with integers'''<br /> &lt;html&gt;&lt;div data-datacamp-exercise data-lang=&quot;python&quot; data-height=&quot;auto&quot;&gt;<br /> &lt;code data-type=&quot;sample-code&quot;&gt;<br /> import numpy as np<br /> <br /> print(np.int16(32767))<br /> print(np.int16(32767+1))<br /> print(np.int16(32767+2))<br /> <br /> # Smallest and largest 16-bit integer<br /> print(np.iinfo(np.int16).min, np.iinfo(np.int16).max)<br /> <br /> # Smallest and largest 32-bit integer<br /> print(np.iinfo(np.int32).min, np.iinfo(np.int32).max)<br /> &lt;/code&gt;<br /> &lt;/div&gt;&lt;/html&gt;<br /> <br /> '''Working with floats'''<br /> &lt;html&gt;&lt;div data-datacamp-exercise data-lang=&quot;python&quot; data-height=&quot;auto&quot;&gt;<br /> &lt;code data-type=&quot;sample-code&quot;&gt;<br /> import numpy as np<br /> <br /> help(np.finfo) # Read what the np.finfo function does<br /> <br /> f = np.float32 # single precision, 32-bit float, 4 bytes<br /> f = np.float64 # double precision, 64-bit float, 8 bytes<br /> <br /> print('machine precision = eps = %.10g' % np.finfo(f).eps)<br /> print('number of exponent bits = %.10g' % np.finfo(f).iexp)<br /> print('number of significand bits = %.10g' % np.finfo(f).nmant)<br /> print('smallest floating point value = %.10g' % np.finfo(f).min)<br /> print('largest floating point value = %.10g' % np.finfo(f).max)<br /> <br /> # Approximate number of decimal digits to which this kind<br /> # of float is precise.<br /> print('decimal precision = %.10g' % np.finfo(f).precision) <br /> &lt;/code&gt;<br /> &lt;/div&gt;&lt;/html&gt;<br /> <br /> '''Special numbers'''<br /> &lt;html&gt;&lt;div data-datacamp-exercise data-lang=&quot;python&quot; data-height=&quot;auto&quot;&gt;<br /> &lt;code data-type=&quot;sample-code&quot;&gt;<br /> import numpy as np<br /> <br /> # Infinities<br /> print(np.inf, -np.inf)<br /> print(np.float(1E400)) # inf, number exceeds maximum value that<br /> # is possible with a 64-bit float: overflow<br /> print(np.inf * -4.0) # -inf<br /> print(np.divide(2.4, 0.0)) # inf<br /> <br /> # NaN's<br /> a = np.float(-2.3)<br /> print(np.sqrt(a)) # nan<br /> print(np.log(a)) # nan<br /> <br /> # Negative zeros<br /> a = np.float(0.0)<br /> b = np.float(-4.0)<br /> c = a/b<br /> print(c) # -0.0<br /> print(c * c) # 0.0<br /> <br /> eps = np.finfo(np.float).eps<br /> e = eps/3.0 # create a number smaller than machine precision<br /> # Question: why can we create a number smaller than eps?<br /> print(e)<br /> <br /> # Interesting property: non-commutative operations can occur<br /> # when working with values smaller than eps. Why?<br /> # The print out here should &quot;True&quot;, but it prints &quot;False&quot;<br /> print((1.0 + (e + e)) == (1.0 + e + e)) <br /> &lt;/code&gt;<br /> &lt;/div&gt;&lt;/html&gt;<br /> <br /> ==Practice questions==<br /> <br /> &lt;ol&gt;<br /> &lt;li&gt;From the [[Suggested readings | Hangos and Cameron]] reference, ([http://macc.mcmaster.ca/sites/default/files/theses/Hangos_Cameron_2001_pp1-161.pdf available here]] - accessible from McMaster computers only)<br /> <br /> * Work through example 2.4.1 on page 33<br /> * Exercise A 2.1 and A 2.2 on page 37<br /> * Exercise A 2.4: which controlling mechanisms would you consider?<br /> <br /> &lt;li&gt;Homework problem, similar to the case presented on slide 18, except<br /> * Use two inlet streams $$\sf F_1$$ and $$\sf F_2$$, and assume they are volumetric flow rates<br /> * An irreversible reaction occurs, $$\sf A + 3B \stackrel{r}{\rightarrow} 2C$$<br /> * The reaction rate for A = $$\sf -r_A = -kC_\text{A} C_\text{B}^3$$<br /> # Derive the time-varying component mass balance for species B.<br /> #* $$V\frac{dC_B}{dt} = F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} C_{\sf B} + 0 - 3 kC_{\sf A} C_{\sf B}^3 V$$<br /> # What is the steady state value of $$\sf C_B$$? Can it be calculated without knowing the steady state value of $$\sf C_A$$?<br /> #* $$F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} \overline{C}_{\sf B} - 3 k \overline{C}_{\sf A} \overline{C}^3_{\sf B} V$$ - we require the steady state value of $$C_{\sf A}$$, denoted as $$\overline{C}_{\sf A}$$, to calculate $$\overline{C}_{\sf B}$$.<br /> &lt;/ol&gt;<br /> <br /> [[Practice questions | More exercises available here]]</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Modelling_and_scientific_computing&diff=1116 Modelling and scientific computing 2019-01-14T21:27:55Z <p>Kevindunn: /* Approximation and computer representation */</p> <hr /> <div>== Process modelling slides ==<br /> <br /> Please [[Media:A-Modelling-20-Sept-2010.pdf|download the lecture slides]]. 13 September 2010 (slides 1 to 8)&lt;br&gt;15 September 2010 (slides 9 to 15)&lt;br&gt;16 September 2010 (slides 16 to 19)&lt;br&gt;20 September 2010 (slides 20 to the end)<br /> <br /> <br /> <br /> == Approximation and computer representation ==<br /> <br /> Please [[Media:A-Approximation-and-roundoff-23-Sept-2010.pdf|download the lecture slides]]. 22 and 23 September (updated)<br /> <br /> * More about [http://www.around.com/ariane.html the Ariane 5 rocket] 16-bit overflow<br /> * Python code used in class for ...<br /> <br /> '''calculating relative error'''<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> y = 13.0<br /> n = 3 # number of significant figures<br /> rel_error = 0.5 * 10 ** (2-n) # relative error calculation<br /> x = y / 2.0<br /> x_prev = 0.0<br /> iter = 0<br /> while abs(x - x_prev)/x &gt; rel_error:<br /> x_prev = x<br /> x = (x + y/x) / 2.0<br /> print(abs(x - x_prev)/x)<br /> iter += 1<br /> <br /> print('Used %d iterations to calculate sqrt(%f) = %.20f; '<br /> 'true value = %.20f\n ' % (iter, y, x, np.sqrt(y)))<br /> &lt;/syntaxhighlight&gt;<br /> <br /> <br /> '''working with integers'''<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> <br /> print(np.int16(32767))<br /> print(np.int16(32767+1))<br /> print(np.int16(32767+2))<br /> <br /> # Smallest and largest 16-bit integer<br /> print(np.iinfo(np.int16).min, np.iinfo(np.int16).max)<br /> <br /> # Smallest and largest 32-bit integer<br /> print(np.iinfo(np.int32).min, np.iinfo(np.int32).max)<br /> &lt;/syntaxhighlight&gt;<br /> <br /> '''working with floats'''<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> <br /> help(np.finfo) # Read what the np.finfo function does<br /> <br /> f = np.float32 # single precision, 32-bit float, 4 bytes<br /> f = np.float64 # double precision, 64-bit float, 8 bytes<br /> <br /> print('machine precision = eps = %.10g' % np.finfo(f).eps)<br /> print('number of exponent bits = %.10g' % np.finfo(f).iexp)<br /> print('number of significand bits = %.10g' % np.finfo(f).nmant)<br /> print('smallest floating point value = %.10g' % np.finfo(f).min)<br /> print('largest floating point value = %.10g' % np.finfo(f).max)<br /> <br /> # Approximate number of decimal digits to which this kind<br /> # of float is precise.<br /> print('decimal precision = %.10g' % np.finfo(f).precision) <br /> &lt;/syntaxhighlight&gt;<br /> <br /> <br /> '''special numbers'''<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> <br /> # Infinities<br /> print(np.inf, -np.inf)<br /> print(np.float(1E400)) # inf, number exceeds maximum value that<br /> # is possible with a 64-bit float: overflow<br /> print(np.inf * -4.0) # -inf<br /> print(np.divide(2.4, 0.0)) # inf<br /> <br /> # NaN's<br /> a = np.float(-2.3)<br /> print(np.sqrt(a)) # nan<br /> print(np.log(a)) # nan<br /> <br /> # Negative zeros<br /> a = np.float(0.0)<br /> b = np.float(-4.0)<br /> c = a/b<br /> print(c) # -0.0<br /> print(c * c) # 0.0<br /> <br /> eps = np.finfo(np.float).eps<br /> e = eps/3.0 # create a number smaller than machine precision<br /> # Question: why can we create a number smaller than eps?<br /> print(e)<br /> <br /> # Interesting property: non-commutative operations can occur<br /> # when working with values smaller than eps. Why?<br /> # The print out here should &quot;True&quot;, but it prints &quot;False&quot;<br /> print((1.0 + (e + e)) == (1.0 + e + e)) <br /> &lt;/syntaxhighlight&gt;<br /> <br /> ==Practice questions==<br /> <br /> &lt;ol&gt;<br /> &lt;li&gt;From the [[Suggested readings | Hangos and Cameron]] reference, ([http://macc.mcmaster.ca/sites/default/files/theses/Hangos_Cameron_2001_pp1-161.pdf available here]] - accessible from McMaster computers only)<br /> <br /> * Work through example 2.4.1 on page 33<br /> * Exercise A 2.1 and A 2.2 on page 37<br /> * Exercise A 2.4: which controlling mechanisms would you consider?<br /> <br /> &lt;li&gt;Homework problem, similar to the case presented on slide 18, except<br /> * Use two inlet streams $$\sf F_1$$ and $$\sf F_2$$, and assume they are volumetric flow rates<br /> * An irreversible reaction occurs, $$\sf A + 3B \stackrel{r}{\rightarrow} 2C$$<br /> * The reaction rate for A = $$\sf -r_A = -kC_\text{A} C_\text{B}^3$$<br /> # Derive the time-varying component mass balance for species B.<br /> #* $$V\frac{dC_B}{dt} = F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} C_{\sf B} + 0 - 3 kC_{\sf A} C_{\sf B}^3 V$$<br /> # What is the steady state value of $$\sf C_B$$? Can it be calculated without knowing the steady state value of $$\sf C_A$$?<br /> #* $$F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} \overline{C}_{\sf B} - 3 k \overline{C}_{\sf A} \overline{C}^3_{\sf B} V$$ - we require the steady state value of $$C_{\sf A}$$, denoted as $$\overline{C}_{\sf A}$$, to calculate $$\overline{C}_{\sf B}$$.<br /> &lt;/ol&gt;<br /> <br /> [[Practice questions | More exercises available here]]</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Modelling_and_scientific_computing&diff=1115 Modelling and scientific computing 2019-01-14T21:26:05Z <p>Kevindunn: </p> <hr /> <div>== Process modelling slides ==<br /> <br /> Please [[Media:A-Modelling-20-Sept-2010.pdf|download the lecture slides]]. 13 September 2010 (slides 1 to 8)&lt;br&gt;15 September 2010 (slides 9 to 15)&lt;br&gt;16 September 2010 (slides 16 to 19)&lt;br&gt;20 September 2010 (slides 20 to the end)<br /> <br /> <br /> <br /> == Approximation and computer representation ==<br /> <br /> Please [[Media:A-Approximation-and-roundoff-23-Sept-2010.pdf|download the lecture slides]]. 22 and 23 September (updated)<br /> <br /> * More about [http://www.around.com/ariane.html the Ariane 5 rocket] 16-bit overflow<br /> * Python code used in class for ...<br /> {| class=&quot;wikitable&quot;<br /> |-<br /> ! calculating relative error<br /> ! working with integers<br /> |-<br /> | width=&quot;50%&quot; valign=&quot;top&quot; class=&quot;mainpage_hubbox&quot;|<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> y = 13.0<br /> n = 3 # number of significant figures<br /> rel_error = 0.5 * 10 ** (2-n) # relative error calculation<br /> x = y / 2.0<br /> x_prev = 0.0<br /> iter = 0<br /> while abs(x - x_prev)/x &gt; rel_error:<br /> x_prev = x<br /> x = (x + y/x) / 2.0<br /> print(abs(x - x_prev)/x)<br /> iter += 1<br /> <br /> print('Used %d iterations to calculate sqrt(%f) = %.20f; '<br /> 'true value = %.20f\n ' % (iter, y, x, np.sqrt(y)))<br /> &lt;/syntaxhighlight&gt;<br /> | width=&quot;50%&quot; valign=&quot;top&quot; class=&quot;mainpage_hubbox&quot;|<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> <br /> print(np.int16(32767))<br /> print(np.int16(32767+1))<br /> print(np.int16(32767+2))<br /> <br /> # Smallest and largest 16-bit integer<br /> print(np.iinfo(np.int16).min, np.iinfo(np.int16).max)<br /> <br /> # Smallest and largest 32-bit integer<br /> print(np.iinfo(np.int32).min, np.iinfo(np.int32).max)<br /> &lt;/syntaxhighlight&gt;<br /> |-<br /> ! working with floats<br /> ! special numbers<br /> |-<br /> | width=&quot;50%&quot; valign=&quot;top&quot; class=&quot;mainpage_hubbox&quot;|<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> <br /> help(np.finfo) # Read what the np.finfo function does<br /> <br /> f = np.float32 # single precision, 32-bit float, 4 bytes<br /> f = np.float64 # double precision, 64-bit float, 8 bytes<br /> <br /> print('machine precision = eps = %.10g' % np.finfo(f).eps)<br /> print('number of exponent bits = %.10g' % np.finfo(f).iexp)<br /> print('number of significand bits = %.10g' % np.finfo(f).nmant)<br /> print('smallest floating point value = %.10g' % np.finfo(f).min)<br /> print('largest floating point value = %.10g' % np.finfo(f).max)<br /> <br /> # Approximate number of decimal digits to which this kind<br /> # of float is precise.<br /> print('decimal precision = %.10g' % np.finfo(f).precision) <br /> &lt;/syntaxhighlight&gt;<br /> |<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> <br /> # Infinities<br /> print(np.inf, -np.inf)<br /> print(np.float(1E400)) # inf, number exceeds maximum value that<br /> # is possible with a 64-bit float: overflow<br /> print(np.inf * -4.0) # -inf<br /> print(np.divide(2.4, 0.0)) # inf<br /> <br /> # NaN's<br /> a = np.float(-2.3)<br /> print(np.sqrt(a)) # nan<br /> print(np.log(a)) # nan<br /> <br /> # Negative zeros<br /> a = np.float(0.0)<br /> b = np.float(-4.0)<br /> c = a/b<br /> print(c) # -0.0<br /> print(c * c) # 0.0<br /> <br /> eps = np.finfo(np.float).eps<br /> e = eps/3.0 # create a number smaller than machine precision<br /> # Question: why can we create a number smaller than eps?<br /> print(e)<br /> <br /> # Interesting property: non-commutative operations can occur<br /> # when working with values smaller than eps. Why?<br /> # The print out here should &quot;True&quot;, but it prints &quot;False&quot;<br /> print((1.0 + (e + e)) == (1.0 + e + e)) <br /> &lt;/syntaxhighlight&gt;<br /> |}<br /> <br /> ==Practice questions==<br /> <br /> &lt;ol&gt;<br /> &lt;li&gt;From the [[Suggested readings | Hangos and Cameron]] reference, ([http://macc.mcmaster.ca/sites/default/files/theses/Hangos_Cameron_2001_pp1-161.pdf available here]] - accessible from McMaster computers only)<br /> <br /> * Work through example 2.4.1 on page 33<br /> * Exercise A 2.1 and A 2.2 on page 37<br /> * Exercise A 2.4: which controlling mechanisms would you consider?<br /> <br /> &lt;li&gt;Homework problem, similar to the case presented on slide 18, except<br /> * Use two inlet streams $$\sf F_1$$ and $$\sf F_2$$, and assume they are volumetric flow rates<br /> * An irreversible reaction occurs, $$\sf A + 3B \stackrel{r}{\rightarrow} 2C$$<br /> * The reaction rate for A = $$\sf -r_A = -kC_\text{A} C_\text{B}^3$$<br /> # Derive the time-varying component mass balance for species B.<br /> #* $$V\frac{dC_B}{dt} = F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} C_{\sf B} + 0 - 3 kC_{\sf A} C_{\sf B}^3 V$$<br /> # What is the steady state value of $$\sf C_B$$? Can it be calculated without knowing the steady state value of $$\sf C_A$$?<br /> #* $$F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} \overline{C}_{\sf B} - 3 k \overline{C}_{\sf A} \overline{C}^3_{\sf B} V$$ - we require the steady state value of $$C_{\sf A}$$, denoted as $$\overline{C}_{\sf A}$$, to calculate $$\overline{C}_{\sf B}$$.<br /> &lt;/ol&gt;<br /> <br /> [[Practice questions | More exercises available here]]</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Modelling_and_scientific_computing&diff=1114 Modelling and scientific computing 2019-01-14T18:55:22Z <p>Kevindunn: </p> <hr /> <div>== Process modelling slides ==<br /> <br /> Please [[Media:A-Modelling-20-Sept-2010.pdf|download the lecture slides]]. 13 September 2010 (slides 1 to 8)&lt;br&gt;15 September 2010 (slides 9 to 15)&lt;br&gt;16 September 2010 (slides 16 to 19)&lt;br&gt;20 September 2010 (slides 20 to the end)<br /> <br /> &lt;html&gt;<br /> &lt;div data-datacamp-exercise data-lang=&quot;python&quot;&gt;<br /> &lt;code data-type=&quot;sample-code&quot;&gt;<br /> import numpy as np<br /> y = 13.0<br /> n = 3 # number of significant figures<br /> rel_error = 0.5 * 10 ** (2-n) # relative error calculation<br /> x = y / 2.0<br /> x_prev = 0.0<br /> iter = 0<br /> while abs(x - x_prev)/x &gt; rel_error:<br /> x_prev = x<br /> x = (x + y/x) / 2.0<br /> print(abs(x - x_prev)/x)<br /> iter += 1<br /> <br /> print('Used %d iterations to calculate sqrt(%f) = %.20f; '<br /> 'true value = %.20f\n ' % (iter, y, x, np.sqrt(y)))<br /> &lt;/code&gt;<br /> &lt;/div&gt;<br /> &lt;/html&gt;<br /> <br /> == Approximation and computer representation ==<br /> <br /> Please [[Media:A-Approximation-and-roundoff-23-Sept-2010.pdf|download the lecture slides]]. 22 and 23 September (updated)<br /> <br /> * More about [http://www.around.com/ariane.html the Ariane 5 rocket] 16-bit overflow<br /> * Python code used in class for ...<br /> {| class=&quot;wikitable&quot;<br /> |-<br /> ! calculating relative error<br /> ! working with integers<br /> |-<br /> | width=&quot;50%&quot; valign=&quot;top&quot; class=&quot;mainpage_hubbox&quot;|<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> y = 13.0<br /> n = 3 # number of significant figures<br /> rel_error = 0.5 * 10 ** (2-n) # relative error calculation<br /> x = y / 2.0<br /> x_prev = 0.0<br /> iter = 0<br /> while abs(x - x_prev)/x &gt; rel_error:<br /> x_prev = x<br /> x = (x + y/x) / 2.0<br /> print(abs(x - x_prev)/x)<br /> iter += 1<br /> <br /> print('Used %d iterations to calculate sqrt(%f) = %.20f; '<br /> 'true value = %.20f\n ' % (iter, y, x, np.sqrt(y)))<br /> &lt;/syntaxhighlight&gt;<br /> | width=&quot;50%&quot; valign=&quot;top&quot; class=&quot;mainpage_hubbox&quot;|<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> <br /> print(np.int16(32767))<br /> print(np.int16(32767+1))<br /> print(np.int16(32767+2))<br /> <br /> # Smallest and largest 16-bit integer<br /> print(np.iinfo(np.int16).min, np.iinfo(np.int16).max)<br /> <br /> # Smallest and largest 32-bit integer<br /> print(np.iinfo(np.int32).min, np.iinfo(np.int32).max)<br /> &lt;/syntaxhighlight&gt;<br /> |-<br /> ! working with floats<br /> ! special numbers<br /> |-<br /> | width=&quot;50%&quot; valign=&quot;top&quot; class=&quot;mainpage_hubbox&quot;|<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> <br /> help(np.finfo) # Read what the np.finfo function does<br /> <br /> f = np.float32 # single precision, 32-bit float, 4 bytes<br /> f = np.float64 # double precision, 64-bit float, 8 bytes<br /> <br /> print('machine precision = eps = %.10g' % np.finfo(f).eps)<br /> print('number of exponent bits = %.10g' % np.finfo(f).iexp)<br /> print('number of significand bits = %.10g' % np.finfo(f).nmant)<br /> print('smallest floating point value = %.10g' % np.finfo(f).min)<br /> print('largest floating point value = %.10g' % np.finfo(f).max)<br /> <br /> # Approximate number of decimal digits to which this kind<br /> # of float is precise.<br /> print('decimal precision = %.10g' % np.finfo(f).precision) <br /> &lt;/syntaxhighlight&gt;<br /> |<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> <br /> # Infinities<br /> print(np.inf, -np.inf)<br /> print(np.float(1E400)) # inf, number exceeds maximum value that<br /> # is possible with a 64-bit float: overflow<br /> print(np.inf * -4.0) # -inf<br /> print(np.divide(2.4, 0.0)) # inf<br /> <br /> # NaN's<br /> a = np.float(-2.3)<br /> print(np.sqrt(a)) # nan<br /> print(np.log(a)) # nan<br /> <br /> # Negative zeros<br /> a = np.float(0.0)<br /> b = np.float(-4.0)<br /> c = a/b<br /> print(c) # -0.0<br /> print(c * c) # 0.0<br /> <br /> eps = np.finfo(np.float).eps<br /> e = eps/3.0 # create a number smaller than machine precision<br /> # Question: why can we create a number smaller than eps?<br /> print(e)<br /> <br /> # Interesting property: non-commutative operations can occur<br /> # when working with values smaller than eps. Why?<br /> # The print out here should &quot;True&quot;, but it prints &quot;False&quot;<br /> print((1.0 + (e + e)) == (1.0 + e + e)) <br /> &lt;/syntaxhighlight&gt;<br /> |}<br /> <br /> ==Practice questions==<br /> <br /> &lt;ol&gt;<br /> &lt;li&gt;From the [[Suggested readings | Hangos and Cameron]] reference, ([http://macc.mcmaster.ca/sites/default/files/theses/Hangos_Cameron_2001_pp1-161.pdf available here]] - accessible from McMaster computers only)<br /> <br /> * Work through example 2.4.1 on page 33<br /> * Exercise A 2.1 and A 2.2 on page 37<br /> * Exercise A 2.4: which controlling mechanisms would you consider?<br /> <br /> &lt;li&gt;Homework problem, similar to the case presented on slide 18, except<br /> * Use two inlet streams $$\sf F_1$$ and $$\sf F_2$$, and assume they are volumetric flow rates<br /> * An irreversible reaction occurs, $$\sf A + 3B \stackrel{r}{\rightarrow} 2C$$<br /> * The reaction rate for A = $$\sf -r_A = -kC_\text{A} C_\text{B}^3$$<br /> # Derive the time-varying component mass balance for species B.<br /> #* $$V\frac{dC_B}{dt} = F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} C_{\sf B} + 0 - 3 kC_{\sf A} C_{\sf B}^3 V$$<br /> # What is the steady state value of $$\sf C_B$$? Can it be calculated without knowing the steady state value of $$\sf C_A$$?<br /> #* $$F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} \overline{C}_{\sf B} - 3 k \overline{C}_{\sf A} \overline{C}^3_{\sf B} V$$ - we require the steady state value of $$C_{\sf A}$$, denoted as $$\overline{C}_{\sf A}$$, to calculate $$\overline{C}_{\sf B}$$.<br /> &lt;/ol&gt;<br /> <br /> [[Practice questions | More exercises available here]]</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Modelling_and_scientific_computing&diff=1113 Modelling and scientific computing 2019-01-14T18:30:34Z <p>Kevindunn: </p> <hr /> <div>== Process modelling slides ==<br /> <br /> Please [[Media:A-Modelling-20-Sept-2010.pdf|download the lecture slides]]. 13 September 2010 (slides 1 to 8)&lt;br&gt;15 September 2010 (slides 9 to 15)&lt;br&gt;16 September 2010 (slides 16 to 19)&lt;br&gt;20 September 2010 (slides 20 to the end)<br /> <br /> &lt;math&gt;y = \dfrac{a}{x^2}&lt;/math&gt;<br /> <br /> == Approximation and computer representation ==<br /> <br /> Please [[Media:A-Approximation-and-roundoff-23-Sept-2010.pdf|download the lecture slides]]. 22 and 23 September (updated)<br /> <br /> * More about [http://www.around.com/ariane.html the Ariane 5 rocket] 16-bit overflow<br /> * Python code used in class for ...<br /> {| class=&quot;wikitable&quot;<br /> |-<br /> ! calculating relative error<br /> ! working with integers<br /> |-<br /> | width=&quot;50%&quot; valign=&quot;top&quot; class=&quot;mainpage_hubbox&quot;|<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> y = 13.0<br /> n = 3 # number of significant figures<br /> rel_error = 0.5 * 10 ** (2-n) # relative error calculation<br /> x = y / 2.0<br /> x_prev = 0.0<br /> iter = 0<br /> while abs(x - x_prev)/x &gt; rel_error:<br /> x_prev = x<br /> x = (x + y/x) / 2.0<br /> print(abs(x - x_prev)/x)<br /> iter += 1<br /> <br /> print('Used %d iterations to calculate sqrt(%f) = %.20f; '<br /> 'true value = %.20f\n ' % (iter, y, x, np.sqrt(y)))<br /> &lt;/syntaxhighlight&gt;<br /> | width=&quot;50%&quot; valign=&quot;top&quot; class=&quot;mainpage_hubbox&quot;|<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> <br /> print(np.int16(32767))<br /> print(np.int16(32767+1))<br /> print(np.int16(32767+2))<br /> <br /> # Smallest and largest 16-bit integer<br /> print(np.iinfo(np.int16).min, np.iinfo(np.int16).max)<br /> <br /> # Smallest and largest 32-bit integer<br /> print(np.iinfo(np.int32).min, np.iinfo(np.int32).max)<br /> &lt;/syntaxhighlight&gt;<br /> |-<br /> ! working with floats<br /> ! special numbers<br /> |-<br /> | width=&quot;50%&quot; valign=&quot;top&quot; class=&quot;mainpage_hubbox&quot;|<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> <br /> help(np.finfo) # Read what the np.finfo function does<br /> <br /> f = np.float32 # single precision, 32-bit float, 4 bytes<br /> f = np.float64 # double precision, 64-bit float, 8 bytes<br /> <br /> print('machine precision = eps = %.10g' % np.finfo(f).eps)<br /> print('number of exponent bits = %.10g' % np.finfo(f).iexp)<br /> print('number of significand bits = %.10g' % np.finfo(f).nmant)<br /> print('smallest floating point value = %.10g' % np.finfo(f).min)<br /> print('largest floating point value = %.10g' % np.finfo(f).max)<br /> <br /> # Approximate number of decimal digits to which this kind<br /> # of float is precise.<br /> print('decimal precision = %.10g' % np.finfo(f).precision) <br /> &lt;/syntaxhighlight&gt;<br /> |<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> <br /> # Infinities<br /> print(np.inf, -np.inf)<br /> print(np.float(1E400)) # inf, number exceeds maximum value that<br /> # is possible with a 64-bit float: overflow<br /> print(np.inf * -4.0) # -inf<br /> print(np.divide(2.4, 0.0)) # inf<br /> <br /> # NaN's<br /> a = np.float(-2.3)<br /> print(np.sqrt(a)) # nan<br /> print(np.log(a)) # nan<br /> <br /> # Negative zeros<br /> a = np.float(0.0)<br /> b = np.float(-4.0)<br /> c = a/b<br /> print(c) # -0.0<br /> print(c * c) # 0.0<br /> <br /> eps = np.finfo(np.float).eps<br /> e = eps/3.0 # create a number smaller than machine precision<br /> # Question: why can we create a number smaller than eps?<br /> print(e)<br /> <br /> # Interesting property: non-commutative operations can occur<br /> # when working with values smaller than eps. Why?<br /> # The print out here should &quot;True&quot;, but it prints &quot;False&quot;<br /> print((1.0 + (e + e)) == (1.0 + e + e)) <br /> &lt;/syntaxhighlight&gt;<br /> |}<br /> <br /> ==Practice questions==<br /> <br /> &lt;ol&gt;<br /> &lt;li&gt;From the [[Suggested readings | Hangos and Cameron]] reference, ([http://macc.mcmaster.ca/sites/default/files/theses/Hangos_Cameron_2001_pp1-161.pdf available here]] - accessible from McMaster computers only)<br /> <br /> * Work through example 2.4.1 on page 33<br /> * Exercise A 2.1 and A 2.2 on page 37<br /> * Exercise A 2.4: which controlling mechanisms would you consider?<br /> <br /> &lt;li&gt;Homework problem, similar to the case presented on slide 18, except<br /> * Use two inlet streams $$\sf F_1$$ and $$\sf F_2$$, and assume they are volumetric flow rates<br /> * An irreversible reaction occurs, $$\sf A + 3B \stackrel{r}{\rightarrow} 2C$$<br /> * The reaction rate for A = $$\sf -r_A = -kC_\text{A} C_\text{B}^3$$<br /> # Derive the time-varying component mass balance for species B.<br /> #* $$V\frac{dC_B}{dt} = F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} C_{\sf B} + 0 - 3 kC_{\sf A} C_{\sf B}^3 V$$<br /> # What is the steady state value of $$\sf C_B$$? Can it be calculated without knowing the steady state value of $$\sf C_A$$?<br /> #* $$F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} \overline{C}_{\sf B} - 3 k \overline{C}_{\sf A} \overline{C}^3_{\sf B} V$$ - we require the steady state value of $$C_{\sf A}$$, denoted as $$\overline{C}_{\sf A}$$, to calculate $$\overline{C}_{\sf B}$$.<br /> &lt;/ol&gt;<br /> <br /> [[Practice questions | More exercises available here]]</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Nonlinear_algebraic_equations&diff=1112 Nonlinear algebraic equations 2018-09-16T14:44:03Z <p>Kevindunn: </p> <hr /> <div>== Taylor series review ==<br /> <br /> ''We will start this section of the course with a review of Taylor series (1 class). <br /> <br /> The Taylor series notes will also be useful for later sections in the course.'' <br /> <br /> Please [[Media:C-Taylor-series-07-Oct-2010.pdf | download the lecture slides here]].<br /> <br /> <br /> <br /> == Nonlinear algebraic equations ==<br /> <br /> * 13 October (slides 1 to 9)<br /> * 14 October (slides 10 to 18)<br /> * 20 and 21 October (slides 19 to 32)<br /> * [[Media:C-NLAE-17-Oct-2010.pdf | Please download the lecture slides here]]<br /> <br /> <br /> ''Reference in course textbook for multivariable nonlinear systems, using Newton's method''<br /> <br /> Unfortunately, the course textbook has poor treatment of this topic due to the fact they treat nonlinear equations before linear equations! So it is spread over two parts:<br /> * section 6.6 (page 167 in the 6th edition)<br /> * section 9.6 (page 267 in the 6th edition)</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Tutorial_3_-_2010_-_Solution&diff=1111 Tutorial 3 - 2010 - Solution 2018-09-16T14:39:37Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 30 September 2010<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = Tutorial-3-2010.pdf<br /> | questions_text_alt = Tutorial questions <br /> | solutions_PDF = Tutorial-3-2010-Solution.pdf<br /> | solutions_text_alt = Solutions, prepared by Elliot Cameron<br /> | other_instructions = Hand-in at class.<br /> }}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> <br /> .. |m3| replace:: m\ :sup:3<br /> .. highlight:: python<br /> <br /> .. rubric:: Tutorial solutions: Elliot Cameron.<br /> <br /> .. rubric:: Tutorial objectives<br /> <br /> * Understand computer representation of decimal numbers and chemical engineering data.<br /> <br /> Question 1 <br /> ===============<br /> <br /> * Convert from binary to decimal: :math:(0.10101)_{2}<br /> * Convert from decimal to binary: :math:(32.625)_{10}<br /> * Convert from decimal to binary: :math:(0.2)_{10} <br /> <br /> Solution<br /> --------<br /> <br /> #.<br /> :math:(0.10101)_{2}<br /> <br /> :math:\underbrace{\begin{array}{ccccccc} \underbrace{0}_{0\text{x}2^{0}} &amp; . &amp; \underbrace{1}_{1\text{x}2^{-1}} &amp; \underbrace{0}_{0\text{x}2^{-2}} &amp; \underbrace{1}_{1\text{x}2^{-3}} &amp; \underbrace{0}_{0\text{x}2^{-4}} &amp; \underbrace{1}_{1\text{x}2^{-5}} \end{array}}_{0.0 + 0.5 + 0.0 + 0.125 + 0.0 + 0.03125 = 0.65625}<br /> <br /> Therefore<br /> <br /> :math:(0.10101)_{2} = (0.65625)_{10}<br /> <br /> #.<br /> :math:(32.625)_{10}<br /> <br /> Start to the left of the decimal point (i.e. Representing 32 in binary)<br /> <br /> :math:log_{2}(32) = 5 \rightarrow \text{This helps us determine how many binary digits are required}<br /> <br /> Rounding 5 up to 5 (obviously)<br /> <br /> :math:2^{5} = 32 \rightarrow (32 - 32 = 0)<br /> <br /> Therefore<br /> <br /> :math:1\text{x}2^{5}+0\text{x}2^{4}+0\text{x}2^{3}+0\text{x}2^{2}+0\text{x}2^{1}+0\text{x}2^{0} = (100000)_{2} = (32)_{10}<br /> <br /> Moving to the right of the decimal place<br /> <br /> :math:2^{-1} = 0.5 \rightarrow (0.625 - 0.5 = 0.125)<br /> <br /> :math:2^{-2} = 0.25 \rightarrow (0.125 &lt; 0.2) : therefore we move to the next digit<br /> <br /> :math:2^{-3} = 0.125 \rightarrow (0.125 - 0.125 = 0.0)<br /> <br /> Solution:<br /> <br /> :math:1\text{x}2^{5}+0\text{x}2^{4}+0\text{x}2^{3}+0\text{x}2^{2}+0\text{x}2^{1}+0\text{x}2^{0}+1\text{x}2^{-1}+0\text{x}2^{-2}+1\text{x}2^{-3} = (100000.101)_{2} = (32.625)_{10}<br /> <br /> #.<br /> :math:(0.2)_{10}<br /> <br /> Start to the left of the decimal point (i.e. Representing 0 in binary)<br /> <br /> :math:(0)_{10} = (0)_{2}<br /> <br /> Moving to the right of the decimal place<br /> <br /> :math:2^{-1} = 0.5 \rightarrow (0.2 &lt; 0.5) : therefore we move to the next digit<br /> <br /> :math:2^{-2} = 0.25 \rightarrow (0.2 &lt; 0.25) : therefore we move to the next digit<br /> <br /> :math:2^{-3} = 0.125 \rightarrow (0.2 - 0.125 = 0.075)<br /> <br /> :math:2^{-4} = 0.0625 \rightarrow (0.075 - 0.0625 = 0.0125)<br /> <br /> :math:2^{-5} = 0.03125 \rightarrow (0.0125 &lt; 0.03125) : therefore we move to the next digit<br /> <br /> :math:2^{-6} = 0.015625 \rightarrow (0.0125 &lt; 0.015625) : therefore we move to the next digit<br /> <br /> :math:2^{-7} = 0.0078125 \rightarrow (0.0125 - 0.0078125 = 0.0046875)<br /> <br /> :math:2^{-8} = 0.00390625 \rightarrow (0.0046875 - 0.00390625 = 0.00078125)<br /> <br /> :math:2^{-9} = 0.001953125 \rightarrow (0.00078125 &lt; 0.001953125) :move to the next digit<br /> <br /> :math:2^{-10} = 0.0009765625 \rightarrow (0.00078125 &lt; 0.0009765625) : move to the next digit<br /> <br /> :math:2^{-11} = 0.00048828125 \rightarrow (0.00078125 - 0.00048828125 = 0.00029296875)<br /> <br /> :math:2^{-12} = 0.000244140625 \rightarrow (0.00029296875 - 0.000244140625 = 0.000048828125)<br /> <br /> :math:\ldots<br /> <br /> Solution<br /> <br /> .. math::<br /> 0\text{x}2^{0}+0\text{x}2^{-1}+0\text{x}2^{-2}+1\text{x}2^{-3}+1\text{x}2^{-4}+0\text{x}2^{-5}+0\text{x}2^{-6}+1\text{x}2^{-7}+1\text{x}2^{-8}+0\text{x}2^{-9}+0\text{x}2^{-10}+1\text{x}2^{-11}+1\text{x}2^{-12}\\ \approx (0.{\bf \overline{0011}})_{2} = (0.2)_{10}<br /> <br /> What this questions aims to show you is, just as in the decimal system, there are numbers that cannot be represented finitely in binary (think 1/3 in decimal notation).<br /> <br /> Question 2 <br /> ===============<br /> <br /> #. Write, in binary form, the representation for the most negative floating point number that can be stored in an 8-bit word, using 1 bit for the sign, 3 bits for the signed exponent and the remainder for the significand (in normalized form). <br /> #. What is the decimal equivalent of this number? <br /> #. What is the machine number next to this one (both in binary and in decimal please)? <br /> #. Calculate the maximal interval-to-value ratio around these two values. Does it agree with the limit for theoretical machine precision?<br /> #. As which machine number would -7.22 be stored if the machine used (a) chopping, or (b) rounding?<br /> <br /> Solution<br /> --------<br /> <br /> #. <br /> We start by recalling that normalized scientific notation assumes that the significand starts with an implied leading digit of 0 (i.e. it falls between the following bounds):<br /> <br /> :math:\frac{1}{b} \leq m &lt; 1<br /> <br /> Where<br /> <br /> :math:m = significand<br /> <br /> :math:b = base<br /> <br /> :math:e = exponent<br /> <br /> Therefore the most negative 8-bit binary number with 1 bit for the sign, 3 bits for the signed exponent, and 4 bits for the normalized significand is:<br /> <br /> .. math::<br /> \begin{array}{ccc} \underbrace{1}_{\text{sign}} &amp; \underbrace{011}_{\text{exponent}} &amp; \underbrace{1111}_{\text{significand}} \rightarrow -\left(1\text{x}2^{-1}+1\text{x}2^{-2}+1\text{x}2^{-3}+1\text{x}2^{-4}\right)\text{x}2^{\left(1\text{x}2^{0}+1\text{x}2^{1}\right)} = -0.9375 \text{x} 2^{3} \end{array}<br /> <br /> #. .. math::<br /> \begin{array}{ccc} \underbrace{1}_{\text{sign}} &amp; \underbrace{011}_{\text{exponent}} &amp; \underbrace{1111}_{\text{significand}} = -0.9375 \text{x} 2^{3} = -0.9375 \text{x} 8 = -7.5 \end{array}<br /> <br /> #. <br /> The next closest machine number is the next physically representable number in the 8-bit floating point system. Since no more space exists &quot;above&quot; the current significand we must go &quot;down&quot; in magnitude. Therefore:<br /> <br /> .. math::<br /> \begin{array}{ccc} \underbrace{1}_{\text{sign}} &amp; \underbrace{011}_{\text{exponent}} &amp; \underbrace{1110}_{\text{significand}} = -0.8750 \text{x} 2^{3} = -7.0 \end{array}<br /> <br /> #. <br /> We start by estimating the theoretical machine precision:<br /> <br /> :math:t = 4 : number of significant digits in the significand<br /> <br /> :math:\beta = 2 : base of number system<br /> <br /> :math:\epsilon_{mach} = \beta^{1-t} = 2^{1-4} = 0.125<br /> <br /> Next we test the maximal interval-to-ratio value on either side of the values from part (a)/(b) and (c)<br /> <br /> :math:\frac{|x_{1}-x_{2}|}{|x_{1}|} = \frac{|(-7.0)-(-7.5)|}{|(-7.0)|} = 0.0714\ldots \leq \epsilon_{mach}<br /> <br /> :math:\frac{|x_{1}-x_{2}|}{|x_{2}|} = \frac{|(-7.0)-(-7.5)|}{|(-7.5)|} = 0.0666\ldots \leq \epsilon_{mach}<br /> <br /> It is easy to see above that the maximal interval-to-value ratios agree with the upper :math:\epsilon_{mach} limit<br /> <br /> #. <br /> As observed in parts (a) - (c) the two closest machine numbers to -7.22 are -7.0 and -7.5. Therefore the effect of chopping (rounding towards 0) and rounding (rounding to the closest avaiable machine number) would result in the same floating point value: **-7.0**.<br /> <br /> Question 3 [1.5]<br /> =================<br /> <br /> #. Increasingly we are seeing cameras being used in chemical processes to monitor and control the process, especially systems that deal with foods and solid products. How much space, in kilobytes, is required to store a digital photo with 640 rows and 480 columns of pixels and 3 layers (red, green and blue) using uint8 integer representation?<br /> #. Computer systems are used to archive data from each electronic measurement, such as temperature, pressure, flow measurements, *etc*. Each measurement is called a tag. At your plant, you wish to store 16,525 tags, recorded once per second and stored in double precision. How much space would be required on the company's server, in terabytes, to store a single copy of 1 year of data? What difference does it make to store the data in single precision?<br /> #. How many data points can you store in double precision in 1500 megabytes of RAM? (For example, MATLAB on a 32-bit Windows Vista machine cannot create arrays greater than 1428 megabytes.)<br /> <br /> Use the fact that:<br /> <br /> * 1024 bytes = 1 kilobyte<br /> * 1024 kilobytes = 1 megabyte<br /> * 1024 megabytes = 1 gigabyte<br /> * 1024 gigabyte = 1 terabyte = 2\ :sup:40 bytes<br /> <br /> Solution<br /> --------<br /> <br /> #. <br /> Recall that the uint8 integer representation refers to an unsigned integer with no exponential term. Therefore it refers to an integer that can store a value from **0 - 255**. As such this question is simply asking us how much memory would be required to store a 640x480 pixel image using the 256/256/256 RGB colour notation. Now, if each pixel in each of the RGB matrices is represented by a uint8 then **each value will logically take up 8 bits of memory**. If each of the three colour matrices contains 640x480 values and we assume a standard 8 bit byte then the total space required to store the photo would be:<br /> <br /> .. math::<br /> Space = \underbrace{3}_{\text{rgb}} \times \underbrace{640*480}_{\text{pixels}} \times \underbrace{8\;\text{bits}}_{\text{uint8}} = 7372800\;\text{bits}*\frac{1\;\text{byte}}{8\;\text{bit}}*\frac{1\;\text{kilobyte}}{1024\;\text{byte}} = 900\;\text{kilobytes}<br /> <br /> <br /> #. <br /> Recall that a standard floating point double precision number takes up 64 bits (8 bytes) of memory. We start this problem by calculating the number of values to be stored in one year.<br /> <br /> .. math::<br /> \text{Values} = 16,525\frac{\text{values}}{s}\times\frac{60\;s}{1\;\text{min}}\times\frac{60\;\text{min}}{1\;\text{hr}}\times\frac{24\;\text{hr}}{1\;\text{day}}\times\frac{365\;\text{day}}{1\;\text{year}} = 521,132,400,000\frac{\text{values}}{\text{year}}<br /> <br /> Therefore the memory requirement is:<br /> <br /> .. math::<br /> Memory = 521,132,400,000\frac{\text{values}}{\text{year}}\cdot8\frac{\text{bytes}}{\text{value}}\cdot\frac{1\;\text{kilobyte}}{1024\;\text{byte}}\cdot\frac{1\;\text{megabyte}}{1024\;\text{kilobyte}}\cdot\frac{1\;\text{gigabyte}}{1024\;\text{megabyte}}\cdot\frac{1\;\text{terabyte}}{1024\;\text{gigabyte}} = 3.79\;\text{terabytes}<br /> <br /> A floating point single precision value takes up exactly half as much space as a floating point double precision number (i.e. 32 bits = 4 bytes). Therefore storing the same tags in single precision would take up :math:1.9\;TB.<br /> <br /> #. <br /> If we take the absolute value of RAM available (i.e. 1500 MB) and the standard definition of a floating point double precision number (i.e. 64 bits = 8 bytes), then<br /> <br /> :math:1,500\;\text{MB}\times\frac{1024\;\text{KB}}{1\;\text{MB}}\times\frac{1024\;\text{B}}{1\;\text{kB}} = 1,572,864,000\;\text{bytes}<br /> <br /> Therefore the number of double precision values that could be stored would be.<br /> <br /> :math:\text{Number} = 1,572,864,000\;\text{bytes}\times\frac{1\;\text{value}}{8\;\text{bytes}} = 196,608,000\;\text{values}<br /> <br /> If we use the maximum MATLAB array size then.<br /> <br /> :math:1,428\;\text{MB}\times\frac{1024\;\text{KB}}{1\;\text{MB}}\times\frac{1024\;\text{bytes}}{1\;\text{kB}} = 1,497,366,528\;\text{bytes}<br /> <br /> Therefore the number of double precision values that could be stored in MATLAB would be.<br /> <br /> :math:\text{Number} = 1,497,366,528\;\text{bytes}\times\frac{1\;\text{value}}{8\;\text{bytes}} = 187,170,816\;\text{values}<br /> <br /> Question 4 <br /> =================<br /> <br /> Consider the following system of linear algebraic equations:<br /> <br /> .. math::<br /> <br /> \left\{\begin{array}{rcl} 2 x_1 -2x_2 +4 x_3 &amp; = &amp; 0 \\ x_1 -3 x_2 + 4x_3 &amp; = &amp; -1 \\ 3x_1 - x_2 +5x_3 &amp;= &amp; 0 \end{array}\right.<br /> <br /> #. Use Gauss elimination (forward elimination and backward substitution) to solve these equations for :math:(x_1,x_2,x_3).<br /> #. Validate your solution in either Python or MATLAB.<br /> <br /> Solution<br /> ---------<br /> <br /> #.<br /> We are asked to solve this system of equations using Gauss Elimination (without partial pivoting).<br /> <br /> .. math::<br /> \begin{array}{ccccc} 2x_{1} &amp; -2x_{2} &amp; +4x_{3} &amp; = &amp; 0\\ x_{1} &amp; -3x_{2} &amp; +4x_{3} &amp; = &amp; -1\\ 3x_{1} &amp; -x_{2} &amp; +5x_{3} &amp; = &amp; 0 \end{array} \rightarrow \left[ \begin{array}{ccc} 2 &amp; -2 &amp; 4 \\ 1 &amp; -3 &amp; 4 \\ 3 &amp; -1 &amp; 5 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 0 \\ -1 \\ 0 \end{array} \right]<br /> <br /> **Forward elimination**<br /> <br /> Divide row 1 by element (1,1)<br /> <br /> .. math::<br /> \left[ \begin{array}{ccc} 1 &amp; -1 &amp; 2 \\ 1 &amp; -3 &amp; 4 \\ 3 &amp; -1 &amp; 5 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 0 \\ -1 \\ 0 \end{array} \right]<br /> <br /> Subtract row 1 from row 2 and 3 times row 1 from row 3 to eliminate all elements in column 1 below the diagonal element<br /> <br /> .. math::<br /> \left[ \begin{array}{ccc} 1 &amp; -1 &amp; 2 \\ 0 &amp; -2 &amp; 2 \\ 0 &amp; 2 &amp; -1 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 0 \\ -1 \\ 0 \end{array} \right]<br /> <br /> Divide row 2 by element (2,2)<br /> <br /> .. math:: <br /> \left[ \begin{array}{ccc} 1 &amp; -1 &amp; 2 \\ 0 &amp; 1 &amp; -1 \\ 0 &amp; 2 &amp; -1 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 0 \\ 0.5 \\ 0 \end{array} \right]<br /> <br /> Subtract 2 times row 2 from row 3 to eliminate all elements in column 2 below the diagonal element<br /> <br /> .. math::<br /> \left[ \begin{array}{ccc} 1 &amp; -1 &amp; 2 \\ 0 &amp; 1 &amp; -1 \\ 0 &amp; 0 &amp; 1 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 0 \\ 0.5 \\ -1 \end{array} \right]<br /> <br /> **Backwards substitution**<br /> <br /> Subtract -1 times row 3 from row 2 and 2 times row 3 from row 1 to eliminate all elements in column 3 above the diagonal element<br /> <br /> .. math::<br /> \left[ \begin{array}{ccc} 1 &amp; -1 &amp; 0 \\ 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 1 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 2 \\ -0.5 \\ -1 \end{array} \right]<br /> <br /> Subtract -1 times row 2 from row 1 to eliminate all elements in column 2 above the diagonal element<br /> <br /> .. math::<br /> \left[ \begin{array}{ccc} 1 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 1 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 1.5 \\ -0.5 \\ -1 \end{array} \right]<br /> <br /> #.<br /> Checking this solution in MATLAB:<br /> <br /> ::<br /> <br /> EDU&gt;&gt; A = [2,-2,4;<br /> 1,-3,4;<br /> 3,-1,5];<br /> EDU&gt;&gt; b = [0;-1;0];<br /> EDU&gt;&gt; x = A\b<br /> <br /> x =<br /> <br /> 1.5000<br /> -0.5000<br /> -1.0000<br /> <br /> Checking this solution in Python:<br /> <br /> ::<br /> <br /> In : A = np.array([[2,-2,4],[1,-3,4],[3,-1,5]])<br /> <br /> In : b = np.array([,[-1],])<br /> <br /> In : x = np.linalg.solve(A,b)<br /> <br /> In : print(x)<br /> <br /> [[ 1.5]<br /> [-0.5]<br /> [-1. ]]<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.25cm}<br /> \hrule<br /> %\begin{center}END\end{center}<br /> <br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Take-home_exam_-_Solution&diff=1110 Take-home exam - Solution 2018-09-16T14:37:29Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 17:30 on '''22 November 2010'''.<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = Take-home-exam-solution.pdf<br /> | questions_text_alt = Take home solution<br /> | solutions_PDF = <br /> | solutions_text_alt = Solutions by Kevin, Ali and Elliot.<br /> | other_instructions = Hand-in at class.<br /> }}<br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = True/&gt;<br /> <br /> .. Sections<br /> <br /> Modelling: series of CSTRs <br /> Linear algebraic equations flowsheet <br /> Nonlinear zeros flowsheet<br /> Curve fitting assumed in other questions<br /> Numerical integration and differentiation blood question<br /> ODEs waste water plant<br /> <br /> Instructions<br /> ===============<br /> <br /> The purpose of this exam is only a little different to other exams. The difference is that this exam will assess your ability to answer more realistic problems that require more time, thought, and access to a computer. You are expected to use **any appropriate tools** to solve the problems in this exam, particularly the tools learnt in this course. <br /> <br /> The 3E4 course teaches you a collection of tools that will be useful in your engineering career. And the challenges you will face do not come neatly posed as problems of the type: &quot;Solve this linear system using Gauss elimination&quot; -- rather you need to reformulate your challenge and pick the appropriate tool to solve the problem, taking into account the level of complexity.<br /> <br /> In that regard there are many correct ways to achieve a result. In this exam about 80% of the grade will be given for **how** you solve the problem, and we will pay particular attention to the **justifications** you provide along the way. You must clearly outline **why** you choose the method and **why** you did not choose alternative methods. The **accuracy of how you implement your method** is also important, but the actual solution is of little value for determining your grade.<br /> <br /> Also, as you will find in your career as an engineer, you will have to apply tools that you learnt about long ago, tools just learnt recently, or you may have do some research and figure out how to use a tool you've never used before. This exam has those aspects as well.<br /> <br /> Some other points are:<br /> <br /> * Complete this exam with 1 other person, or by yourself.<br /> * Please identify the person you work with at the top of your answer submission. **Please note: submit one solution per group**.<br /> * Please attribute any sources of reference you used (textbooks, websites, and so on).<br /> * The intention of the group work is that you discuss the questions and collaborate in the same way you have done in the assignments.<br /> * But **do not share** any electronic files (e.g. Word documents, source code) outside your group; take care in the computer labs to safeguard your work.<br /> * Like any other exam, neither the TAs nor myself are able to answer direct questions about the exam. Similarly you *should not* look for help about a *specific question* from other resources (e.g. asking for help with the question on a website, friends, other students in the class, *etc*).<br /> * You may use the course notes and any other textbooks and resources though.<br /> * There is no make-up for this exam.<br /> * You will benefit from going through the software tutorials &lt;https://learnche.org/3E4/Software_tutorial&gt;_ on the course website.<br /> * Your answers should preferably be typed up.<br /> * Your answers must be structured like a report and flow together logically. Please do not submit pages and pages of computer printouts and source code in appendices - unfortunately this will be penalized.<br /> <br /> - Hand out date: 17 November 2010,<br /> - **Due date**: <br /> - Paper hand-in: by **17:30 on 22 November 2010, in class**<br /> - Totally electronic hand in: email to dunnkg@mcmaster.ca before the above time and date.<br /> - A valid electronic hand in must be in PDF format *only* (no separate Word, MATLAB or Python files). If you are on Windows, you could use PDFCreator &lt;http://sourceforge.net/projects/pdfcreator/&gt;_ to make PDF's; Macs and Linux have built-in PDF capability.<br /> <br /> .. Note:: There are a maximum of 30 grade points available in the 5 questions, plus a variable amount of bonus points. <br /> <br /> .. raw:: latex<br /> <br /> \pagestyle{plain}<br /> \vspace*{0.2cm}<br /> \hrule<br /> \vspace*{0.2cm}<br /> %\newpage<br /> <br /> **Only brief solutions are provided as we covered all the questions in class. The grading schedule is provided with each question.**<br /> <br /> Question 1 [7 = 2 + 2 + 2 + 1]<br /> ===============================<br /> <br /> .. Green modelling book: p 316<br /> .. Tests numerical differentiation, modelling, curve fitting and common sense.<br /> <br /> Background: Chemical engineers have made many contributions to the medical field; similarly in this question you must use your engineering knowledge in a different context. <br /> <br /> Consider the case of injecting a medical treatment into a person's blood system. Subsequent to the injection, the body starts to metabolize (break down, or use up) the drug. In this study a drug was injected into the patient, and samples taken over the next 10 hours. This table reports the concentration of the drug in the blood stream, :math:C:<br /> <br /> ================== ==== ==== ==== ==== ==== ==== ==== <br /> :math:t [hour] 1 2 3 5 6 8 10 <br /> ------------------ ---- ---- ---- ---- ---- ---- ---- <br /> :math:C [mg/L] 10.1 7.5 5.7 3.1 2.4 1.3 0.7<br /> ================== ==== ==== ==== ==== ==== ==== ==== <br /> <br /> Medical models often use a well-mixed reactor to approximate the time-dependent behaviour of parts of the body: e.g. kidneys, stomach, and other organs. In this question you can assume the blood system to be a well-mixed CSTR, operating in batch mode: i.e. no inlet, or outlet, and of constant volume (about 5L for a typical adult). Assume the injection immediately mixes with all the blood, and shows up as :math:C(t=0). <br /> <br /> #. Provide an estimate of the drug's concentration at :math:t=0 hours. Please justify your choice of method, and provide a plot to assess if your estimate is reasonable.<br /> <br /> #. Please provide the lowest error estimate of :math:\frac{dC}{dt} for every data point in the table (without using Richardson's extrapolation).<br /> <br /> #. Next assume the drug is eliminated from the patient's blood into the rest of the body using a first-order rate expression, :math:r = -kC. Obviously the mechanism by which the drug is metabolized is much more complex, but we don't know the cellular pathways and their rate constants: so lump them together and assume the overall mechanism is first order. Derive the dynamic equation that would describe the concentration of the drug in the bloodstream as a function of time.<br /> <br /> #. Using your values from part 2, calculate the average rate constant. **Bonus mark of 0.5** if you use least squares regression instead of just calculating the average.<br /> <br /> Solution<br /> ---------<br /> <br /> .. See code in code/injection.py<br /> <br /> #. The time range given in the table does not contain :math:t=0. So, any method used to estimate :math:C(t=0) will be an extrapolation. A simple way to estimate :math:C(t=0) is by a linear extrapolation, which can be justified by the small distance between :math:t=0 and :math:t=1 and also :math:t=1 and :math:t=2 (so a linear behaviour can be assumed over that range). We pass a line through :math:(1, 10.1) and :math:(2, 7.5) as :math:C = -2.6t + 12.7, so :math:C(t=0) = 12.7.<br /> <br /> A spline or polynomial fit, through the first 3 points or so would also be a valid, though longer approach. The polynomial through the first 3 points is :math:C(t) = 13.5 -3.8t + 0.4t^2, so here :math:C(t=0) = 13.5. Both fits are illustrated in the figure.<br /> <br /> .. figure:: ../che3e4/Take-home-midterm/images/take-home-Q1-part1.png<br /> :scale: 50<br /> :align: center<br /> <br /> As explained in class, neither value is correct in practice, since :math:C(t=0) = 0 the moment the injection is administered.<br /> <br /> #. The lowest error estimate of :math:\frac{dC}{dt}:<br /> <br /> .. math::<br /> <br /> \begin{array}{llll} t &amp; C &amp; \text{Lowest error method} &amp; Result \\ \hline 1 &amp; 10.1 &amp; \text{forward diff} &amp; -2.60 \\ 2 &amp; 7.5 &amp; \text{central diff} &amp; -2.20 \\ 3 &amp; 5.7 &amp; \text{central diff} &amp; -1.750 \\ 5 &amp; 3.1 &amp; \text{central diff} &amp; -1.033 \\ 6 &amp; 2.4 &amp; \text{backward diff} &amp; -0.70 \\ 8 &amp; 1.3 &amp; \text{central diff} &amp; -0.4250 \\ 10 &amp; 0.7 &amp; \text{backward diff} &amp; -0.30 \\ \hline \end{array}<br /> <br /> #. In this problem our *control volume* to be modelled is the entire human's body. So, the bloodstream inside the body makes up a batch system because there is no blood flow into/out of the body. Another assumption is that we have well-mixed system; that is there is no spatial distribution of quantities through the body. This assumption means that the blood circulation is fast enough, making a well-mixed environment. Also, the system is aqueous and the volume is constant. Given these assumptions, the dynamical model would be as follows:<br /> <br /> .. math::<br /> <br /> \text{Accumulation} &amp;= \text{Input} - \text{Output} + \text{Generation} - \text{Consumption}\\ \displaystyle\frac{d(VC)}{dt} &amp;= 0 - 0 + 0 - rV\\ \displaystyle\frac{dC}{dt} &amp;= -kC<br /> <br /> #. In the model we just obtained, the parameter :math:k is not known. But we can compute an approximation of it using the dynamical model, and the estimated values of the derivative :math:\frac{dC}{dt} at different concentrations :math:C. One way is to obtain values of :math:k_i corresponding to each data point :math:C_i, then computing an average of the :math:k_i values:<br /> <br /> .. math::<br /> <br /> \begin{array}{llll} t &amp; C &amp; \displaystyle\frac{dC}{dt} &amp; k \\ \hline 1 &amp; 10.1 &amp; -2.60 &amp; 0.2574 \\ 2 &amp; 7.5 &amp; -2.20 &amp; 0.2933 \\ 3 &amp; 5.7 &amp; -1.750 &amp; 0.3070 \\ 5 &amp; 3.1 &amp; -1.033 &amp; 0.3332 \\ 6 &amp; 2.4 &amp; -0.70 &amp; 0.2917 \\ 8 &amp; 1.3 &amp; -0.4250 &amp; 0.3269 \\ 10 &amp; 0.7 &amp; -0.30 &amp; 0.4286 \\ \hline \end{array}<br /> <br /> The average rate constant is :math:\bar{k}= \displaystyle\frac{\sum_{i=1}^{7} {k_i}}{7}=0.3197<br /> <br /> Now we try to obtain :math:k using regression. Let us denote :math:\displaystyle\frac{dC}{dt} by :math:y, and :math:y_{\text{model}}=\displaystyle\frac{dC}{dt}=k(-C) represent the derivative that we obtain from the model. On the other hand, we also calculated the derivatives in Part 1 using the experimental data (even though approximate!). Let us denote them by :math:y_{\text{exp}}=(\displaystyle\frac{dC}{dt})_{\text{exp}}. Now we can set up a least square regression as follows.<br /> <br /> .. math::<br /> <br /> \text{min}\quad J= \displaystyle\sum_{i=1}^{7} {(y_{\text{model,i}}-y_{\text{exp,i}}})^2<br /> <br /> In order to minimize the above error function :math:J, we have to equate its derivative w.r.t. :math:k to zero. That is, :math:\displaystyle\frac{dJ}{dk}=0<br /> <br /> .. math::<br /> <br /> \displaystyle\frac{dJ}{dk}= 2\displaystyle\sum_{i=1}^{7} (\displaystyle\frac{dy_{\text{model,i}}}{dk})(y_{\text{model,i}}-y_{\text{exp,i}})=0<br /> <br /> Note that :math:y_{\text{model,i}}=k(-C_i). So :math:\displaystyle\frac{dy_{\text{model,i}}}{dk}=-C_i. Substituting these into the above equation will give:<br /> <br /> .. math::<br /> <br /> \displaystyle\frac{dJ}{dk}= \displaystyle\sum_{i=1}^{7} (C_i)({k(-C_i)}-y_{\text{exp,i}})=0<br /> <br /> Rearranging the above equation and solving it for :math:k (left to you as an exercise) will complete the derivation:<br /> <br /> .. math::<br /> <br /> k &amp;= \displaystyle\frac{\sum_{i=1}^{7} -C_i y_{\text{exp,i}}}{\sum_{i=1}^{7} C_i^2} \\ k &amp;=\displaystyle\frac{-(10.10\times -2.60+ 7.50\times -2.20+ 5.70\times -1.75+ 3.10\times -1.033+2.40\times -0.70+1.30\times -0.425+0.70\times -0.30)}{10.10^2+7.50^2+5.70^2+3.10^2+2.40^2+1.30^2+0.70^2}\\ k &amp;=0.2803<br /> <br /> **Note**: According to the model :math:y_{\text{model}}=\displaystyle\frac{dC}{dt}=k(-C), the slope :math:k is the only coefficient to be obtained. But if you use MATLAB's command polyfit or Excel's linear regression, by default they will calculate two coefficients, one is the slope and the other one is the intercept. So be careful: having an intercept may not be physically meaningful in your system. In such a case (as in this problem), you should have MATLAB or Excel set the intercept to zero or do the regression yourself.<br /> <br /> **Note**: we showed an alternative method in class that first integrated the equation to obtain :math:\ln[C] = \ln[C(t=0)] - kt. By regressing values of :math:\ln[C] onto :math:t we can estimate both the rate constant and the initial concentration. Using this method you will obtain :math:k=0.295\,\text{hours}^{-1} and :math:C(t=0) = 13.7 mg/L.<br /> <br /> <br /> <br /> Question 2 [5 = 1 + 1 + 3]<br /> ===========================<br /> <br /> A batch bioreactor (well-mixed tank, but no inlet or outlet) is charged with substrate at time :math:t=0 to start a reaction that will consume the substrate. The reaction mechanism is too complex to model exactly, but previous experience suggests it is roughly a first-order reaction. The apparent first-order rate expression is :math:r = kS, where :math:k = 0.58\,\,\text{hours}^{-1}, and :math:{\sf S} is the concentration of the substrate being depleted.<br /> <br /> #. Show that the dynamic rate of change of :math:{\sf S} is given by:<br /> <br /> .. math::<br /> <br /> \frac{d{\sf S}(t)}{dt} = -k {\sf S}(t)<br /> <br /> Also state any assumptions that were made in arriving at this expression.<br /> <br /> #. The time required to consume the substrate from a starting level of 260 g/L to 5 g/L can be calculated by analytical integration of the above expression. What is the total time required?<br /> <br /> #. Had you forgotten how to integrate the expression analytically you could resort to using the trapezoidal rule or Simpson's 1/3 rule. **Without** implementing either method, calculate how many panels would be required to ensure a numerical estimate of the time to achieve the above conversion is accurate within 0.01 hours. In other words, how many panels are required to solve the left hand side integral <br /> <br /> .. math::<br /> <br /> \int_{260}^{5}{-\frac{1}{k\sf S}\, d{\sf S}} = \int_{0}^{t}{d{t}} = t<br /> <br /> to that level of accuracy:<br /> <br /> * when using the trapezoidal rule;<br /> * when using Simpson's 1/3 method.<br /> <br /> <br /> Solution<br /> ---------<br /> <br /> #. Ordinary batch system balance, with no in or out flows, only a reaction term :math:-k \sf S and the accumulation term, :math:\frac{d {\sf S}}{dt}. The key assumption is that all other properties, such as batch volume and physical properties are constant. The system is also assumed to be well-mixed, as stated. Other assumptions would be that the energy inputs and output are negligible (i.e. shaft work, heat losses to the environment and specifically that the reaction rate is not a function of temperature).<br /> <br /> #. :math:\ln(\frac{5}{260}) = -0.58t = 6.81 hours, or 409 minutes.<br /> <br /> #. This question turned out to be less useful than I had intended. I had intended that you calculate and realize how a much smaller number of panels are required for Simpson's rule than the trapezoidal rule - in order to get the same level of error. However, due to:<br /> <br /> * an unfortunate error in the PDF course notes<br /> * an integral that wasn't expressed in terms of time<br /> * a difference between the 5th and 6th edition of the course textbooks<br /> <br /> I've decided to omit this question from the exam, but give 1 bonus point to everyone that seriously attempted the question (i.e. the exam is out of 27 points, but you probably got a bonus point).<br /> <br /> The way to attempt this question was to first rearrange the left-hand integral so that it had units of time, since the error was asked to be below 0.01 hours. The original question was expressed as shown below in the first line, but then later changed to read as the second line.<br /> <br /> .. math::<br /> <br /> \int_{260}^{5}{\frac{1}{\sf S}\, d{\sf S}} &amp;= -k\int_{0}^{t}{d{t}} = -kt \\ \int_{260}^{5}{-\frac{1}{k\sf S}\, d{\sf S}} &amp;= \int_{0}^{t}{d{t}} = t<br /> <br /> We can use the form shown in the first line, as long as we multiply our answer by :math:k afterwards.<br /> <br /> <br /> So the function we are integrating is :math:f(S) = S^{-1} and has units of L/g. We will require derivatives of this function, :math:f''(S) =2S^{-3}, with units of :math:\text{L}^3/\text{g}^3, and :math:f^{(4)}(S) =24S^{-5} with units of :math:\text{L}^5/\text{g}^5.<br /> <br /> The main problem was from the PDF course notes. There was an inaccuracy that said you could find the number of panels for the trapezoidal rule if this expression was satisfied:<br /> <br /> .. math::<br /> <br /> \frac{(b-a)^3}{12n^2}f''(\xi) &lt; 0.01 \qquad\,\text{for some value of $\xi$ in the range $[5, 260]$}<br /> <br /> <br /> However, the correct expression for the error is:<br /> <br /> .. math::<br /> <br /> \left| -\frac{(b-a)^3}{12n^3} \sum_{1}^{n}{f''(\xi_i)} \right| &lt; \left| -kt\right| = (0.58)(0.01) = 0.0058 <br /> <br /> Note the units: :math:a and :math:b are in g/L while :math:f''({\sf S}) has units of :math:\text{L}^3/\text{g}^3, so everything balances.<br /> <br /> The problem is evaluating the summation: you have to find the worst value of :math:f''({\sf S}) within every panel, but the purpose of this question is to find the number of panels! So the course textbook shows that we rather find the average error, and that was the equation in the PDF course notes, except it was incorrect to have the part that :math:\xi had to be in the range from :math:[a, b].<br /> <br /> When I set the exam I used the PDF course notes, not the course textbook, so I was unaware of this complication in the question. My apologies if you spent a lot of time on this question. <br /> <br /> .. A few students resorted to the course textbook version and use an iterative method to solve for the number of panels. But as the equation above shows it is not possible to find the error without knowing the panel locations - but once you know the panel locations, then you can use the second derivative equation and find the error. However, if the error is too high or too low, you need to repeat everything again with a different number of panels.<br /> <br /> Question 3 [7 = 3 + 1 + 3]<br /> ===========================<br /> <br /> The following question is based on the flowsheet below, where the :math:m_i values are the mass flow of the species of interest, the reactant. The purpose of the flowsheet is to eliminate the incoming species at :math:m_1 = 10 kg/hour and convert it to by-products. There is some residual reactant leaving the flowsheet in stream :math:m_{12}.<br /> <br /> .. figure:: ../che3e4/Take-home-midterm/images/take-home-reactor-network.png<br /> :scale: 40<br /> :align: center<br /> <br /> The following additional information is known:<br /> <br /> * :math:m_1 = 10 kg/hour.<br /> * :math:m_2 = 2 kg/hour.<br /> * Unit A operates with 40% efficiency; i.e. of all the material coming in, only 40% of it is converted to other products.<br /> * Unit B is just a mixing tank.<br /> * Unit C is just a mixer/splitter and is operated so that the mass-flow in the outlets are all in the same proportion.<br /> * Unit D operates with 82% efficiency, so 18% of the incoming stream is left unconverted. The two outlets are split in equal proportion as well.<br /> * Unit E operates with 15% efficiency to convert the incoming streams to by-products.<br /> * The split after unit E is operated such that :math:\alpha m_8 is recycled in stream 11, while :math:(1-\alpha)m_8 exits in stream :math:m_{12}.<br /> <br /> #. Write a system of equations in the form :math:{\bf Ax = b} that can be used to calculate the mass flow rate of every stream in the flowsheet. <br /> <br /> The row order in a system of equations has no effect on the solution :math:{\bf x}. But to make grading this question easier, please write your system of equations using :math:{\bf x} = [m_1,\, m_2\, \ldots\,,\,m_{11},\,m_{12}] and so that matrix :math:{\bf A} has 1's on the diagonal. <br /> <br /> #. Solve this system of equations using computer software, at the value of :math:\alpha = 0.20. <br /> <br /> * Show your matrix :math:{\bf A}<br /> * Report the flow of the species in every stream, and in particular, the flow in stream :math:m_{12}.<br /> <br /> #. The next objective is to adjust the recycle ratio, :math:\alpha, so that the mass flow of the species leaving the flowsheet is :math:m_{12} = 1.0 kg/hour.<br /> <br /> * You may use any technique you like to find :math:\alpha, but every time you solve the system of equations :math:{\bf Ax = b} for a given value of :math:\alpha it will count as one function evaluation.<br /> * If you plan to plot the system at various values of :math:\alpha, then each point on the plot is counted as a function evaluation.<br /> * The solution in part 2, with :math:\alpha=0.20 counts as one function evaluation already, and should be your starting point for solving this part of the question. <br /> * Clearly explain your strategy.<br /> * Report the total number of function evaluations you used to achieve an :math:\alpha value that is accurate within :math:1\times 10^{-2}. There is **one bonus mark** available for getting an answer within 5 function evaluations or less.<br /> <br /> Solution <br /> --------<br /> <br /> #. *Marking scheme:*<br /> <br /> * 3/12 = 0.25 marks awarded for each equation in the system<br /> * Small errors in transferring equations to the :math:Ax = b form were ignored (where initial equation derivations were given)<br /> <br /> The first part of this question tasks us with developing the steady state mass balances for this system. Since we are dealing solely with the mass flows between units and not the internal dynamics of each unit operation this simplifies our modelling substantially and reduces the number of assumptions we need to make. We already know that mass must obviously be conserved, so really the only assumption we need to make is that the generalizations of each unit operation given above are correct (kind of an arbitrary assumption). Since we are operating at steady state we know that the overall mass balance around each unit must satisfy the following equality for the species of interest:<br /> <br /> .. math::<br /> \left\{\text{Species in}\right\} - \left\{\text{Species out}\right\} + \left\{\text{Species Generated}\right\} - \left\{\text{Species consumed}\right\} = 0<br /> <br /> **Reactor A**<br /> <br /> Here we are given information on the overall mass conversion (40%) of reactor A and so know the consumption term of the reactor. We also know the inlet and outlet flows from the diagram itself. <br /> <br /> .. math::<br /> \left\{m_{1} + m_{10}\right\}-\left\{m_{3}\right\}+\left\{0\right\}-\left\{\left(0.40\right)\left(m_{1} + m_{10}\right)\right\} &amp;= 0\\ m_{3} &amp;= \left(1-0.40\right)\left(m_{1} + m_{10}\right)\\ m_{3} &amp;= \left(0.60\right)\left(m_{1} + m_{10}\right)\\<br /> <br /> **Mixing tank B**<br /> <br /> .. math::<br /> \left\{m_{2} + m_{3} + m_{9}\right\}-\left\{m_{4}\right\}+\left\{0\right\}-\left\{0\right\} &amp;= 0\\ m_{4} &amp;= m_{2} + m_{3} + m_{9}\\<br /> <br /> **Mixing tank C**<br /> <br /> Here we are given additional information on the exact splits of the outlet terms (1/3 for each outlet) and so can partition the overall mass balance into 3 separate balances.<br /> <br /> .. math::<br /> m_{5} &amp;= m_{6} = m_{10} = \left(\frac{1}{3}\right)\left\{\text{species out}\right\}\\ m_{5} &amp;= \left(\frac{1}{3}\right)\left(m_{4} + m_{11}\right)\\ m_{6} &amp;= \left(\frac{1}{3}\right)\left(m_{4} + m_{11}\right)\\ m_{10} &amp;= \left(\frac{1}{3}\right)\left(m_{4} + m_{11}\right)\\<br /> <br /> **Reactor D**<br /> <br /> Here we are given information on the overall mass conversion (82%) of reactor D and so know the consumption term of the reactor. We are also given additional information on the exact splits of the outlet terms (1/2 for each outlet) and so can partition the overall mass balance into 2 separate balances.<br /> <br /> .. math::<br /> m_{7} &amp;= m_{9} = \left(\frac{1}{2}\right)\left\{\text{species out}\right\}\\ &amp; \\ \left\{\text{species out}\right\} &amp;= \left(0.18\right)m_{5}\\ &amp; \\ m_{7} &amp;= \left(\frac{1}{2}\right)\left(0.18\right)m_{5}\\ m_{7} &amp;= \left(0.09\right)m_{5}\\ &amp; \\ m_{9} &amp;= \left(\frac{1}{2}\right)\left(0.18\right)m_{5}\\ m_{9} &amp;= \left(0.09\right)m_{5}\\<br /> <br /> **Reactor E**<br /> <br /> Here we are given information on the overall mass conversion (15%) of reactor E and so know the consumption term of the reactor. We also know the inlet and outlet flows from the diagram itself.<br /> <br /> .. math:: <br /> m_{8} &amp;= \left(1-0.15\right)\left(m_{6} + m_{7}\right)\\ m_{8} &amp;= \left(0.85\right)\left(m_{6} + m_{7}\right)\\<br /> <br /> **Final split**<br /> <br /> Here we are given information on the split following Reactor E as a function of split factor :math:\alpha.<br /> <br /> .. math::<br /> m_{11} &amp;= \alpha \left\{\text{species out}\right\}\\ &amp; \\ m_{12} &amp;= \left(1-\alpha\right) \left\{\text{species out}\right\}\\ &amp; \\ \left\{\text{species out}\right\} &amp;= m_{8}\\ &amp; \\ m_{11} &amp;= \alpha \left\{\text{species out}\right\}\\ m_{11} &amp;= \alpha m_{8}\\ &amp; \\ m_{12} &amp;= \left(1-\alpha\right) \left\{\text{species out}\right\}\\ m_{12} &amp;= \left(1-\alpha\right) m_{8}\\<br /> <br /> **Extra info**<br /> <br /> So far we have been able to develop 10 steady state balances. However, we have 12 variables (13 if you count :math:\alpha) and so we need 2 additional pieces of information and an :math:\alpha value to make this system of equations solvable. Luckily these have been provided in the problem statement in the form of the specified inlet flows :math:m_{1} and :math:m_{2}.<br /> <br /> .. math::<br /> m_{1} &amp;= 10.0\;[kg/hour]\\ &amp; \\ m_{2} &amp;= 2.0\;[kg/hour]\\ &amp; \\ \alpha &amp;= 0.2\\<br /> <br /> Converting the system to the form :math:Ax = b<br /> <br /> .. math::<br /> \left[\begin{array}{cccccccccccc} 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ -0.6 &amp; 0 &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; -0.6 &amp; 0 &amp; 0 \\ 0 &amp; -1 &amp; -1 &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; -1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; -\frac{1}{3} &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; -\frac{1}{3} &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; -\frac{1}{3} &amp; 0 &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; -\frac{1}{3} &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; -0.09 &amp; 0 &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; -0.85 &amp; -0.85 &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; -0.09 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; -\frac{1}{3} &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; -\frac{1}{3} &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; -\alpha &amp; 0 &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; -(1-\alpha) &amp; 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{c} m_{1}\\ m_{2}\\ m_{3}\\ m_{4}\\ m_{5}\\ m_{6}\\ m_{7}\\ m_{8}\\ m_{9}\\ m_{10}\\ m_{11}\\ m_{12}\\ \end{array}\right] = \left[\begin{array}{c} 10\\ 2\\ 0\\ 0\\ 0\\ 0\\ 0\\ 0\\ 0\\ 0\\ 0\\ 0\\ \end{array}\right]<br /> <br /> #. *Marking scheme:*<br /> <br /> * 0.75 for correct code or process used to solve the problem<br /> * 0.25 for correct answer<br /> <br /> .. math::<br /> <br /> {\bf x} &amp;= [m_1,\, m_2\, \ldots\,,\,m_{11},\,m_{12}] \\ &amp;= [10.0, 2.0, 8.26, 10.6, 3.77, 3.77, 0.34, 3.49, 0.34, 3.77, 0.7, {\bf 2.79}]<br /> <br /> #. *Marking Scheme:*<br /> <br /> Given the open ended nature of this question it is hard to provide a hard and fast marking scheme. Roughly speaking 0.5 marks were awarded for acknowledging the type of problem and properly coding/explaining the NLAE function, 2.0 marks were awarded for properly explaining/coding/executing your chosen root finding method, and 0.5 marks were awarded for the final answer. Small mistakes were ignored (ex. typos) as long as not too many were encountered but serious flaws in logic were naturally penalized.<br /> <br /> The crux of solving this problem lies in realizing that if you treat the LAE in part 1 as a black box function of one variable (alpha) then this problem boils down to a root finding exercise involving one NLAE. Therefore you can apply any of the NLAE techniques (barring Newton-Raphson because you do not know the exact function, thus black box...) covered in slide set C. In order to solve the system in 5 - 6 iterations (and thus get the bonus marks) the best approach to apply would be the secant method. I found that whether the system solved in 5 or 6 iterations depended on the second point provided as an initial guess. The function itself has the basic shape shown below (note that this is the function :math:f(\alpha)=m_{12}(\alpha)-1.0). It is easy to see that the function has a very gentle slope and so is very well approximated by a straight line, which explains why the secant method works so well.<br /> <br /> .. figure:: ../che3e4/Take-home-midterm/images/take-home-midterm-Q3-alpha.png<br /> :scale: 80<br /> :align: center<br /> <br /> Regardless of the method you end up using you must start by defining a function file to calculate :math:f(\alpha). Since we are trying to solve for when :math:m_{12} = 1.0 it is also important to remember to subtract 1.0 from the :math:m_{12} value calculated using the LAE system from part 1, so that we are dealing with an appropriate root finding function form. The secant method will be used here to provide an example of how to approach this problem, but really any root finding method would be acceptable as long as you justified your choice and explained your process. Note that the MATLAB files have been appended to make the display look nicer in this document but in order to run the code you would need to split the provided code into its three original files as indicated.<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Take-home-midterm/code/q3_p3_main_combined.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Take-home-midterm/code/flowsheet.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> .. raw:: latex<br /> <br /> \newpage<br /> <br /> <br /> Question 4 [6 = 2 + 4]<br /> =======================<br /> <br /> .. note:: Even though this question is typical of material learnt about in later courses, you are still able to solve this question using tools just covered in this course.<br /> <br /> A polymer compound is being held in a small stirred tank that is externally heated. The polymer leaving the tank must have a certain viscosity so that the downstream processes can operate as required. The primary method of adjusting the viscosity is by setting the temperature of the tank. Several experiments, performed by your colleagues, have determined the system behaves in a second-order manner (you will learn how to determine this in your process control course). <br /> <br /> .. math::<br /> <br /> \tau^2 \frac{{\rm d}^2\mu(t)}{{\rm d}t^2} + \tau\frac{{\rm d}\mu(t)}{{\rm d}t} = K_p(T(t) - 318)<br /> <br /> where:<br /> <br /> * :math:\mu(t) is the viscosity measured in centipoise, cP and the target viscosity required is 30 cP<br /> * :math:T(t) is the tank temperature, measured in Kelvin<br /> * :math:K_p = -0.25 cP/K<br /> * :math:\tau = 2.1 minutes<br /> <br /> A feedback control system is implemented to keep :math:\mu at its target of 30 cP. The type of feedback controller used is a *proportional-only* controller, meaning the tank temperature, :math:T(t) is adjusted in proportion to the deviation (error) in viscosity, as given by:<br /> <br /> .. math::<br /> <br /> T(t) = K_c\big[30 - \mu(t)\big] + 318<br /> <br /> where :math:K_c = -1.9 K/cP. At steady state, the tank temperature is normally set to 318 K to maintain the required viscosity of 30 cP.<br /> <br /> #. According to this feedback control law, what should the tank temperature be set to if the viscosity is currently at 35 cP? And for 28 cP? Comment whether or not these answers make sense.<br /> <br /> #. Someone has turned the tank's heating system off overnight and the viscosity of the material is currently at 40 cP. Create a plot, as a function of time, that shows how the material's viscosity is brought back to the target of 30 cP. Let :math:t=0 be the moment the heating system is turned back on. Using your plot, estimate how much time will be required to reach **stable** operation (i.e. return to a constant viscosity of 30 cP)?<br /> <br /> **Bonus question [1 mark]**: The value :math:K_c is called the feedback controller's gain (aggressiveness). Show your plot from part 2 of the question, but superimpose the time-varying viscosity curves for two other values of :math:K_c, one higher and one lower. How would you adjust :math:K_c to achieve the target of 30 cP in a shorter amount of time? Can you suggest a better :math:K_c value than the current setting?<br /> <br /> Solution<br /> ---------<br /> <br /> #. The question tells us that at steady state, the temperature is set to 318K to maintain the viscosity at 30cP. We can see this also by substituting into :math:T(t) = K_c\big[30 - \mu(t)\big] + 318 = -1.9\big[30 - 30\big] + 318 = 318 K. But when the viscosity starts to deviate from target:<br /> <br /> * e.g. at 35 cP, then the tank temperature is set to :math:T(t) = -1.9 \big[30 - 35\big] + 318 = 327.5 K<br /> * e.g. at 28 cP, then :math:T(t) = -1.9 \big[30 - 28\big] + 318 = 314.2 K. <br /> <br /> This values do makes sense: when :math:\mu = 35 cP (viscosity is above target) the temperature is increased above 318K, meaning that the viscosity will start to decrease to reach the target of 30 cP. As the viscosity gets closer and closer to the target, the temperature gets closer to its steady-state value of 318 K.<br /> <br /> Similarly if the viscosity is too low, then the temperature is lowered so that the viscosity will rise with time.<br /> <br /> #. *Grading*: 1 to convert to coupled ODEs, 1 for initial conditions, 1 for plot, 1 for determining stable operation (any reasonable answer got full grade).<br /> <br /> We must convert the second order ODE system to a set of two coupled first-order ODEs. Let :math:y_1 = \mu and let :math:y_2 = \frac{d\mu}{dt}. Then:<br /> <br /> .. math::<br /> <br /> \frac{dy_1}{dt} = \frac{d\mu}{dt} = y_2 \\ \frac{dy_2}{dt} = \frac{K_p (T(t) - 318.0)}{\tau^2} - {y_2}{\tau}\\<br /> <br /> with the algebraic equation :math:T(t) = K_c\big[30 - \mu(t)\big] + 318. Please note: there is no need to substitute these values into the derivatives and simplify. Rather let the computer do the calculations, than you making an error.<br /> <br /> The plot from the ODE integrator output (code is shown also) shows the system reaching a stable point at about 24 minutes.<br /> <br /> .. figure:: ../che3e4/Take-home-midterm/images/take-home-mideterm-feedback-control.png<br /> :scale: 75<br /> :align: center<br /> <br /> The Python code used to generate the above figure is shown here. MATLAB code would be very similar.<br /> <br /> .. literalinclude:: ../che3e4/Take-home-midterm/code/feedback.py<br /> <br /> <br /> Question 5 <br /> ================<br /> <br /> This question considers a pilot-plant CSTR, liquid reaction process, fed with reactants A, B and C. There are two reactions taking place in the system:<br /> <br /> .. math::<br /> :nowrap:<br /> <br /> \begin{align*} {\sf A} + {\sf B} &amp;\rightarrow 2{\sf D} + {\sf E} \\ 2{\sf C} + {\sf D} &amp;\rightarrow {\sf F} + {\sf E} \end{align*} <br /> <br /> The desired products are :math:{\sf E} and :math:{\sf F}, while species :math:{\sf D} is an intermediate. From economic considerations, it is desirable to operate the reactor to maximize the production of :math:{\sf E} and :math:{\sf F}. This is achieved by minimizing the amount of unreacted :math:{\sf D} in the outlet from the reactor.<br /> <br /> One way this can be done is by adjusting the tank's jacket temperature, which in turn affects the reaction kinetics and alters the balance of products in the outlet. Unfortunately the first principles rate expressions for the reactions are not known, so derivation of mass and energy balances to find this jacket temperature is not possible. <br /> <br /> You have time and resources to run the pilot-plant reactor at only 8 different jacket temperatures, between a minimum and maximum range of 278 K to 355 K. You are expected to use any of the tools from this course to find a jacket temperature that leads to the lowest possible concentration of :math:{\sf D} [mol/L]. You may use of the fact (learnt in your calculus course) that the optimum -- either a maximum or a minimum -- of a function :math:f(x) occurs at :math:\displaystyle \frac{df(x)}{dx} = 0; and that optimum is a minimum if :math:\displaystyle \frac{df^2(x)}{dx^2} &gt; 0.<br /> <br /> A model of the process has been simulated, and is available on the course website &lt;https://takehome.learnche.org/&gt;_. Nominate *one of your group members* and email their name and student number to the course instructor, together with the name of the other, optional, group member. This will activate an account for your group. Once you sign into the account you will be able to enter in various values of the jacket temperature, and the server will simulate the experiment and return the corresponding concentration of :math:{\sf D}, as if the pilot plant was operated at that jacket temperature. <br /> <br /> Four of the grading points for this question will be marked mainly on the *description of your strategy and the systematic methodology followed* to locate a suitable jacket temperature. Make sure you explain your approach clearly and present good visualization plot(s), exactly as you would have to in a report to your manager. Your final answer (one point) must provide a recommendation to your manager for the jacket temperature.<br /> <br /> Also note: <br /> <br /> * There is error (noise) in the concentration measurements of :math:\pm 1.0 mol/L.<br /> * The server will return different results for each group.<br /> * Please enter your jacket temperature settings carefully - if you input incorrect values you will have to work with those results. **You will only be able to run at most 8 experiments.** <br /> * The server will also keep track of and display all your previous experiments on a results sheet.<br /> <br /> Once you have completed the question, include the result sheet from the server in your answerr. (The true jacket temperature for your team will be be available on the website after the exam is handed in). Please note, there is more than one correct method to approach the problem.<br /> <br /> There is **1 bonus mark** if you are able to locate the jacket temperature within :math:\pm 0.5 K and additional bonus marks as follows: :math:\displaystyle \frac{\max\left\{6-N,0\right\}}{4}, where :math:N is the number of experiments you used.<br /> <br /> .. For next time: add a note to the question: <br /> <br /> the reason for every run must be given: i.e. accounted for<br /> <br /> Maybe add? highly recommend you write your solution to the question as you evaluate points; many groups do all the experiments, but then write up afterwards, trying to justify (incorrectly) their experiments.<br /> <br /> <br /> Solution<br /> ---------<br /> <br /> Grading was awarded as follows (with discretion for cases that didn't quite meet all these criteria):<br /> <br /> *  for discussing how the initial points were chosen (equally spaced, or some other systematic, i.e. non-random, way): though using **all your experiments** with equally spaced points makes little sense, so 0.25 was deducted for that.<br /> *  discussion of any systematic method used (e.g. Newton polynomials, spline, *etc*)<br /> *  for discussing how error in the concentration values will affect your method chosen (spline and polynomial fitting assumes no error, but you can accommodate for it by fitting the points relative far apart, so error doesn't affect your results too much)<br /> *  for good, clear plots that show intermediate curve fitting, and how/why you decided to add extra experiments, or stop adding experiments<br /> *  for your calculations from the curve fit to present your final answer; note that the final answer need not be an experiment that was actually run<br /> * Bonus grading was changed so that you got 1 mark if you were within :math:\pm 0.5 K of the true value (original it was :math:\pm 0.25 K, which was quite a demanding criterion).<br /> <br /> A 0.75 mark was deducted if you:<br /> <br /> * fit splines/polynomials through points close together, since there is error and you will get thrown off<br /> * fit a spline over the entire range of data, which is also not suitable since you cannot hope to capture the phenomena from only a few data points over a wide range<br /> <br /> A 0.25 mark was deducted if plots weren't clear, experimental points weren't labelled (managers find it easier to understand your approach on a clear plot than reading paragraphs of text).<br /> <br /> Some notes regarding this question:<br /> <br /> * When fitting a function to points, equally spaced points are a good starting point if you have no other prior knowledge. However, you will get better information if you used four points at roughly 15%, 35%, 65%, 85% across the range. Most groups fit 5 points equally spaced. Points right at the lower and upper bound can be avoided: I would rather fit 4 than 5 points - if you need to fit a point at 0% or 100% then you have the option later on.<br /> * However, as you start to accumulate points you now have some inference of the system's behaviour and you should adjust your strategy.<br /> * For example, if you see your minimum in the left side of the plot, do not add any points to the right side: these are not going to improve your fit.<br /> * Can view this question as solving :math:f'(x) = 0, but you have to first find :math:f(x) by selecting points and fitting some sort of curve.<br /> * Don't place points close to each other, to account for error in D. Also not too far from each, so that you model the local minimum.<br /> * Point placement is not an easy problem, as most of you resorted to evenly spaced points. The best time to do an experiment is after the experiment.<br /> * Place your points where you will get maximum information (value) from them.<br /> * Don't fit the spline across points from beginning to end of the range.<br /> <br /> I showed examples in class of various cases that helped illustrate these points.<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.5cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Take-home_exam_-_Solution&diff=1109 Take-home exam - Solution 2018-09-16T14:35:40Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 17:30 on '''22 November 2010'''.<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = Take-home-exam-solution.pdf<br /> | questions_text_alt = Take home solution<br /> | solutions_PDF = <br /> | solutions_text_alt = Solutions by Kevin, Ali and Elliot.<br /> | other_instructions = Hand-in at class.<br /> }}<br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = True/&gt;<br /> <br /> .. Sections<br /> <br /> Modelling: series of CSTRs <br /> Linear algebraic equations flowsheet <br /> Nonlinear zeros flowsheet<br /> Curve fitting assumed in other questions<br /> Numerical integration and differentiation blood question<br /> ODEs waste water plant<br /> <br /> Instructions<br /> ===============<br /> <br /> The purpose of this exam is only a little different to other exams. The difference is that this exam will assess your ability to answer more realistic problems that require more time, thought, and access to a computer. You are expected to use **any appropriate tools** to solve the problems in this exam, particularly the tools learnt in this course. <br /> <br /> The 3E4 course teaches you a collection of tools that will be useful in your engineering career. And the challenges you will face do not come neatly posed as problems of the type: &quot;Solve this linear system using Gauss elimination&quot; -- rather you need to reformulate your challenge and pick the appropriate tool to solve the problem, taking into account the level of complexity.<br /> <br /> In that regard there are many correct ways to achieve a result. In this exam about 80% of the grade will be given for **how** you solve the problem, and we will pay particular attention to the **justifications** you provide along the way. You must clearly outline **why** you choose the method and **why** you did not choose alternative methods. The **accuracy of how you implement your method** is also important, but the actual solution is of little value for determining your grade.<br /> <br /> Also, as you will find in your career as an engineer, you will have to apply tools that you learnt about long ago, tools just learnt recently, or you may have do some research and figure out how to use a tool you've never used before. This exam has those aspects as well.<br /> <br /> Some other points are:<br /> <br /> * Complete this exam with 1 other person, or by yourself.<br /> * Please identify the person you work with at the top of your answer submission. **Please note: submit one solution per group**.<br /> * Please attribute any sources of reference you used (textbooks, websites, and so on).<br /> * The intention of the group work is that you discuss the questions and collaborate in the same way you have done in the assignments.<br /> * But **do not share** any electronic files (e.g. Word documents, source code) outside your group; take care in the computer labs to safeguard your work.<br /> * Like any other exam, neither the TAs nor myself are able to answer direct questions about the exam. Similarly you *should not* look for help about a *specific question* from other resources (e.g. asking for help with the question on a website, friends, other students in the class, *etc*).<br /> * You may use the course notes and any other textbooks and resources though.<br /> * There is no make-up for this exam.<br /> * You will benefit from going through the software tutorials &lt;https://learnche.org/3E4/Software_tutorial&gt;_ on the course website.<br /> * Your answers should preferably be typed up.<br /> * Your answers must be structured like a report and flow together logically. Please do not submit pages and pages of computer printouts and source code in appendices - unfortunately this will be penalized.<br /> <br /> - Hand out date: 17 November 2010,<br /> - **Due date**: <br /> - Paper hand-in: by **17:30 on 22 November 2010, in class**<br /> - Totally electronic hand in: email to dunnkg@mcmaster.ca before the above time and date.<br /> - A valid electronic hand in must be in PDF format *only* (no separate Word, MATLAB or Python files). If you are on Windows, you could use PDFCreator &lt;http://sourceforge.net/projects/pdfcreator/&gt;_ to make PDF's; Macs and Linux have built-in PDF capability.<br /> <br /> .. Note:: There are a maximum of 30 grade points available in the 5 questions, plus a variable amount of bonus points. <br /> <br /> .. raw:: latex<br /> <br /> \pagestyle{plain}<br /> \vspace*{0.2cm}<br /> \hrule<br /> \vspace*{0.2cm}<br /> %\newpage<br /> <br /> **Only brief solutions are provided as we covered all the questions in class. The grading schedule is provided with each question.**<br /> <br /> Question 1 [7 = 2 + 2 + 2 + 1]<br /> ===============================<br /> <br /> .. Green modelling book: p 316<br /> .. Tests numerical differentiation, modelling, curve fitting and common sense.<br /> <br /> Background: Chemical engineers have made many contributions to the medical field; similarly in this question you must use your engineering knowledge in a different context. <br /> <br /> Consider the case of injecting a medical treatment into a person's blood system. Subsequent to the injection, the body starts to metabolize (break down, or use up) the drug. In this study a drug was injected into the patient, and samples taken over the next 10 hours. This table reports the concentration of the drug in the blood stream, :math:C:<br /> <br /> ================== ==== ==== ==== ==== ==== ==== ==== <br /> :math:t [hour] 1 2 3 5 6 8 10 <br /> ------------------ ---- ---- ---- ---- ---- ---- ---- <br /> :math:C [mg/L] 10.1 7.5 5.7 3.1 2.4 1.3 0.7<br /> ================== ==== ==== ==== ==== ==== ==== ==== <br /> <br /> Medical models often use a well-mixed reactor to approximate the time-dependent behaviour of parts of the body: e.g. kidneys, stomach, and other organs. In this question you can assume the blood system to be a well-mixed CSTR, operating in batch mode: i.e. no inlet, or outlet, and of constant volume (about 5L for a typical adult). Assume the injection immediately mixes with all the blood, and shows up as :math:C(t=0). <br /> <br /> #. Provide an estimate of the drug's concentration at :math:t=0 hours. Please justify your choice of method, and provide a plot to assess if your estimate is reasonable.<br /> <br /> #. Please provide the lowest error estimate of :math:\frac{dC}{dt} for every data point in the table (without using Richardson's extrapolation).<br /> <br /> #. Next assume the drug is eliminated from the patient's blood into the rest of the body using a first-order rate expression, :math:r = -kC. Obviously the mechanism by which the drug is metabolized is much more complex, but we don't know the cellular pathways and their rate constants: so lump them together and assume the overall mechanism is first order. Derive the dynamic equation that would describe the concentration of the drug in the bloodstream as a function of time.<br /> <br /> #. Using your values from part 2, calculate the average rate constant. **Bonus mark of 0.5** if you use least squares regression instead of just calculating the average.<br /> <br /> Solution<br /> ---------<br /> <br /> .. See code in code/injection.py<br /> <br /> #. The time range given in the table does not contain :math:t=0. So, any method used to estimate :math:C(t=0) will be an extrapolation. A simple way to estimate :math:C(t=0) is by a linear extrapolation, which can be justified by the small distance between :math:t=0 and :math:t=1 and also :math:t=1 and :math:t=2 (so a linear behaviour can be assumed over that range). We pass a line through :math:(1, 10.1) and :math:(2, 7.5) as :math:C = -2.6t + 12.7, so :math:C(t=0) = 12.7.<br /> <br /> A spline or polynomial fit, through the first 3 points or so would also be a valid, though longer approach. The polynomial through the first 3 points is :math:C(t) = 13.5 -3.8t + 0.4t^2, so here :math:C(t=0) = 13.5. Both fits are illustrated in the figure.<br /> <br /> .. figure:: ../che3e4/Take-home-midterm/images/take-home-Q1-part1.png<br /> :scale: 50<br /> :align: center<br /> <br /> As explained in class, neither value is correct in practice, since :math:C(t=0) = 0 the moment the injection is administered.<br /> <br /> #. The lowest error estimate of :math:\frac{dC}{dt}:<br /> <br /> .. math::<br /> <br /> \begin{array}{llll} t &amp; C &amp; \text{Lowest error method} &amp; Result \\ \hline 1 &amp; 10.1 &amp; \text{forward diff} &amp; -2.60 \\ 2 &amp; 7.5 &amp; \text{central diff} &amp; -2.20 \\ 3 &amp; 5.7 &amp; \text{central diff} &amp; -1.750 \\ 5 &amp; 3.1 &amp; \text{central diff} &amp; -1.033 \\ 6 &amp; 2.4 &amp; \text{backward diff} &amp; -0.70 \\ 8 &amp; 1.3 &amp; \text{central diff} &amp; -0.4250 \\ 10 &amp; 0.7 &amp; \text{backward diff} &amp; -0.30 \\ \hline \end{array}<br /> <br /> #. In this problem our *control volume* to be modelled is the entire human's body. So, the bloodstream inside the body makes up a batch system because there is no blood flow into/out of the body. Another assumption is that we have well-mixed system; that is there is no spatial distribution of quantities through the body. This assumption means that the blood circulation is fast enough, making a well-mixed environment. Also, the system is aqueous and the volume is constant. Given these assumptions, the dynamical model would be as follows:<br /> <br /> .. math::<br /> <br /> \text{Accumulation} &amp;= \text{Input} - \text{Output} + \text{Generation} - \text{Consumption}\\ \displaystyle\frac{d(VC)}{dt} &amp;= 0 - 0 + 0 - rV\\ \displaystyle\frac{dC}{dt} &amp;= -kC<br /> <br /> #. In the model we just obtained, the parameter :math:k is not known. But we can compute an approximation of it using the dynamical model, and the estimated values of the derivative :math:\frac{dC}{dt} at different concentrations :math:C. One way is to obtain values of :math:k_i corresponding to each data point :math:C_i, then computing an average of the :math:k_i values:<br /> <br /> .. math::<br /> <br /> \begin{array}{llll} t &amp; C &amp; \displaystyle\frac{dC}{dt} &amp; k \\ \hline 1 &amp; 10.1 &amp; -2.60 &amp; 0.2574 \\ 2 &amp; 7.5 &amp; -2.20 &amp; 0.2933 \\ 3 &amp; 5.7 &amp; -1.750 &amp; 0.3070 \\ 5 &amp; 3.1 &amp; -1.033 &amp; 0.3332 \\ 6 &amp; 2.4 &amp; -0.70 &amp; 0.2917 \\ 8 &amp; 1.3 &amp; -0.4250 &amp; 0.3269 \\ 10 &amp; 0.7 &amp; -0.30 &amp; 0.4286 \\ \hline \end{array}<br /> <br /> The average rate constant is :math:\bar{k}= \displaystyle\frac{\sum_{i=1}^{7} {k_i}}{7}=0.3197<br /> <br /> Now we try to obtain :math:k using regression. Let us denote :math:\displaystyle\frac{dC}{dt} by :math:y, and :math:y_{\text{model}}=\displaystyle\frac{dC}{dt}=k(-C) represent the derivative that we obtain from the model. On the other hand, we also calculated the derivatives in Part 1 using the experimental data (even though approximate!). Let us denote them by :math:y_{\text{exp}}=(\displaystyle\frac{dC}{dt})_{\text{exp}}. Now we can set up a least square regression as follows.<br /> <br /> .. math::<br /> <br /> \text{min}\quad J= \displaystyle\sum_{i=1}^{7} {(y_{\text{model,i}}-y_{\text{exp,i}}})^2<br /> <br /> In order to minimize the above error function :math:J, we have to equate its derivative w.r.t. :math:k to zero. That is, :math:\displaystyle\frac{dJ}{dk}=0<br /> <br /> .. math::<br /> <br /> \displaystyle\frac{dJ}{dk}= 2\displaystyle\sum_{i=1}^{7} (\displaystyle\frac{dy_{\text{model,i}}}{dk})(y_{\text{model,i}}-y_{\text{exp,i}})=0<br /> <br /> Note that :math:y_{\text{model,i}}=k(-C_i). So :math:\displaystyle\frac{dy_{\text{model,i}}}{dk}=-C_i. Substituting these into the above equation will give:<br /> <br /> .. math::<br /> <br /> \displaystyle\frac{dJ}{dk}= \displaystyle\sum_{i=1}^{7} (C_i)({k(-C_i)}-y_{\text{exp,i}})=0<br /> <br /> Rearranging the above equation and solving it for :math:k (left to you as an exercise) will complete the derivation:<br /> <br /> .. math::<br /> <br /> k &amp;= \displaystyle\frac{\sum_{i=1}^{7} -C_i y_{\text{exp,i}}}{\sum_{i=1}^{7} C_i^2} \\ k &amp;=\displaystyle\frac{-(10.10\times -2.60+ 7.50\times -2.20+ 5.70\times -1.75+ 3.10\times -1.033+2.40\times -0.70+1.30\times -0.425+0.70\times -0.30)}{10.10^2+7.50^2+5.70^2+3.10^2+2.40^2+1.30^2+0.70^2}\\ k &amp;=0.2803<br /> <br /> **Note**: According to the model :math:y_{\text{model}}=\displaystyle\frac{dC}{dt}=k(-C), the slope :math:k is the only coefficient to be obtained. But if you use MATLAB's command polyfit or Excel's linear regression, by default they will calculate two coefficients, one is the slope and the other one is the intercept. So be careful: having an intercept may not be physically meaningful in your system. In such a case (as in this problem), you should have MATLAB or Excel set the intercept to zero or do the regression yourself.<br /> <br /> **Note**: we showed an alternative method in class that first integrated the equation to obtain :math:\ln[C] = \ln[C(t=0)] - kt. By regressing values of :math:\ln[C] onto :math:t we can estimate both the rate constant and the initial concentration. Using this method you will obtain :math:k=0.295\,\text{hours}^{-1} and :math:C(t=0) = 13.7 mg/L.<br /> <br /> <br /> <br /> Question 2 [5 = 1 + 1 + 3]<br /> ===========================<br /> <br /> A batch bioreactor (well-mixed tank, but no inlet or outlet) is charged with substrate at time :math:t=0 to start a reaction that will consume the substrate. The reaction mechanism is too complex to model exactly, but previous experience suggests it is roughly a first-order reaction. The apparent first-order rate expression is :math:r = kS, where :math:k = 0.58\,\,\text{hours}^{-1}, and :math:{\sf S} is the concentration of the substrate being depleted.<br /> <br /> #. Show that the dynamic rate of change of :math:{\sf S} is given by:<br /> <br /> .. math::<br /> <br /> \frac{d{\sf S}(t)}{dt} = -k {\sf S}(t)<br /> <br /> Also state any assumptions that were made in arriving at this expression.<br /> <br /> #. The time required to consume the substrate from a starting level of 260 g/L to 5 g/L can be calculated by analytical integration of the above expression. What is the total time required?<br /> <br /> #. Had you forgotten how to integrate the expression analytically you could resort to using the trapezoidal rule or Simpson's 1/3 rule. **Without** implementing either method, calculate how many panels would be required to ensure a numerical estimate of the time to achieve the above conversion is accurate within 0.01 hours. In other words, how many panels are required to solve the left hand side integral <br /> <br /> .. math::<br /> <br /> \int_{260}^{5}{-\frac{1}{k\sf S}\, d{\sf S}} &amp;= \int_{0}^{t}{d{t}} = t<br /> <br /> to that level of accuracy:<br /> <br /> * when using the trapezoidal rule;<br /> * when using Simpson's 1/3 method.<br /> <br /> <br /> Solution<br /> ---------<br /> <br /> #. Ordinary batch system balance, with no in or out flows, only a reaction term :math:-k \sf S and the accumulation term, :math:\frac{d {\sf S}}{dt}. The key assumption is that all other properties, such as batch volume and physical properties are constant. The system is also assumed to be well-mixed, as stated. Other assumptions would be that the energy inputs and output are negligible (i.e. shaft work, heat losses to the environment and specifically that the reaction rate is not a function of temperature).<br /> <br /> #. :math:\ln(\frac{5}{260}) = -0.58t = 6.81 hours, or 409 minutes.<br /> <br /> #. This question turned out to be less useful than I had intended. I had intended that you calculate and realize how a much smaller number of panels are required for Simpson's rule than the trapezoidal rule - in order to get the same level of error. However, due to:<br /> <br /> * an unfortunate error in the PDF course notes<br /> * an integral that wasn't expressed in terms of time<br /> * a difference between the 5th and 6th edition of the course textbooks<br /> <br /> I've decided to omit this question from the exam, but give 1 bonus point to everyone that seriously attempted the question (i.e. the exam is out of 27 points, but you probably got a bonus point).<br /> <br /> The way to attempt this question was to first rearrange the left-hand integral so that it had units of time, since the error was asked to be below 0.01 hours. The original question was expressed as shown below in the first line, but then later changed to read as the second line.<br /> <br /> .. math::<br /> <br /> \int_{260}^{5}{\frac{1}{\sf S}\, d{\sf S}} &amp;= -k\int_{0}^{t}{d{t}} = -kt \\ \int_{260}^{5}{-\frac{1}{k\sf S}\, d{\sf S}} &amp;= \int_{0}^{t}{d{t}} = t<br /> <br /> We can use the form shown in the first line, as long as we multiply our answer by :math:k afterwards.<br /> <br /> <br /> So the function we are integrating is :math:f(S) = S^{-1} and has units of L/g. We will require derivatives of this function, :math:f''(S) =2S^{-3}, with units of :math:\text{L}^3/\text{g}^3, and :math:f^{(4)}(S) =24S^{-5} with units of :math:\text{L}^5/\text{g}^5.<br /> <br /> The main problem was from the PDF course notes. There was an inaccuracy that said you could find the number of panels for the trapezoidal rule if this expression was satisfied:<br /> <br /> .. math::<br /> <br /> \frac{(b-a)^3}{12n^2}f''(\xi) &lt; 0.01 \qquad\,\text{for some value of $\xi$ in the range $[5, 260]$}<br /> <br /> <br /> However, the correct expression for the error is:<br /> <br /> .. math::<br /> <br /> \left| -\frac{(b-a)^3}{12n^3} \sum_{1}^{n}{f''(\xi_i)} \right| &lt; \left| -kt\right| = (0.58)(0.01) = 0.0058 <br /> <br /> Note the units: :math:a and :math:b are in g/L while :math:f''({\sf S}) has units of :math:\text{L}^3/\text{g}^3, so everything balances.<br /> <br /> The problem is evaluating the summation: you have to find the worst value of :math:f''({\sf S}) within every panel, but the purpose of this question is to find the number of panels! So the course textbook shows that we rather find the average error, and that was the equation in the PDF course notes, except it was incorrect to have the part that :math:\xi had to be in the range from :math:[a, b].<br /> <br /> When I set the exam I used the PDF course notes, not the course textbook, so I was unaware of this complication in the question. My apologies if you spent a lot of time on this question. <br /> <br /> .. A few students resorted to the course textbook version and use an iterative method to solve for the number of panels. But as the equation above shows it is not possible to find the error without knowing the panel locations - but once you know the panel locations, then you can use the second derivative equation and find the error. However, if the error is too high or too low, you need to repeat everything again with a different number of panels.<br /> <br /> Question 3 [7 = 3 + 1 + 3]<br /> ===========================<br /> <br /> The following question is based on the flowsheet below, where the :math:m_i values are the mass flow of the species of interest, the reactant. The purpose of the flowsheet is to eliminate the incoming species at :math:m_1 = 10 kg/hour and convert it to by-products. There is some residual reactant leaving the flowsheet in stream :math:m_{12}.<br /> <br /> .. figure:: ../che3e4/Take-home-midterm/images/take-home-reactor-network.png<br /> :scale: 40<br /> :align: center<br /> <br /> The following additional information is known:<br /> <br /> * :math:m_1 = 10 kg/hour.<br /> * :math:m_2 = 2 kg/hour.<br /> * Unit A operates with 40% efficiency; i.e. of all the material coming in, only 40% of it is converted to other products.<br /> * Unit B is just a mixing tank.<br /> * Unit C is just a mixer/splitter and is operated so that the mass-flow in the outlets are all in the same proportion.<br /> * Unit D operates with 82% efficiency, so 18% of the incoming stream is left unconverted. The two outlets are split in equal proportion as well.<br /> * Unit E operates with 15% efficiency to convert the incoming streams to by-products.<br /> * The split after unit E is operated such that :math:\alpha m_8 is recycled in stream 11, while :math:(1-\alpha)m_8 exits in stream :math:m_{12}.<br /> <br /> #. Write a system of equations in the form :math:{\bf Ax = b} that can be used to calculate the mass flow rate of every stream in the flowsheet. <br /> <br /> The row order in a system of equations has no effect on the solution :math:{\bf x}. But to make grading this question easier, please write your system of equations using :math:{\bf x} = [m_1,\, m_2\, \ldots\,,\,m_{11},\,m_{12}] and so that matrix :math:{\bf A} has 1's on the diagonal. <br /> <br /> #. Solve this system of equations using computer software, at the value of :math:\alpha = 0.20. <br /> <br /> * Show your matrix :math:{\bf A}<br /> * Report the flow of the species in every stream, and in particular, the flow in stream :math:m_{12}.<br /> <br /> #. The next objective is to adjust the recycle ratio, :math:\alpha, so that the mass flow of the species leaving the flowsheet is :math:m_{12} = 1.0 kg/hour.<br /> <br /> * You may use any technique you like to find :math:\alpha, but every time you solve the system of equations :math:{\bf Ax = b} for a given value of :math:\alpha it will count as one function evaluation.<br /> * If you plan to plot the system at various values of :math:\alpha, then each point on the plot is counted as a function evaluation.<br /> * The solution in part 2, with :math:\alpha=0.20 counts as one function evaluation already, and should be your starting point for solving this part of the question. <br /> * Clearly explain your strategy.<br /> * Report the total number of function evaluations you used to achieve an :math:\alpha value that is accurate within :math:1\times 10^{-2}. There is **one bonus mark** available for getting an answer within 5 function evaluations or less.<br /> <br /> Solution <br /> --------<br /> <br /> #. *Marking scheme:*<br /> <br /> * 3/12 = 0.25 marks awarded for each equation in the system<br /> * Small errors in transferring equations to the :math:Ax = b form were ignored (where initial equation derivations were given)<br /> <br /> The first part of this question tasks us with developing the steady state mass balances for this system. Since we are dealing solely with the mass flows between units and not the internal dynamics of each unit operation this simplifies our modelling substantially and reduces the number of assumptions we need to make. We already know that mass must obviously be conserved, so really the only assumption we need to make is that the generalizations of each unit operation given above are correct (kind of an arbitrary assumption). Since we are operating at steady state we know that the overall mass balance around each unit must satisfy the following equality for the species of interest:<br /> <br /> .. math::<br /> \left\{\text{Species in}\right\} - \left\{\text{Species out}\right\} + \left\{\text{Species Generated}\right\} - \left\{\text{Species consumed}\right\} = 0<br /> <br /> **Reactor A**<br /> <br /> Here we are given information on the overall mass conversion (40%) of reactor A and so know the consumption term of the reactor. We also know the inlet and outlet flows from the diagram itself. <br /> <br /> .. math::<br /> \left\{m_{1} + m_{10}\right\}-\left\{m_{3}\right\}+\left\{0\right\}-\left\{\left(0.40\right)\left(m_{1} + m_{10}\right)\right\} &amp;= 0\\ m_{3} &amp;= \left(1-0.40\right)\left(m_{1} + m_{10}\right)\\ m_{3} &amp;= \left(0.60\right)\left(m_{1} + m_{10}\right)\\<br /> <br /> **Mixing tank B**<br /> <br /> .. math::<br /> \left\{m_{2} + m_{3} + m_{9}\right\}-\left\{m_{4}\right\}+\left\{0\right\}-\left\{0\right\} &amp;= 0\\ m_{4} &amp;= m_{2} + m_{3} + m_{9}\\<br /> <br /> **Mixing tank C**<br /> <br /> Here we are given additional information on the exact splits of the outlet terms (1/3 for each outlet) and so can partition the overall mass balance into 3 separate balances.<br /> <br /> .. math::<br /> m_{5} &amp;= m_{6} = m_{10} = \left(\frac{1}{3}\right)\left\{\text{species out}\right\}\\ m_{5} &amp;= \left(\frac{1}{3}\right)\left(m_{4} + m_{11}\right)\\ m_{6} &amp;= \left(\frac{1}{3}\right)\left(m_{4} + m_{11}\right)\\ m_{10} &amp;= \left(\frac{1}{3}\right)\left(m_{4} + m_{11}\right)\\<br /> <br /> **Reactor D**<br /> <br /> Here we are given information on the overall mass conversion (82%) of reactor D and so know the consumption term of the reactor. We are also given additional information on the exact splits of the outlet terms (1/2 for each outlet) and so can partition the overall mass balance into 2 separate balances.<br /> <br /> .. math::<br /> m_{7} &amp;= m_{9} = \left(\frac{1}{2}\right)\left\{\text{species out}\right\}\\ &amp; \\ \left\{\text{species out}\right\} &amp;= \left(0.18\right)m_{5}\\ &amp; \\ m_{7} &amp;= \left(\frac{1}{2}\right)\left(0.18\right)m_{5}\\ m_{7} &amp;= \left(0.09\right)m_{5}\\ &amp; \\ m_{9} &amp;= \left(\frac{1}{2}\right)\left(0.18\right)m_{5}\\ m_{9} &amp;= \left(0.09\right)m_{5}\\<br /> <br /> **Reactor E**<br /> <br /> Here we are given information on the overall mass conversion (15%) of reactor E and so know the consumption term of the reactor. We also know the inlet and outlet flows from the diagram itself.<br /> <br /> .. math:: <br /> m_{8} &amp;= \left(1-0.15\right)\left(m_{6} + m_{7}\right)\\ m_{8} &amp;= \left(0.85\right)\left(m_{6} + m_{7}\right)\\<br /> <br /> **Final split**<br /> <br /> Here we are given information on the split following Reactor E as a function of split factor :math:\alpha.<br /> <br /> .. math::<br /> m_{11} &amp;= \alpha \left\{\text{species out}\right\}\\ &amp; \\ m_{12} &amp;= \left(1-\alpha\right) \left\{\text{species out}\right\}\\ &amp; \\ \left\{\text{species out}\right\} &amp;= m_{8}\\ &amp; \\ m_{11} &amp;= \alpha \left\{\text{species out}\right\}\\ m_{11} &amp;= \alpha m_{8}\\ &amp; \\ m_{12} &amp;= \left(1-\alpha\right) \left\{\text{species out}\right\}\\ m_{12} &amp;= \left(1-\alpha\right) m_{8}\\<br /> <br /> **Extra info**<br /> <br /> So far we have been able to develop 10 steady state balances. However, we have 12 variables (13 if you count :math:\alpha) and so we need 2 additional pieces of information and an :math:\alpha value to make this system of equations solvable. Luckily these have been provided in the problem statement in the form of the specified inlet flows :math:m_{1} and :math:m_{2}.<br /> <br /> .. math::<br /> m_{1} &amp;= 10.0\;[kg/hour]\\ &amp; \\ m_{2} &amp;= 2.0\;[kg/hour]\\ &amp; \\ \alpha &amp;= 0.2\\<br /> <br /> Converting the system to the form :math:Ax = b<br /> <br /> .. math::<br /> \left[\begin{array}{cccccccccccc} 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ -0.6 &amp; 0 &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; -0.6 &amp; 0 &amp; 0 \\ 0 &amp; -1 &amp; -1 &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; -1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; -\frac{1}{3} &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; -\frac{1}{3} &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; -\frac{1}{3} &amp; 0 &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; -\frac{1}{3} &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; -0.09 &amp; 0 &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; -0.85 &amp; -0.85 &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; -0.09 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; -\frac{1}{3} &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; -\frac{1}{3} &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; -\alpha &amp; 0 &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; -(1-\alpha) &amp; 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{c} m_{1}\\ m_{2}\\ m_{3}\\ m_{4}\\ m_{5}\\ m_{6}\\ m_{7}\\ m_{8}\\ m_{9}\\ m_{10}\\ m_{11}\\ m_{12}\\ \end{array}\right] = \left[\begin{array}{c} 10\\ 2\\ 0\\ 0\\ 0\\ 0\\ 0\\ 0\\ 0\\ 0\\ 0\\ 0\\ \end{array}\right]<br /> <br /> #. *Marking scheme:*<br /> <br /> * 0.75 for correct code or process used to solve the problem<br /> * 0.25 for correct answer<br /> <br /> .. math::<br /> <br /> {\bf x} &amp;= [m_1,\, m_2\, \ldots\,,\,m_{11},\,m_{12}] \\ &amp;= [10.0, 2.0, 8.26, 10.6, 3.77, 3.77, 0.34, 3.49, 0.34, 3.77, 0.7, {\bf 2.79}]<br /> <br /> #. *Marking Scheme:*<br /> <br /> Given the open ended nature of this question it is hard to provide a hard and fast marking scheme. Roughly speaking 0.5 marks were awarded for acknowledging the type of problem and properly coding/explaining the NLAE function, 2.0 marks were awarded for properly explaining/coding/executing your chosen root finding method, and 0.5 marks were awarded for the final answer. Small mistakes were ignored (ex. typos) as long as not too many were encountered but serious flaws in logic were naturally penalized.<br /> <br /> The crux of solving this problem lies in realizing that if you treat the LAE in part 1 as a black box function of one variable (alpha) then this problem boils down to a root finding exercise involving one NLAE. Therefore you can apply any of the NLAE techniques (barring Newton-Raphson because you do not know the exact function, thus black box...) covered in slide set C. In order to solve the system in 5 - 6 iterations (and thus get the bonus marks) the best approach to apply would be the secant method. I found that whether the system solved in 5 or 6 iterations depended on the second point provided as an initial guess. The function itself has the basic shape shown below (note that this is the function :math:f(\alpha)=m_{12}(\alpha)-1.0). It is easy to see that the function has a very gentle slope and so is very well approximated by a straight line, which explains why the secant method works so well.<br /> <br /> .. figure:: ../che3e4/Take-home-midterm/images/take-home-midterm-Q3-alpha.png<br /> :scale: 80<br /> :align: center<br /> <br /> Regardless of the method you end up using you must start by defining a function file to calculate :math:f(\alpha). Since we are trying to solve for when :math:m_{12} = 1.0 it is also important to remember to subtract 1.0 from the :math:m_{12} value calculated using the LAE system from part 1, so that we are dealing with an appropriate root finding function form. The secant method will be used here to provide an example of how to approach this problem, but really any root finding method would be acceptable as long as you justified your choice and explained your process. Note that the MATLAB files have been appended to make the display look nicer in this document but in order to run the code you would need to split the provided code into its three original files as indicated.<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Take-home-midterm/code/q3_p3_main_combined.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Take-home-midterm/code/flowsheet.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> .. raw:: latex<br /> <br /> \newpage<br /> <br /> <br /> Question 4 [6 = 2 + 4]<br /> =======================<br /> <br /> .. note:: Even though this question is typical of material learnt about in later courses, you are still able to solve this question using tools just covered in this course.<br /> <br /> A polymer compound is being held in a small stirred tank that is externally heated. The polymer leaving the tank must have a certain viscosity so that the downstream processes can operate as required. The primary method of adjusting the viscosity is by setting the temperature of the tank. Several experiments, performed by your colleagues, have determined the system behaves in a second-order manner (you will learn how to determine this in your process control course). <br /> <br /> .. math::<br /> <br /> \tau^2 \frac{{\rm d}^2\mu(t)}{{\rm d}t^2} + \tau\frac{{\rm d}\mu(t)}{{\rm d}t} = K_p(T(t) - 318)<br /> <br /> where:<br /> <br /> * :math:\mu(t) is the viscosity measured in centipoise, cP and the target viscosity required is 30 cP<br /> * :math:T(t) is the tank temperature, measured in Kelvin<br /> * :math:K_p = -0.25 cP/K<br /> * :math:\tau = 2.1 minutes<br /> <br /> A feedback control system is implemented to keep :math:\mu at its target of 30 cP. The type of feedback controller used is a *proportional-only* controller, meaning the tank temperature, :math:T(t) is adjusted in proportion to the deviation (error) in viscosity, as given by:<br /> <br /> .. math::<br /> <br /> T(t) = K_c\big[30 - \mu(t)\big] + 318<br /> <br /> where :math:K_c = -1.9 K/cP. At steady state, the tank temperature is normally set to 318 K to maintain the required viscosity of 30 cP.<br /> <br /> #. According to this feedback control law, what should the tank temperature be set to if the viscosity is currently at 35 cP? And for 28 cP? Comment whether or not these answers make sense.<br /> <br /> #. Someone has turned the tank's heating system off overnight and the viscosity of the material is currently at 40 cP. Create a plot, as a function of time, that shows how the material's viscosity is brought back to the target of 30 cP. Let :math:t=0 be the moment the heating system is turned back on. Using your plot, estimate how much time will be required to reach **stable** operation (i.e. return to a constant viscosity of 30 cP)?<br /> <br /> **Bonus question [1 mark]**: The value :math:K_c is called the feedback controller's gain (aggressiveness). Show your plot from part 2 of the question, but superimpose the time-varying viscosity curves for two other values of :math:K_c, one higher and one lower. How would you adjust :math:K_c to achieve the target of 30 cP in a shorter amount of time? Can you suggest a better :math:K_c value than the current setting?<br /> <br /> Solution<br /> ---------<br /> <br /> #. The question tells us that at steady state, the temperature is set to 318K to maintain the viscosity at 30cP. We can see this also by substituting into :math:T(t) = K_c\big[30 - \mu(t)\big] + 318 = -1.9\big[30 - 30\big] + 318 = 318 K. But when the viscosity starts to deviate from target:<br /> <br /> * e.g. at 35 cP, then the tank temperature is set to :math:T(t) = -1.9 \big[30 - 35\big] + 318 = 327.5 K<br /> * e.g. at 28 cP, then :math:T(t) = -1.9 \big[30 - 28\big] + 318 = 314.2 K. <br /> <br /> This values do makes sense: when :math:\mu = 35 cP (viscosity is above target) the temperature is increased above 318K, meaning that the viscosity will start to decrease to reach the target of 30 cP. As the viscosity gets closer and closer to the target, the temperature gets closer to its steady-state value of 318 K.<br /> <br /> Similarly if the viscosity is too low, then the temperature is lowered so that the viscosity will rise with time.<br /> <br /> #. *Grading*: 1 to convert to coupled ODEs, 1 for initial conditions, 1 for plot, 1 for determining stable operation (any reasonable answer got full grade).<br /> <br /> We must convert the second order ODE system to a set of two coupled first-order ODEs. Let :math:y_1 = \mu and let :math:y_2 = \frac{d\mu}{dt}. Then:<br /> <br /> .. math::<br /> <br /> \frac{dy_1}{dt} = \frac{d\mu}{dt} = y_2 \\ \frac{dy_2}{dt} = \frac{K_p (T(t) - 318.0)}{\tau^2} - {y_2}{\tau}\\<br /> <br /> with the algebraic equation :math:T(t) = K_c\big[30 - \mu(t)\big] + 318. Please note: there is no need to substitute these values into the derivatives and simplify. Rather let the computer do the calculations, than you making an error.<br /> <br /> The plot from the ODE integrator output (code is shown also) shows the system reaching a stable point at about 24 minutes.<br /> <br /> .. figure:: ../che3e4/Take-home-midterm/images/take-home-mideterm-feedback-control.png<br /> :scale: 75<br /> :align: center<br /> <br /> The Python code used to generate the above figure is shown here. MATLAB code would be very similar.<br /> <br /> .. literalinclude:: ../che3e4/Take-home-midterm/code/feedback.py<br /> <br /> <br /> Question 5 <br /> ================<br /> <br /> This question considers a pilot-plant CSTR, liquid reaction process, fed with reactants A, B and C. There are two reactions taking place in the system:<br /> <br /> .. math::<br /> :nowrap:<br /> <br /> \begin{align*} {\sf A} + {\sf B} &amp;\rightarrow 2{\sf D} + {\sf E} \\ 2{\sf C} + {\sf D} &amp;\rightarrow {\sf F} + {\sf E} \end{align*} <br /> <br /> The desired products are :math:{\sf E} and :math:{\sf F}, while species :math:{\sf D} is an intermediate. From economic considerations, it is desirable to operate the reactor to maximize the production of :math:{\sf E} and :math:{\sf F}. This is achieved by minimizing the amount of unreacted :math:{\sf D} in the outlet from the reactor.<br /> <br /> One way this can be done is by adjusting the tank's jacket temperature, which in turn affects the reaction kinetics and alters the balance of products in the outlet. Unfortunately the first principles rate expressions for the reactions are not known, so derivation of mass and energy balances to find this jacket temperature is not possible. <br /> <br /> You have time and resources to run the pilot-plant reactor at only 8 different jacket temperatures, between a minimum and maximum range of 278 K to 355 K. You are expected to use any of the tools from this course to find a jacket temperature that leads to the lowest possible concentration of :math:{\sf D} [mol/L]. You may use of the fact (learnt in your calculus course) that the optimum -- either a maximum or a minimum -- of a function :math:f(x) occurs at :math:\displaystyle \frac{df(x)}{dx} = 0; and that optimum is a minimum if :math:\displaystyle \frac{df^2(x)}{dx^2} &gt; 0.<br /> <br /> A model of the process has been simulated, and is available on the course website &lt;https://takehome.learnche.org/&gt;_. Nominate *one of your group members* and email their name and student number to the course instructor, together with the name of the other, optional, group member. This will activate an account for your group. Once you sign into the account you will be able to enter in various values of the jacket temperature, and the server will simulate the experiment and return the corresponding concentration of :math:{\sf D}, as if the pilot plant was operated at that jacket temperature. <br /> <br /> Four of the grading points for this question will be marked mainly on the *description of your strategy and the systematic methodology followed* to locate a suitable jacket temperature. Make sure you explain your approach clearly and present good visualization plot(s), exactly as you would have to in a report to your manager. Your final answer (one point) must provide a recommendation to your manager for the jacket temperature.<br /> <br /> Also note: <br /> <br /> * There is error (noise) in the concentration measurements of :math:\pm 1.0 mol/L.<br /> * The server will return different results for each group.<br /> * Please enter your jacket temperature settings carefully - if you input incorrect values you will have to work with those results. **You will only be able to run at most 8 experiments.** <br /> * The server will also keep track of and display all your previous experiments on a results sheet.<br /> <br /> Once you have completed the question, include the result sheet from the server in your answerr. (The true jacket temperature for your team will be be available on the website after the exam is handed in). Please note, there is more than one correct method to approach the problem.<br /> <br /> There is **1 bonus mark** if you are able to locate the jacket temperature within :math:\pm 0.5 K and additional bonus marks as follows: :math:\displaystyle \frac{\max\left\{6-N,0\right\}}{4}, where :math:N is the number of experiments you used.<br /> <br /> .. For next time: add a note to the question: <br /> <br /> the reason for every run must be given: i.e. accounted for<br /> <br /> Maybe add? highly recommend you write your solution to the question as you evaluate points; many groups do all the experiments, but then write up afterwards, trying to justify (incorrectly) their experiments.<br /> <br /> <br /> Solution<br /> ---------<br /> <br /> Grading was awarded as follows (with discretion for cases that didn't quite meet all these criteria):<br /> <br /> *  for discussing how the initial points were chosen (equally spaced, or some other systematic, i.e. non-random, way): though using **all your experiments** with equally spaced points makes little sense, so 0.25 was deducted for that.<br /> *  discussion of any systematic method used (e.g. Newton polynomials, spline, *etc*)<br /> *  for discussing how error in the concentration values will affect your method chosen (spline and polynomial fitting assumes no error, but you can accommodate for it by fitting the points relative far apart, so error doesn't affect your results too much)<br /> *  for good, clear plots that show intermediate curve fitting, and how/why you decided to add extra experiments, or stop adding experiments<br /> *  for your calculations from the curve fit to present your final answer; note that the final answer need not be an experiment that was actually run<br /> * Bonus grading was changed so that you got 1 mark if you were within :math:\pm 0.5 K of the true value (original it was :math:\pm 0.25 K, which was quite a demanding criterion).<br /> <br /> A 0.75 mark was deducted if you:<br /> <br /> * fit splines/polynomials through points close together, since there is error and you will get thrown off<br /> * fit a spline over the entire range of data, which is also not suitable since you cannot hope to capture the phenomena from only a few data points over a wide range<br /> <br /> A 0.25 mark was deducted if plots weren't clear, experimental points weren't labelled (managers find it easier to understand your approach on a clear plot than reading paragraphs of text).<br /> <br /> Some notes regarding this question:<br /> <br /> * When fitting a function to points, equally spaced points are a good starting point if you have no other prior knowledge. However, you will get better information if you used four points at roughly 15%, 35%, 65%, 85% across the range. Most groups fit 5 points equally spaced. Points right at the lower and upper bound can be avoided: I would rather fit 4 than 5 points - if you need to fit a point at 0% or 100% then you have the option later on.<br /> * However, as you start to accumulate points you now have some inference of the system's behaviour and you should adjust your strategy.<br /> * For example, if you see your minimum in the left side of the plot, do not add any points to the right side: these are not going to improve your fit.<br /> * Can view this question as solving :math:f'(x) = 0, but you have to first find :math:f(x) by selecting points and fitting some sort of curve.<br /> * Don't place points close to each other, to account for error in D. Also not too far from each, so that you model the local minimum.<br /> * Point placement is not an easy problem, as most of you resorted to evenly spaced points. The best time to do an experiment is after the experiment.<br /> * Place your points where you will get maximum information (value) from them.<br /> * Don't fit the spline across points from beginning to end of the range.<br /> <br /> I showed examples in class of various cases that helped illustrate these points.<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.5cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Take-home_exam&diff=1108 Take-home exam 2018-09-16T14:32:06Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 17:30 on '''22 November 2010'''; no late hand ins.<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = Take-home-exam.pdf<br /> | questions_text_alt = Take home questions<br /> | solutions_PDF = <br /> | solutions_text_alt = Solutions by Kevin<br /> | other_instructions = Hand-in at class.<br /> }}<br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = True/&gt;<br /> Instructions<br /> ===============<br /> <br /> The purpose of this exam is only a little different to other exams. The difference is that this exam will assess your ability to answer more realistic problems that require more time, thought, and access to a computer. You are expected to use **any appropriate tools** to solve the problems in this exam, particularly the tools learnt in this course. <br /> <br /> The 3E4 course teaches you a collection of tools that will be useful in your engineering career. And the challenges you will face do not come neatly posed as problems of the type: &quot;Solve this linear system using Gauss elimination&quot; -- rather you need to reformulate your challenge and pick the appropriate tool to solve the problem, taking into account the level of complexity.<br /> <br /> In that regard there are many correct ways to achieve a result. In this exam about 80% of the grade will be given for **how** you solve the problem, and we will pay particular attention to the **justifications** you provide along the way. You must clearly outline **why** you choose the method and **why** you did not choose alternative methods. The **accuracy of how you implement your method** is also important, but the actual solution is of little value for determining your grade.<br /> <br /> Also, as you will find in your career as an engineer, you will have to apply tools that you learnt about long ago, tools just learnt recently, or you may have do some research and figure out how to use a tool you've never used before. This exam has those aspects as well.<br /> <br /> Some other points are:<br /> <br /> * Complete this exam with 1 other person, or by yourself.<br /> * Please identify the person you work with at the top of your answer submission. **Please note: submit one solution per group**.<br /> * Please attribute any sources of reference you used (textbooks, websites, and so on).<br /> * The intention of the group work is that you discuss the questions and collaborate in the same way you have done in the assignments.<br /> * But **do not share** any electronic files (e.g. Word documents, source code) outside your group; take care in the computer labs to safeguard your work.<br /> * Like any other exam, neither the TAs nor myself are able to answer direct questions about the exam. Similarly you *should not* look for help about a *specific question* from other resources (e.g. asking for help with the question on a website, friends, other students in the class, *etc*).<br /> * You may use the course notes and any other textbooks and resources though.<br /> * There is no make-up for this exam.<br /> * You will benefit from going through the software tutorials &lt;https://learnche.org/3E4/Software_tutorial&gt;_ on the course website.<br /> * Your answers should preferably be typed up.<br /> * Your answers must be structured like a report and flow together logically. Please do not submit pages and pages of computer printouts and source code in appendices - unfortunately this will be penalized.<br /> <br /> - Hand out date: 17 November 2010,<br /> - **Due date**: <br /> - Paper hand-in: by **17:30 on 22 November 2010, in class**<br /> - Totally electronic hand in: email to dunnkg@mcmaster.ca before the above time and date.<br /> - A valid electronic hand in must be in PDF format *only* (no separate Word, MATLAB or Python files). If you are on Windows, you could use PDFCreator &lt;http://sourceforge.net/projects/pdfcreator/&gt;_ to make PDF's; Macs and Linux have built-in PDF capability.<br /> <br /> .. Note:: There are a maximum of 30 grade points available in the 5 questions, plus a variable amount of bonus points. <br /> <br /> .. raw:: latex<br /> <br /> \pagestyle{plain}<br /> \vspace*{0.2cm}<br /> \hrule<br /> \vspace*{0.2cm}<br /> %\newpage<br /> <br /> Question 1 [7 = 2 + 2 + 2 + 1]<br /> ===============================<br /> <br /> .. Green modelling book: p 316<br /> .. Tests numerical differentiation, modelling, curve fitting and common sense.<br /> <br /> Background: Chemical engineers have made many contributions to the medical field; similarly in this question you must use your engineering knowledge in a different context. <br /> <br /> Consider the case of injecting a medical treatment into a person's blood system. Subsequent to the injection, the body starts to metabolize (break down, or use up) the drug. In this study a drug was injected into the patient, and samples taken over the next 10 hours. This table reports the concentration of the drug in the blood stream, :math:C:<br /> <br /> ================== ==== ==== ==== ==== ==== ==== ==== <br /> :math:t [hour] 1 2 3 5 6 8 10 <br /> ------------------ ---- ---- ---- ---- ---- ---- ---- <br /> :math:C [mg/L] 10.1 7.5 5.7 3.1 2.4 1.3 0.7<br /> ================== ==== ==== ==== ==== ==== ==== ==== <br /> <br /> Medical models often use a well-mixed reactor to approximate the time-dependent behaviour of parts of the body: e.g. kidneys, stomach, and other organs. In this question you can assume the blood system to be a well-mixed CSTR, operating in batch mode: i.e. no inlet, or outlet, and of constant volume (about 5L for a typical adult). Assume the injection immediately mixes with all the blood, and shows up as :math:C(t=0). <br /> <br /> #. Provide an estimate of the drug's concentration at :math:t=0 hours. Please justify your choice of method, and provide a plot to assess if your estimate is reasonable.<br /> <br /> #. Please provide the lowest error estimate of :math:\frac{dC}{dt} for every data point in the table (without using Richardson's extrapolation).<br /> <br /> #. Next assume the drug is eliminated from the patient's blood into the rest of the body using a first-order rate expression, :math:r = kC. Obviously the mechanism by which the drug is metabolized is much more complex, but we don't know the cellular pathways and their rate constants: so lump them together and assume the overall mechanism is first order. Derive the dynamic equation that would describe the concentration of the drug in the bloodstream as a function of time.<br /> <br /> #. Using your values from part 2, calculate the average rate constant. **Bonus mark of 0.5** if you use least squares regression instead of just calculating the average.<br /> <br /> Question 2 [5 = 1 + 1 + 3]<br /> ===========================<br /> <br /> A batch bioreactor (well-mixed tank, but no inlet or outlet) is charged with substrate at time :math:t=0 to start a reaction that will consume the substrate. The reaction mechanism is too complex to model exactly, but previous experience suggests it is roughly a first-order reaction. The apparent first-order rate expression is :math:r = kS, where :math:k = 0.58\,\,\text{hours}^{-1}, and :math:{\sf S} is the concentration of the substrate being depleted.<br /> <br /> #. Show that the dynamic rate of change of :math:{\sf S} is given by:<br /> <br /> .. math::<br /> <br /> \frac{d{\sf S}(t)}{dt} = -k {\sf S}(t)<br /> <br /> Also state any assumptions that were made in arriving at this expression.<br /> <br /> #. The time required to consume the substrate from a starting level of 260 g/L to 5 g/L can be calculated by analytical integration of the above expression. What is the total time required?<br /> <br /> #. Had you forgotten how to integrate the expression analytically you could resort to using the trapezoidal rule or Simpson's 1/3 rule. **Without** implementing either method, calculate how many panels would be required to ensure a numerical estimate of the time to achieve the above conversion is accurate within 0.01 hours. In other words, how many panels are required to solve the left hand side integral <br /> <br /> .. math::<br /> <br /> \int_{260}^{5}{-\frac{1}{k\sf S}\, d{\sf S}} = \int_{0}^{t}{d{t}} = t<br /> <br /> to that level of accuracy:<br /> <br /> * when using the trapezoidal rule;<br /> * when using Simpson's 1/3 method.<br /> <br /> Question 3 [7 = 3 + 1 + 3]<br /> ===========================<br /> <br /> The following question is based on the flowsheet below, where the :math:m_i values are the mass flow of the species of interest, the reactant. The purpose of the flowsheet is to eliminate the incoming species at :math:m_1 = 10 kg/hour and convert it to by-products. There is some residual reactant leaving the flowsheet in stream :math:m_{12}.<br /> <br /> .. figure:: images/take-home-reactor-network.png<br /> :scale: 40<br /> :align: center<br /> <br /> The following additional information is known:<br /> <br /> * :math:m_1 = 10 kg/hour.<br /> * :math:m_2 = 2 kg/hour.<br /> * Unit A operates with 40% efficiency; i.e. of all the material coming in, only 40% of it is converted to other products.<br /> * Unit B is just a mixing tank.<br /> * Unit C is just a mixer/splitter and is operated so that the mass-flow in the outlets are all in the same proportion.<br /> * Unit D operates with 82% efficiency, so 18% of the incoming stream is left unconverted. The two outlets are split in equal proportion as well.<br /> * Unit E operates with 15% efficiency to convert the incoming streams to by-products.<br /> * The split after unit E is operated such that :math:\alpha m_8 is recycled in stream 11, while :math:(1-\alpha)m_8 exits in stream :math:m_{12}.<br /> <br /> #. Write a system of equations in the form :math:{\bf Ax = b} that can be used to calculate the mass flow rate of every stream in the flowsheet. <br /> <br /> The row order in a system of equations has no effect on the solution :math:{\bf x}. But to make grading this question easier, please write your system of equations using :math:{\bf x} = [m_1,\, m_2\, \ldots\,,\,m_{11},\,m_{12}] and so that matrix :math:{\bf A} has 1's on the diagonal. <br /> <br /> #. Solve this system of equations using computer software, at the value of :math:\alpha = 0.20. <br /> <br /> * Show your matrix :math:{\bf A}<br /> * Report the flow of the species in every stream, and in particular, the flow in stream :math:m_{12}.<br /> <br /> #. The next objective is to adjust the recycle ratio, :math:\alpha, so that the mass flow of the species leaving the flowsheet is :math:m_{12} = 1.0 kg/hour.<br /> <br /> * You may use any technique you like to find :math:\alpha, but every time you solve the system of equations :math:{\bf Ax = b} for a given value of :math:\alpha it will count as one function evaluation.<br /> * If you plan to plot the system at various values of :math:\alpha, then each point on the plot is counted as a function evaluation.<br /> * The solution in part 2, with :math:\alpha=0.20 counts as one function evaluation already, and should be your starting point for solving this part of the question. <br /> * Clearly explain your strategy.<br /> * Report the total number of function evaluations you used to achieve an :math:\alpha value that is accurate within :math:1\times 10^{-2}. There is **one bonus mark** available for getting an answer within 5 function evaluations or less.<br /> <br /> Question 4 [6 = 2 + 4]<br /> =======================<br /> <br /> .. note:: Even though this question is typical of material learnt about in later courses, you are still able to solve this question using tools just covered in this course.<br /> <br /> A polymer compound is being held in a small stirred tank that is externally heated. The polymer leaving the tank must have a certain viscosity so that the downstream processes can operate as required. The primary method of adjusting the viscosity is by setting the temperature of the tank. Several experiments, performed by your colleagues, have determined the system behaves in a second-order manner (you will learn how to determine this in your process control course). <br /> <br /> .. math::<br /> <br /> \tau^2 \frac{{\rm d}^2\mu(t)}{{\rm d}t^2} + \tau\frac{{\rm d}\mu(t)}{{\rm d}t} = K_p(T(t) - 318)<br /> <br /> where:<br /> <br /> * :math:\mu(t) is the viscosity measured in centipoise, cP and the target viscosity required is 30 cP<br /> * :math:T(t) is the tank temperature, measured in Kelvin<br /> * :math:K_p = -0.25 cP/K<br /> * :math:\tau = 2.1 minutes<br /> <br /> A feedback control system is implemented to keep :math:\mu at its target of 30 cP. The type of feedback controller used is a *proportional-only* controller, meaning the tank temperature, :math:T(t) is adjusted in proportion to the deviation (error) in viscosity, as given by:<br /> <br /> .. math::<br /> <br /> T(t) = K_c\big[30 - \mu(t)\big] + 318<br /> <br /> where :math:K_c = -1.9 K/cP. At steady state, the tank temperature is normally set to 318 K to maintain the required viscosity of 30 cP.<br /> <br /> #. According to this feedback control law, what should the tank temperature be set to if the viscosity is currently at 35 cP? And for 28 cP? Comment whether or not these answers make sense.<br /> <br /> #. Someone has turned the tank's heating system off overnight and the viscosity of the material is currently at 40 cP. Create a plot, as a function of time, that shows how the material's viscosity is brought back to the target of 30 cP. Let :math:t=0 be the moment the heating system is turned back on. Using your plot, estimate how much time will be required to reach **stable** operation (i.e. return to a constant viscosity of 30 cP)?<br /> <br /> **Bonus question [1 mark]**: The value :math:K_c is called the feedback controller's gain (aggressiveness). Show your plot from part 2 of the question, but superimpose the time-varying viscosity curves for two other values of :math:K_c, one higher and one lower. How would you adjust :math:K_c to achieve the target of 30 cP in a shorter amount of time? Can you suggest a better :math:K_c value than the current setting?<br /> <br /> Question 5 <br /> ================<br /> <br /> This question considers a pilot-plant CSTR, liquid reaction process, fed with reactants A, B and C. There are two reactions taking place in the system:<br /> <br /> .. math::<br /> :nowrap:<br /> <br /> \begin{align*} {\sf A} + {\sf B} &amp;\rightarrow 2{\sf D} + {\sf E} \\ 2{\sf C} + {\sf D} &amp;\rightarrow {\sf F} + {\sf E} \end{align*} <br /> <br /> The desired products are :math:{\sf E} and :math:{\sf F}, while species :math:{\sf D} is an intermediate. From economic considerations, it is desirable to operate the reactor to maximize the production of :math:{\sf E} and :math:{\sf F}. This is achieved by minimizing the amount of unreacted :math:{\sf D} in the outlet from the reactor.<br /> <br /> One way this can be done is by adjusting the tank's jacket temperature, which in turn affects the reaction kinetics and alters the balance of products in the outlet. Unfortunately the first principles rate expressions for the reactions are not known, so derivation of mass and energy balances to find this jacket temperature is not possible. <br /> <br /> You have time and resources to run the pilot-plant reactor at only 8 different jacket temperatures, between a minimum and maximum range of 278 K to 355 K. You are expected to use any of the tools from this course to find a jacket temperature that leads to the lowest possible concentration of :math:{\sf D} [mol/L]. You may use of the fact (learnt in your calculus course) that the optimum -- either a maximum or a minimum -- of a function :math:f(x) occurs at :math:\displaystyle \frac{df(x)}{dx} = 0; and that optimum is a minimum if :math:\displaystyle \frac{df^2(x)}{dx^2} &gt; 0.<br /> <br /> A model of the process has been simulated, and is available on the course website &lt;https://takehome.learnche.org/&gt;_. Nominate *one of your group members* and email their name and student number to the course instructor, together with the name of the other, optional, group member. This will activate an account for your group. Once you sign into the account you will be able to enter in various values of the jacket temperature, and the server will simulate the experiment and return the corresponding concentration of :math:{\sf D}, as if the pilot plant was operated at that jacket temperature. <br /> <br /> Four of the grading points for this question will be marked mainly on the *description of your strategy and the systematic methodology followed* to locate a suitable jacket temperature. Make sure you explain your approach clearly and present good visualization plot(s), exactly as you would have to in a report to your manager. Your final answer (one point) must provide a recommendation to your manager for the jacket temperature.<br /> <br /> Also note: <br /> <br /> * There is error (noise) in the concentration measurements of :math:\pm 1.0 mol/L.<br /> * The server will return different results for each group.<br /> * Please enter your jacket temperature settings carefully - if you input incorrect values you will have to work with those results. **You will only be able to run at most 8 experiments.** <br /> * The server will also keep track of and display all your previous experiments on a results sheet.<br /> <br /> Once you have completed the question, include the result sheet from the server in your answer. (The true jacket temperature for your team will be be available on the website after the exam is handed in). Please note, there is more than one correct method to approach the problem.<br /> <br /> There is **1 bonus mark** if you are able to locate the jacket temperature within :math:\pm 0.25 K and additional bonus marks as follows: :math:\displaystyle \frac{\max\left\{6-N,0\right\}}{4}, where :math:N is the number of experiments you used.<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.5cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Take-home_exam&diff=1107 Take-home exam 2018-09-16T11:21:55Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 17:30 on '''22 November 2010'''; no late hand ins.<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = Take-home-exam.pdf<br /> | questions_text_alt = Take home questions<br /> | solutions_PDF = <br /> | solutions_text_alt = Solutions by Kevin<br /> | other_instructions = Hand-in at class.<br /> }}<br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = True/&gt;<br /> Instructions<br /> ===============<br /> <br /> The purpose of this exam is only a little different to other exams. The difference is that this exam will assess your ability to answer more realistic problems that require more time, thought, and access to a computer. You are expected to use **any appropriate tools** to solve the problems in this exam, particularly the tools learnt in this course. <br /> <br /> The 3E4 course teaches you a collection of tools that will be useful in your engineering career. And the challenges you will face do not come neatly posed as problems of the type: &quot;Solve this linear system using Gauss elimination&quot; -- rather you need to reformulate your challenge and pick the appropriate tool to solve the problem, taking into account the level of complexity.<br /> <br /> In that regard there are many correct ways to achieve a result. In this exam about 80% of the grade will be given for **how** you solve the problem, and we will pay particular attention to the **justifications** you provide along the way. You must clearly outline **why** you choose the method and **why** you did not choose alternative methods. The **accuracy of how you implement your method** is also important, but the actual solution is of little value for determining your grade.<br /> <br /> Also, as you will find in your career as an engineer, you will have to apply tools that you learnt about long ago, tools just learnt recently, or you may have do some research and figure out how to use a tool you've never used before. This exam has those aspects as well.<br /> <br /> Some other points are:<br /> <br /> * Complete this exam with 1 other person, or by yourself.<br /> * Please identify the person you work with at the top of your answer submission. **Please note: submit one solution per group**.<br /> * Please attribute any sources of reference you used (textbooks, websites, and so on).<br /> * The intention of the group work is that you discuss the questions and collaborate in the same way you have done in the assignments.<br /> * But **do not share** any electronic files (e.g. Word documents, source code) outside your group; take care in the computer labs to safeguard your work.<br /> * Like any other exam, neither the TAs nor myself are able to answer direct questions about the exam. Similarly you *should not* look for help about a *specific question* from other resources (e.g. asking for help with the question on a website, friends, other students in the class, *etc*).<br /> * You may use the course notes and any other textbooks and resources though.<br /> * There is no make-up for this exam.<br /> * You will benefit from going through the software tutorials &lt;http://modelling3e4.connectmv.com/wiki/Software_tutorial&gt;_ on the course website.<br /> * Your answers should preferably be typed up.<br /> * Your answers must be structured like a report and flow together logically. Please do not submit pages and pages of computer printouts and source code in appendices - unfortunately this will be penalized.<br /> <br /> - Hand out date: 17 November 2010,<br /> - **Due date**: <br /> - Paper hand-in: by **17:30 on 22 November 2010, in class**<br /> - Totally electronic hand in: email to dunnkg@mcmaster.ca before the above time and date.<br /> - A valid electronic hand in must be in PDF format *only* (no separate Word, MATLAB or Python files). If you are on Windows, you could use PDFCreator &lt;http://sourceforge.net/projects/pdfcreator/&gt;_ to make PDF's; Macs and Linux have built-in PDF capability.<br /> <br /> .. Note:: There are a maximum of 30 grade points available in the 5 questions, plus a variable amount of bonus points. <br /> <br /> .. raw:: latex<br /> <br /> \pagestyle{plain}<br /> \vspace*{0.2cm}<br /> \hrule<br /> \vspace*{0.2cm}<br /> %\newpage<br /> <br /> Question 1 [7 = 2 + 2 + 2 + 1]<br /> ===============================<br /> <br /> .. Green modelling book: p 316<br /> .. Tests numerical differentiation, modelling, curve fitting and common sense.<br /> <br /> Background: Chemical engineers have made many contributions to the medical field; similarly in this question you must use your engineering knowledge in a different context. <br /> <br /> Consider the case of injecting a medical treatment into a person's blood system. Subsequent to the injection, the body starts to metabolize (break down, or use up) the drug. In this study a drug was injected into the patient, and samples taken over the next 10 hours. This table reports the concentration of the drug in the blood stream, :math:C:<br /> <br /> ================== ==== ==== ==== ==== ==== ==== ==== <br /> :math:t [hour] 1 2 3 5 6 8 10 <br /> ------------------ ---- ---- ---- ---- ---- ---- ---- <br /> :math:C [mg/L] 10.1 7.5 5.7 3.1 2.4 1.3 0.7<br /> ================== ==== ==== ==== ==== ==== ==== ==== <br /> <br /> Medical models often use a well-mixed reactor to approximate the time-dependent behaviour of parts of the body: e.g. kidneys, stomach, and other organs. In this question you can assume the blood system to be a well-mixed CSTR, operating in batch mode: i.e. no inlet, or outlet, and of constant volume (about 5L for a typical adult). Assume the injection immediately mixes with all the blood, and shows up as :math:C(t=0). <br /> <br /> #. Provide an estimate of the drug's concentration at :math:t=0 hours. Please justify your choice of method, and provide a plot to assess if your estimate is reasonable.<br /> <br /> #. Please provide the lowest error estimate of :math:\frac{dC}{dt} for every data point in the table (without using Richardson's extrapolation).<br /> <br /> #. Next assume the drug is eliminated from the patient's blood into the rest of the body using a first-order rate expression, :math:r = kC. Obviously the mechanism by which the drug is metabolized is much more complex, but we don't know the cellular pathways and their rate constants: so lump them together and assume the overall mechanism is first order. Derive the dynamic equation that would describe the concentration of the drug in the bloodstream as a function of time.<br /> <br /> #. Using your values from part 2, calculate the average rate constant. **Bonus mark of 0.5** if you use least squares regression instead of just calculating the average.<br /> <br /> Question 2 [5 = 1 + 1 + 3]<br /> ===========================<br /> <br /> A batch bioreactor (well-mixed tank, but no inlet or outlet) is charged with substrate at time :math:t=0 to start a reaction that will consume the substrate. The reaction mechanism is too complex to model exactly, but previous experience suggests it is roughly a first-order reaction. The apparent first-order rate expression is :math:r = kS, where :math:k = 0.58\,\,\text{hours}^{-1}, and :math:{\sf S} is the concentration of the substrate being depleted.<br /> <br /> #. Show that the dynamic rate of change of :math:{\sf S} is given by:<br /> <br /> .. math::<br /> <br /> \frac{d{\sf S}(t)}{dt} = -k {\sf S}(t)<br /> <br /> Also state any assumptions that were made in arriving at this expression.<br /> <br /> #. The time required to consume the substrate from a starting level of 260 g/L to 5 g/L can be calculated by analytical integration of the above expression. What is the total time required?<br /> <br /> #. Had you forgotten how to integrate the expression analytically you could resort to using the trapezoidal rule or Simpson's 1/3 rule. **Without** implementing either method, calculate how many panels would be required to ensure a numerical estimate of the time to achieve the above conversion is accurate within 0.01 hours. In other words, how many panels are required to solve the left hand side integral <br /> <br /> .. math::<br /> <br /> \int_{260}^{5}{-\frac{1}{k\sf S}\, d{\sf S}} = \int_{0}^{t}{d{t}} = t<br /> <br /> to that level of accuracy:<br /> <br /> * when using the trapezoidal rule;<br /> * when using Simpson's 1/3 method.<br /> <br /> Question 3 [7 = 3 + 1 + 3]<br /> ===========================<br /> <br /> The following question is based on the flowsheet below, where the :math:m_i values are the mass flow of the species of interest, the reactant. The purpose of the flowsheet is to eliminate the incoming species at :math:m_1 = 10 kg/hour and convert it to by-products. There is some residual reactant leaving the flowsheet in stream :math:m_{12}.<br /> <br /> .. figure:: images/take-home-reactor-network.png<br /> :scale: 40<br /> :align: center<br /> <br /> The following additional information is known:<br /> <br /> * :math:m_1 = 10 kg/hour.<br /> * :math:m_2 = 2 kg/hour.<br /> * Unit A operates with 40% efficiency; i.e. of all the material coming in, only 40% of it is converted to other products.<br /> * Unit B is just a mixing tank.<br /> * Unit C is just a mixer/splitter and is operated so that the mass-flow in the outlets are all in the same proportion.<br /> * Unit D operates with 82% efficiency, so 18% of the incoming stream is left unconverted. The two outlets are split in equal proportion as well.<br /> * Unit E operates with 15% efficiency to convert the incoming streams to by-products.<br /> * The split after unit E is operated such that :math:\alpha m_8 is recycled in stream 11, while :math:(1-\alpha)m_8 exits in stream :math:m_{12}.<br /> <br /> #. Write a system of equations in the form :math:{\bf Ax = b} that can be used to calculate the mass flow rate of every stream in the flowsheet. <br /> <br /> The row order in a system of equations has no effect on the solution :math:{\bf x}. But to make grading this question easier, please write your system of equations using :math:{\bf x} = [m_1,\, m_2\, \ldots\,,\,m_{11},\,m_{12}] and so that matrix :math:{\bf A} has 1's on the diagonal. <br /> <br /> #. Solve this system of equations using computer software, at the value of :math:\alpha = 0.20. <br /> <br /> * Show your matrix :math:{\bf A}<br /> * Report the flow of the species in every stream, and in particular, the flow in stream :math:m_{12}.<br /> <br /> #. The next objective is to adjust the recycle ratio, :math:\alpha, so that the mass flow of the species leaving the flowsheet is :math:m_{12} = 1.0 kg/hour.<br /> <br /> * You may use any technique you like to find :math:\alpha, but every time you solve the system of equations :math:{\bf Ax = b} for a given value of :math:\alpha it will count as one function evaluation.<br /> * If you plan to plot the system at various values of :math:\alpha, then each point on the plot is counted as a function evaluation.<br /> * The solution in part 2, with :math:\alpha=0.20 counts as one function evaluation already, and should be your starting point for solving this part of the question. <br /> * Clearly explain your strategy.<br /> * Report the total number of function evaluations you used to achieve an :math:\alpha value that is accurate within :math:1\times 10^{-2}. There is **one bonus mark** available for getting an answer within 5 function evaluations or less.<br /> <br /> Question 4 [6 = 2 + 4]<br /> =======================<br /> <br /> .. note:: Even though this question is typical of material learnt about in later courses, you are still able to solve this question using tools just covered in this course.<br /> <br /> A polymer compound is being held in a small stirred tank that is externally heated. The polymer leaving the tank must have a certain viscosity so that the downstream processes can operate as required. The primary method of adjusting the viscosity is by setting the temperature of the tank. Several experiments, performed by your colleagues, have determined the system behaves in a second-order manner (you will learn how to determine this in your process control course). <br /> <br /> .. math::<br /> <br /> \tau^2 \frac{{\rm d}^2\mu(t)}{{\rm d}t^2} + \tau\frac{{\rm d}\mu(t)}{{\rm d}t} = K_p(T(t) - 318)<br /> <br /> where:<br /> <br /> * :math:\mu(t) is the viscosity measured in centipoise, cP and the target viscosity required is 30 cP<br /> * :math:T(t) is the tank temperature, measured in Kelvin<br /> * :math:K_p = -0.25 cP/K<br /> * :math:\tau = 2.1 minutes<br /> <br /> A feedback control system is implemented to keep :math:\mu at its target of 30 cP. The type of feedback controller used is a *proportional-only* controller, meaning the tank temperature, :math:T(t) is adjusted in proportion to the deviation (error) in viscosity, as given by:<br /> <br /> .. math::<br /> <br /> T(t) = K_c\big[30 - \mu(t)\big] + 318<br /> <br /> where :math:K_c = -1.9 K/cP. At steady state, the tank temperature is normally set to 318 K to maintain the required viscosity of 30 cP.<br /> <br /> #. According to this feedback control law, what should the tank temperature be set to if the viscosity is currently at 35 cP? And for 28 cP? Comment whether or not these answers make sense.<br /> <br /> #. Someone has turned the tank's heating system off overnight and the viscosity of the material is currently at 40 cP. Create a plot, as a function of time, that shows how the material's viscosity is brought back to the target of 30 cP. Let :math:t=0 be the moment the heating system is turned back on. Using your plot, estimate how much time will be required to reach **stable** operation (i.e. return to a constant viscosity of 30 cP)?<br /> <br /> **Bonus question [1 mark]**: The value :math:K_c is called the feedback controller's gain (aggressiveness). Show your plot from part 2 of the question, but superimpose the time-varying viscosity curves for two other values of :math:K_c, one higher and one lower. How would you adjust :math:K_c to achieve the target of 30 cP in a shorter amount of time? Can you suggest a better :math:K_c value than the current setting?<br /> <br /> Question 5 <br /> ================<br /> <br /> This question considers a pilot-plant CSTR, liquid reaction process, fed with reactants A, B and C. There are two reactions taking place in the system:<br /> <br /> .. math::<br /> :nowrap:<br /> <br /> \begin{align*} {\sf A} + {\sf B} &amp;\rightarrow 2{\sf D} + {\sf E} \\ 2{\sf C} + {\sf D} &amp;\rightarrow {\sf F} + {\sf E} \end{align*} <br /> <br /> The desired products are :math:{\sf E} and :math:{\sf F}, while species :math:{\sf D} is an intermediate. From economic considerations, it is desirable to operate the reactor to maximize the production of :math:{\sf E} and :math:{\sf F}. This is achieved by minimizing the amount of unreacted :math:{\sf D} in the outlet from the reactor.<br /> <br /> One way this can be done is by adjusting the tank's jacket temperature, which in turn affects the reaction kinetics and alters the balance of products in the outlet. Unfortunately the first principles rate expressions for the reactions are not known, so derivation of mass and energy balances to find this jacket temperature is not possible. <br /> <br /> You have time and resources to run the pilot-plant reactor at only 8 different jacket temperatures, between a minimum and maximum range of 278 K to 355 K. You are expected to use any of the tools from this course to find a jacket temperature that leads to the lowest possible concentration of :math:{\sf D} [mol/L]. You may use of the fact (learnt in your calculus course) that the optimum -- either a maximum or a minimum -- of a function :math:f(x) occurs at :math:\displaystyle \frac{df(x)}{dx} = 0; and that optimum is a minimum if :math:\displaystyle \frac{df^2(x)}{dx^2} &gt; 0.<br /> <br /> A model of the process has been simulated, and is available on the course website &lt;http://takehome.modelling3e4.connectmv.com/&gt;_. Nominate *one of your group members* and email their name and student number to the course instructor, together with the name of the other, optional, group member. This will activate an account for your group. Once you sign into the account you will be able to enter in various values of the jacket temperature, and the server will simulate the experiment and return the corresponding concentration of :math:{\sf D}, as if the pilot plant was operated at that jacket temperature. <br /> <br /> Four of the grading points for this question will be marked mainly on the *description of your strategy and the systematic methodology followed* to locate a suitable jacket temperature. Make sure you explain your approach clearly and present good visualization plot(s), exactly as you would have to in a report to your manager. Your final answer (one point) must provide a recommendation to your manager for the jacket temperature.<br /> <br /> Also note: <br /> <br /> * There is error (noise) in the concentration measurements of :math:\pm 1.0 mol/L.<br /> * The server will return different results for each group.<br /> * Please enter your jacket temperature settings carefully - if you input incorrect values you will have to work with those results. **You will only be able to run at most 8 experiments.** <br /> * The server will also keep track of and display all your previous experiments on a results sheet.<br /> <br /> Once you have completed the question, include the result sheet from the server in your answer. (The true jacket temperature for your team will be be available on the website after the exam is handed in). Please note, there is more than one correct method to approach the problem.<br /> <br /> There is **1 bonus mark** if you are able to locate the jacket temperature within :math:\pm 0.25 K and additional bonus marks as follows: :math:\displaystyle \frac{\max\left\{6-N,0\right\}}{4}, where :math:N is the number of experiments you used.<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.5cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Written_midterm_-_Solution&diff=1106 Written midterm - Solution 2018-09-16T11:20:45Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 18 October 2010<br /> | dates_alt_text = Date<br /> | questions_PDF = Midterm-ChE3E4-2010.pdf<br /> | questions_text_alt = Questions <br /> | solutions_PDF = Midterm-ChE3E4-2010-Solutions.pdf<br /> | solutions_text_alt = Solutions by Ali, Elliot and Kevin<br /> | other_instructions = <br /> }}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> <br /> .. rubric:: Special instructions<br /> <br /> - You may bring in any printed materials to the final; **any** textbooks, **any** papers, *etc*.<br /> - You may use any calculator during the test.<br /> - You may not use a cellphone as a calculator. Nor may you use any other communication device (web, email, chat, etc) during the test.<br /> - You may not use any type of computer.<br /> - You may answer the questions in any order in the answer booklet.<br /> - The test covers all material taught so far in the course.<br /> - Make sure that you provide explanations with your calculations.<br /> - If you are not sure about the meaning of a problem, please write out your interpretation and follow through with the calculation.<br /> - If you are short of time, please write what you would have done in this test, if you had more time.<br /> - There are a total of **45 marks** available.<br /> <br /> .. raw:: latex<br /> <br /> \pagestyle{plain}<br /> \vspace*{0.2cm}<br /> \hrule<br /> \vspace*{0.2cm}<br /> Solutions by: Ali (Q4, Q7, Q8), Elliot (Q1, Q2, Q3), and Kevin (Q5, Q6)<br /> \newpage<br /> <br /> <br /> .. Chemical engineering modelling <br /> .. Roundoff and computer representation <br /> .. Linear algebraic equations <br /> .. Taylor-series expansion <br /> .. Non-linear equation solution <br /> <br /> Question 1 <br /> ===============<br /> <br /> Consider the following 3 reactions taking place in a liquid-based system: a continuously stirred tank reactor (CSTR).<br /> <br /> * :math:{\sf A} \stackrel{r_1}{\rightarrow} {\sf 2B}, where :math:r_1 = k_1 C_{\sf A}<br /> * :math:{\sf A} \displaystyle \mathop{\rightleftharpoons}_{r_3}^{r_2} {\sf C}, where :math:r_2=k_2 C_{\sf A}^{1.5} and :math:r_3 = k_3 C_{\sf C}^2<br /> * :math:{\sf 2B} \stackrel{r_4}{\rightarrow} {\sf 3D + C}, where :math:r_4 = k_4 C_{\sf B}^2 <br /> <br /> Derive the equations that can be solved for the steady state concentrations of :math:\sf A, B, C and :math:\sf D. You may assume that species :math:\sf A is fed to the reactor in a single stream, flowing in at :math:Q \text{m}^3/\text{s}, with a concentration of :math:C_{\sf A}^{\text{in}}. Please state all other assumptions that you make while answering the question.<br /> <br /> Note: please **do not simplify** your equations.<br /> <br /> Solution<br /> ---------<br /> <br /> **ASSUMPTIONS AND DIAGRAM [3 marks]**<br /> <br /> * 1 mark for a proper diagram<br /> * 1 mark for each strong assumption<br /> * Additional assumptions may offset lack of diagram<br /> * Marks were removed for poor or incorrect assumptions on a case by case basis<br /> <br /> A diagram of the process is provided below:<br /> <br /> .. figure:: images/MidTerm_Q1.png<br /> :scale: 40<br /> :align: center<br /> <br /> The following is a list of appropriate assumptions for performing the required mass balances:<br /> <br /> * The reactor is operating at a constant volume (i.e. :math:Q_{in} = Q_{out} = Q)<br /> * Mixture physical properties are constant and temperature independent<br /> * Reactor is perfectly mixed<br /> * Reactor is sealed (i.e. no vapour losses)<br /> <br /> **BALANCES [8 marks = 2 marks each]**<br /> <br /> * Marks removed for incomplete or incorrect balances<br /> * All 4 mol balances start from this relationship:<br /> <br /> .. math::<br /> <br /> \displaystyle \frac{dn_{i}}{dt} = \sum(n_{i,in}) - \sum(n_{i,\text{out}}) + \text{Generation} - \text{Consumption}<br /> <br /> <br /> *Balance on A*<br /> <br /> .. math::<br /> \begin{array}{rl} \displaystyle \frac{d(V C_{\sf A})}{dt} &amp;= Q C_{\sf A,in} - Q C_{\sf A} + (V k_{3} C_{\sf C}^{2}) - (V k_{1} C_{\sf A} + V k_{2} C_{\sf A}^{1.5}) \\ \\ \displaystyle \frac{d C_{\sf A}}{dt} &amp;= \frac{Q}{V}(C_{\sf A,in} - C_{\sf A}) + k_{3} C_{\sf C}^{2} - k_{1} C_{\sf A} - k_{2} C_{\sf A}^{1.5} \\ \\ \displaystyle (0) &amp;= \frac{Q}{V}(C_{\sf A,in} - C_{\sf A}) + k_{3} C_{\sf C}^{2} - k_{1} C_{\sf A} - k_{2} C_{\sf A}^{1.5}\end{array}<br /> <br /> *Balance on B*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \displaystyle \frac{d(V C_{\sf B})}{dt} &amp;= (0) - Q C_{\sf B} + (V 2 k_{1} C_{\sf A}) - (V 2 k_{4} C_{\sf B}^{2}) \\ \\ \displaystyle \frac{d C_{\sf B}}{dt} &amp;= - \frac{Q}{V} C_{\sf B} + 2 k_{1} C_{\sf A} - 2 k_{4} C_{\sf B}^{2} \\ \\ \displaystyle (0) &amp;= - \frac{Q}{V} C_{\sf B} + 2 k_{1} C_{\sf A} - 2 k_{4} C_{\sf B}^{2} \end{array}<br /> <br /> *Balance on C*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \displaystyle \frac{d(V C_{\sf C})}{dt} &amp;= (0) - Q C_{\sf C} + (V k_{2} C_{\sf A}^{1.5} + V k_{4} C_{\sf B}^{2}) - (V k_{3} C_{\sf C}^{2}) \\ \\ \displaystyle \frac{d C_{\sf C}}{dt} &amp;= - \frac{Q}{V} C_{\sf C} + k_{2} C_{\sf A}^{1.5} + k_{4} C_{\sf B}^{2} - k_{3} C_{\sf C}^{2} \\ \\ \displaystyle (0) &amp;= - \frac{Q}{V} C_{\sf C} + k_{2} C_{\sf A}^{1.5} + k_{4} C_{\sf B}^{2} - k_{3} C_{\sf C}^{2} \end{array}<br /> <br /> *Balance on D*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \displaystyle \frac{d(V C_{\sf D})}{dt} &amp;= (0) - Q C_{\sf D} + (V 3 k_{4} C_{\sf B}^{2}) - (0) \\ \\ \displaystyle \frac{d C_{\sf C}}{dt} &amp;= - \frac{Q}{V} C_{\sf D} + V 3 k_{4} C_{\sf B}^{2} \\ \\ \displaystyle (0) &amp;= - \frac{Q}{V} C_{\sf D} + 3 k_{4} C_{\sf B}^{2} \end{array}<br /> <br /> <br /> Question 2 <br /> ==============<br /> <br /> Consider the system in question 1 again, but modified now to be a semi-batch system - material only enters the reactor, but nothing leaves. Continue on from question 1, using any equations derived there and any assumptions stated there, and derive the dynamic balances **only for species** :math:C_{\sf A} **and** :math:C_{\sf D}.<br /> <br /> Solution<br /> --------<br /> <br /> **BALANCES [5 marks = 2.5 marks each]**<br /> <br /> * Since the majority of the equation derivation for this question was taken from Question 1 marking was much harsher for missing the key differences that made this question distinct<br /> * 1 mark for each equation was removed (i.e. resulting in a 3/5 all else considered) if no acknowledgment was made that volume is no longer constant. This included either not writing this fact explicitly or writing the V term outside of the concentration derivative without performing proper chain rule expansion<br /> * 1 mark for each equation was removed (i.e. resulting in a 3/5 all else considered) if the outlet terms were not removed<br /> * Additional marks were lost for general errors made in the model derivation<br /> <br /> To change the system to a semi-batch model we need to remove the outlet terms and account for the changes in volume. In other words, the volume is now a function of time, and cannot simply be removed from the derivative: in the equations below use the fact that :math:V = V(t)<br /> <br /> *Overall balance*<br /> <br /> .. math::<br /> \begin{array}{rl} \displaystyle \frac{dM}{dt} = \sum(M_{in}) - \sum(M_{out}) = \displaystyle \frac{d(\rho V)}{dt} &amp;= \rho Q_{in} - (0) \\ \displaystyle \frac{dV}{dt} &amp;= Q_{in} = Q \end{array}<br /> <br /> *Balance on A*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \displaystyle \frac{dn_{\sf A}}{dt} &amp;= \sum(n_{A,in}) - \sum(n_{A,\text{out}}) + \text{Generation} - \text{Consumption} \\ \\ \displaystyle \frac{d(V C_{\sf A})}{dt} &amp;= Q C_{\sf A,in} - (0) + (V k_{3} C_{\sf C}^{2}) - (V k_{1} C_{\sf A} + V k_{2} C_{\sf A}^{1.5}) \\ \\ \displaystyle V\frac{dC_{\sf A}}{dt} + C_{\sf A}\frac{dV}{dt} &amp;= Q C_{\sf A,in} + V k_{3} C_{\sf C}^{2} - V k_{1} C_{\sf A} - V k_{2} C_{\sf A}^{1.5} \\ \\ \displaystyle V\frac{dC_{\sf A}}{dt} + (Q) C_{\sf A} &amp;= Q C_{\sf A,in} + V k_{3} C_{\sf C}^{2} - V k_{1} C_{\sf A} - V k_{2} C_{\sf A}^{1.5} \\ \\ \displaystyle \frac{dC_{\sf A}}{dt} &amp;= \frac{Q}{V}(C_{\sf A,in}-C_{\sf A}) + k_{3} C_{\sf C}^{2} - k_{1} C_{\sf A} - k_{2} C_{\sf A}^{1.5} \end{array}<br /> <br /> *Balance on D*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \displaystyle \frac{dn_{\sf D}}{dt} &amp;= \sum(n_{D,in}) - \sum(n_{D,\text{out}}) + \text{Generation} - \text{Consumption} \\ \\ \displaystyle \frac{d(V C_{\sf D})}{dt} &amp;= (0) - (0) + (V 3 k_{4} C_{\sf B}^{2}) - (0) \\ \\ \displaystyle V\frac{dC_{\sf D}}{dt} + C_{\sf D}\frac{dV}{dt} &amp;= V 3 k_{4} C_{\sf B}^{2} \\ \\ \displaystyle V\frac{dC_{\sf D}}{dt} + (Q) C_{\sf D} &amp;= V 3 k_{4} C_{\sf B}^{2} \\ \\ \displaystyle \frac{dC_{\sf D}}{dt} &amp;= - \frac{Q}{V} C_{\sf D} + 3 k_{4} C_{\sf B}^{2} \end{array}<br /> <br /> <br /> Question 3 <br /> ==============<br /> <br /> Represent the decimal number, -1.0, in an 8-bit word that uses 1 bit for the sign, 3 bits for the signed exponent and the remaining bits for the significand. Normalization must be used for the significand and only zero or one entries can used in each bit position.<br /> <br /> Solution<br /> ---------<br /> <br /> * 0.5 was awarded for getting the sign bit correct<br /> * 0.75 was awarded for getting the exponent bits correct<br /> * 0.75 was awarded for getting the mantissa bits correct<br /> * In the case where the logic was correctly explained but the value was not normalized, 1.5 points were awarded<br /> * In a few cases it was noticed that the logic was correct and the normalization was correct but the exponents were written backwards (i.e. in the form &quot;sign, :math:2^{0}, :math:2^{1}&quot; as opposed to the correct form &quot;sign, :math:2^{1}, :math:2^{0}&quot;). This was likely the fault of the TA, who gave an incorrect explanation of how the exponents should be written in one of the tutorials. In these specific instances where it was obvious this had happened, full marks were awarded and an explanation of the error was given<br /> <br /> Normalization requires that the mantissa have a magnitude between 1 and 1/b (i.e. the first bit is equal to 1). To convert -1.0 to a normalized 8-bit binary number with a 1-bit sign, 3-bit exponent, and 4-bit mantissa we must first realize that :math:-1.0 = -2^{0}. This means that -1.0 is directly representable in binary form. To ensure the mantissa is normalized we simply shift the exponent over by 1 value. This is the same as saying :math:-1.0 = -2^{1}2^{-1}. The second term is 0.5, which is a value between 1 and 1/2 = 0.5. Therefore if we take the term :math:2^{1} as the exponent term and :math:2^{-1} as the mantissa then the 8-bit representation of -1.0 is:<br /> <br /> .. math::<br /> <br /> \underbrace{1}_{\text{sign}}\underbrace{001}_{\text{exponent}}\underbrace{1000}_{\text{mantissa}}<br /> <br /> <br /> Question 4 [4 = 2 + 1 + 1]<br /> ===========================<br /> <br /> **4.1** A linearized system of equations, :math:Ax = b, has been constructed for a very high accuracy model of a distillation column. There are at most 12,000 equations in the system. What is the size, in megabytes, required to store :math:A at double precision? <br /> <br /> **4.2** Can this matrix be stored in 1 gigabyte of RAM?<br /> <br /> **4.3** The matrix :math:A is sparse (i.e. consists mostly of zeros). The number of non-zero elements is about 16,000. How much RAM, in megabytes, would be required to only store the non-zero entries?<br /> <br /> Solution<br /> ---------<br /> <br /> **4.1** Having 12,000 equations implies that there are also 12,000 unknowns in the system. The linear system will then have :math:12000 \times 12000 coefficients to be stored in the matrix :math:A. Because we work in double precision, each coefficient (entry) requires 8 bytes (or 64 bits) of memory. Given the conversion factor of 1024 to convert from bytes to kilobytes and also from kilobytes to megabytes, the total memory is obtained as follows:<br /> <br /> :math:12000 \times 12000 \times 8 \frac{\text{bytes}}{\text{entry}} \times \frac{\text{kilobyte}}{1024 \,\text{bytes}} \times \frac{\text{megabyte}}{1024 \,\text{kilobytes}} = **1098.6 megabytes**.<br /> <br /> **4.2** **No**, the matrix cannot be stored in 1 gigabyte of RAM, since 1 gigabyte of RAM is 1024 Mb.<br /> <br /> **4.3** Now We only store the 16,000 non-zero entries instead of the entire :math:12000 \times 12000 entries: :math:16000 \times 8 \frac{\text{bytes}}{\text{entry}} \times \frac{\text{kilobyte}}{1024 \,\text{bytes}} \times \frac{\text{megabyte}}{1024\, \text{kilobytes}} = **0.122 megabytes**. It is interesting to see that a much smaller RAM is required in this case.<br /> <br /> *Aside*: Of course we would need to store indicies corresponding to the non-zero entries as well to fully reconstruct :math:A, but that look-up table would still require much less storage than 1099 Mb required from part 4.1. <br /> <br /> **Good to know**: Sparse matrices occur frequently in engineering applications (why? consider the tray column examples you have solved before). In linear algebra, there are a variaty of techniques that take advantage of the sparsity to imporve the efficiency and storage requirements of solving large engineering problems.<br /> <br /> .. raw:: latex<br /> <br /> \newpage<br /> <br /> Question 5 [11 = 2 + 2 + 2 + 5]<br /> ========================================<br /> <br /> This questions considers a simple model of a 3-stage absorber (a similar model was used in assignment 2 in the course, but it is not exactly the same as the model used here). <br /> <br /> An illustration of the system, and the steady-state equations for system can be shown to be:<br /> <br /> .. figure:: images/absorber-column-midterm.png<br /> :scale: 30<br /> :align: center<br /> <br /> .. math::<br /> <br /> y_F &amp;= 0.5\\ \frac{1}{\beta} y_F - (\delta+1)x_1 + \delta x_2 &amp;= 0 \\ x_1 - (\delta+1)x_2 + \delta x_3 &amp;= 0 \\ x_2 - (\delta+1)x_3 + \delta x_F &amp;= 0 \\ x_F &amp;= 0.1 <br /> <br /> where :math:\beta is a coefficient, assumed constant throughout the absorber tower, that relates the liquid phase composition, :math:x_n, to the gas-phase composition, :math:y_n = \beta x_n, assuming of course that equilibrium is achieved in each stage. <br /> <br /> The :math:\delta coefficient is a dimensionless number defined as a function of the molar gas and liquid flows in the column, and :math:\beta, so that :math:\delta = \displaystyle \frac{L}{G \beta}<br /> <br /> **5.1** Write the given steady-state equations in the form :math:A{\bf x} = b where :math:{\bf x} = \left[y_F, x_1, x_2, x_3, x_F\right]. Report the :math:A matrix and :math:b vector.<br /> <br /> **5.2** What condition(s) must be satisfied so that matrix :math:A is diagonally dominant?<br /> <br /> **5.3** You decide to use the Gauss-Seidel method to solve this system of equations. Also, you are given that :math:L/G = 1.5, and :math:\beta = 0.8. Is this method guaranteed to converge for these coefficient values?<br /> <br /> **5.4** Perform one iteration of the Gauss-Seidel method, starting from a suitable initial guess that you believe will converge in fewer iterations than simply using the default guess of :math:{\bf x} = \left[y_F, x_1, x_2, x_3, x_F\right] = [0, 0, 0, 0, 0]. Use :math:L/G = 1.5, and :math:\beta = 0.8 in your matrix, and briefly explain your choice for the initial guess.<br /> <br /> Solution<br /> ---------<br /> <br /> **5.1** <br /> <br /> .. math::<br /> <br /> A = \left( \begin{array}{ccccc} 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0\\ \displaystyle \frac{1}{\beta} &amp; -(\delta+1) &amp; \delta &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; -(\delta+1) &amp; \delta &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; -(\delta+1) &amp; \delta\\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right) \hspace{2cm} b = \left(\begin{array}{c} 0.5\\ 0\\ 0\\ 0\\ 0.1 \end{array}\right)<br /> <br /> <br /> .. sidebar:: Grading<br /> <br /> Stating the general conditions for diagonal dominance for any matrix :math:A would have got you full marks for question 5.2, because the question could also have been interpreted that way. However you would need to do the calculations shown here either in question 5.2 or 5.3.<br /> <br /> **5.2** Rows 1 and 5 already meet the criteria for diagonal dominance. Rows 3 and 4 meet the first criterion for diagonal dominance, in that :math:\| \delta + 1 \| \geq \| \delta + 1 \|. However row 2 must satisfy the criterion that :math:\| \delta + 1 \| \geq \| \frac{1}{\beta} + \delta \| which simplifies to :math:\beta \geq 1 (using the fact that :math:\delta and :math:\beta are guaranteed to always be positive quantities: *why*?). So the only criterion required for diagonal dominance *for this system* is that :math:\beta \geq 1.<br /> <br /> **5.3** Both the Gauss-Seidel method, and Jacobi method are guaranteed to converge if the system is diagonally dominant. Since :math:\beta &lt; 1.0 in our case, the system is **not** diagonally dominant, as shown in the previous question. However, it might still converge, since diagonal dominance is only a sufficient criterion.<br /> <br /> **5.4** <br /> <br /> **[2 points for initial guess (IG)]**<br /> <br /> **[3 pts for calculations]**<br /> <br /> A good initial guess is any guess where the :math:x-values have concentrations between the feed and exit conditions. In this system the entry concentration of impurity in the liquid phase is 0.1. The impurity is being transferred from the gas to the liquid phase, so the liquid leaving each tray should have an increasing quantity of impurity, or mathematically: :math:x_F = 0.1 &lt; x_3 &lt; x_2 &lt; x_1. The upper bound on these concentrations is expected to be :math:y_F = 0.5, although depending on the VLE, it could be higher, but unlikely. <br /> <br /> Initial guesses for :math:y_F = 0.5 and :math:x_F = 0.1 must be used, because they are set from the initial conditions given in the problem statement.<br /> <br /> So a good guess for :math:{\bf x}^{(0)} is \left[y_F = 0.5, x_1 = 0.4, x_2 = 0.3, x_3 = 0.2, x_F = 0.1\right], or similar. <br /> <br /> Using this guess to solve for the first Gauss-Seidel iteration. There is no need to run the iterations for :math:y_F and :math:x_F as they are trivially solved.<br /> <br /> .. math::<br /> <br /> x_1^{(1)} &amp;= \frac{-0.5/0.8 -1.875 \times 0.3}{-2.875} = 0.413 \\ x_2^{(1)} &amp;= \frac{-0.413 -1.875 \times 0.2}{-2.875} = 0.274 \\ x_3^{(1)} &amp;= \frac{-0.274 -1.875 \times 0.1}{-2.875} = 0.161<br /> <br /> Out of interest, the actual solution: :math:x = [0.5, \,\, 0.358, \,\, 0.216, \,\, 0.140, \,\, 0.1]<br /> <br /> Question 6 [5 = 4 + 1]<br /> ========================================<br /> <br /> **6.1** When implementing either the Gauss-Seidel method, or the Jacobi method to solve **any** system of equations, :math:A{\bf x} = b, you can experience trouble with convergence. What are 2 options you can implement to try obtain convergence with these methods? Clearly explain what each option does to try achieve convergence.<br /> <br /> **6.2** The Gauss elimination process to solve the system of equations, :math:A{\bf x} = b uses partial pivoting. Please give a reason why this partial pivoting process is performed.<br /> <br /> Solution<br /> --------<br /> <br /> **6.1** One option is to use relaxation, with :math:0 \le \omega \le 1 in the equation :math:x^{(k+1)} = \omega x^{(k+1)} + (1-\omega)x^{(k)}, to avoid taking too large a step with each iteration. This reduction in step size prevents overshoot and achieves convergence by approaching the solution more gradually.<br /> <br /> Another option is to interchange rows in the :math:A matrix to try and achieve diagonal dominance, which is a sufficient condition for convergence (i.e. if :math:A is diagonally dominant, then we are guaranteed to converge).<br /> <br /> An alternative, though less systematic option, is to choose a different initial guess and hope for the best. The other two methods mentioned above should be used instead, because they will likely work. Choosing initial guesses close to the expected solution is very difficult for large-scale systems (big :math:A matrices).<br /> <br /> **6.2** Pivoting eliminates zeros on the diagonal line (to prevent division by zero); it also reduces round-off error, since the pivot element (the diagonal element) is the divisor in the elimination process. It is not correct to say that pivoting in Gauss Elimination is used to achieve diagonal dominance, since diagonal dominance is only an issue for iterative methods (e.g Jacobi or Gauss-Seidel method). Gauss elimination will **always** converge, if a solution exists and if round-off error does not accumulate.<br /> <br /> Question 7 <br /> ================<br /> <br /> In solving the equation, :math:f(x)=0 for a single variable, :math:x, how many steps of the bisection method are necessary to guarantee the solution is within an interval of width :math:\varepsilon = 2 \times 10^{-6}, starting with an interval that is 10 units wide?<br /> <br /> Solution<br /> --------<br /> <br /> In each bisection, the interval of search is shrunk by half. If we start from the interval :math:[a,b], we know that the solution is within :math:\displaystyle\frac{b-a}{2} after we do the first bisection. After the second bisection, the solution is guaranteed to be within an interval of :math:\displaystyle\frac{b-a}{2^2}. Similarly, after :math:n bisections, the solution is guaranteed to be within an interval of :math:\displaystyle\frac{b-a}{2^n}.<br /> <br /> Now if we would like the solution interval to be within a given :math:\varepsilon, the number of bisections :math:n can be obtained as follows:<br /> <br /> .. math::<br /> <br /> \displaystyle\frac{b-a}{2^n}\le \varepsilon<br /> <br /> On solving for :math:n:<br /> <br /> .. math::<br /> n &gt; \log_2\displaystyle\frac{b-a}{\varepsilon}<br /> <br /> We use the above equation to see how many bisections we need given the width of the inital interval :math:b-a=10, and :math:\varepsilon = 2 \times 10^{-6}.<br /> <br /> :math:n &gt; \log_2 \left(\displaystyle \frac{10}{2\times 10^{-6}} \right)=\log_2 \left({5\times 10^6}\right)<br /> <br /> **Math refresher**: Computing logarithm in an arbitrary base :math:b: :math:\displaystyle \log_b {x} = \frac{\log_a{x}}{\log_a{b}}. <br /> <br /> Therefore, :math:\displaystyle\log_2\displaystyle ({5\times 10^6})=\frac{\log_{10} ({5\times 10^6})}{\log_{10} 2}=22.2.<br /> <br /> :math:n&gt;22.2\rightarrow n= **23 steps** must be performed.<br /> <br /> .. raw:: latex<br /> <br /> \newpage<br /> <br /> Question 8 <br /> ===================<br /> <br /> Consider the modelling of a jacketed CSTR, fed with a single inlet stream. Under some fairly straightforward assumptions, one can show that the steady state temperature of the fluid leaving the tank is given by: <br /> <br /> .. figure:: images/CSTR2_1feed_jacket.png<br /> :scale: 76<br /> :align: center<br /> <br /> .. math::<br /> <br /> \frac{F^{\rm in}}{V}[T^{\rm in}-T] - \frac{UA_s}{\rho\,c_p\,V}[T - T_j] + \frac{2\,k_0\,(-\Delta H_r)}{\rho\,c_p}\,C_{\sf A}^2\,e^{ -\dfrac{E_a}{RT}} = 0<br /> <br /> After substituting in relevant values for the constant physical properties, and fixing the value of :math:C_{\sf A}, the equation can be reduced to:<br /> <br /> .. math::<br /> <br /> 2.6 - 1.45 T + 5 \times 10^{5} e ^{-\frac{2000}{T}} = 0<br /> <br /> Please perform 2 iterations of the Newton-Raphson algorithm for finding the roots of a nonlinear equation. A reasonable starting guess for :math:T^{(0)} is the same temperature as the inlet stream: i.e. :math:T^{(0)} = T^{\rm in} = 290 {\rm K}. Note: you do not need to derive the energy-balance equation above.<br /> <br /> Solution<br /> ----------<br /> <br /> We write the Newton-Raphson method to solve for the unknown temperature :math:T as:<br /> <br /> .. math::<br /> <br /> T_{n+1} = T_n -\frac{f(T_n)}{f'(T_n)}<br /> <br /> where :math:f(T_n) and :math:f'(T_n) are evaluated as follows:<br /> <br /> .. math::<br /> <br /> f(T) &amp;= 2.6 - 1.45 T + 5 \times 10^{5} e ^{-\tfrac{2000}{T}} \\ f'(T) &amp;= -1.45 - 5 \times 10^5 \times \left(-\frac{2000}{T^2}\right) e ^{-\tfrac{2000}{T}}<br /> <br /> * :math:T^{(0)} = 290.0 K<br /> * :math:f(290) = 87.73, and that :math:f'(290) = 10.57, so :math:\Delta T = \displaystyle -\frac{f(290)}{f'(290)} = \frac{87.73}{10.57}.<br /> * :math:T^{(1)} = T^{(0)} + \Delta T = 290.0 - 8.296 = 281.7 K<br /> * :math:f(281.7) = 6.79, and that :math:f'(281.7) = 8.95, so :math:\Delta T = \displaystyle -\frac{f(281.7)}{f'(281.7)} = \frac{6.79}{8.95}.<br /> * :math:T^{(2)} = T^{(1)} + \Delta T = 281.7 - 0.76 = 280.9 K<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.5cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Written_midterm_-_Solution&diff=1105 Written midterm - Solution 2018-09-16T09:04:32Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 18 October 2010<br /> | dates_alt_text = Date<br /> | questions_PDF = Midterm-ChE3E4-2010.pdf<br /> | questions_text_alt = Questions <br /> | solutions_PDF = Midterm-ChE3E4-2010-Solutions.pdf<br /> | solutions_text_alt = Solutions by Ali, Elliot and Kevin<br /> | other_instructions = <br /> }}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> <br /> .. rubric:: Special instructions<br /> <br /> - You may bring in any printed materials to the final; **any** textbooks, **any** papers, *etc*.<br /> - You may use any calculator during the test.<br /> - You may not use a cellphone as a calculator. Nor may you use any other communication device (web, email, chat, etc) during the test.<br /> - You may not use any type of computer.<br /> - You may answer the questions in any order in the answer booklet.<br /> - The test covers all material taught so far in the course.<br /> - Make sure that you provide explanations with your calculations.<br /> - If you are not sure about the meaning of a problem, please write out your interpretation and follow through with the calculation.<br /> - If you are short of time, please write what you would have done in this test, if you had more time.<br /> - There are a total of **45 marks** available.<br /> <br /> .. raw:: latex<br /> <br /> \pagestyle{plain}<br /> \vspace*{0.2cm}<br /> \hrule<br /> \vspace*{0.2cm}<br /> Solutions by: Ali (Q4, Q7, Q8), Elliot (Q1, Q2, Q3), and Kevin (Q5, Q6)<br /> \newpage<br /> <br /> <br /> .. Chemical engineering modelling <br /> .. Roundoff and computer representation <br /> .. Linear algebraic equations <br /> .. Taylor-series expansion <br /> .. Non-linear equation solution <br /> <br /> Question 1 <br /> ===============<br /> <br /> Consider the following 3 reactions taking place in a liquid-based system: a continuously stirred tank reactor (CSTR).<br /> <br /> * :math:{\sf A} \stackrel{r_1}{\rightarrow} {\sf 2B}, where :math:r_1 = k_1 C_{\sf A}<br /> * :math:{\sf A} \displaystyle \mathop{\rightleftharpoons}_{r_3}^{r_2} {\sf C}, where :math:r_2=k_2 C_{\sf A}^{1.5} and :math:r_3 = k_3 C_{\sf C}^2<br /> * :math:{\sf 2B} \stackrel{r_4}{\rightarrow} {\sf 3D + C}, where :math:r_4 = k_4 C_{\sf B}^2 <br /> <br /> Derive the equations that can be solved for the steady state concentrations of :math:\sf A, B, C and :math:\sf D. You may assume that species :math:\sf A is fed to the reactor in a single stream, flowing in at :math:Q \text{m}^3/\text{s}, with a concentration of :math:C_{\sf A}^{\text{in}}. Please state all other assumptions that you make while answering the question.<br /> <br /> Note: please **do not simplify** your equations.<br /> <br /> Solution<br /> ---------<br /> <br /> **ASSUMPTIONS AND DIAGRAM [3 marks]**<br /> <br /> * 1 mark for a proper diagram<br /> * 1 mark for each strong assumption<br /> * Additional assumptions may offset lack of diagram<br /> * Marks were removed for poor or incorrect assumptions on a case by case basis<br /> <br /> A diagram of the process is provided below:<br /> <br /> .. figure:: images/MidTerm_Q1.png<br /> :scale: 40<br /> :align: center<br /> <br /> The following is a list of appropriate assumptions for performing the required mass balances:<br /> <br /> * The reactor is operating at a constant volume (i.e. :math:Q_{in} = Q_{out} = Q)<br /> * Mixture physical properties are constant and temperature independent<br /> * Reactor is perfectly mixed<br /> * Reactor is sealed (i.e. no vapour losses)<br /> <br /> **BALANCES [8 marks = 2 marks each]**<br /> <br /> * Marks removed for incomplete or incorrect balances<br /> * All 4 mol balances start from this relationship:<br /> <br /> .. math::<br /> <br /> \displaystyle \frac{dn_{i}}{dt} = \sum(n_{i,in}) - \sum(n_{i,\text{out}}) + \text{Generation} - \text{Consumption}<br /> <br /> <br /> *Balance on A*<br /> <br /> .. math::<br /> \begin{array}{rl} \displaystyle \frac{d(V C_{\sf A})}{dt} &amp;= Q C_{\sf A,in} - Q C_{\sf A} + (V k_{3} C_{\sf C}^{2}) - (V k_{1} C_{\sf A} + V k_{2} C_{\sf A}^{1.5}) \\ \\ \displaystyle \frac{d C_{\sf A}}{dt} &amp;= \frac{Q}{V}(C_{\sf A,in} - C_{\sf A}) + k_{3} C_{\sf C}^{2} - k_{1} C_{\sf A} - k_{2} C_{\sf A}^{1.5} \\ \\ \displaystyle (0) &amp;= \frac{Q}{V}(C_{\sf A,in} - C_{\sf A}) + k_{3} C_{\sf C}^{2} - k_{1} C_{\sf A} - k_{2} C_{\sf A}^{1.5}\end{array}<br /> <br /> *Balance on B*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \displaystyle \frac{d(V C_{\sf B})}{dt} &amp;= (0) - Q C_{\sf B} + (V 2 k_{1} C_{\sf A}) - (V 2 k_{4} C_{\sf B}^{2}) \\ \\ \displaystyle \frac{d C_{\sf B}}{dt} &amp;= - \frac{Q}{V} C_{\sf B} + 2 k_{1} C_{\sf A} - 2 k_{4} C_{\sf B}^{2} \\ \\ \displaystyle (0) &amp;= - \frac{Q}{V} C_{\sf B} + 2 k_{1} C_{\sf A} - 2 k_{4} C_{\sf B}^{2} \end{array}<br /> <br /> *Balance on C*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \displaystyle \frac{d(V C_{\sf C})}{dt} &amp;= (0) - Q C_{\sf C} + (V k_{2} C_{\sf A}^{1.5} + V k_{4} C_{\sf B}^{2}) - (V k_{3} C_{\sf C}^{2}) \\ \\ \displaystyle \frac{d C_{\sf C}}{dt} &amp;= - \frac{Q}{V} C_{\sf C} + k_{2} C_{\sf A}^{1.5} + k_{4} C_{\sf B}^{2} - k_{3} C_{\sf C}^{2} \\ \\ \displaystyle (0) &amp;= - \frac{Q}{V} C_{\sf C} + k_{2} C_{\sf A}^{1.5} + k_{4} C_{\sf B}^{2} - k_{3} C_{\sf C}^{2} \end{array}<br /> <br /> *Balance on D*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \displaystyle \frac{d(V C_{\sf D})}{dt} &amp;= (0) - Q C_{\sf D} + (V 3 k_{4} C_{\sf B}^{2}) - (0) \\ \\ \displaystyle \frac{d C_{\sf C}}{dt} &amp;= - \frac{Q}{V} C_{\sf D} + V 3 k_{4} C_{\sf B}^{2} \\ \\ \displaystyle (0) &amp;= - \frac{Q}{V} C_{\sf D} + 3 k_{4} C_{\sf B}^{2} \end{array}<br /> <br /> <br /> Question 2 <br /> ==============<br /> <br /> Consider the system in question 1 again, but modified now to be a semi-batch system - material only enters the reactor, but nothing leaves. Continue on from question 1, using any equations derived there and any assumptions stated there, and derive the dynamic balances **only for species** :math:C_{\sf A} **and** :math:C_{\sf D}.<br /> <br /> Solution<br /> --------<br /> <br /> **BALANCES [5 marks = 2.5 marks each]**<br /> <br /> * Since the majority of the equation derivation for this question was taken from Question 1 marking was much harsher for missing the key differences that made this question distinct<br /> * 1 mark for each equation was removed (i.e. resulting in a 3/5 all else considered) if no acknowledgment was made that volume is no longer constant. This included either not writing this fact explicitly or writing the V term outside of the concentration derivative without performing proper chain rule expansion<br /> * 1 mark for each equation was removed (i.e. resulting in a 3/5 all else considered) if the outlet terms were not removed<br /> * Additional marks were lost for general errors made in the model derivation<br /> <br /> To change the system to a semi-batch model we need to remove the outlet terms and account for the changes in volume. In other words, the volume is now a function of time, and cannot simply be removed from the derivative: in the equations below use the fact that :math:V = V(t)<br /> <br /> *Overall balance*<br /> <br /> .. math::<br /> \begin{array}{rl} \displaystyle \frac{dM}{dt} = \sum(M_{in}) - \sum(M_{out}) = \displaystyle \frac{d(\rho V)}{dt} &amp;= \rho Q_{in} - (0) \\ \displaystyle \frac{dV}{dt} &amp;= Q_{in} = Q \end{array}<br /> <br /> *Balance on A*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \displaystyle \frac{dn_{\sf A}}{dt} &amp;= \sum(n_{A,in}) - \sum(n_{A,\text{out}}) + \text{Generation} - \text{Consumption} \\ \\ \displaystyle \frac{d(V C_{\sf A})}{dt} &amp;= Q C_{\sf A,in} - (0) + (V k_{3} C_{\sf C}^{2}) - (V k_{1} C_{\sf A} + V k_{2} C_{\sf A}^{1.5}) \\ \\ \displaystyle V\frac{dC_{\sf A}}{dt} + C_{\sf A}\frac{dV}{dt} &amp;= Q C_{\sf A,in} + V k_{3} C_{\sf C}^{2} - V k_{1} C_{\sf A} - V k_{2} C_{\sf A}^{1.5} \\ \\ \displaystyle V\frac{dC_{\sf A}}{dt} + (Q) C_{\sf A} &amp;= Q C_{\sf A,in} + V k_{3} C_{\sf C}^{2} - V k_{1} C_{\sf A} - V k_{2} C_{\sf A}^{1.5} \\ \\ \displaystyle \frac{dC_{\sf A}}{dt} &amp;= \frac{Q}{V}(C_{\sf A,in}-C_{\sf A}) + k_{3} C_{\sf C}^{2} - k_{1} C_{\sf A} - k_{2} C_{\sf A}^{1.5} \end{array}<br /> <br /> *Balance on D*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \displaystyle \frac{dn_{\sf D}}{dt} &amp;= \sum(n_{D,in}) - \sum(n_{D,\text{out}}) + \text{Generation} - \text{Consumption} \\ \\ \displaystyle \frac{d(V C_{\sf D})}{dt} &amp;= (0) - (0) + (V 3 k_{4} C_{\sf B}^{2}) - (0) \\ \\ \displaystyle V\frac{dC_{\sf D}}{dt} + C_{\sf D}\frac{dV}{dt} &amp;= V 3 k_{4} C_{\sf B}^{2} \\ \\ \displaystyle V\frac{dC_{\sf D}}{dt} + (Q) C_{\sf D} &amp;= V 3 k_{4} C_{\sf B}^{2} \\ \\ \displaystyle \frac{dC_{\sf D}}{dt} &amp;= - \frac{Q}{V} C_{\sf D} + 3 k_{4} C_{\sf B}^{2} \end{array}<br /> <br /> <br /> Question 3 <br /> ==============<br /> <br /> Represent the decimal number, -1.0, in an 8-bit word that uses 1 bit for the sign, 3 bits for the signed exponent and the remaining bits for the significand. Normalization must be used for the significand and only zero or one entries can used in each bit position.<br /> <br /> Solution<br /> ---------<br /> <br /> * 0.5 was awarded for getting the sign bit correct<br /> * 0.75 was awarded for getting the exponent bits correct<br /> * 0.75 was awarded for getting the mantissa bits correct<br /> * In the case where the logic was correctly explained but the value was not normalized, 1.5 points were awarded<br /> * In a few cases it was noticed that the logic was correct and the normalization was correct but the exponents were written backwards (i.e. in the form &quot;sign, :math:2^{0}, :math:2^{1}&quot; as opposed to the correct form &quot;sign, :math:2^{1}, :math:2^{0}&quot;). This was likely the fault of the TA, who gave an incorrect explanation of how the exponents should be written in one of the tutorials. In these specific instances where it was obvious this had happened, full marks were awarded and an explanation of the error was given<br /> <br /> Normalization requires that the mantissa have a magnitude between 1 and 1/b (i.e. the first bit is equal to 1). To convert -1.0 to a normalized 8-bit binary number with a 1-bit sign, 3-bit exponent, and 4-bit mantissa we must first realize that :math:-1.0 = -2^{0}. This means that -1.0 is directly representable in binary form. To ensure the mantissa is normalized we simply shift the exponent over by 1 value. This is the same as saying :math:-1.0 = -2^{1}2^{-1}. The second term is 0.5, which is a value between 1 and 1/2 = 0.5. Therefore if we take the term :math:2^{1} as the exponent term and :math:2^{-1} as the mantissa then the 8-bit representation of -1.0 is:<br /> <br /> .. math::<br /> <br /> \underbrace{1}_{\text{sign}}\underbrace{001}_{\text{exponent}}\underbrace{1000}_{\text{mantissa}}<br /> <br /> <br /> Question 4 [4 = 2 + 1 + 1]<br /> ===========================<br /> <br /> **4.1** A linearized system of equations, :math:Ax = b, has been constructed for a very high accuracy model of a distillation column. There are at most 12,000 equations in the system. What is the size, in megabytes, required to store :math:A at double precision? <br /> <br /> **4.2** Can this matrix be stored in 1 gigabyte of RAM?<br /> <br /> **4.3** The matrix :math:A is sparse (i.e. consists mostly of zeros). The number of non-zero elements is about 16,000. How much RAM, in megabytes, would be required to only store the non-zero entries?<br /> <br /> Solution<br /> ---------<br /> <br /> **4.1** Having 12,000 equations implies that there are also 12,000 unknowns in the system. The linear system will then have :math:12000 \times 12000 coefficients to be stored in the matrix :math:A. Because we work in double precision, each coefficient (entry) requires 8 bytes (or 64 bits) of memory. Given the conversion factor of 1024 to convert from bytes to kilobytes and also from kilobytes to megabytes, the total memory is obtained as follows:<br /> <br /> :math:12000 \times 12000 \times 8 \frac{\text{bytes}}{\text{entry}} \times \frac{\text{kilobyte}}{1024 \,\text{bytes}} \times \frac{\text{megabyte}}{1024 \,\text{kilobytes}} = **1098.6 megabytes**.<br /> <br /> **4.2** **No**, the matrix cannot be stored in 1 gigabyte of RAM, since 1 gigabyte of RAM is 1024 Mb.<br /> <br /> **4.3** Now We only store the 16,000 non-zero entries instead of the entire :math:12000 \times 12000 entries: :math:16000 \times 8 \frac{\text{bytes}}{\text{entry}} \times \frac{\text{kilobyte}}{1024 \,\text{bytes}} \times \frac{\text{megabyte}}{1024\, \text{kilobytes}} = **0.122 megabytes**. It is interesting to see that a much smaller RAM is required in this case.<br /> <br /> *Aside*: Of course we would need to store indicies corresponding to the non-zero entries as well to fully reconstruct :math:A, but that look-up table would still require much less storage than 1099 Mb required from part 4.1. <br /> <br /> **Good to know**: Sparse matrices occur frequently in engineering applications (why? consider the tray column examples you have solved before). In linear algebra, there are a variaty of techniques that take advantage of the sparsity to imporve the efficiency and storage requirements of solving large engineering problems.<br /> <br /> .. raw:: latex<br /> <br /> \newpage<br /> <br /> Question 5 [11 = 2 + 2 + 2 + 5]<br /> ========================================<br /> <br /> This questions considers a simple model of a 3-stage absorber (a similar model was used in assignment 2 in the course, but it is not exactly the same as the model used here). <br /> <br /> An illustration of the system, and the steady-state equations for system can be shown to be:<br /> <br /> .. figure:: images/absorber-column-midterm.png<br /> :scale: 30<br /> :align: center<br /> <br /> .. math::<br /> <br /> y_F &amp;= 0.5\\ \frac{1}{\beta} y_F - (\delta+1)x_1 + \delta x_2 &amp;= 0 \\ x_1 - (\delta+1)x_2 + \delta x_3 &amp;= 0 \\ x_2 - (\delta+1)x_3 + \delta x_F &amp;= 0 \\ x_F &amp;= 0.1 <br /> <br /> where :math:\beta is a coefficient, assumed constant throughout the absorber tower, that relates the liquid phase composition, :math:x_n, to the gas-phase composition, :math:y_n = \beta x_n, assuming of course that equilibrium is achieved in each stage. <br /> <br /> The :math:\delta coefficient is a dimensionless number defined as a function of the molar gas and liquid flows in the column, and :math:\beta, so that :math:\delta = \displaystyle \frac{L}{G \beta}<br /> <br /> **5.1** Write the given steady-state equations in the form :math:A{\bf x} = b where :math:{\bf x} = \left[y_F, x_1, x_2, x_3, x_F\right]. Report the :math:A matrix and :math:b vector.<br /> <br /> **5.2** What condition(s) must be satisfied so that matrix :math:A is diagonally dominant?<br /> <br /> **5.3** You decide to use the Gauss-Seidel method to solve this system of equations. Also, you are given that :math:L/G = 1.5, and :math:\beta = 0.8. Is this method guaranteed to converge for these coefficient values?<br /> <br /> **5.4** Perform one iteration of the Gauss-Seidel method, starting from a suitable initial guess that you believe will converge in fewer iterations than simply using the default guess of :math:{\bf x} = \left[y_F, x_1, x_2, x_3, x_F\right] = [0, 0, 0, 0, 0]. Use :math:L/G = 1.5, and :math:\beta = 0.8 in your matrix, and briefly explain your choice for the initial guess.<br /> <br /> Solution<br /> ---------<br /> <br /> **5.1** <br /> <br /> .. math::<br /> <br /> A = \left( \begin{array}{ccccc} 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0\\ \displaystyle \frac{1}{\beta} &amp; -(\delta+1) &amp; \delta &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; -(\delta+1) &amp; \delta &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; -(\delta+1) &amp; \delta\\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right) \hspace{2cm} b = \left(\begin{array}{c} 0.5\\ 0\\ 0\\ 0\\ 0.1 \end{array}\right)<br /> <br /> <br /> .. sidebar:: Grading<br /> <br /> Stating the general conditions for diagonal dominance for any matrix :math:A would have got you full marks for question 5.2, because the question could also have been interpreted that way. However you would need to do the calculations shown here either in question 5.2 or 5.3.<br /> <br /> **5.2** Rows 1 and 5 already meet the criteria for diagonal dominance. Rows 3 and 4 meet the first criterion for diagonal dominance, in that :math:\| \delta + 1 \| \geq \| \delta + 1 \|. However row 2 must satisfy the criterion that :math:\| \delta + 1 \| \geq \| \frac{1}{\beta} + \delta \| which simplifies to :math:\beta \geq 1 (using the fact that :math:\delta and :math:\beta are guaranteed to always be positive quantities: *why*?). So the only criterion required for diagonal dominance *for this system* is that :math:\beta \geq 1.<br /> <br /> **5.3** Both the Gauss-Seidel method, and Jacobi method are guaranteed to converge if the system is diagonally dominant. Since :math:\beta &lt; 1.0 in our case, the system is **not** diagonally dominant, as shown in the previous question. However, it might still converge, since diagonal dominance is only a sufficient criterion.<br /> <br /> **5.4** <br /> <br /> **[2 points for initial guess (IG)]**<br /> <br /> **[3 pts for calculations]**<br /> <br /> A good initial guess is any guess where the :math:x-values have concentrations between the feed and exit conditions. In this system the entry concentration of impurity in the liquid phase is 0.1. The impurity is being transferred from the gas to the liquid phase, so the liquid leaving each tray should have an increasing quantity of impurity, or mathematically: :math:x_F = 0.1 &lt; x_3 &lt; x_2 &lt; x_1. The upper bound on these concentrations is expected to be :math:y_F = 0.5, although depending on the VLE, it could be higher, but unlikely. <br /> <br /> Initial guesses for :math:y_F = 0.5 and :math:x_F = 0.1 must be used, because they are set from the initial conditions given in the problem statement.<br /> <br /> So a good guess for :math:{\bf x}^{(0)} is \left[y_F = 0.5, x_1 = 0.4, x_2 = 0.3, x_3 = 0.2, x_F = 0.1\right], or similar. <br /> <br /> Using this guess to solve for the first Gauss-Seidel iteration. There is no need to run the iterations for :math:y_F and :math:x_F as they are trivially solved.<br /> <br /> .. math::<br /> <br /> x_1^{(1)} &amp;= \frac{-0.5/0.8 -1.875 \times 0.3}{-2.875} = 0.413 \\ x_2^{(1)} &amp;= \frac{-0.413 -1.875 \times 0.2}{-2.875} = 0.274 \\ x_3^{(1)} &amp;= \frac{-0.274 -1.875 \times 0.1}{-2.875} = 0.161<br /> <br /> Out of interest, the actual solution: :math:x = [0.5, \,\, 0.358, \,\, 0.216, \,\, 0.140, \,\, 0.1]<br /> <br /> Question 6 [5 = 4 + 1]<br /> ========================================<br /> <br /> **6.1** When implementing either the Gauss-Seidel method, or the Jacobi method to solve **any** system of equations, :math:A{\bf x} = b, you can experience trouble with convergence. What are 2 options you can implement to try obtain convergence with these methods? Clearly explain what each option does to try achieve convergence.<br /> <br /> **6.2** The Gauss elimination process to solve the system of equations, :math:A{\bf x} = b uses partial pivoting. Please give a reason why this partial pivoting process is performed.<br /> <br /> Solution<br /> --------<br /> <br /> **6.1** One option is to use relaxation, with :math:0 \le \omega \le 1 in the equation :math:x^{(k+1)} = \omega x^{(k+1)} + (1-\omega)x^{(k)}, to avoid taking too large a step with each iteration. This reduction in step size prevents overshoot and achieves convergence by approaching the solution more gradually.<br /> <br /> Another option is to interchange rows in the :math:A matrix to try and achieve diagonal dominance, which is a sufficient condition for convergence (i.e. if :math:A is diagonally dominant, then we are guaranteed to converge).<br /> <br /> An alternative, though less systematic option, is to choose a different initial guess and hope for the best. The other two methods mentioned above should be used instead, because they will likely work. Choosing initial guesses close to the expected solution is very difficult for large-scale systems (big :math:A matrices).<br /> <br /> **6.2** Pivoting eliminates zeros on the diagonal line (to prevent division by zero); it also reduces round-off error, since the pivot element (the diagonal element) is the divisor in the elimination process. It is not correct to say that pivoting in Gauss Elimination is used to achieve diagonal dominance, since diagonal dominance is only an issue for iterative methods (e.g Jacobi or Gauss-Seidel method). Gauss elimination will **always** converge, if a solution exists and if round-off error does not accumulate.<br /> <br /> Question 7 <br /> ================<br /> <br /> In solving the equation, :math:f(x)=0 for a single variable, :math:x, how many steps of the bisection method are necessary to guarantee the solution is within an interval of width :math:\varepsilon = 2 \times 10^{-6}, starting with an interval that is 10 units wide?<br /> <br /> Solution<br /> --------<br /> <br /> In each bisection, the interval of search is shrunk by half. If we start from the interval :math:[a,b], we know that the solution is within :math:\displaystyle\frac{b-a}{2} after we do the first bisection. After the second bisection, the solution is guaranteed to be within an interval of :math:\displaystyle\frac{b-a}{2^2}. Similarly, after :math:n bisections, the solution is guaranteed to be within an interval of :math:\displaystyle\frac{b-a}{2^n}.<br /> <br /> Now if we would like the solution interval to be within a given :math:\varepsilon, the number of bisections :math:n can be obtained as follows:<br /> <br /> .. math::<br /> <br /> \displaystyle\frac{b-a}{2^n}\le \varepsilon<br /> <br /> On solving for :math:n:<br /> <br /> .. math::<br /> n &gt; \log_2\displaystyle\frac{b-a}{\varepsilon}<br /> <br /> We use the above equation to see how many bisections we need given the width of the inital interval :math:b-a=10, and :math:\varepsilon = 2 \times 10^{-6}.<br /> <br /> :math:n &gt; \log_2 \left(\displaystyle \frac{10}{2\times 10^{-6}} \right)=\log_2 \left({5\times 10^6}\right)<br /> <br /> **Math refresher**: Computing logarithm in an arbitrary base :math:b: :math:\displaystyle \log_b {x} = \frac{\log_a{x}}{\log_a{b}}. <br /> <br /> Therefore, :math:\displaystyle\log_2\displaystyle ({5\times 10^6})=\frac{\log_{10} ({5\times 10^6})}{\log_{10} 2}=22.2.<br /> <br /> :math:n&gt;22.2\rightarrow n= **23 steps** must be performed.<br /> <br /> .. raw:: latex<br /> <br /> \newpage<br /> <br /> Question 8 <br /> ===================<br /> <br /> Consider the modelling of a jacketed CSTR, fed with a single inlet stream. Under some fairly straightforward assumptions, one can show that the steady state temperature of the fluid leaving the tank is given by: <br /> <br /> .. figure:: images/CSTR2_1feed_jacket.png<br /> :scale: 76<br /> :align: center<br /> <br /> .. math::<br /> <br /> \frac{F^{\rm in}}{V}[T^{\rm in}-T] - \frac{UA_s}{\rho\,c_p\,V}[T - T_j] + \frac{2\,k_0\,(-\Delta H_r)}{\rho\,c_p}\,C_{\sf A}^2\,e^{ -\dfrac{E_a}{RT}} &amp;= 0<br /> <br /> After substituting in relevant values for the constant physical properties, and fixing the value of :math:C_{\sf A}, the equation can be reduced to:<br /> <br /> .. math::<br /> <br /> 2.6 - 1.45 T + 5 \times 10^{5} e ^{-\tfrac{2000}{T}} &amp;= 0<br /> <br /> Please perform 2 iterations of the Newton-Raphson algorithm for finding the roots of a nonlinear equation. A reasonable starting guess for :math:T^{(0)} is the same temperature as the inlet stream: i.e. :math:T^{(0)} = T^{\rm in} = 290 {\rm K}. Note: you do not need to derive the energy-balance equation above.<br /> <br /> Solution<br /> ----------<br /> <br /> We write the Newton-Raphson method to solve for the unknown temperature :math:T as:<br /> <br /> .. math::<br /> <br /> T_{n+1} = T_n -\frac{f(T_n)}{f'(T_n)}<br /> <br /> where :math:f(T_n) and :math:f'(T_n) are evaluated as follows:<br /> <br /> .. math::<br /> <br /> f(T) &amp;= 2.6 - 1.45 T + 5 \times 10^{5} e ^{-\tfrac{2000}{T}} \\ f'(T) &amp;= -1.45 - 5 \times 10^5 \times \left(-\frac{2000}{T^2}\right) e ^{-\tfrac{2000}{T}}<br /> <br /> * :math:T^{(0)} = 290.0 K<br /> * :math:f(290) = 87.73, and that :math:f'(290) = 10.57, so :math:\Delta T = \displaystyle -\frac{f(290)}{f'(290)} = \frac{87.73}{10.57}.<br /> * :math:T^{(1)} = T^{(0)} + \Delta T = 290.0 - 8.296 = 281.7 K<br /> * :math:f(281.7) = 6.79, and that :math:f'(281.7) = 8.95, so :math:\Delta T = \displaystyle -\frac{f(281.7)}{f'(281.7)} = \frac{6.79}{8.95}.<br /> * :math:T^{(2)} = T^{(1)} + \Delta T = 281.7 - 0.76 = 280.9 K<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.5cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Written_midterm_-_Solution&diff=1104 Written midterm - Solution 2018-09-16T09:03:22Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 18 October 2010<br /> | dates_alt_text = Date<br /> | questions_PDF = Midterm-ChE3E4-2010.pdf<br /> | questions_text_alt = Questions <br /> | solutions_PDF = Midterm-ChE3E4-2010-Solutions.pdf<br /> | solutions_text_alt = Solutions by Ali, Elliot and Kevin<br /> | other_instructions = <br /> }}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> <br /> .. rubric:: Special instructions<br /> <br /> - You may bring in any printed materials to the final; **any** textbooks, **any** papers, *etc*.<br /> - You may use any calculator during the test.<br /> - You may not use a cellphone as a calculator. Nor may you use any other communication device (web, email, chat, etc) during the test.<br /> - You may not use any type of computer.<br /> - You may answer the questions in any order in the answer booklet.<br /> - The test covers all material taught so far in the course.<br /> - Make sure that you provide explanations with your calculations.<br /> - If you are not sure about the meaning of a problem, please write out your interpretation and follow through with the calculation.<br /> - If you are short of time, please write what you would have done in this test, if you had more time.<br /> - There are a total of **45 marks** available.<br /> <br /> .. raw:: latex<br /> <br /> \pagestyle{plain}<br /> \vspace*{0.2cm}<br /> \hrule<br /> \vspace*{0.2cm}<br /> Solutions by: Ali (Q4, Q7, Q8), Elliot (Q1, Q2, Q3), and Kevin (Q5, Q6)<br /> \newpage<br /> <br /> <br /> .. Chemical engineering modelling <br /> .. Roundoff and computer representation <br /> .. Linear algebraic equations <br /> .. Taylor-series expansion <br /> .. Non-linear equation solution <br /> <br /> Question 1 <br /> ===============<br /> <br /> Consider the following 3 reactions taking place in a liquid-based system: a continuously stirred tank reactor (CSTR).<br /> <br /> * :math:{\sf A} \stackrel{r_1}{\rightarrow} {\sf 2B}, where :math:r_1 = k_1 C_{\sf A}<br /> * :math:{\sf A} \displaystyle \mathop{\rightleftharpoons}_{r_3}^{r_2} {\sf C}, where :math:r_2=k_2 C_{\sf A}^{1.5} and :math:r_3 = k_3 C_{\sf C}^2<br /> * :math:{\sf 2B} \stackrel{r_4}{\rightarrow} {\sf 3D + C}, where :math:r_4 = k_4 C_{\sf B}^2 <br /> <br /> Derive the equations that can be solved for the steady state concentrations of :math:\sf A, B, C and :math:\sf D. You may assume that species :math:\sf A is fed to the reactor in a single stream, flowing in at :math:Q \text{m}^3/\text{s}, with a concentration of :math:C_{\sf A}^{\text{in}}. Please state all other assumptions that you make while answering the question.<br /> <br /> Note: please **do not simplify** your equations.<br /> <br /> Solution<br /> ---------<br /> <br /> **ASSUMPTIONS AND DIAGRAM [3 marks]**<br /> <br /> * 1 mark for a proper diagram<br /> * 1 mark for each strong assumption<br /> * Additional assumptions may offset lack of diagram<br /> * Marks were removed for poor or incorrect assumptions on a case by case basis<br /> <br /> A diagram of the process is provided below:<br /> <br /> .. figure:: images/MidTerm_Q1.png<br /> :scale: 40<br /> :align: center<br /> <br /> The following is a list of appropriate assumptions for performing the required mass balances:<br /> <br /> * The reactor is operating at a constant volume (i.e. :math:Q_{in} = Q_{out} = Q)<br /> * Mixture physical properties are constant and temperature independent<br /> * Reactor is perfectly mixed<br /> * Reactor is sealed (i.e. no vapour losses)<br /> <br /> **BALANCES [8 marks = 2 marks each]**<br /> <br /> * Marks removed for incomplete or incorrect balances<br /> * All 4 mol balances start from this relationship:<br /> <br /> .. math::<br /> <br /> \displaystyle \frac{dn_{i}}{dt} = \sum(n_{i,in}) - \sum(n_{i,\text{out}}) + \text{Generation} - \text{Consumption}<br /> <br /> <br /> *Balance on A*<br /> <br /> .. math::<br /> \begin{array}{rl} \displaystyle \frac{d(V C_{\sf A})}{dt} &amp;= Q C_{\sf A,in} - Q C_{\sf A} + (V k_{3} C_{\sf C}^{2}) - (V k_{1} C_{\sf A} + V k_{2} C_{\sf A}^{1.5}) \\ \\ \displaystyle \frac{d C_{\sf A}}{dt} &amp;= \frac{Q}{V}(C_{\sf A,in} - C_{\sf A}) + k_{3} C_{\sf C}^{2} - k_{1} C_{\sf A} - k_{2} C_{\sf A}^{1.5} \\ \\ \displaystyle (0) &amp;= \frac{Q}{V}(C_{\sf A,in} - C_{\sf A}) + k_{3} C_{\sf C}^{2} - k_{1} C_{\sf A} - k_{2} C_{\sf A}^{1.5}\end{array}<br /> <br /> *Balance on B*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \displaystyle \frac{d(V C_{\sf B})}{dt} &amp;= (0) - Q C_{\sf B} + (V 2 k_{1} C_{\sf A}) - (V 2 k_{4} C_{\sf B}^{2}) \\ \\ \displaystyle \frac{d C_{\sf B}}{dt} &amp;= - \frac{Q}{V} C_{\sf B} + 2 k_{1} C_{\sf A} - 2 k_{4} C_{\sf B}^{2} \\ \\ \displaystyle (0) &amp;= - \frac{Q}{V} C_{\sf B} + 2 k_{1} C_{\sf A} - 2 k_{4} C_{\sf B}^{2} \end{array}<br /> <br /> *Balance on C*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \displaystyle \frac{d(V C_{\sf C})}{dt} &amp;= (0) - Q C_{\sf C} + (V k_{2} C_{\sf A}^{1.5} + V k_{4} C_{\sf B}^{2}) - (V k_{3} C_{\sf C}^{2}) \\ \\ \displaystyle \frac{d C_{\sf C}}{dt} &amp;= - \frac{Q}{V} C_{\sf C} + k_{2} C_{\sf A}^{1.5} + k_{4} C_{\sf B}^{2} - k_{3} C_{\sf C}^{2} \\ \\ \displaystyle (0) &amp;= - \frac{Q}{V} C_{\sf C} + k_{2} C_{\sf A}^{1.5} + k_{4} C_{\sf B}^{2} - k_{3} C_{\sf C}^{2} \end{array}<br /> <br /> *Balance on D*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \displaystyle \frac{d(V C_{\sf D})}{dt} &amp;= (0) - Q C_{\sf D} + (V 3 k_{4} C_{\sf B}^{2}) - (0) \\ \\ \displaystyle \frac{d C_{\sf C}}{dt} &amp;= - \frac{Q}{V} C_{\sf D} + V 3 k_{4} C_{\sf B}^{2} \\ \\ \displaystyle (0) &amp;= - \frac{Q}{V} C_{\sf D} + 3 k_{4} C_{\sf B}^{2} \end{array}<br /> <br /> <br /> Question 2 <br /> ==============<br /> <br /> Consider the system in question 1 again, but modified now to be a semi-batch system - material only enters the reactor, but nothing leaves. Continue on from question 1, using any equations derived there and any assumptions stated there, and derive the dynamic balances **only for species** :math:C_{\sf A} **and** :math:C_{\sf D}.<br /> <br /> Solution<br /> --------<br /> <br /> **BALANCES [5 marks = 2.5 marks each]**<br /> <br /> * Since the majority of the equation derivation for this question was taken from Question 1 marking was much harsher for missing the key differences that made this question distinct<br /> * 1 mark for each equation was removed (i.e. resulting in a 3/5 all else considered) if no acknowledgment was made that volume is no longer constant. This included either not writing this fact explicitly or writing the V term outside of the concentration derivative without performing proper chain rule expansion<br /> * 1 mark for each equation was removed (i.e. resulting in a 3/5 all else considered) if the outlet terms were not removed<br /> * Additional marks were lost for general errors made in the model derivation<br /> <br /> To change the system to a semi-batch model we need to remove the outlet terms and account for the changes in volume. In other words, the volume is now a function of time, and cannot simply be removed from the derivative: in the equations below use the fact that :math:V = V(t)<br /> <br /> *Overall balance*<br /> <br /> .. math::<br /> \begin{array}{rl} \displaystyle \frac{dM}{dt} = \sum(M_{in}) - \sum(M_{out}) = \displaystyle \frac{d(\rho V)}{dt} &amp;= \rho Q_{in} - (0) \\ \displaystyle \frac{dV}{dt} &amp;= Q_{in} = Q \end{array}<br /> <br /> *Balance on A*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \displaystyle \frac{dn_{\sf A}}{dt} &amp;= \sum(n_{A,in}) - \sum(n_{A,\text{out}}) + \text{Generation} - \text{Consumption} \\ \\ \displaystyle \frac{d(V C_{\sf A})}{dt} &amp;= Q C_{\sf A,in} - (0) + (V k_{3} C_{\sf C}^{2}) - (V k_{1} C_{\sf A} + V k_{2} C_{\sf A}^{1.5}) \\ \\ \displaystyle V\frac{dC_{\sf A}}{dt} + C_{\sf A}\frac{dV}{dt} &amp;= Q C_{\sf A,in} + V k_{3} C_{\sf C}^{2} - V k_{1} C_{\sf A} - V k_{2} C_{\sf A}^{1.5} \\ \\ \displaystyle V\frac{dC_{\sf A}}{dt} + (Q) C_{\sf A} &amp;= Q C_{\sf A,in} + V k_{3} C_{\sf C}^{2} - V k_{1} C_{\sf A} - V k_{2} C_{\sf A}^{1.5} \\ \\ \displaystyle \frac{dC_{\sf A}}{dt} &amp;= \frac{Q}{V}(C_{\sf A,in}-C_{\sf A}) + k_{3} C_{\sf C}^{2} - k_{1} C_{\sf A} - k_{2} C_{\sf A}^{1.5} \end{array}<br /> <br /> *Balance on D*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \displaystyle \frac{dn_{\sf D}}{dt} &amp;= \sum(n_{D,in}) - \sum(n_{D,\text{out}}) + \text{Generation} - \text{Consumption} \\ \\ \displaystyle \frac{d(V C_{\sf D})}{dt} &amp;= (0) - (0) + (V 3 k_{4} C_{\sf B}^{2}) - (0) \\ \\ \displaystyle V\frac{dC_{\sf D}}{dt} + C_{\sf D}\frac{dV}{dt} &amp;= V 3 k_{4} C_{\sf B}^{2} \\ \\ \displaystyle V\frac{dC_{\sf D}}{dt} + (Q) C_{\sf D} &amp;= V 3 k_{4} C_{\sf B}^{2} \\ \\ \displaystyle \frac{dC_{\sf D}}{dt} &amp;= - \frac{Q}{V} C_{\sf D} + 3 k_{4} C_{\sf B}^{2} \end{array}<br /> <br /> <br /> Question 3 <br /> ==============<br /> <br /> Represent the decimal number, -1.0, in an 8-bit word that uses 1 bit for the sign, 3 bits for the signed exponent and the remaining bits for the significand. Normalization must be used for the significand and only zero or one entries can used in each bit position.<br /> <br /> Solution<br /> ---------<br /> <br /> * 0.5 was awarded for getting the sign bit correct<br /> * 0.75 was awarded for getting the exponent bits correct<br /> * 0.75 was awarded for getting the mantissa bits correct<br /> * In the case where the logic was correctly explained but the value was not normalized, 1.5 points were awarded<br /> * In a few cases it was noticed that the logic was correct and the normalization was correct but the exponents were written backwards (i.e. in the form &quot;sign, :math:2^{0}, :math:2^{1}&quot; as opposed to the correct form &quot;sign, :math:2^{1}, :math:2^{0}&quot;). This was likely the fault of the TA, who gave an incorrect explanation of how the exponents should be written in one of the tutorials. In these specific instances where it was obvious this had happened, full marks were awarded and an explanation of the error was given<br /> <br /> Normalization requires that the mantissa have a magnitude between 1 and 1/b (i.e. the first bit is equal to 1). To convert -1.0 to a normalized 8-bit binary number with a 1-bit sign, 3-bit exponent, and 4-bit mantissa we must first realize that :math:-1.0 = -2^{0}. This means that -1.0 is directly representable in binary form. To ensure the mantissa is normalized we simply shift the exponent over by 1 value. This is the same as saying :math:-1.0 = -2^{1}2^{-1}. The second term is 0.5, which is a value between 1 and 1/2 = 0.5. Therefore if we take the term :math:2^{1} as the exponent term and :math:2^{-1} as the mantissa then the 8-bit representation of -1.0 is:<br /> <br /> .. math::<br /> <br /> \underbrace{1}_{\text{sign}}\underbrace{001}_{\text{exponent}}\underbrace{1000}_{\text{mantissa}}<br /> <br /> <br /> Question 4 [4 = 2 + 1 + 1]<br /> ===========================<br /> <br /> **4.1** A linearized system of equations, :math:Ax = b, has been constructed for a very high accuracy model of a distillation column. There are at most 12,000 equations in the system. What is the size, in megabytes, required to store :math:A at double precision? <br /> <br /> **4.2** Can this matrix be stored in 1 gigabyte of RAM?<br /> <br /> **4.3** The matrix :math:A is sparse (i.e. consists mostly of zeros). The number of non-zero elements is about 16,000. How much RAM, in megabytes, would be required to only store the non-zero entries?<br /> <br /> Solution<br /> ---------<br /> <br /> **4.1** Having 12,000 equations implies that there are also 12,000 unknowns in the system. The linear system will then have :math:12000 \times 12000 coefficients to be stored in the matrix :math:A. Because we work in double precision, each coefficient (entry) requires 8 bytes (or 64 bits) of memory. Given the conversion factor of 1024 to convert from bytes to kilobytes and also from kilobytes to megabytes, the total memory is obtained as follows:<br /> <br /> :math:12000 \times 12000 \times 8 \frac{\text{bytes}}{\text{entry}} \times \frac{\text{kilobyte}}{1024 \,\text{bytes}} \times \frac{\text{megabyte}}{1024 \,\text{kilobytes}} = **1098.6 megabytes**.<br /> <br /> **4.2** **No**, the matrix cannot be stored in 1 gigabyte of RAM, since 1 gigabyte of RAM is 1024 Mb.<br /> <br /> **4.3** Now We only store the 16,000 non-zero entries instead of the entire :math:12000 \times 12000 entries: :math:16000 \times 8 \frac{\text{bytes}}{\text{entry}} \times \frac{\text{kilobyte}}{1024 \,\text{bytes}} \times \frac{\text{megabyte}}{1024\, \text{kilobytes}} = **0.122 megabytes**. It is interesting to see that a much smaller RAM is required in this case.<br /> <br /> *Aside*: Of course we would need to store indicies corresponding to the non-zero entries as well to fully reconstruct :math:A, but that look-up table would still require much less storage than 1099 Mb required from part 4.1. <br /> <br /> **Good to know**: Sparse matrices occur frequently in engineering applications (why? consider the tray column examples you have solved before). In linear algebra, there are a variaty of techniques that take advantage of the sparsity to imporve the efficiency and storage requirements of solving large engineering problems.<br /> <br /> .. raw:: latex<br /> <br /> \newpage<br /> <br /> Question 5 [11 = 2 + 2 + 2 + 5]<br /> ========================================<br /> <br /> This questions considers a simple model of a 3-stage absorber (a similar model was used in assignment 2 in the course, but it is not exactly the same as the model used here). <br /> <br /> An illustration of the system, and the steady-state equations for system can be shown to be:<br /> <br /> .. figure:: images/absorber-column-midterm.png<br /> :scale: 30<br /> :align: center<br /> <br /> .. math::<br /> <br /> y_F &amp;= 0.5\\ \frac{1}{\beta} y_F - (\delta+1)x_1 + \delta x_2 &amp;= 0 \\ x_1 - (\delta+1)x_2 + \delta x_3 &amp;= 0 \\ x_2 - (\delta+1)x_3 + \delta x_F &amp;= 0 \\ x_F &amp;= 0.1 <br /> <br /> where :math:\beta is a coefficient, assumed constant throughout the absorber tower, that relates the liquid phase composition, :math:x_n, to the gas-phase composition, :math:y_n = \beta x_n, assuming of course that equilibrium is achieved in each stage. <br /> <br /> The :math:\delta coefficient is a dimensionless number defined as a function of the molar gas and liquid flows in the column, and :math:\beta, so that :math:\delta = \displaystyle \frac{L}{G \beta}<br /> <br /> **5.1** Write the given steady-state equations in the form :math:A{\bf x} = b where :math:{\bf x} = \left[y_F, x_1, x_2, x_3, x_F\right]. Report the :math:A matrix and :math:b vector.<br /> <br /> **5.2** What condition(s) must be satisfied so that matrix :math:A is diagonally dominant?<br /> <br /> **5.3** You decide to use the Gauss-Seidel method to solve this system of equations. Also, you are given that :math:L/G = 1.5, and :math:\beta = 0.8. Is this method guaranteed to converge for these coefficient values?<br /> <br /> **5.4** Perform one iteration of the Gauss-Seidel method, starting from a suitable initial guess that you believe will converge in fewer iterations than simply using the default guess of :math:{\bf x} = \left[y_F, x_1, x_2, x_3, x_F\right] = [0, 0, 0, 0, 0]. Use :math:L/G = 1.5, and :math:\beta = 0.8 in your matrix, and briefly explain your choice for the initial guess.<br /> <br /> Solution<br /> ---------<br /> <br /> **5.1** <br /> <br /> .. math::<br /> <br /> A = \left( \begin{array}{ccccc} 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0\\ \displaystyle \frac{1}{\beta} &amp; -(\delta+1) &amp; \delta &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; -(\delta+1) &amp; \delta &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; -(\delta+1) &amp; \delta\\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right) \hspace{2cm} b = \left(\begin{array}{c} 0.5\\ 0\\ 0\\ 0\\ 0.1 \end{array}\right)<br /> <br /> <br /> .. sidebar:: Grading<br /> <br /> Stating the general conditions for diagonal dominance for any matrix :math:A would have got you full marks for question 5.2, because the question could also have been interpreted that way. However you would need to do the calculations shown here either in question 5.2 or 5.3.<br /> <br /> **5.2** Rows 1 and 5 already meet the criteria for diagonal dominance. Rows 3 and 4 meet the first criterion for diagonal dominance, in that :math:\| \delta + 1 \| \geq \| \delta + 1 \|. However row 2 must satisfy the criterion that :math:\| \delta + 1 \| \geq \| \frac{1}{\beta} + \delta \| which simplifies to :math:\beta \geq 1 (using the fact that :math:\delta and :math:\beta are guaranteed to always be positive quantities: *why*?). So the only criterion required for diagonal dominance *for this system* is that :math:\beta \geq 1.<br /> <br /> **5.3** Both the Gauss-Seidel method, and Jacobi method are guaranteed to converge if the system is diagonally dominant. Since :math:\beta &lt; 1.0 in our case, the system is **not** diagonally dominant, as shown in the previous question. However, it might still converge, since diagonal dominance is only a sufficient criterion.<br /> <br /> **5.4** <br /> <br /> **[2 points for initial guess (IG)]**<br /> <br /> **[3 pts for calculations]**<br /> <br /> A good initial guess is any guess where the :math:x-values have concentrations between the feed and exit conditions. In this system the entry concentration of impurity in the liquid phase is 0.1. The impurity is being transferred from the gas to the liquid phase, so the liquid leaving each tray should have an increasing quantity of impurity, or mathematically: :math:x_F = 0.1 &lt; x_3 &lt; x_2 &lt; x_1. The upper bound on these concentrations is expected to be :math:y_F = 0.5, although depending on the VLE, it could be higher, but unlikely. <br /> <br /> Initial guesses for :math:y_F = 0.5 and :math:x_F = 0.1 must be used, because they are set from the initial conditions given in the problem statement.<br /> <br /> So a good guess for :math:{\bf x}^{(0)} is \left[y_F = 0.5, x_1 = 0.4, x_2 = 0.3, x_3 = 0.2, x_F = 0.1\right], or similar. <br /> <br /> Using this guess to solve for the first Gauss-Seidel iteration. There is no need to run the iterations for :math:y_F and :math:x_F as they are trivially solved.<br /> <br /> .. math::<br /> <br /> x_1^{(1)} &amp;= \frac{-0.5/0.8 -1.875 \times 0.3}{-2.875} = 0.413 \\ x_2^{(1)} &amp;= \frac{-0.413 -1.875 \times 0.2}{-2.875} = 0.274 \\ x_3^{(1)} &amp;= \frac{-0.274 -1.875 \times 0.1}{-2.875} = 0.161<br /> <br /> Out of interest, the actual solution: :math:x = [0.5, \,\, 0.358, \,\, 0.216, \,\, 0.140, \,\, 0.1]<br /> <br /> Question 6 [5 = 4 + 1]<br /> ========================================<br /> <br /> **6.1** When implementing either the Gauss-Seidel method, or the Jacobi method to solve **any** system of equations, :math:A{\bf x} = b, you can experience trouble with convergence. What are 2 options you can implement to try obtain convergence with these methods? Clearly explain what each option does to try achieve convergence.<br /> <br /> **6.2** The Gauss elimination process to solve the system of equations, :math:A{\bf x} = b uses partial pivoting. Please give a reason why this partial pivoting process is performed.<br /> <br /> Solution<br /> --------<br /> <br /> **6.1** One option is to use relaxation, with :math:0 \le \omega \le 1 in the equation :math:x^{(k+1)} = \omega x^{(k+1)} + (1-\omega)x^{(k)}, to avoid taking too large a step with each iteration. This reduction in step size prevents overshoot and achieves convergence by approaching the solution more gradually.<br /> <br /> Another option is to interchange rows in the :math:A matrix to try and achieve diagonal dominance, which is a sufficient condition for convergence (i.e. if :math:A is diagonally dominant, then we are guaranteed to converge).<br /> <br /> An alternative, though less systematic option, is to choose a different initial guess and hope for the best. The other two methods mentioned above should be used instead, because they will likely work. Choosing initial guesses close to the expected solution is very difficult for large-scale systems (big :math:A matrices).<br /> <br /> **6.2** Pivoting eliminates zeros on the diagonal line (to prevent division by zero); it also reduces round-off error, since the pivot element (the diagonal element) is the divisor in the elimination process. It is not correct to say that pivoting in Gauss Elimination is used to achieve diagonal dominance, since diagonal dominance is only an issue for iterative methods (e.g Jacobi or Gauss-Seidel method). Gauss elimination will **always** converge, if a solution exists and if round-off error does not accumulate.<br /> <br /> Question 7 <br /> ================<br /> <br /> In solving the equation, :math:f(x)=0 for a single variable, :math:x, how many steps of the bisection method are necessary to guarantee the solution is within an interval of width :math:\varepsilon = 2 \times 10^{-6}, starting with an interval that is 10 units wide?<br /> <br /> Solution<br /> --------<br /> <br /> In each bisection, the interval of search is shrunk by half. If we start from the interval :math:[a,b], we know that the solution is within :math:\displaystyle\frac{b-a}{2} after we do the first bisection. After the second bisection, the solution is guaranteed to be within an interval of :math:\displaystyle\frac{b-a}{2^2}. Similarly, after :math:n bisections, the solution is guaranteed to be within an interval of :math:\displaystyle\frac{b-a}{2^n}.<br /> <br /> Now if we would like the solution interval to be within a given :math:\varepsilon, the number of bisections :math:n can be obtained as follows:<br /> <br /> .. math::<br /> <br /> \displaystyle\frac{b-a}{2^n}\le \varepsilon<br /> <br /> On solving for :math:n:<br /> <br /> .. math::<br /> n &gt; \log_2\displaystyle\frac{b-a}{\varepsilon}<br /> <br /> We use the above equation to see how many bisections we need given the width of the inital interval :math:b-a=10, and :math:\varepsilon = 2 \times 10^{-6}.<br /> <br /> :math:n &gt; \log_2 \left(\displaystyle \frac{10}{2\times 10^{-6}} \right)=\log_2 \left({5\times 10^6}\right)<br /> <br /> **Math refresher**: Computing logarithm in an arbitrary base :math:b: :math:\displaystyle \log_b {x} = \frac{\log_a{x}}{\log_a{b}}. <br /> <br /> Therefore, :math:\displaystyle\log_2\displaystyle ({5\times 10^6})=\frac{\log_{10} ({5\times 10^6})}{\log_{10} 2}=22.2.<br /> <br /> :math:n&gt;22.2\rightarrow n= **23 steps** must be performed.<br /> <br /> .. raw:: latex<br /> <br /> \newpage<br /> <br /> Question 8 <br /> ===================<br /> <br /> Consider the modelling of a jacketed CSTR, fed with a single inlet stream. Under some fairly straightforward assumptions, one can show that the steady state temperature of the fluid leaving the tank is given by: <br /> <br /> .. figure:: images/CSTR2_1feed_jacket.png<br /> :scale: 76<br /> :align: center<br /> <br /> .. math::<br /> <br /> \frac{F^{\rm in}}{V}[T^{\rm in}-T] - \frac{UA_s}{\rho\,c_p\,V}[T - T_j] + \frac{2\,k_0\,(-\Delta H_r)}{\rho\,c_p}\,C_{\sf A}^2\,e^{\displaystyle -\frac{E_a}{RT}} &amp;= 0<br /> <br /> After substituting in relevant values for the constant physical properties, and fixing the value of :math:C_{\sf A}, the equation can be reduced to:<br /> <br /> .. math::<br /> <br /> 2.6 - 1.45 T + 5 \times 10^{5} e ^{-\tfrac{2000}{T}} &amp;= 0<br /> <br /> Please perform 2 iterations of the Newton-Raphson algorithm for finding the roots of a nonlinear equation. A reasonable starting guess for :math:T^{(0)} is the same temperature as the inlet stream: i.e. :math:T^{(0)} = T^{\rm in} = 290 {\rm K}. Note: you do not need to derive the energy-balance equation above.<br /> <br /> Solution<br /> ----------<br /> <br /> We write the Newton-Raphson method to solve for the unknown temperature :math:T as:<br /> <br /> .. math::<br /> <br /> T_{n+1} = T_n -\frac{f(T_n)}{f'(T_n)}<br /> <br /> where :math:f(T_n) and :math:f'(T_n) are evaluated as follows:<br /> <br /> .. math::<br /> <br /> f(T) &amp;= 2.6 - 1.45 T + 5 \times 10^{5} e ^{-\tfrac{2000}{T}} \\ f'(T) &amp;= -1.45 - 5 \times 10^5 \times \left(-\frac{2000}{T^2}\right) e ^{-\tfrac{2000}{T}}<br /> <br /> * :math:T^{(0)} = 290.0 K<br /> * :math:f(290) = 87.73, and that :math:f'(290) = 10.57, so :math:\Delta T = \displaystyle -\frac{f(290)}{f'(290)} = \frac{87.73}{10.57}.<br /> * :math:T^{(1)} = T^{(0)} + \Delta T = 290.0 - 8.296 = 281.7 K<br /> * :math:f(281.7) = 6.79, and that :math:f'(281.7) = 8.95, so :math:\Delta T = \displaystyle -\frac{f(281.7)}{f'(281.7)} = \frac{6.79}{8.95}.<br /> * :math:T^{(2)} = T^{(1)} + \Delta T = 281.7 - 0.76 = 280.9 K<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.5cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Assignment_5_-_2010_-_Solution&diff=1103 Assignment 5 - 2010 - Solution 2018-09-16T09:01:24Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 17 November 2010<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = Assignment-5-2010.pdf<br /> | questions_text_alt = Assignment questions<br /> | solutions_PDF = Assignment-5-2010-Solutions.pdf<br /> | solutions_text_alt = Solutions, prepared by Ali and Kevin<br /> | other_instructions = Hand-in at class.<br /> }}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> .. |m3| replace:: m\ :sup:3<br /> <br /> Question 1 [1.5]<br /> =================<br /> <br /> Richardson's extrapolation allows us to accelerate convergence. The technique was used to decrease the error for the forward difference approximation for numerical differentiation. <br /> <br /> Using Richardson's extrapolation technique, but this time with the central difference approximation -- an :math:O(h^2) method -- derive an improved (accelerated accuracy) approximation. What is the order of this approximation?<br /> <br /> Solution<br /> --------<br /> <br /> The central difference approximation was derived from the Taylor Series expansion:<br /> <br /> .. math::<br /> <br /> f'(x_0+h) &amp;= f(x_0) + f'(x_0)h + \frac{f''(x_0)h^2}{2} + \frac{f'''(x_0)h^3}{6} + \frac{f''''(x_0)h^4}{24} + O(h^5) \\ f'(x_0-h) &amp;= f(x_0) - f'(x_0)h + \frac{f''(x_0)h^2}{2} - \frac{f'''(x_0)h^3}{6} + \frac{f''''(x_0)h^4}{24} + O(h^5)<br /> <br /> Subtracting one equation from the other, and simplifying:<br /> <br /> .. math::<br /> <br /> f'(x_0) &amp;= \frac{f(x_0+h) - f(x_0-h)}{2h} + \frac{f'''(x_0)}{12}h^2 + O(h^4) \\<br /> <br /> <br /> So the central difference approximate is an O(h^2) approximation, but this series is only a function of the *even* powers of :math:h. So applying Richardson's extrapolation, we may write:<br /> <br /> .. math::<br /> <br /> \begin{array}{rll} \text{A} \qquad f'(x_0) &amp;= \displaystyle \frac{f(x_0+h) - f(x_0-h)}{2h} &amp;+ c_2h^2 + O(h^4) \\ &amp;= Q_2(h) &amp;+ c_2h^2 + O(h^4) \\ \text{B} \qquad f'(x_0) &amp;= \displaystyle \frac{f(x_0+h/2) - f(x_0-h/2)}{h} &amp;+ \displaystyle c_2\frac{h^2}{4} + O(h^4) \\ &amp;= Q_2(h/2) &amp;+ \displaystyle c_2\frac{h^2}{4} + O(h^4) \end{array}<br /> <br /> Subtracting: :math:4\text{B} - \text{A}:<br /> <br /> .. math::<br /> <br /> 3f'(x_0) &amp;= 4Q_2(h/2) - Q_2(h) + O(h^4) \\ f'(x_0) &amp;= \displaystyle \frac{4Q_2(h/2) - Q_2(h)}{3} + O(h^4) \\ f'(x_0) &amp;= Q_4(h) + O(h^4)<br /> <br /> So this improved (accelerated) approximation for :math:f'(x) will be of the order :math:O(h^4).<br /> <br /> .. raw:: latex<br /> <br /> \newpage<br /> <br /> Question 2 <br /> ==============<br /> <br /> .. note:: Adapted from the course textbook, problem 24.11<br /> <br /> A colleague has designed a new transdermal patch to deliver insulin through the skin to diabetic patients, eliminating the need for painful injections. She has collected the following data on the mass flux, :math:f, of insulin being delivered through the patch (and skin), as a function of time, :math:t. [Mass flux is the flow rate through a unit area.]<br /> <br /> ================================== === === === === === === === === === === === <br /> :math:t [hour] 0 1 2 3 4 5 6 10 14 18 24 <br /> ---------------------------------- --- --- --- --- --- --- --- --- --- --- --- <br /> :math:f [mg/(cm\ :sup:2.hr)] 15 14 12 11 9 8 7.2 5 2.5 2 1 <br /> ================================== === === === === === === === === === === === <br /> <br /> #. Provide an estimate, using the trapezoidal rule, of the mass of drug delivered to the patient over a 24 hour period with a patch of 10 cm\ :sup:2.<br /> <br /> #. Can you provide an alternate, more accurate estimate, using another technique we learned about? If so, provide the answer; if not, explain why.<br /> <br /> Solution<br /> -----------<br /> <br /> Note that not all the points are equally spaced. From :math:t=0 to :math:t=6 the step size is :math:h=1. From :math:t=6 to :math:t=18 the step size is :math:h=4. And for the last two points, :math:h=6. So, we can apply the composite trapezoidal to each of these three partitions:<br /> <br /> .. math::<br /> <br /> \begin{array}{llrl} \text{Partition 1}&amp; h=1: &amp; I_1=\int_{0}^{6}{f(t)dt}&amp;\approx \frac{1}{2}(f(0)+2f(1)+2f(2)+2f(3)+2f(4)+2f(5)+f(6))\\ &amp;&amp;&amp;=\frac{1}{2}(15+2(14) +2(12)+2(11)+2(9)+2(8)+7.2) = 65.10\\ \text{Partition 2}&amp; h=4: &amp; I_2=\int_{6}^{18}{f(t)dt}&amp;\approx \frac{4}{2}(f(6)+2f(10)+2f(14)+f(18))\\ &amp;&amp;&amp;=\frac{4}{2}(7.2+2(5)+2(2.5)+2)=48.40\\ \text{Partition 3}&amp; h=6: &amp; I_3=\int_{18}^{24}{f(t)dt}&amp;\approx \frac{6}{2}(f(18)+f(24))\\ &amp;&amp;&amp;=\frac{6}{2}(2+1)=9\\ &amp;&amp;\int_{0}^{24}{f(t)dt} &amp;\approx I_1+I_2+I_3=\textbf{122.50} \end{array}<br /> <br /> Note the above result is the mass flux. To obtain the mass, we multiply this by the surface area: :math:M=122.50\times 10=1225.00 <br /> <br /> A more accurate result can be obtained by using Simpson's rule, where possible, instead of the trapezoidal rule. Simpson's 1/3 rule can be applied on every three points if they are equally spaced. According to the given table, we can apply composite Simpson's 1/3 rule from :math:t=0 to :math:t=6 and use Simpson's 3/8 rule from :math:t=6 to :math:t=18. For the last two points, we again use the trapezoidal method. Summing the results of the above partitions we get:<br /> <br /> .. math::<br /> <br /> \begin{array}{llrl} \text{Partition 1}&amp; h=1:&amp; I_1= \int_{0}^{6}{f(t)dt}&amp;\approx\frac{1}{3}(f(0)+4f(1)+2f(2)+4f(3)+2f(4)+4f(5)+f(6))\\ &amp;&amp;&amp;=\frac{1}{3}(15+4(14) +2(12)+4(11)+2(9)+4(8)+7.2)=65.40 \\ \text{Partition 2}&amp; h=4:&amp; I_2=\int_{6}^{18}{f(t)dt}&amp;\approx\frac{3\times 4}{8}(f(6)+3f(10)+3f(14)+f(18))\\ &amp;&amp;&amp;=\frac{3\times 4}{8}(7.2+3(5)+3(2.5)+2)=47.55\\ \text{Partition 3}&amp; h=6: &amp; I_3=\int_{18}^{24}{f(t)dt}&amp;\approx \frac{6}{2}(f(18)+f(24))\\ &amp;&amp;&amp;=\frac{6}{2}(2+1)=9\\ &amp;&amp;\int_{0}^{24}{f(t)dt}&amp;\approx I_1+I_2+I_3=\textbf{121.95} \end{array}<br /> <br /> Again, we multiply the above value by 10 cm\ :sup:2 to get the mass of the drug: :math:M=121.95\times 10=1219.50 <br /> <br /> .. raw:: latex<br /> <br /> \newpage<br /> <br /> Question 3 <br /> ==============<br /> <br /> .. note:: This question is a little unconventional; but we strongly recommended you struggle with it, without asking for help from TA's or other group members.<br /> <br /> The following reaction is taking place in a large, well-mixed reactor: :math:{\sf N} \rightarrow {\sf B} with rate expression:<br /> <br /> .. math::<br /> <br /> r = \displaystyle \frac{k_1 C_{\sf N}}{k_2 + C_{\sf N}} \qquad\qquad\,\, k_1 = 30.0\,\,\text{g/(m$^3$.day)}\qquad\qquad\,\, k_2 = 20.4\,\,\text{g/m$^3$}<br /> <br /> #. Derive a dynamic mass balance for species :math:{\sf N}, given that:<br /> <br /> * the flow into the large tank is :math:F^\text{in}(t) = 500 m\ :sup:3/day<br /> * the inlet stream only contains species :math:{\sf N}, approximately constant at 100 g/m\ :sup:3 <br /> * the tank is operated at constant volume, :math:V = 2000 m\ :sup:3<br /> <br /> #. Using techniques learned in this course up to 11 November, calculate the concentration of :math:{\sf N} as a function of time over the first 3 days (roughly), as the reactor is starting up. The tank was initially filled to 2000 m\ :sup:3 with material at a concentration of :math:C_{\sf N} = 100 g/m\ :sup:3. <br /> <br /> Show your calculations, source code and plots. Please do not simply attach your source code in an appendix; your code should be worked into your solution, as part of your explanations. Also note that you should **not** use methods such as Euler's methods, or MATLAB/Python functions such as scipy.integrate.ode or ode45.<br /> <br /> Solution<br /> ---------<br /> <br /> The main purpose of this question was for you to struggle with the concept of time-based functions, derivatives and integrals in the expectation that when you see ODEs in part F of the course that they will make sense. Any serious attempt at this question will get a :math:\beta, while any solutions that calculate a reasonable plot of concentration over time, with concentration decreasing in time will get an :math:\alpha.<br /> <br /> #. The species mass balance for :math:{\sf N} is:<br /> <br /> * In = :math:C_{\sf N}^\text{in}(t)F^\text{in}(t) with units of g/day<br /> * Out = :math:C_{\sf N}(t)F^\text{in}(t)<br /> * Depletion of :math:{\sf N} = :math:- \displaystyle \frac{k_1 C_{\sf N}(t)}{k_2 + C_{\sf N}(t)} V(t) also with units of g/day<br /> * Accumulation = :math:\displaystyle \frac{V(t)C_{\sf N}(t)}{dt} with units of g/day<br /> <br /> Assumptions made in the above are that the tank is well mixed and that the inflow and outflow are the same, which should be true if operating at constant volume. In the bullet points we had all the terms as a function of time. When forming the dynamic balance below we have removed the time-dependence from any terms which are constant.<br /> <br /> .. math::<br /> <br /> \frac{d\left[V(t)C_{\sf N}(t)\right]}{dt} = V\frac{dC_{\sf N}(t)}{dt} &amp;= F^\text{in}\left[C_{\sf N}^\text{in} - C_{\sf N}(t) \right] - \frac{k_1 C_{\sf N}(t)}{k_2 + C_{\sf N}(t)} V \\ \frac{dC_{\sf N}(t)}{dt} &amp;= \frac{F^\text{in}}{V}\left[C_{\sf N}^\text{in} - C_{\sf N}(t) \right] - \frac{k_1 C_{\sf N}(t)}{k_2 + C_{\sf N}(t)} <br /> <br /> <br /> #. Integrating the dynamic balance between time :math:t=0 and :math:t=3 days, and initial concentration :math:C_{\sf N}(t=0) = 100 g/m\ :sup:3 can be expressed as:<br /> <br /> .. math::<br /> <br /> \int_{100}^{C_{\sf N}(3)}{f(C_{\sf N}) dC_{\sf N}} &amp;= \int_{0}^{3} dt \\ \int_{100}^{C_{\sf N}(3)}{ \left[\frac{F^\text{in}}{V}\left[C_{\sf N}^\text{in} - C_{\sf N}(t) \right] - \frac{k_1 C_{\sf N}(t)}{k_2 + C_{\sf N}(t)} \right]^{-1} dC_{\sf N}} &amp;= 3.0<br /> <br /> This is not easy to integrate analytically, so the tools we have learned in the course can be used now to approximate the integral on the left hand side starting from :math:C_{\sf N}(t=0) = 100 g/m\ :sup:3 up to some unknown concentration, :math:C_{\sf N}(3) g/m\ :sup:3. We will add to the approximation until the left integral is equal to right integral of 3.0 days. This process seems suited to using the trapezoidal rule, or Simpson's method. We can use panels to approximate the integral, and keep adding panels until our approximation to the integral is roughly 3.0 days. <br /> <br /> Either way, we need to decide where to place our divisions to form the integrating panels. Do we add them to the left or to the right of the starting point :math:C_{\sf N}(t=0) = 100 g/m\ :sup:3? To help us decide this, we can evaluate the derivative at time :math:t=0 days:<br /> <br /> .. math::<br /> <br /> \frac{C_{\sf N}(t)}{dt} &amp;= \frac{500.0}{2000.0}\left[100 - 100\right] - \frac{30 \times 100}{20.4 + 100.0} \\ &amp;= 0 - 24.9\,\, \text{g/(m$^3$.day)}<br /> <br /> This gives two pieces of information. Firstly, that the concentration will decrease by about 25 units per day at the startup of the reactor. Secondly it helps us decide on the number of panels. We can use panels about 5 units wide, giving about 5 divisions per day. i.e. :math:C_{\sf N} = [100, 95, 90, 85, \ldots ]. So we will keep adding panels until the approximation is roughly equal to 3.0 days.<br /> <br /> Using the trapezoidal approximation:<br /> <br /> * From :math:C_{\sf N} = 100 to 95 g/m\ :sup:3: area = :math:\frac{(95 - 100)}{2} \cdot \left[f(95)+f(100)\right] = :math:\frac{-5}{2} \cdot \left[-0.04013 - -0.04265 \right] = 0.20695, total area so far = 0.21 days.<br /> * From :math:C_{\sf N} = 95 to 90 g/m\ :sup:3: area = :math:\frac{(90 - 95)}{2} \cdot \left[f(90)+f(95)\right] = :math:\frac{-5}{2} \cdot \left[-0.04265 - -0.04554 \right] = 0.2205, total area so far = 0.43 days.<br /> * From :math:C_{\sf N} = 90 to 85 g/m\ :sup:3: area = 0.2361, area so far = 0.6636 days.<br /> * From :math:C_{\sf N} = 85 to 80 g/m\ :sup:3: area = 0.2545, area so far = 0.9181 days.<br /> * From :math:C_{\sf N} = 80 to 75 g/m\ :sup:3: area = 0.2765, area so far = 1.195 days (which we expected from our initial derivative)<br /> * From :math:C_{\sf N} = 75 to 70 g/m\ :sup:3: area = 0.3031, area so far = 1.498 days.<br /> * From :math:C_{\sf N} = 70 to 65 g/m\ :sup:3: area = 0.3364, area so far = 1.834 days.<br /> * From :math:C_{\sf N} = 65 to 60 g/m\ :sup:3: area = 0.3793, area so far = 2.213 days.<br /> * From :math:C_{\sf N} = 60 to 55 g/m\ :sup:3: area = 0.4369, area so far = 2.650 days.<br /> * From :math:C_{\sf N} = 55 to 50 g/m\ :sup:3: area = 0.5190, area so far = 3.169 days.<br /> <br /> The following code was used:<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-5/code/integrate_q3.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-5/code/integrate_q3.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> So we now have the concentrations in the tank as a function of time, over a period of 3 days. We can plot these as shown below (the calculations above are to the left of the red line). The points beyond the red line were calculated when continuing beyond 3 days.<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-5/images/assign5_q3_plot.png<br /> :scale: 55<br /> :align: center<br /> <br /> The approximation error will obviously get worse and worse with this crude panel size, so we should start using a smaller panel size as we keep going.<br /> <br /> The same approach as above could have been performed using Simpson's 1/3 rule for more accurate estimates.<br /> <br /> Question 4 [1.5]<br /> =================<br /> <br /> .. note:: From the 2009 final exam, 3 points out of 50; 3 hours.<br /> <br /> You are given a set of algorithms for numerical integration. You are also given a set of problems. You are to match every problem (1, 2, 3) with an algorithm (A, B, or C). You may use each algorithm **only once**. Motivate each of your answers **carefully**.<br /> <br /> **Problem**<br /> <br /> **1**: Determine the integral of a very nonlinear function with high accuracy. <br /> <br /> **2**: Determine the integral of a computationally expensive, black-box function with reasonable accuracy.<br /> <br /> **3**: Determine the integral of a cubic model for a pure component's specific heat capacity: e.g. :math:C_p(T) = a + bT + cT^2 + dT^3<br /> <br /> **Algorithms**<br /> <br /> **A**: Simpson's 1/3 rule<br /> <br /> **B**: Romberg method<br /> <br /> **C**: 4-point Gauss quadrature<br /> <br /> Solution<br /> ---------<br /> <br /> Most of the grade in this question is for your justification. Below is one potential set of links:<br /> <br /> **1**: Determine the integral of a very nonlinear function with high accuracy: use Romberg-method; because Romberg builds on the trapezoidal method, potentially many function evaluations are made. The Romberg method improves its accuracy by order :math:O(h^2p) in every row in the Romberg table. So just 4 rows in the table would have an error of :math:O(h^8). We can guarantee the error, even if the function is nonlinear.<br /> <br /> **2**: Determine the integral of a computationally expensive, black-box function with reasonable accuracy: use 4-point Gauss quadrature; because Gauss quadrature has low error for very few function evaluations. A 4-point quadrature would be exact for 7th-order polynomials, so we would get very good accuracy with only 4 function evaluations.<br /> <br /> **3**: Determine the integral of a cubic model for a pure component's specific heat capacity: e.g. :math:C_p(T) = a + bT + cT^2 + dT^3: I would use Simpson's 1/3 method because it is exact for cubic polynomials and lower - so we would get an exact answer for this type of polynomial.<br /> <br /> <br /> Question 5 <br /> ===============<br /> <br /> .. note:: From the 2009 final exam, 2+1.5 points, out of 50; 3 hours. There were other parts to the question, but they are not relevant here.<br /> <br /> Consider a CSTR, fed with one inlet stream at volumetric flow rate :math:F^{\rm in} and containing species :math:{\sf A} at concentration :math:C_{\sf A}^{\rm in}=1 mol/L. There is no outlet from the reactor. A fluid of constant density is considered throughout this question.<br /> <br /> The reactor is initially empty, and the fluid is fed into the tank via the inlet stream at the volumetric flow rate given by :math:F^{\rm in}(t) = 1-\exp(-t) [in m\ :sup:3/min].<br /> <br /> #. Determine the volume of liquid :math:V(t) accumulated at :math:t_1=5 min using Romberg integration (perform **3 steps** of Romberg's method; that is, 3 columns in the graphical depiction).<br /> <br /> #. Repeat the calculation using two-point Gauss quadrature, and compare the results. Which method is likely to provide the best estimate?<br /> <br /> <br /> Solution<br /> --------<br /> <br /> This question asks to evaluate the cumulative volume in the tank after a certain time:<br /> <br /> .. math::<br /> <br /> \frac{dV}{dt} &amp;= F^{\rm in}(t) \\ \int_{0}^{V}{dV} &amp;= V = \int_{0}^{t}{F(t) dt} = \int_{0}^{t=5}{1-e^{-t} dt}<br /> <br /> #. Using Romberg integration:<br /> <br /> The Romberg table can be constructed as follows:<br /> <br /> .. math::<br /> <br /> \begin{array}{llll} h &amp; I_1, O(h^2) &amp; I_2, O(h^4) &amp; I_3, O(h^6) \\ \hline h=5 &amp; I_1(5) = 2.4832 &amp; \\ h=2.5 &amp; I_1(2.5) = 3.5364 &amp; I_2(2.5) = 3.8874 &amp;\\ h=1.25 &amp; I_1(1.25) = 3.8807 &amp; I_2(1.25) = 3.9955 &amp; I_3(1.25) = 4.0027\\ \hline \end{array}<br /> <br /> The following expanded entries were used in the above table:<br /> <br /> * :math:I_1(5) = \frac{5}{2}\left[f(0)+f(5)\right] = 2.4832<br /> * :math:I_1(2.5) = \frac{5}{2 \times 2}\left[f(0)+2f(2.5) + f(5)\right] = 3.5364<br /> * :math:I_1(1.25) = \frac{5}{4 \times 2}\left[f(0)+ 2f(1.25) + 2f(2.5) + 2f(3.75) + f(5)\right] = 3.8807<br /> * :math:I_2(2.5) = \frac{4I_1(2.5) - I_1(5)}{4-1} = \frac{4 \times 3.5364 - 2.4832}{3} = 3.8875 using the messy formula with :math:j=1 and :math:k=1<br /> * :math:I_2(1.25) = \frac{4I_1(1.25) - I_1(2.5)}{4-1} = \frac{4 \times 3.8807 - 3.5364}{3} = 3.9955 with :math:j=1 and :math:k=2<br /> * :math:I_3(1.25) = \frac{16I_2(1.25) - I_2(2.5)}{16-1} = \frac{16 \times 3.9955 - 3.8875}{15} = 4.0027 with :math:j=2 and :math:k=2<br /> <br /> The final estimate for the integral is the last column in the last row, so :math:I_3(1.25) = 4.003<br /> <br /> #. Using Gauss quadrature: our function to be integrated is :math:f(t) = 1-e^{-t} between limits :math:a=0 and :math:b=5. Since Gaussian quadrature is integrated between -1 and +1, our formula for the change in variables is:<br /> <br /> .. math::<br /> <br /> t &amp;= \frac{b+a}{2} + \frac{b-a}{2}\xi \\ t &amp;= \frac{5}{2} + \frac{5}{2}\xi~~\text{where}\,\,-1 \leq \xi \leq +1<br /> <br /> .. math::<br /> <br /> I_2 &amp;= \omega_1 f(\xi_1) + \omega_2 f(\xi_2)\\ &amp;= \bigg[\omega_1 f(t_1) + \omega_2 f(t_2)\bigg] \frac{dt}{d\xi}\\ &amp;= \bigg[1.0 f\left(\frac{5}{2} - \frac{5}{2}\cdot\frac{1}{\sqrt{3}}\right) + 1.0 f\left(\frac{5}{2} + \frac{5}{2}\cdot\frac{1}{\sqrt{3}}\right)\bigg] \frac{5}{2}\\ &amp;= \bigg[1.0 f(1.0566) + 1.0 f(3.9434)\bigg] \frac{5}{2}\\ &amp;= \bigg[0.6524 + 0.9806 \bigg] \frac{5}{2}\\ &amp;= 4.083<br /> <br /> Romberg quadrature taken to three rows will have error in the order of :math:O(h^6), while 2-point Gaussian quadrature will be exact for polynomials of order 3 and below: i.e. the error of Gaussian quadrature will be some function of the fourth derivative of :math:f(t). Errors from Gaussian quadrature are not a function of :math:h, since there is no :math:h in Gaussian quadrature. Since the function we are integrating is not polynomial, I would expect error in both methods.<br /> <br /> The true value of the integral is easy to find: :math:\int_{0}^{t=5}{1-e^{-t} dt} = t + e^{-t}\big|_{0}^{5} = 4 + e^{-5} = 4.0067379; using this we can see the Romberg method had lower error.<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.5cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> <br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Written_midterm&diff=1102 Written midterm 2018-09-16T09:01:01Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 18 October 2010<br /> | dates_alt_text = Date<br /> | questions_PDF = Midterm-ChE3E4-2010.pdf<br /> | questions_text_alt = Question paper<br /> | solutions_PDF = <br /> | solutions_text_alt = Solutions by Ali, Elliot and Kevin<br /> | other_instructions = <br /> }}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> <br /> .. rubric:: Special instructions<br /> <br /> - You may bring in any printed materials to the final; **any** textbooks, **any** papers, *etc*.<br /> - You may use any calculator during the test.<br /> - You may not use a cellphone as a calculator. Nor may you use any other communication device (web, email, chat, etc) during the test.<br /> - You may not use any type of computer.<br /> - You may answer the questions in any order in the answer booklet.<br /> - The test covers all material taught so far in the course.<br /> - Make sure that you provide explanations with your calculations.<br /> - If you are not sure about the meaning of a problem, please write out your interpretation and follow through with the calculation.<br /> - If you are short of time, please write what you would have done in this test, if you had more time.<br /> - There are a total of **45 marks** available.<br /> <br /> .. raw:: latex<br /> <br /> \pagestyle{plain}<br /> \vspace*{0.2cm}<br /> \hrule<br /> \vspace*{0.2cm}<br /> \newpage<br /> <br /> .. Chemical engineering modelling <br /> .. Roundoff and computer representation <br /> .. Linear algebraic equations <br /> .. Taylor-series expansion <br /> .. Non-linear equation solution <br /> <br /> Question 1 <br /> ===============<br /> <br /> Consider the following 3 reactions taking place in a liquid-based system: a continuously stirred tank reactor (CSTR).<br /> <br /> * :math:{\sf A} \stackrel{r_1}{\rightarrow} {\sf 2B}, where :math:r_1 = k_1 C_{\sf A}<br /> * :math:{\sf A} \displaystyle \mathop{\rightleftharpoons}_{r_3}^{r_2} {\sf C}, where :math:r_2=k_2 C_{\sf A}^{1.5} and :math:r_3 = k_3 C_{\sf C}^2<br /> * :math:{\sf 2B} \stackrel{r_4}{\rightarrow} {\sf 3D + C}, where :math:r_4 = k_4 C_{\sf B}^2 <br /> <br /> Derive the equations that can be solved for the steady state concentrations of :math:\sf A, B, C and :math:\sf D. You may assume that species :math:\sf A is fed to the reactor in a single stream, flowing in at :math:Q \text{m}^3/\text{s}, with a concentration of :math:C_{\sf A}^{\text{in}}. Please state all other assumptions that you make while answering the question.<br /> <br /> Note: please **do not simplify** your equations.<br /> <br /> Question 2 <br /> ==============<br /> <br /> Consider the system in question 1 again, but modified now to be a semi-batch system - material only enters the reactor, but nothing leaves. Continue on from question 1, using any equations derived there and any assumptions stated there, and derive the dynamic balances **only for species** :math:C_{\sf A} **and** :math:C_{\sf D}.<br /> <br /> Question 3 <br /> ==============<br /> <br /> Represent the decimal number, -1.0, in an 8-bit word that uses 1 bit for the sign, 3 bits for the signed exponent and the remaining bits for the significand. Normalization must be used for the significand and only zero or one entries can used in each bit position.<br /> <br /> Question 4 [4 = 2 + 1 + 1]<br /> ===========================<br /> <br /> 4.1 A linearized system of equations, :math:Ax = b, has been constructed for a very high accuracy model of a distillation column. There are at most 12,000 equations in the system. What is the size, in megabytes, required to store :math:A at double precision? <br /> <br /> 4.2 Can this matrix be stored in 1 gigabyte of RAM?<br /> <br /> 4.3 The matrix :math:A is sparse (i.e. consists mostly of zeros). The number of non-zero elements is about 16,000. How much RAM, in megabytes, would be required to only store the non-zero entries?<br /> <br /> Question 5 [11 = 2 + 2 + 2 + 5]<br /> ========================================<br /> <br /> This questions considers a simple model of a 3-stage absorber (a similar model was used in assignment 2 in the course, but it is not exactly the same as the model used here). <br /> <br /> An illustration of the system, and the steady-state equations for system can be shown to be:<br /> <br /> .. figure:: images/absorber-column-midterm.png<br /> :scale: 30<br /> :align: center<br /> <br /> .. math::<br /> <br /> y_F &amp;= 0.5\\ \frac{1}{\beta} y_F - (\delta+1)x_1 + \delta x_2 &amp;= 0 \\ x_1 - (\delta+1)x_2 + \delta x_3 &amp;= 0 \\ x_2 - (\delta+1)x_3 + \delta x_F &amp;= 0 \\ x_F &amp;= 0.1 <br /> <br /> where :math:\beta is a coefficient, assumed constant throughout the absorber tower, that relates the liquid phase composition, :math:x_n, to the gas-phase composition, :math:y_n = \beta x_n, assuming of course that equilibrium is achieved in each stage. <br /> <br /> The :math:\delta coefficient is a dimensionless number defined as a function of the molar gas and liquid flows in the column, and :math:\beta, so that :math:\delta = \displaystyle \frac{L}{G \beta}<br /> <br /> **5.1** Write the given steady-state equations in the form :math:A{\bf x} = b where :math:{\bf x} = \left[y_F, x_1, x_2, x_3, x_F\right]. Report the :math:A matrix and :math:b vector.<br /> <br /> **5.2** What condition(s) must be satisfied so that matrix :math:A is diagonally dominant?<br /> <br /> **5.3** You decide to use the Gauss-Seidel method to solve this system of equations. Also, you are given that :math:L/G = 1.5, and :math:\beta = 0.8. Is this method guaranteed to converge for these coefficient values?<br /> <br /> **5.4** Perform one iteration of the Gauss-Seidel method, starting from a suitable initial guess that you believe will converge in fewer iterations than simply using the default guess of :math:{\bf x} = \left[y_F, x_1, x_2, x_3, x_F\right] = [0, 0, 0, 0, 0]. Use :math:L/G = 1.5, and :math:\beta = 0.8 in your matrix, and briefly explain your choice for the initial guess.<br /> <br /> Question 6 [5 = 4 + 1]<br /> ========================================<br /> <br /> **6.1** When implementing either the Gauss-Seidel method, or the Jacobi method to solve **any** system of equations, :math:A{\bf x} = b, you can experience trouble with convergence. What are 2 options you can implement to try obtain convergence with these methods? Clearly explain what each option does to try achieve convergence.<br /> <br /> **6.2** The Gauss elimination process to solve the system of equations, :math:A{\bf x} = b uses partial pivoting. Please give a reason why this partial pivoting process is performed.<br /> <br /> Question 7 <br /> ================<br /> <br /> In solving the equation, :math:f(x)=0 for a single variable, :math:x, how many steps of the bisection method are necessary to guarantee the solution is within an interval of width :math:\varepsilon = 2 \times 10^{-6}, starting with an interval that is 10 units wide?<br /> <br /> Question 8 <br /> ===================<br /> <br /> Consider the modelling of a jacketed CSTR, fed with a single inlet stream. Under some fairly straightforward assumptions, one can show that the steady state temperature of the fluid leaving the tank is given by: <br /> <br /> .. figure:: images/CSTR2_1feed_jacket.png<br /> :scale: 76<br /> :align: center<br /> <br /> .. math::<br /> <br /> \frac{F^{\rm in}}{V}[T^{\rm in}-T] - \frac{UA_s}{\rho\,c_p\,V}[T - T_j] + \frac{2\,k_0\,(-\Delta H_r)}{\rho\,c_p}\,C_{\sf A}^2\,e^{\displaystyle -\frac{E_a}{RT}} = 0 \\ \text{After substituting in relevant values for the constant physical properties, and fixing the value of $C_{\sf A}$, the equation can be reduced to:}&amp; \\ 2.6 - 1.45 T + 5 \times 10^{5} e ^{-\tfrac{2000}{T}} = 0<br /> <br /> Please perform 2 iterations of the Newton-Raphson algorithm for finding the roots of a nonlinear equation. A reasonable starting guess for :math:T^{(0)} is the same temperature as the inlet stream: i.e. :math:T^{(0)} = T^{\rm in} = 290 {\rm K}. Note: you do not need to derive the energy-balance equation above.<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.5cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Written_midterm&diff=1101 Written midterm 2018-09-16T09:00:42Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 18 October 2010<br /> | dates_alt_text = Date<br /> | questions_PDF = Midterm-ChE3E4-2010.pdf<br /> | questions_text_alt = Question paper<br /> | solutions_PDF = <br /> | solutions_text_alt = Solutions by Ali, Elliot and Kevin<br /> | other_instructions = <br /> }}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> <br /> .. rubric:: Special instructions<br /> <br /> - You may bring in any printed materials to the final; **any** textbooks, **any** papers, *etc*.<br /> - You may use any calculator during the test.<br /> - You may not use a cellphone as a calculator. Nor may you use any other communication device (web, email, chat, etc) during the test.<br /> - You may not use any type of computer.<br /> - You may answer the questions in any order in the answer booklet.<br /> - The test covers all material taught so far in the course.<br /> - Make sure that you provide explanations with your calculations.<br /> - If you are not sure about the meaning of a problem, please write out your interpretation and follow through with the calculation.<br /> - If you are short of time, please write what you would have done in this test, if you had more time.<br /> - There are a total of **45 marks** available.<br /> <br /> .. raw:: latex<br /> <br /> \pagestyle{plain}<br /> \vspace*{0.2cm}<br /> \hrule<br /> \vspace*{0.2cm}<br /> \newpage<br /> <br /> .. Chemical engineering modelling <br /> .. Roundoff and computer representation <br /> .. Linear algebraic equations <br /> .. Taylor-series expansion <br /> .. Non-linear equation solution <br /> <br /> Question 1 <br /> ===============<br /> <br /> Consider the following 3 reactions taking place in a liquid-based system: a continuously stirred tank reactor (CSTR).<br /> <br /> * :math:{\sf A} \stackrel{r_1}{\rightarrow} {\sf 2B}, where :math:r_1 = k_1 C_{\sf A}<br /> * :math:{\sf A} \displaystyle \mathop{\rightleftharpoons}_{r_3}^{r_2} {\sf C}, where :math:r_2=k_2 C_{\sf A}^{1.5} and :math:r_3 = k_3 C_{\sf C}^2<br /> * :math:{\sf 2B} \stackrel{r_4}{\rightarrow} {\sf 3D + C}, where :math:r_4 = k_4 C_{\sf B}^2 <br /> <br /> Derive the equations that can be solved for the steady state concentrations of :math:\sf A, B, C and :math:\sf D. You may assume that species :math:\sf A is fed to the reactor in a single stream, flowing in at :math:Q \text{m}^3/\text{s}, with a concentration of :math:C_{\sf A}^{\text{in}}. Please state all other assumptions that you make while answering the question.<br /> <br /> Note: please **do not simplify** your equations.<br /> <br /> Question 2 <br /> ==============<br /> <br /> Consider the system in question 1 again, but modified now to be a semi-batch system - material only enters the reactor, but nothing leaves. Continue on from question 1, using any equations derived there and any assumptions stated there, and derive the dynamic balances **only for species** :math:C_{\sf A} **and** :math:C_{\sf D}.<br /> <br /> Question 3 <br /> ==============<br /> <br /> Represent the decimal number, -1.0, in an 8-bit word that uses 1 bit for the sign, 3 bits for the signed exponent and the remaining bits for the significand. Normalization must be used for the significand and only zero or one entries can used in each bit position.<br /> <br /> Question 4 [4 = 2 + 1 + 1]<br /> ===========================<br /> <br /> 4.1 A linearized system of equations, :math:Ax = b, has been constructed for a very high accuracy model of a distillation column. There are at most 12,000 equations in the system. What is the size, in megabytes, required to store :math:A at double precision? <br /> <br /> 4.2 Can this matrix be stored in 1 gigabyte of RAM?<br /> <br /> 4.3 The matrix :math:A is sparse (i.e. consists mostly of zeros). The number of non-zero elements is about 16,000. How much RAM, in megabytes, would be required to only store the non-zero entries?<br /> <br /> Question 5 [11 = 2 + 2 + 2 + 5]<br /> ========================================<br /> <br /> This questions considers a simple model of a 3-stage absorber (a similar model was used in assignment 2 in the course, but it is not exactly the same as the model used here). <br /> <br /> An illustration of the system, and the steady-state equations for system can be shown to be:<br /> <br /> .. figure:: images/absorber-column-midterm.png<br /> :scale: 30<br /> :align: center<br /> <br /> .. math::<br /> <br /> y_F &amp;= 0.5\\<br /> \frac{1}{\beta} y_F - (\delta+1)x_1 + \delta x_2 &amp;= 0 \\<br /> x_1 - (\delta+1)x_2 + \delta x_3 &amp;= 0 \\<br /> x_2 - (\delta+1)x_3 + \delta x_F &amp;= 0 \\<br /> x_F &amp;= 0.1 <br /> <br /> where :math:\beta is a coefficient, assumed constant throughout the absorber tower, that relates the liquid phase composition, :math:x_n, to the gas-phase composition, :math:y_n = \beta x_n, assuming of course that equilibrium is achieved in each stage. <br /> <br /> The :math:\delta coefficient is a dimensionless number defined as a function of the molar gas and liquid flows in the column, and :math:\beta, so that :math:\delta = \displaystyle \frac{L}{G \beta}<br /> <br /> **5.1** Write the given steady-state equations in the form :math:A{\bf x} = b where :math:{\bf x} = \left[y_F, x_1, x_2, x_3, x_F\right]. Report the :math:A matrix and :math:b vector.<br /> <br /> **5.2** What condition(s) must be satisfied so that matrix :math:A is diagonally dominant?<br /> <br /> **5.3** You decide to use the Gauss-Seidel method to solve this system of equations. Also, you are given that :math:L/G = 1.5, and :math:\beta = 0.8. Is this method guaranteed to converge for these coefficient values?<br /> <br /> **5.4** Perform one iteration of the Gauss-Seidel method, starting from a suitable initial guess that you believe will converge in fewer iterations than simply using the default guess of :math:{\bf x} = \left[y_F, x_1, x_2, x_3, x_F\right] = [0, 0, 0, 0, 0]. Use :math:L/G = 1.5, and :math:\beta = 0.8 in your matrix, and briefly explain your choice for the initial guess.<br /> <br /> Question 6 [5 = 4 + 1]<br /> ========================================<br /> <br /> **6.1** When implementing either the Gauss-Seidel method, or the Jacobi method to solve **any** system of equations, :math:A{\bf x} = b, you can experience trouble with convergence. What are 2 options you can implement to try obtain convergence with these methods? Clearly explain what each option does to try achieve convergence.<br /> <br /> **6.2** The Gauss elimination process to solve the system of equations, :math:A{\bf x} = b uses partial pivoting. Please give a reason why this partial pivoting process is performed.<br /> <br /> Question 7 <br /> ================<br /> <br /> In solving the equation, :math:f(x)=0 for a single variable, :math:x, how many steps of the bisection method are necessary to guarantee the solution is within an interval of width :math:\varepsilon = 2 \times 10^{-6}, starting with an interval that is 10 units wide?<br /> <br /> Question 8 <br /> ===================<br /> <br /> Consider the modelling of a jacketed CSTR, fed with a single inlet stream. Under some fairly straightforward assumptions, one can show that the steady state temperature of the fluid leaving the tank is given by: <br /> <br /> .. figure:: images/CSTR2_1feed_jacket.png<br /> :scale: 76<br /> :align: center<br /> <br /> .. math::<br /> <br /> \frac{F^{\rm in}}{V}[T^{\rm in}-T] - \frac{UA_s}{\rho\,c_p\,V}[T - T_j] + \frac{2\,k_0\,(-\Delta H_r)}{\rho\,c_p}\,C_{\sf A}^2\,e^{\displaystyle -\frac{E_a}{RT}} = 0 \\ \text{After substituting in relevant values for the constant physical properties, and fixing the value of $C_{\sf A}$, the equation can be reduced to:}&amp; \\ 2.6 - 1.45 T + 5 \times 10^{5} e ^{-\tfrac{2000}{T}} = 0<br /> <br /> Please perform 2 iterations of the Newton-Raphson algorithm for finding the roots of a nonlinear equation. A reasonable starting guess for :math:T^{(0)} is the same temperature as the inlet stream: i.e. :math:T^{(0)} = T^{\rm in} = 290 {\rm K}. Note: you do not need to derive the energy-balance equation above.<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.5cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Assignment_5_-_2010_-_Solution&diff=1100 Assignment 5 - 2010 - Solution 2018-09-16T08:59:27Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 17 November 2010<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = Assignment-5-2010.pdf<br /> | questions_text_alt = Assignment questions<br /> | solutions_PDF = Assignment-5-2010-Solutions.pdf<br /> | solutions_text_alt = Solutions, prepared by Ali and Kevin<br /> | other_instructions = Hand-in at class.<br /> }}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> .. |m3| replace:: m\ :sup:3<br /> <br /> Question 1 [1.5]<br /> =================<br /> <br /> Richardson's extrapolation allows us to accelerate convergence. The technique was used to decrease the error for the forward difference approximation for numerical differentiation. <br /> <br /> Using Richardson's extrapolation technique, but this time with the central difference approximation -- an :math:O(h^2) method -- derive an improved (accelerated accuracy) approximation. What is the order of this approximation?<br /> <br /> Solution<br /> --------<br /> <br /> The central difference approximation was derived from the Taylor Series expansion:<br /> <br /> .. math::<br /> <br /> f'(x_0+h) &amp;= f(x_0) + f'(x_0)h + \frac{f''(x_0)h^2}{2} + \frac{f'''(x_0)h^3}{6} + \frac{f''''(x_0)h^4}{24} + O(h^5) \\<br /> f'(x_0-h) &amp;= f(x_0) - f'(x_0)h + \frac{f''(x_0)h^2}{2} - \frac{f'''(x_0)h^3}{6} + \frac{f''''(x_0)h^4}{24} + O(h^5)<br /> <br /> Subtracting one equation from the other, and simplifying:<br /> <br /> .. math::<br /> <br /> f'(x_0) &amp;= \frac{f(x_0+h) - f(x_0-h)}{2h} + \frac{f'''(x_0)}{12}h^2 + O(h^4) \\<br /> <br /> <br /> So the central difference approximate is an O(h^2) approximation, but this series is only a function of the *even* powers of :math:h. So applying Richardson's extrapolation, we may write:<br /> <br /> .. math::<br /> <br /> \begin{array}{rll} \text{A} \qquad f'(x_0) &amp;= \displaystyle \frac{f(x_0+h) - f(x_0-h)}{2h} &amp;+ c_2h^2 + O(h^4) \\ &amp;= Q_2(h) &amp;+ c_2h^2 + O(h^4) \\ \text{B} \qquad f'(x_0) &amp;= \displaystyle \frac{f(x_0+h/2) - f(x_0-h/2)}{h} &amp;+ \displaystyle c_2\frac{h^2}{4} + O(h^4) \\ &amp;= Q_2(h/2) &amp;+ \displaystyle c_2\frac{h^2}{4} + O(h^4) \end{array}<br /> <br /> Subtracting: :math:4\text{B} - \text{A}:<br /> <br /> .. math::<br /> <br /> 3f'(x_0) &amp;= 4Q_2(h/2) - Q_2(h) + O(h^4) \\ f'(x_0) &amp;= \displaystyle \frac{4Q_2(h/2) - Q_2(h)}{3} + O(h^4) \\ f'(x_0) &amp;= Q_4(h) + O(h^4)<br /> <br /> So this improved (accelerated) approximation for :math:f'(x) will be of the order :math:O(h^4).<br /> <br /> .. raw:: latex<br /> <br /> \newpage<br /> <br /> Question 2 <br /> ==============<br /> <br /> .. note:: Adapted from the course textbook, problem 24.11<br /> <br /> A colleague has designed a new transdermal patch to deliver insulin through the skin to diabetic patients, eliminating the need for painful injections. She has collected the following data on the mass flux, :math:f, of insulin being delivered through the patch (and skin), as a function of time, :math:t. [Mass flux is the flow rate through a unit area.]<br /> <br /> ================================== === === === === === === === === === === === <br /> :math:t [hour] 0 1 2 3 4 5 6 10 14 18 24 <br /> ---------------------------------- --- --- --- --- --- --- --- --- --- --- --- <br /> :math:f [mg/(cm\ :sup:2.hr)] 15 14 12 11 9 8 7.2 5 2.5 2 1 <br /> ================================== === === === === === === === === === === === <br /> <br /> #. Provide an estimate, using the trapezoidal rule, of the mass of drug delivered to the patient over a 24 hour period with a patch of 10 cm\ :sup:2.<br /> <br /> #. Can you provide an alternate, more accurate estimate, using another technique we learned about? If so, provide the answer; if not, explain why.<br /> <br /> Solution<br /> -----------<br /> <br /> Note that not all the points are equally spaced. From :math:t=0 to :math:t=6 the step size is :math:h=1. From :math:t=6 to :math:t=18 the step size is :math:h=4. And for the last two points, :math:h=6. So, we can apply the composite trapezoidal to each of these three partitions:<br /> <br /> .. math::<br /> <br /> \begin{array}{llrl} \text{Partition 1}&amp; h=1: &amp; I_1=\int_{0}^{6}{f(t)dt}&amp;\approx \frac{1}{2}(f(0)+2f(1)+2f(2)+2f(3)+2f(4)+2f(5)+f(6))\\ &amp;&amp;&amp;=\frac{1}{2}(15+2(14) +2(12)+2(11)+2(9)+2(8)+7.2) = 65.10\\ \text{Partition 2}&amp; h=4: &amp; I_2=\int_{6}^{18}{f(t)dt}&amp;\approx \frac{4}{2}(f(6)+2f(10)+2f(14)+f(18))\\ &amp;&amp;&amp;=\frac{4}{2}(7.2+2(5)+2(2.5)+2)=48.40\\ \text{Partition 3}&amp; h=6: &amp; I_3=\int_{18}^{24}{f(t)dt}&amp;\approx \frac{6}{2}(f(18)+f(24))\\ &amp;&amp;&amp;=\frac{6}{2}(2+1)=9\\ &amp;&amp;\int_{0}^{24}{f(t)dt} &amp;\approx I_1+I_2+I_3=\textbf{122.50} \end{array}<br /> <br /> Note the above result is the mass flux. To obtain the mass, we multiply this by the surface area: :math:M=122.50\times 10=1225.00 <br /> <br /> A more accurate result can be obtained by using Simpson's rule, where possible, instead of the trapezoidal rule. Simpson's 1/3 rule can be applied on every three points if they are equally spaced. According to the given table, we can apply composite Simpson's 1/3 rule from :math:t=0 to :math:t=6 and use Simpson's 3/8 rule from :math:t=6 to :math:t=18. For the last two points, we again use the trapezoidal method. Summing the results of the above partitions we get:<br /> <br /> .. math::<br /> <br /> \begin{array}{llrl} \text{Partition 1}&amp; h=1:&amp; I_1= \int_{0}^{6}{f(t)dt}&amp;\approx\frac{1}{3}(f(0)+4f(1)+2f(2)+4f(3)+2f(4)+4f(5)+f(6))\\ &amp;&amp;&amp;=\frac{1}{3}(15+4(14) +2(12)+4(11)+2(9)+4(8)+7.2)=65.40 \\ \text{Partition 2}&amp; h=4:&amp; I_2=\int_{6}^{18}{f(t)dt}&amp;\approx\frac{3\times 4}{8}(f(6)+3f(10)+3f(14)+f(18))\\ &amp;&amp;&amp;=\frac{3\times 4}{8}(7.2+3(5)+3(2.5)+2)=47.55\\ \text{Partition 3}&amp; h=6: &amp; I_3=\int_{18}^{24}{f(t)dt}&amp;\approx \frac{6}{2}(f(18)+f(24))\\ &amp;&amp;&amp;=\frac{6}{2}(2+1)=9\\ &amp;&amp;\int_{0}^{24}{f(t)dt}&amp;\approx I_1+I_2+I_3=\textbf{121.95} \end{array}<br /> <br /> Again, we multiply the above value by 10 cm\ :sup:2 to get the mass of the drug: :math:M=121.95\times 10=1219.50 <br /> <br /> .. raw:: latex<br /> <br /> \newpage<br /> <br /> Question 3 <br /> ==============<br /> <br /> .. note:: This question is a little unconventional; but we strongly recommended you struggle with it, without asking for help from TA's or other group members.<br /> <br /> The following reaction is taking place in a large, well-mixed reactor: :math:{\sf N} \rightarrow {\sf B} with rate expression:<br /> <br /> .. math::<br /> <br /> r = \displaystyle \frac{k_1 C_{\sf N}}{k_2 + C_{\sf N}} \qquad\qquad\,\, k_1 = 30.0\,\,\text{g/(m$^3$.day)}\qquad\qquad\,\, k_2 = 20.4\,\,\text{g/m$^3$}<br /> <br /> #. Derive a dynamic mass balance for species :math:{\sf N}, given that:<br /> <br /> * the flow into the large tank is :math:F^\text{in}(t) = 500 m\ :sup:3/day<br /> * the inlet stream only contains species :math:{\sf N}, approximately constant at 100 g/m\ :sup:3 <br /> * the tank is operated at constant volume, :math:V = 2000 m\ :sup:3<br /> <br /> #. Using techniques learned in this course up to 11 November, calculate the concentration of :math:{\sf N} as a function of time over the first 3 days (roughly), as the reactor is starting up. The tank was initially filled to 2000 m\ :sup:3 with material at a concentration of :math:C_{\sf N} = 100 g/m\ :sup:3. <br /> <br /> Show your calculations, source code and plots. Please do not simply attach your source code in an appendix; your code should be worked into your solution, as part of your explanations. Also note that you should **not** use methods such as Euler's methods, or MATLAB/Python functions such as scipy.integrate.ode or ode45.<br /> <br /> Solution<br /> ---------<br /> <br /> The main purpose of this question was for you to struggle with the concept of time-based functions, derivatives and integrals in the expectation that when you see ODEs in part F of the course that they will make sense. Any serious attempt at this question will get a :math:\beta, while any solutions that calculate a reasonable plot of concentration over time, with concentration decreasing in time will get an :math:\alpha.<br /> <br /> #. The species mass balance for :math:{\sf N} is:<br /> <br /> * In = :math:C_{\sf N}^\text{in}(t)F^\text{in}(t) with units of g/day<br /> * Out = :math:C_{\sf N}(t)F^\text{in}(t)<br /> * Depletion of :math:{\sf N} = :math:- \displaystyle \frac{k_1 C_{\sf N}(t)}{k_2 + C_{\sf N}(t)} V(t) also with units of g/day<br /> * Accumulation = :math:\displaystyle \frac{V(t)C_{\sf N}(t)}{dt} with units of g/day<br /> <br /> Assumptions made in the above are that the tank is well mixed and that the inflow and outflow are the same, which should be true if operating at constant volume. In the bullet points we had all the terms as a function of time. When forming the dynamic balance below we have removed the time-dependence from any terms which are constant.<br /> <br /> .. math::<br /> <br /> \frac{d\left[V(t)C_{\sf N}(t)\right]}{dt} = V\frac{dC_{\sf N}(t)}{dt} &amp;= F^\text{in}\left[C_{\sf N}^\text{in} - C_{\sf N}(t) \right] - \frac{k_1 C_{\sf N}(t)}{k_2 + C_{\sf N}(t)} V \\ \frac{dC_{\sf N}(t)}{dt} &amp;= \frac{F^\text{in}}{V}\left[C_{\sf N}^\text{in} - C_{\sf N}(t) \right] - \frac{k_1 C_{\sf N}(t)}{k_2 + C_{\sf N}(t)} <br /> <br /> <br /> #. Integrating the dynamic balance between time :math:t=0 and :math:t=3 days, and initial concentration :math:C_{\sf N}(t=0) = 100 g/m\ :sup:3 can be expressed as:<br /> <br /> .. math::<br /> <br /> \int_{100}^{C_{\sf N}(3)}{f(C_{\sf N}) dC_{\sf N}} &amp;= \int_{0}^{3} dt \\ \int_{100}^{C_{\sf N}(3)}{ \left[\frac{F^\text{in}}{V}\left[C_{\sf N}^\text{in} - C_{\sf N}(t) \right] - \frac{k_1 C_{\sf N}(t)}{k_2 + C_{\sf N}(t)} \right]^{-1} dC_{\sf N}} &amp;= 3.0<br /> <br /> This is not easy to integrate analytically, so the tools we have learned in the course can be used now to approximate the integral on the left hand side starting from :math:C_{\sf N}(t=0) = 100 g/m\ :sup:3 up to some unknown concentration, :math:C_{\sf N}(3) g/m\ :sup:3. We will add to the approximation until the left integral is equal to right integral of 3.0 days. This process seems suited to using the trapezoidal rule, or Simpson's method. We can use panels to approximate the integral, and keep adding panels until our approximation to the integral is roughly 3.0 days. <br /> <br /> Either way, we need to decide where to place our divisions to form the integrating panels. Do we add them to the left or to the right of the starting point :math:C_{\sf N}(t=0) = 100 g/m\ :sup:3? To help us decide this, we can evaluate the derivative at time :math:t=0 days:<br /> <br /> .. math::<br /> <br /> \frac{C_{\sf N}(t)}{dt} &amp;= \frac{500.0}{2000.0}\left[100 - 100\right] - \frac{30 \times 100}{20.4 + 100.0} \\ &amp;= 0 - 24.9\,\, \text{g/(m$^3$.day)}<br /> <br /> This gives two pieces of information. Firstly, that the concentration will decrease by about 25 units per day at the startup of the reactor. Secondly it helps us decide on the number of panels. We can use panels about 5 units wide, giving about 5 divisions per day. i.e. :math:C_{\sf N} = [100, 95, 90, 85, \ldots ]. So we will keep adding panels until the approximation is roughly equal to 3.0 days.<br /> <br /> Using the trapezoidal approximation:<br /> <br /> * From :math:C_{\sf N} = 100 to 95 g/m\ :sup:3: area = :math:\frac{(95 - 100)}{2} \cdot \left[f(95)+f(100)\right] = :math:\frac{-5}{2} \cdot \left[-0.04013 - -0.04265 \right] = 0.20695, total area so far = 0.21 days.<br /> * From :math:C_{\sf N} = 95 to 90 g/m\ :sup:3: area = :math:\frac{(90 - 95)}{2} \cdot \left[f(90)+f(95)\right] = :math:\frac{-5}{2} \cdot \left[-0.04265 - -0.04554 \right] = 0.2205, total area so far = 0.43 days.<br /> * From :math:C_{\sf N} = 90 to 85 g/m\ :sup:3: area = 0.2361, area so far = 0.6636 days.<br /> * From :math:C_{\sf N} = 85 to 80 g/m\ :sup:3: area = 0.2545, area so far = 0.9181 days.<br /> * From :math:C_{\sf N} = 80 to 75 g/m\ :sup:3: area = 0.2765, area so far = 1.195 days (which we expected from our initial derivative)<br /> * From :math:C_{\sf N} = 75 to 70 g/m\ :sup:3: area = 0.3031, area so far = 1.498 days.<br /> * From :math:C_{\sf N} = 70 to 65 g/m\ :sup:3: area = 0.3364, area so far = 1.834 days.<br /> * From :math:C_{\sf N} = 65 to 60 g/m\ :sup:3: area = 0.3793, area so far = 2.213 days.<br /> * From :math:C_{\sf N} = 60 to 55 g/m\ :sup:3: area = 0.4369, area so far = 2.650 days.<br /> * From :math:C_{\sf N} = 55 to 50 g/m\ :sup:3: area = 0.5190, area so far = 3.169 days.<br /> <br /> The following code was used:<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-5/code/integrate_q3.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-5/code/integrate_q3.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> So we now have the concentrations in the tank as a function of time, over a period of 3 days. We can plot these as shown below (the calculations above are to the left of the red line). The points beyond the red line were calculated when continuing beyond 3 days.<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-5/images/assign5_q3_plot.png<br /> :scale: 55<br /> :align: center<br /> <br /> The approximation error will obviously get worse and worse with this crude panel size, so we should start using a smaller panel size as we keep going.<br /> <br /> The same approach as above could have been performed using Simpson's 1/3 rule for more accurate estimates.<br /> <br /> Question 4 [1.5]<br /> =================<br /> <br /> .. note:: From the 2009 final exam, 3 points out of 50; 3 hours.<br /> <br /> You are given a set of algorithms for numerical integration. You are also given a set of problems. You are to match every problem (1, 2, 3) with an algorithm (A, B, or C). You may use each algorithm **only once**. Motivate each of your answers **carefully**.<br /> <br /> **Problem**<br /> <br /> **1**: Determine the integral of a very nonlinear function with high accuracy. <br /> <br /> **2**: Determine the integral of a computationally expensive, black-box function with reasonable accuracy.<br /> <br /> **3**: Determine the integral of a cubic model for a pure component's specific heat capacity: e.g. :math:C_p(T) = a + bT + cT^2 + dT^3<br /> <br /> **Algorithms**<br /> <br /> **A**: Simpson's 1/3 rule<br /> <br /> **B**: Romberg method<br /> <br /> **C**: 4-point Gauss quadrature<br /> <br /> Solution<br /> ---------<br /> <br /> Most of the grade in this question is for your justification. Below is one potential set of links:<br /> <br /> **1**: Determine the integral of a very nonlinear function with high accuracy: use Romberg-method; because Romberg builds on the trapezoidal method, potentially many function evaluations are made. The Romberg method improves its accuracy by order :math:O(h^2p) in every row in the Romberg table. So just 4 rows in the table would have an error of :math:O(h^8). We can guarantee the error, even if the function is nonlinear.<br /> <br /> **2**: Determine the integral of a computationally expensive, black-box function with reasonable accuracy: use 4-point Gauss quadrature; because Gauss quadrature has low error for very few function evaluations. A 4-point quadrature would be exact for 7th-order polynomials, so we would get very good accuracy with only 4 function evaluations.<br /> <br /> **3**: Determine the integral of a cubic model for a pure component's specific heat capacity: e.g. :math:C_p(T) = a + bT + cT^2 + dT^3: I would use Simpson's 1/3 method because it is exact for cubic polynomials and lower - so we would get an exact answer for this type of polynomial.<br /> <br /> <br /> Question 5 <br /> ===============<br /> <br /> .. note:: From the 2009 final exam, 2+1.5 points, out of 50; 3 hours. There were other parts to the question, but they are not relevant here.<br /> <br /> Consider a CSTR, fed with one inlet stream at volumetric flow rate :math:F^{\rm in} and containing species :math:{\sf A} at concentration :math:C_{\sf A}^{\rm in}=1 mol/L. There is no outlet from the reactor. A fluid of constant density is considered throughout this question.<br /> <br /> The reactor is initially empty, and the fluid is fed into the tank via the inlet stream at the volumetric flow rate given by :math:F^{\rm in}(t) = 1-\exp(-t) [in m\ :sup:3/min].<br /> <br /> #. Determine the volume of liquid :math:V(t) accumulated at :math:t_1=5 min using Romberg integration (perform **3 steps** of Romberg's method; that is, 3 columns in the graphical depiction).<br /> <br /> #. Repeat the calculation using two-point Gauss quadrature, and compare the results. Which method is likely to provide the best estimate?<br /> <br /> <br /> Solution<br /> --------<br /> <br /> This question asks to evaluate the cumulative volume in the tank after a certain time:<br /> <br /> .. math::<br /> <br /> \frac{dV}{dt} &amp;= F^{\rm in}(t) \\ \int_{0}^{V}{dV} &amp;= V = \int_{0}^{t}{F(t) dt} = \int_{0}^{t=5}{1-e^{-t} dt}<br /> <br /> #. Using Romberg integration:<br /> <br /> The Romberg table can be constructed as follows:<br /> <br /> .. math::<br /> <br /> \begin{array}{llll} h &amp; I_1, O(h^2) &amp; I_2, O(h^4) &amp; I_3, O(h^6) \\ \hline h=5 &amp; I_1(5) = 2.4832 &amp; \\ h=2.5 &amp; I_1(2.5) = 3.5364 &amp; I_2(2.5) = 3.8874 &amp;\\ h=1.25 &amp; I_1(1.25) = 3.8807 &amp; I_2(1.25) = 3.9955 &amp; I_3(1.25) = 4.0027\\ \hline \end{array}<br /> <br /> The following expanded entries were used in the above table:<br /> <br /> * :math:I_1(5) = \frac{5}{2}\left[f(0)+f(5)\right] = 2.4832<br /> * :math:I_1(2.5) = \frac{5}{2 \times 2}\left[f(0)+2f(2.5) + f(5)\right] = 3.5364<br /> * :math:I_1(1.25) = \frac{5}{4 \times 2}\left[f(0)+ 2f(1.25) + 2f(2.5) + 2f(3.75) + f(5)\right] = 3.8807<br /> * :math:I_2(2.5) = \frac{4I_1(2.5) - I_1(5)}{4-1} = \frac{4 \times 3.5364 - 2.4832}{3} = 3.8875 using the messy formula with :math:j=1 and :math:k=1<br /> * :math:I_2(1.25) = \frac{4I_1(1.25) - I_1(2.5)}{4-1} = \frac{4 \times 3.8807 - 3.5364}{3} = 3.9955 with :math:j=1 and :math:k=2<br /> * :math:I_3(1.25) = \frac{16I_2(1.25) - I_2(2.5)}{16-1} = \frac{16 \times 3.9955 - 3.8875}{15} = 4.0027 with :math:j=2 and :math:k=2<br /> <br /> The final estimate for the integral is the last column in the last row, so :math:I_3(1.25) = 4.003<br /> <br /> #. Using Gauss quadrature: our function to be integrated is :math:f(t) = 1-e^{-t} between limits :math:a=0 and :math:b=5. Since Gaussian quadrature is integrated between -1 and +1, our formula for the change in variables is:<br /> <br /> .. math::<br /> <br /> t &amp;= \frac{b+a}{2} + \frac{b-a}{2}\xi \\ t &amp;= \frac{5}{2} + \frac{5}{2}\xi~~\text{where}\,\,-1 \leq \xi \leq +1<br /> <br /> .. math::<br /> <br /> I_2 &amp;= \omega_1 f(\xi_1) + \omega_2 f(\xi_2)\\ &amp;= \bigg[\omega_1 f(t_1) + \omega_2 f(t_2)\bigg] \frac{dt}{d\xi}\\ &amp;= \bigg[1.0 f\left(\frac{5}{2} - \frac{5}{2}\cdot\frac{1}{\sqrt{3}}\right) + 1.0 f\left(\frac{5}{2} + \frac{5}{2}\cdot\frac{1}{\sqrt{3}}\right)\bigg] \frac{5}{2}\\ &amp;= \bigg[1.0 f(1.0566) + 1.0 f(3.9434)\bigg] \frac{5}{2}\\ &amp;= \bigg[0.6524 + 0.9806 \bigg] \frac{5}{2}\\ &amp;= 4.083<br /> <br /> Romberg quadrature taken to three rows will have error in the order of :math:O(h^6), while 2-point Gaussian quadrature will be exact for polynomials of order 3 and below: i.e. the error of Gaussian quadrature will be some function of the fourth derivative of :math:f(t). Errors from Gaussian quadrature are not a function of :math:h, since there is no :math:h in Gaussian quadrature. Since the function we are integrating is not polynomial, I would expect error in both methods.<br /> <br /> The true value of the integral is easy to find: :math:\int_{0}^{t=5}{1-e^{-t} dt} = t + e^{-t}\big|_{0}^{5} = 4 + e^{-5} = 4.0067379; using this we can see the Romberg method had lower error.<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.5cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> <br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Assignment_5_-_2010_-_Solution&diff=1099 Assignment 5 - 2010 - Solution 2018-09-16T08:58:16Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 17 November 2010<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = Assignment-5-2010.pdf<br /> | questions_text_alt = Assignment questions<br /> | solutions_PDF = Assignment-5-2010-Solutions.pdf<br /> | solutions_text_alt = Solutions, prepared by Ali and Kevin<br /> | other_instructions = Hand-in at class.<br /> }}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> .. |m3| replace:: m\ :sup:3<br /> <br /> Question 1 [1.5]<br /> =================<br /> <br /> Richardson's extrapolation allows us to accelerate convergence. The technique was used to decrease the error for the forward difference approximation for numerical differentiation. <br /> <br /> Using Richardson's extrapolation technique, but this time with the central difference approximation -- an :math:O(h^2) method -- derive an improved (accelerated accuracy) approximation. What is the order of this approximation?<br /> <br /> Solution<br /> --------<br /> <br /> The central difference approximation was derived from the Taylor Series expansion:<br /> <br /> .. math::<br /> <br /> f'(x_0+h) &amp;= f(x_0) + f'(x_0)h + \frac{f''(x_0)h^2}{2} + \frac{f'''(x_0)h^3}{6} + \frac{f''''(x_0)h^4}{24} + O(h^5) \\<br /> f'(x_0-h) &amp;= f(x_0) - f'(x_0)h + \frac{f''(x_0)h^2}{2} - \frac{f'''(x_0)h^3}{6} + \frac{f''''(x_0)h^4}{24} + O(h^5)<br /> <br /> Subtracting one equation from the other, and simplifying:<br /> <br /> .. math::<br /> <br /> f'(x_0) &amp;= \frac{f(x_0+h) - f(x_0-h)}{2h} + \frac{f'''(x_0)}{12}h^2 + O(h^4) \\<br /> <br /> <br /> So the central difference approximate is an O(h^2) approximation, but this series is only a function of the *even* powers of :math:h. So applying Richardson's extrapolation, we may write:<br /> <br /> .. math::<br /> <br /> \begin{array}{rll} \text{A} \qquad f'(x_0) &amp;= \displaystyle \frac{f(x_0+h) - f(x_0-h)}{2h} &amp;+ c_2h^2 + O(h^4) \\ &amp;= Q_2(h) &amp;+ c_2h^2 + O(h^4) \\ \text{B} \qquad f'(x_0) &amp;= \displaystyle \frac{f(x_0+h/2) - f(x_0-h/2)}{h} &amp;+ \displaystyle c_2\frac{h^2}{4} + O(h^4) \\ &amp;= Q_2(h/2) &amp;+ \displaystyle c_2\frac{h^2}{4} + O(h^4) \end{array}<br /> <br /> Subtracting: :math:4\text{B} - \text{A}:<br /> <br /> .. math::<br /> <br /> 3f'(x_0) &amp;= 4Q_2(h/2) - Q_2(h) + O(h^4) \\ f'(x_0) &amp;= \displaystyle \frac{4Q_2(h/2) - Q_2(h)}{3} + O(h^4) \\ f'(x_0) &amp;= Q_4(h) + O(h^4)<br /> <br /> So this improved (accelerated) approximation for :math:f'(x) will be of the order :math:O(h^4).<br /> <br /> .. raw:: latex<br /> <br /> \newpage<br /> <br /> Question 2 <br /> ==============<br /> <br /> .. note:: Adapted from the course textbook, problem 24.11<br /> <br /> A colleague has designed a new transdermal patch to deliver insulin through the skin to diabetic patients, eliminating the need for painful injections. She has collected the following data on the mass flux, :math:f, of insulin being delivered through the patch (and skin), as a function of time, :math:t. [Mass flux is the flow rate through a unit area.]<br /> <br /> ================================== === === === === === === === === === === === <br /> :math:t [hour] 0 1 2 3 4 5 6 10 14 18 24 <br /> ---------------------------------- --- --- --- --- --- --- --- --- --- --- --- <br /> :math:f [mg/(cm\ :sup:2.hr)] 15 14 12 11 9 8 7.2 5 2.5 2 1 <br /> ================================== === === === === === === === === === === === <br /> <br /> #. Provide an estimate, using the trapezoidal rule, of the mass of drug delivered to the patient over a 24 hour period with a patch of 10 cm\ :sup:2.<br /> <br /> #. Can you provide an alternate, more accurate estimate, using another technique we learned about? If so, provide the answer; if not, explain why.<br /> <br /> Solution<br /> -----------<br /> <br /> Note that not all the points are equally spaced. From :math:t=0 to :math:t=6 the step size is :math:h=1. From :math:t=6 to :math:t=18 the step size is :math:h=4. And for the last two points, :math:h=6. So, we can apply the composite trapezoidal to each of these three partitions:<br /> <br /> .. math::<br /> <br /> \begin{array}{llrl} \text{Partition 1}&amp; h=1: &amp; I_1=\int_{0}^{6}{f(t)dt}&amp;\approx \frac{1}{2}(f(0)+2f(1)+2f(2)+2f(3)+2f(4)+2f(5)+f(6))\\ &amp;&amp;&amp;=\frac{1}{2}(15+2(14) +2(12)+2(11)+2(9)+2(8)+7.2) = 65.10\\ \text{Partition 2}&amp; h=4: &amp; I_2=\int_{6}^{18}{f(t)dt}&amp;\approx \frac{4}{2}(f(6)+2f(10)+2f(14)+f(18))\\ &amp;&amp;&amp;=\frac{4}{2}(7.2+2(5)+2(2.5)+2)=48.40\\ \text{Partition 3}&amp; h=6: &amp; I_3=\int_{18}^{24}{f(t)dt}&amp;\approx \frac{6}{2}(f(18)+f(24))\\ &amp;&amp;&amp;=\frac{6}{2}(2+1)=9\\ &amp;&amp;\int_{0}^{24}{f(t)dt} &amp;\approx I_1+I_2+I_3=\textbf{122.50} \end{array}<br /> <br /> Note the above result is the mass flux. To obtain the mass, we multiply this by the surface area: :math:M=122.50\times 10=1225.00 <br /> <br /> A more accurate result can be obtained by using Simpson's rule, where possible, instead of the trapezoidal rule. Simpson's 1/3 rule can be applied on every three points if they are equally spaced. According to the given table, we can apply composite Simpson's 1/3 rule from :math:t=0 to :math:t=6 and use Simpson's 3/8 rule from :math:t=6 to :math:t=18. For the last two points, we again use the trapezoidal method. Summing the results of the above partitions we get:<br /> <br /> .. math::<br /> <br /> \begin{array}{llrl} \text{Partition 1}&amp; h=1:&amp; I_1= \int_{0}^{6}{f(t)dt}&amp;\approx\frac{1}{3}(f(0)+4f(1)+2f(2)+4f(3)+2f(4)+4f(5)+f(6))\\ &amp;&amp;&amp;=\frac{1}{3}(15+4(14) +2(12)+4(11)+2(9)+4(8)+7.2)=65.40 \\ \text{Partition 2}&amp; h=4:&amp; I_2=\int_{6}^{18}{f(t)dt}&amp;\approx\frac{3\times 4}{8}(f(6)+3f(10)+3f(14)+f(18))\\ &amp;&amp;&amp;=\frac{3\times 4}{8}(7.2+3(5)+3(2.5)+2)=47.55\\ \text{Partition 3}&amp; h=6: &amp; I_3=\int_{18}^{24}{f(t)dt}&amp;\approx \frac{6}{2}(f(18)+f(24))\\ &amp;&amp;&amp;=\frac{6}{2}(2+1)=9\\ &amp;&amp;\int_{0}^{24}{f(t)dt}&amp;\approx I_1+I_2+I_3=\textbf{121.95} \end{array}<br /> <br /> Again, we multiply the above value by 10 cm\ :sup:2 to get the mass of the drug: :math:M=121.95\times 10=1219.50 <br /> <br /> .. raw:: latex<br /> <br /> \newpage<br /> <br /> Question 3 <br /> ==============<br /> <br /> .. note:: This question is a little unconventional; but we strongly recommended you struggle with it, without asking for help from TA's or other group members.<br /> <br /> The following reaction is taking place in a large, well-mixed reactor: :math:{\sf N} \rightarrow {\sf B} with rate expression:<br /> <br /> .. math::<br /> <br /> r = \displaystyle \frac{k_1 C_{\sf N}}{k_2 + C_{\sf N}} \qquad\qquad\,\, k_1 = 30.0\,\,\text{g/(m$^3$.day)}\qquad\qquad\,\, k_2 = 20.4\,\,\text{g/m$^3$}<br /> <br /> #. Derive a dynamic mass balance for species :math:{\sf N}, given that:<br /> <br /> * the flow into the large tank is :math:F^\text{in}(t) = 500 m\ :sup:3/day<br /> * the inlet stream only contains species :math:{\sf N}, approximately constant at 100 g/m\ :sup:3 <br /> * the tank is operated at constant volume, :math:V = 2000 m\ :sup:3<br /> <br /> #. Using techniques learned in this course up to 11 November, calculate the concentration of :math:{\sf N} as a function of time over the first 3 days (roughly), as the reactor is starting up. The tank was initially filled to 2000 m\ :sup:3 with material at a concentration of :math:C_{\sf N} = 100 g/m\ :sup:3. <br /> <br /> Show your calculations, source code and plots. Please do not simply attach your source code in an appendix; your code should be worked into your solution, as part of your explanations. Also note that you should **not** use methods such as Euler's methods, or MATLAB/Python functions such as scipy.integrate.ode or ode45.<br /> <br /> Solution<br /> ---------<br /> <br /> The main purpose of this question was for you to struggle with the concept of time-based functions, derivatives and integrals in the expectation that when you see ODEs in part F of the course that they will make sense. Any serious attempt at this question will get a :math:\beta, while any solutions that calculate a reasonable plot of concentration over time, with concentration decreasing in time will get an :math:\alpha.<br /> <br /> #. The species mass balance for :math:{\sf N} is:<br /> <br /> * In = :math:C_{\sf N}^\text{in}(t)F^\text{in}(t) with units of g/day<br /> * Out = :math:C_{\sf N}(t)F^\text{in}(t)<br /> * Depletion of :math:{\sf N} = :math:- \displaystyle \frac{k_1 C_{\sf N}(t)}{k_2 + C_{\sf N}(t)} V(t) also with units of g/day<br /> * Accumulation = :math:\displaystyle \frac{V(t)C_{\sf N}(t)}{dt} with units of g/day<br /> <br /> Assumptions made in the above are that the tank is well mixed and that the inflow and outflow are the same, which should be true if operating at constant volume. In the bullet points we had all the terms as a function of time. When forming the dynamic balance below we have removed the time-dependence from any terms which are constant.<br /> <br /> .. math::<br /> <br /> \frac{d\left[V(t)C_{\sf N}(t)\right]}{dt} = V\frac{dC_{\sf N}(t)}{dt} &amp;= F^\text{in}\left[C_{\sf N}^\text{in} - C_{\sf N}(t) \right] - \frac{k_1 C_{\sf N}(t)}{k_2 + C_{\sf N}(t)} V \\ \frac{dC_{\sf N}(t)}{dt} &amp;= \frac{F^\text{in}}{V}\left[C_{\sf N}^\text{in} - C_{\sf N}(t) \right] - \frac{k_1 C_{\sf N}(t)}{k_2 + C_{\sf N}(t)} <br /> <br /> <br /> #. Integrating the dynamic balance between time :math:t=0 and :math:t=3 days, and initial concentration :math:C_{\sf N}(t=0) = 100 g/m\ :sup:3 can be expressed as:<br /> <br /> .. math::<br /> <br /> \int_{100}^{C_{\sf N}(3)}{f(C_{\sf N}) dC_{\sf N}} &amp;= \int_{0}^{3} dt \\ \int_{100}^{C_{\sf N}(3)}{ \left[\frac{F^\text{in}}{V}\left[C_{\sf N}^\text{in} - C_{\sf N}(t) \right] - \frac{k_1 C_{\sf N}(t)}{k_2 + C_{\sf N}(t)} \right]^{-1} dC_{\sf N}} &amp;= 3.0<br /> <br /> This is not easy to integrate analytically, so the tools we have learned in the course can be used now to approximate the integral on the left hand side starting from :math:C_{\sf N}(t=0) = 100 g/m\ :sup:3 up to some unknown concentration, :math:C_{\sf N}(3) g/m\ :sup:3. We will add to the approximation until the left integral is equal to right integral of 3.0 days. This process seems suited to using the trapezoidal rule, or Simpson's method. We can use panels to approximate the integral, and keep adding panels until our approximation to the integral is roughly 3.0 days. <br /> <br /> Either way, we need to decide where to place our divisions to form the integrating panels. Do we add them to the left or to the right of the starting point :math:C_{\sf N}(t=0) = 100 g/m\ :sup:3? To help us decide this, we can evaluate the derivative at time :math:t=0 days:<br /> <br /> .. math::<br /> <br /> \frac{C_{\sf N}(t)}{dt} &amp;= \frac{500.0}{2000.0}\left[100 - 100\right] - \frac{30 \times 100}{20.4 + 100.0} \\ &amp;= 0 - 24.9\,\, \text{g/(m$^3$.day)}<br /> <br /> This gives two pieces of information. Firstly, that the concentration will decrease by about 25 units per day at the startup of the reactor. Secondly it helps us decide on the number of panels. We can use panels about 5 units wide, giving about 5 divisions per day. i.e. :math:C_{\sf N} = [100, 95, 90, 85, \ldots ]. So we will keep adding panels until the approximation is roughly equal to 3.0 days.<br /> <br /> Using the trapezoidal approximation:<br /> <br /> * From :math:C_{\sf N} = 100 to 95 g/m\ :sup:3: area = :math:\frac{(95 - 100)}{2} \cdot \left[f(95)+f(100)\right] = :math:\frac{-5}{2} \cdot \left[-0.04013 - -0.04265 \right] = 0.20695, total area so far = 0.21 days.<br /> * From :math:C_{\sf N} = 95 to 90 g/m\ :sup:3: area = :math:\frac{(90 - 95)}{2} \cdot \left[f(90)+f(95)\right] = :math:\frac{-5}{2} \cdot \left[-0.04265 - -0.04554 \right] = 0.2205, total area so far = 0.43 days.<br /> * From :math:C_{\sf N} = 90 to 85 g/m\ :sup:3: area = 0.2361, area so far = 0.6636 days.<br /> * From :math:C_{\sf N} = 85 to 80 g/m\ :sup:3: area = 0.2545, area so far = 0.9181 days.<br /> * From :math:C_{\sf N} = 80 to 75 g/m\ :sup:3: area = 0.2765, area so far = 1.195 days (which we expected from our initial derivative)<br /> * From :math:C_{\sf N} = 75 to 70 g/m\ :sup:3: area = 0.3031, area so far = 1.498 days.<br /> * From :math:C_{\sf N} = 70 to 65 g/m\ :sup:3: area = 0.3364, area so far = 1.834 days.<br /> * From :math:C_{\sf N} = 65 to 60 g/m\ :sup:3: area = 0.3793, area so far = 2.213 days.<br /> * From :math:C_{\sf N} = 60 to 55 g/m\ :sup:3: area = 0.4369, area so far = 2.650 days.<br /> * From :math:C_{\sf N} = 55 to 50 g/m\ :sup:3: area = 0.5190, area so far = 3.169 days.<br /> <br /> The following code was used:<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-5/code/integrate_q3.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-5/code/integrate_q3.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> So we now have the concentrations in the tank as a function of time, over a period of 3 days. We can plot these as shown below (the calculations above are to the left of the red line). The points beyond the red line were calculated when continuing beyond 3 days.<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-5/images/assign5_q3_plot.png<br /> :scale: 55<br /> :align: center<br /> <br /> The approximation error will obviously get worse and worse with this crude panel size, so we should start using a smaller panel size as we keep going.<br /> <br /> The same approach as above could have been performed using Simpson's 1/3 rule for more accurate estimates.<br /> <br /> Question 4 [1.5]<br /> =================<br /> <br /> .. note:: From the 2009 final exam, 3 points out of 50; 3 hours.<br /> <br /> You are given a set of algorithms for numerical integration. You are also given a set of problems. You are to match every problem (1, 2, 3) with an algorithm (A, B, or C). You may use each algorithm **only once**. Motivate each of your answers **carefully**.<br /> <br /> **Problem**<br /> <br /> **1**: Determine the integral of a very nonlinear function with high accuracy. <br /> <br /> **2**: Determine the integral of a computationally expensive, black-box function with reasonable accuracy.<br /> <br /> **3**: Determine the integral of a cubic model for a pure component's specific heat capacity: e.g. :math:C_p(T) = a + bT + cT^2 + dT^3<br /> <br /> **Algorithms**<br /> <br /> **A**: Simpson's 1/3 rule<br /> <br /> **B**: Romberg method<br /> <br /> **C**: 4-point Gauss quadrature<br /> <br /> Solution<br /> ---------<br /> <br /> Most of the grade in this question is for your justification. Below is one potential set of links:<br /> <br /> **1**: Determine the integral of a very nonlinear function with high accuracy: use Romberg-method; because Romberg builds on the trapezoidal method, potentially many function evaluations are made. The Romberg method improves its accuracy by order :math:O(h^2p) in every row in the Romberg table. So just 4 rows in the table would have an error of :math:O(h^8). We can guarantee the error, even if the function is nonlinear.<br /> <br /> **2**: Determine the integral of a computationally expensive, black-box function with reasonable accuracy: use 4-point Gauss quadrature; because Gauss quadrature has low error for very few function evaluations. A 4-point quadrature would be exact for 7th-order polynomials, so we would get very good accuracy with only 4 function evaluations.<br /> <br /> **3**: Determine the integral of a cubic model for a pure component's specific heat capacity: e.g. :math:C_p(T) = a + bT + cT^2 + dT^3: I would use Simpson's 1/3 method because it is exact for cubic polynomials and lower - so we would get an exact answer for this type of polynomial.<br /> <br /> <br /> Question 5 <br /> ===============<br /> <br /> .. note:: From the 2009 final exam, 2+1.5 points, out of 50; 3 hours. There were other parts to the question, but they are not relevant here.<br /> <br /> Consider a CSTR, fed with one inlet stream at volumetric flow rate :math:F^{\rm in} and containing species :math:{\sf A} at concentration :math:C_{\sf A}^{\rm in}=1 mol/L. There is no outlet from the reactor. A fluid of constant density is considered throughout this question.<br /> <br /> The reactor is initially empty, and the fluid is fed into the tank via the inlet stream at the volumetric flow rate given by :math:F^{\rm in}(t) = 1-\exp(-t) [in m\ :sup:3/min].<br /> <br /> #. Determine the volume of liquid :math:V(t) accumulated at :math:t_1=5 min using Romberg integration (perform **3 steps** of Romberg's method; that is, 3 columns in the graphical depiction).<br /> <br /> #. Repeat the calculation using two-point Gauss quadrature, and compare the results. Which method is likely to provide the best estimate?<br /> <br /> <br /> Solution<br /> --------<br /> <br /> This question asks to evaluate the cumulative volume in the tank after a certain time:<br /> <br /> .. math::<br /> <br /> \frac{dV}{dt} &amp;= F^{\rm in}(t) \\ <br /> \int_{0}^{V}{dV} &amp;= V = \int_{0}^{t}{F(t) dt} = \int_{0}^{t=5}{1-e^{-t} dt}<br /> <br /> #. Using Romberg integration:<br /> <br /> The Romberg table can be constructed as follows:<br /> <br /> .. math::<br /> <br /> \begin{array}{llll} h &amp; I_1, O(h^2) &amp; I_2, O(h^4) &amp; I_3, O(h^6) \\ \hline h=5 &amp; I_1(5) = 2.4832 &amp; \\ h=2.5 &amp; I_1(2.5) = 3.5364 &amp; I_2(2.5) = 3.8874 &amp;\\ h=1.25 &amp; I_1(1.25) = 3.8807 &amp; I_2(1.25) = 3.9955 &amp; I_3(1.25) = 4.0027\\ \hline \end{array}<br /> <br /> The following expanded entries were used in the above table:<br /> <br /> * :math:I_1(5) = \frac{5}{2}\left[f(0)+f(5)\right] = 2.4832<br /> * :math:I_1(2.5) = \frac{5}{2 \times 2}\left[f(0)+2f(2.5) + f(5)\right] = 3.5364<br /> * :math:I_1(1.25) = \frac{5}{4 \times 2}\left[f(0)+ 2f(1.25) + 2f(2.5) + 2f(3.75) + f(5)\right] = 3.8807<br /> * :math:I_2(2.5) = \frac{4I_1(2.5) - I_1(5)}{4-1} = \frac{4 \times 3.5364 - 2.4832}{3} = 3.8875 using the messy formula with :math:j=1 and :math:k=1<br /> * :math:I_2(1.25) = \frac{4I_1(1.25) - I_1(2.5)}{4-1} = \frac{4 \times 3.8807 - 3.5364}{3} = 3.9955 with :math:j=1 and :math:k=2<br /> * :math:I_3(1.25) = \frac{16I_2(1.25) - I_2(2.5)}{16-1} = \frac{16 \times 3.9955 - 3.8875}{15} = 4.0027 with :math:j=2 and :math:k=2<br /> <br /> The final estimate for the integral is the last column in the last row, so :math:I_3(1.25) = 4.003<br /> <br /> #. Using Gauss quadrature: our function to be integrated is :math:f(t) = 1-e^{-t} between limits :math:a=0 and :math:b=5. Since Gaussian quadrature is integrated between -1 and +1, our formula for the change in variables is:<br /> <br /> .. math::<br /> <br /> t &amp;= \frac{b+a}{2} + \frac{b-a}{2}\xi \\ t &amp;= \frac{5}{2} + \frac{5}{2}\xi~~\text{where}\,\,-1 \leq \xi \leq +1<br /> <br /> .. math::<br /> <br /> I_2 &amp;= \omega_1 f(\xi_1) + \omega_2 f(\xi_2)\\ &amp;= \bigg[\omega_1 f(t_1) + \omega_2 f(t_2)\bigg] \frac{dt}{d\xi}\\ &amp;= \bigg[1.0 f\left(\frac{5}{2} - \frac{5}{2}\cdot\frac{1}{\sqrt{3}}\right) + 1.0 f\left(\frac{5}{2} + \frac{5}{2}\cdot\frac{1}{\sqrt{3}}\right)\bigg] \frac{5}{2}\\ &amp;= \bigg[1.0 f(1.0566) + 1.0 f(3.9434)\bigg] \frac{5}{2}\\ &amp;= \bigg[0.6524 + 0.9806 \bigg] \frac{5}{2}\\ &amp;= 4.083<br /> <br /> Romberg quadrature taken to three rows will have error in the order of :math:O(h^6), while 2-point Gaussian quadrature will be exact for polynomials of order 3 and below: i.e. the error of Gaussian quadrature will be some function of the fourth derivative of :math:f(t). Errors from Gaussian quadrature are not a function of :math:h, since there is no :math:h in Gaussian quadrature. Since the function we are integrating is not polynomial, I would expect error in both methods.<br /> <br /> The true value of the integral is easy to find: :math:\int_{0}^{t=5}{1-e^{-t} dt} = t + e^{-t}\big|_{0}^{5} = 4 + e^{-5} = 4.0067379; using this we can see the Romberg method had lower error.<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.5cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> <br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Assignment_3_-_2010_-_Solution&diff=1098 Assignment 3 - 2010 - Solution 2018-09-16T08:57:19Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 01 November 2010<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = Assignment-3-2010.pdf<br /> | questions_text_alt = Assignment questions<br /> | solutions_PDF = Assignment-3-2010-Solutions.pdf<br /> | solutions_text_alt = Assignment solutions, by Ali, Elliot and Kevin<br /> | other_instructions = <br /> }}<br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> .. |m3| replace:: m\ :sup:3<br /> <br /> Question 1 <br /> ================<br /> <br /> The Secant method is the most widely used algorithm for solving a nonlinear equation, in chemical engineering, and other areas. Write a MATLAB or Python function that implements the Secant method, using a minimum number of function evaluations. (Python users: please do not just copy the built-in Secant method).<br /> <br /> Your function should accept the following inputs:<br /> <br /> * a function handle to :math:f(x) [see the software tutorial &lt;https://learnche.org/3E4/Software_tutorial/Functions_as_objects&gt;_ on the course website for a description of function handles]<br /> * a relaxation coefficient, :math:0 &lt; \alpha \leq 1.0<br /> * the stopping tolerance, :math:\varepsilon_\text{tol}<br /> * the maximum number of iterations, maxiter (default = 200)<br /> * a single starting guess, :math:x^{(0)}<br /> <br /> Your code should automatically calculate :math:x^{(-1)} = 1.001x^{(0)}, and should use a minimum number of function evaluations. The stopping criterion should be as shown in the course notes:<br /> <br /> .. math::<br /> <br /> \left|f(x^{(k)})\right| \leq \epsilon_{\rm tol} \qquad \text{and/or} \qquad \left|\frac{x^{(k)}-x^{(k-1)}}{x^{(k)}}\right| \leq \epsilon_{\rm tol}<br /> <br /> Please print out a table at each iteration that shows these 5 outputs:<br /> <br /> * Iteration :math:k <br /> * :math:x^{(k)}<br /> * :math:f(x^{(k)}) <br /> * :math:\displaystyle \left|\frac{x^{(k)}-x^{(k-1)}}{x^{(k)}}\right| <br /> * Relaxed :math:\Delta x <br /> <br /> Solution<br /> ---------<br /> <br /> See the following MATLAB and Python code:<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-3/code/secant_method.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-3/code/secant.py<br /> :language2: python<br /> :header2: Python code <br /> <br /> Question 2 <br /> ===============<br /> <br /> In your reactor design course you will learn about adiabatic CSTR's exhibiting multiple steady states, depending on the reactor inlet temperature. In practice reactors are sometimes operated at an unstable steady point, due to process economics or improved conversion. The equation below was derived for such a system:<br /> <br /> .. math::<br /> <br /> f(T) = (T-T_\text{feed}) - 150 \times \frac{1.34\times 10^9 e^{-\frac{7554}{T}}}{1+1.34\times 10^9 e^{-\frac{7554}{T}}}<br /> <br /> There is more than one temperature at which :math:f(T) = 0; each :math:f(T) = 0 corresponds to a reactor steady state. Use the secant method, with relaxation factor :math:\alpha = 0.9, and :math:T_\text{feed} = 300 K to find one of these steady state temperatures, using starting values of :math:T^{(0)}=320 K and :math:T^{(-1)}= 1.001 \times 320 K.<br /> <br /> #. Show the first 3 iterations by hand. Explains what happens in the 3rd iteration, by means of a plot of :math:f(x), and marking the iterations used so far on this plot.<br /> #. What could you change to avoid the problem in iteration 3?<br /> #. Compare your manual calculations in step 1 with the computer output from the previous question. Also report the temperature your computer code finds for :math:f(T)=0 at this starting guess. Select a reasonable tolerance value.<br /> #. Use the computer code from question 1, but with a starting value of :math:T = 400 K, and report the steady state operating temperature found.<br /> #. Comment on the solutions found from the two different starting points.<br /> <br /> Solution<br /> ----------<br /> <br /> First we evaluate the function at the two initial guesses (given :math:T_\text{feed}=300): :math:f(T^{(0)}=320)=9.535, and :math:f(T^{(-1)})=1.001\times 320)= 9.623. Now we apply the secant formula to find the first approximation :math:T^{(1)}:<br /> <br /> .. math::<br /> <br /> T^{(1)}=T^{(0)}-\alpha\displaystyle\frac{f(T^{(0)})}{\displaystyle\frac{f(T^{(0)})-f(T^{(-1)})}{T^{(0)}-T^{(-1)}}}= 320.0 - 0.9 \displaystyle \frac{9.535}{\displaystyle \frac{9.535 - 9.623}{320 - 320.32}} = 320.0 - 0.9 \times 34.67 = 288.8<br /> <br /> Evaluate :math:f(T^{(1)}), and repeat the above calculation to obtain :math:T^{(1)}:<br /> <br /> .. math::<br /> <br /> T^{(2)}=T^{(1)}-\alpha\displaystyle\frac{f(T^{(1)})}{\displaystyle\frac{f(T^{(1)})-f(T^{(0)})}{T^{(1)}-T^{(0)}}}= 304.5\\<br /> <br /> Evaluate :math:f(T^{(2)}), and repeat the above calculation to obtain :math:T^{(3)}:<br /> <br /> .. math::<br /> T^{(3)}=T^{(2)}-\alpha\displaystyle\frac{f(T^{(2)})}{\displaystyle\frac{f(T^{(2)})-f(T^{(1)})}{T^{(2)}-T^{(1)}}}=303.2<br /> <br /> The first three iterations (and the initial guess) are plotted below:<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-3/images/secant-iterations.png<br /> :scale: 50<br /> :align: center<br /> <br /> As you can see above, the first three iterations result in reasonable values, meaning that, as a sign of convergence, the difference between successive temperatures are getting less and less. Also, the value of the function itself is continually getting close to zero with the iterations. However, if you got, for example, negative absolute temperatures which is not physically acceptable, or the function value diverged from zero instead of getting closer to it, you could change your initial guesses or the :math:\alpha value to avoid the problem. <br /> <br /> The output from the code in question 1 is as follows::<br /> <br /> Iteration x f(x) Rel step alpha * Delta x<br /> 1 320.00000 9.53534 0.00100 -31.17468<br /> 2 288.82532 -12.04971 0.10794 15.66275<br /> 3 304.48807 1.18307 0.05144 -1.26028<br /> 4 303.22778 0.24013 0.00416 -0.28886<br /> 5 302.93892 0.02004 0.00095 -0.02368<br /> 6 302.91525 0.00194 0.00008 -0.00228<br /> 7 302.91296 0.00019 0.00001 -0.00023<br /> 8 302.91274 0.00002 0.00000 -0.00002<br /> 9 302.91271 0.00000 0.00000 -0.00000<br /> 10 302.91271 0.00000 0.00000 -0.00000<br /> 302.912711645<br /> <br /> The output from the code matches the hand calculations (otherwise something is wrong!). With the tolerance chosen as :math:\varepsilon_\text{tol}=\sqrt{\text{eps}}, the obtained temperature on convergence will be :math:T^{(10)}=302.9 K.<br /> <br /> With a different initial guess as :math:T^{(0)}=400.0, and all other settings being the same, the obtained temperature will be: :math:T^7=447.7. You see that a different initial guess resulted in a far different solution. This is because the given function has multiple solutions. When an iterative method is used to solve the function, the obtained solution will depend on the choice of the initial guess. So, in order to find all solutions to a nonlinear equation, one way is to try different initial guesses. (Now try :math:T^{(0)}=340.0 and see what you get!)<br /> <br /> The following computer code was used to solve this question:<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-3/code/Q2_func.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-3/code/reactor.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> Question 3 <br /> ==============<br /> <br /> In assignment 1 we derived the dynamic balance for a bioreactor system. The steady state nutrient concentration, :math:{\sf \overline{N}} and the steady state biomass concentration, :math:{\sf \overline{B}}, can be shown to be:<br /> <br /> .. math::<br /> <br /> \frac{dN}{dt} &amp;= 0 = {\sf \overline{N}}_\text{in}\frac{F}{V} - {\sf \overline{N}}\frac{F}{V} - \left(\frac{1}{Y_{B}}\right)\left(\mu_{\text{max}}\frac{{\sf \overline{N}}}{K+{\sf \overline{N}}}\right){\sf \overline{B}}\\ \frac{dB}{dt} &amp;= 0 = - {\sf \overline{B}}\frac{F}{V} + \left(\mu_{\text{max}}\frac{{\sf \overline{N}}}{K+{\sf \overline{N}}}\right){\sf \overline{B}}<br /> <br /> Although we showed in assignment 1 that we can solve one equation and substitute it into the other, you should not do that for this question. <br /> <br /> Rather, let :math:\displaystyle \frac{dN}{dt} = f_1({\sf \overline{N}}, {\sf \overline{B}}) = 0 and let :math:\displaystyle \frac{dB}{dt} = f_2({\sf \overline{N}}, {\sf \overline{B}}) = 0. Solve this coupled set of nonlinear equations for :math:{\bf x} = ({\sf \overline{N}}, {\sf \overline{B}}) using Newton's method.<br /> <br /> Use the following approach in your answer:<br /> <br /> #. Construct the Jacobian matrix, :math:{\bf J}, symbolically in terms of the variables used in the equations.<br /> #. Simplify the Jacobian by substituting in these known constants:<br /> <br /> * :math:{\sf \overline{N}}_\text{in} = 100 g/|m3|<br /> * :math:F = 5000 |m3|/day<br /> * :math:V = 1600 |m3|<br /> * :math:Y_B = 0.80 conversion efficiency<br /> * :math:\mu_\text{max} = 5 1/day<br /> * :math:K = 20 g/|m3|<br /> <br /> #. Let your initial guess be :math:{\bf x}^{(0)} = (40 \text{g/m$^3$}, 20 \text{g/m$^3$}). Calculate 2 iterations of the multivariate Newton-Raphson method, to show :math:{\bf x}^{(1)} and :math:{\bf x}^{(2)}. Take a full Newton step at every iteration.<br /> #. Calculate and show the convergence tolerance, using the 2-norm for each iteration. <br /> #. Compare these iterations to the true solution presented by Elliot in assignment 1. Is Newton's method converging to this answer?<br /> <br /> Feel free to code up the Jacobian matrix, and the functions :math:f_1 and :math:f_2 in MATLAB or Python and use your code to evaluate :math:{\bf J}({\sf \overline{N}}, {\sf \overline{B}}), to avoid manual calculation errors.<br /> <br /> Solution<br /> ----------<br /> <br /> #. We start by deriving the Jacobian matrix for our two function system:<br /> <br /> .. math::<br /> <br /> J\left({\sf \overline{N}},{\sf \overline{B}}\right) &amp;= \left[\begin{array}{ll} \frac{\partial f_1({\sf \overline{N}}, {\sf \overline{B}})}{\partial {\sf \overline{N}}} &amp; \frac{\partial f_1({\sf \overline{N}}, {\sf \overline{B}})}{\partial {\sf \overline{B}}} \\ \frac{\partial f_2({\sf \overline{N}}, {\sf \overline{B}})}{\partial {\sf \overline{N}}} &amp; \frac{\partial f_2({\sf \overline{N}}, {\sf \overline{B}})}{\partial {\sf \overline{B}}} \\ \end{array}\right]\\ J\left({\sf \overline{N}},{\sf \overline{B}}\right) &amp;= \left[\begin{array}{ccc} - \frac{F}{V} - \left(\frac{1}{Y_{B}}\right)\left(\mu_{\text{max}}\frac{1}{K+{\sf \overline{N}}}\right){\sf \overline{B}} + \left(\frac{1}{Y_{B}}\right)\left(\mu_{\text{max}}\frac{{\sf \overline{N}}}{\left(K+{\sf \overline{N}}\right)^{2}}\right){\sf \overline{B}} &amp; \quad , \quad &amp; - \left(\frac{1}{Y_{B}}\right)\left(\mu_{\text{max}}\frac{{\sf \overline{N}}}{K+{\sf \overline{N}}}\right) \\ \left(\mu_{\text{max}}\frac{1}{K+{\sf \overline{N}}}\right){\sf \overline{B}} - \left(\mu_{\text{max}}\frac{{\sf \overline{N}}}{\left(K+{\sf \overline{N}}\right)^{2}}\right){\sf \overline{B}} &amp; \quad , \quad &amp; - \frac{F}{V} + \left(\mu_{\text{max}}\frac{{\sf \overline{N}}}{K+{\sf \overline{N}}}\right) \\ \end{array}\right] <br /> <br /> #. Subbing in the constant values provided for this question results in the following simplified Jacobian.<br /> <br /> .. raw:: latex<br /> <br /> \\<br /> {\footnotesize [The PDF version of the solutions is cut off on the right edge of the page; please refer to the course website to get the full solution.]}<br /> <br /> .. math::<br /> <br /> J\left({\sf \overline{N}},{\sf \overline{B}}\right) &amp;= \left[\begin{array}{ccc} - \frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)} - \left(\frac{1}{\left(0.80\right)}\right)\left(\left(5 \frac{1}{day}\right)\frac{1}{\left(20 \frac{gCOD}{m^{3}}\right)+{\sf \overline{N}}}\right){\sf \overline{B}} + \left(\frac{1}{\left(0.80\right)}\right)\left(\left(5 \frac{1}{day}\right)\frac{{\sf \overline{N}}}{\left(\left(20 \frac{gCOD}{m^{3}}\right)+{\sf \overline{N}}\right)^{2}}\right){\sf \overline{B}} &amp; \quad , \quad &amp; - \left(\frac{1}{\left(0.80\right)}\right)\left(\left(5 \frac{1}{day}\right)\frac{{\sf \overline{N}}}{\left(20 \frac{gCOD}{m^{3}}\right)+{\sf \overline{N}}}\right) \\ \left(\left(5 \frac{1}{day}\right)\frac{1}{\left(20 \frac{gCOD}{m^{3}}\right)+{\sf \overline{N}}}\right){\sf \overline{B}} - \left(\left(5 \frac{1}{day}\right)\frac{{\sf \overline{N}}}{\left(\left(20 \frac{gCOD}{m^{3}}\right)+{\sf \overline{N}}\right)^{2}}\right){\sf \overline{B}} &amp; \quad , \quad &amp; - \frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)} + \left(\left(5 \frac{1}{day}\right)\frac{{\sf \overline{N}}}{\left(20 \frac{gCOD}{m^{3}}\right)+{\sf \overline{N}}}\right) \\ \end{array}\right] <br /> <br /> #. In this case we are in the luckiest of situations, we know the exact solution so we can track both relative and absolute errors. The exact solution (taken from Assignment 1) is given by the following equations:<br /> <br /> .. math::<br /> <br /> {\sf \overline{N}} &amp;= \left(\frac{\frac{F}{V_{R}}K}{\left(\mu_{\text{max}}-\frac{F}{V_{R}}\right)}\right)\\ {\sf \overline{B}} &amp;= \frac{\frac{F}{V_{R}}\left(N_{0} - {\sf \overline{N}}\right)}{\left(\frac{1}{Y_{B}}\right)\left(\mu_{\text{max}}\frac{{\sf \overline{N}}}{K+{\sf \overline{N}}}\right)}<br /> <br /> Subbing in the given constants<br /> <br /> .. math::<br /> <br /> {\sf \overline{N}} &amp;= \left(\frac{\left(\frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)}\right)\left(20 \frac{gCOD}{m^{3}}\right)}{\left(\left(5 \frac{1}{day}\right)-\left(\frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)}\right)\right)}\right)\\ {\sf \overline{N}} &amp;= 33.\overline{3} \frac{gCOD}{m^{3}}\\ &amp; \\ {\sf \overline{B}} &amp;= \frac{\left(\frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)}\right)\left(\left(100 \frac{gCOD}{m^{3}}\right) - \left(33.\overline{3} \frac{gCOD}{m^{3}}\right)\right)}{\left(\frac{1}{\left(0.80\right)}\right)\left(\left(5 \frac{1}{day}\right)\frac{\left(33.\overline{3} \frac{gCOD}{m^{3}}\right)}{\left(20 \frac{gCOD}{m^{3}}\right)+\left(33.\overline{3} \frac{gCOD}{m^{3}}\right)}\right)}\\ {\sf \overline{B}} &amp;= 53.\overline{3} \frac{gCOD}{m^{3}}<br /> <br /> **ITERATION 1**<br /> <br /> *Calculate Jacobian*<br /> <br /> .. math::<br /> <br /> J^{(0)}\left(40 \frac{gCOD}{m^{3}},20 \frac{gCOD}{m^{3}}\right) &amp;= \left[\begin{array}{ccc} -3.8194 \frac{1}{day} &amp; \quad , \quad &amp; -4.1667 \frac{1}{day}\\ 0.5556 \frac{1}{day} &amp; \quad , \quad &amp; 0.2083 \frac{1}{day}\\ \end{array}\right]<br /> <br /> *Calculate function values*<br /> <br /> .. raw:: latex<br /> <br /> \\<br /> {\footnotesize [The PDF version of the solutions is cut off on the right edge of the page; please refer to the course website to get the full solution.]}<br /> <br /> .. math::<br /> <br /> f^{(0)}\left(40 \frac{gCOD}{m^{3}},20 \frac{gCOD}{m^{3}}\right) &amp;= \left[\begin{array}{c} \frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)}\left(100 \frac{gCOD}{m^{3}}\right) - \frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)}\left(40 \frac{gCOD}{m^{3}}\right) - \left(\frac{1}{\left(0.80\right)}\right)\left(\left(5 \frac{1}{day}\right)\frac{\left(40 \frac{gCOD}{m^{3}}\right)}{\left(20 \frac{gCOD}{m^{3}}\right)+\left(40 \frac{gCOD}{m^{3}}\right)}\right)\left(20 \frac{gCOD}{m^{3}}\right) \\ - \frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)}\left(20 \frac{gCOD}{m^{3}}\right) + \left(\left(5 \frac{1}{day}\right)\frac{\left(40 \frac{gCOD}{m^{3}}\right)}{\left(20 \frac{gCOD}{m^{3}}\right)+\left(40 \frac{gCOD}{m^{3}}\right)}\right)\left(20 \frac{gCOD}{m^{3}}\right) \\ \end{array}\right]\\ &amp;= \left[\begin{array}{c} 104.1667 \frac{gCOD/m^{3}}{day} \\ 4.1667 \frac{gCOD/m^{3}}{day} \\ \end{array}\right]<br /> <br /> *Solve the system* :math:J\Delta x = -f (Use MATLAB or Python)<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{ccc} -3.8194 \frac{1}{day} &amp; \quad , \quad &amp; -4.1667 \frac{1}{day}\\ 0.5556 \frac{1}{day} &amp; \quad , \quad &amp; 0.2083 \frac{1}{day}\\ \end{array}\right] \Delta x &amp;= -\left[\begin{array}{c} 104.1667 \frac{gCOD/m^{3}}{day} \\ 4.1667 \frac{gCOD/m^{3}}{day} \\ \end{array}\right]\\ \Delta x &amp;= \left[\begin{array}{c} -25.7143 \frac{gCOD}{m^{3}} \\ 48.5714 \frac{gCOD}{m^{3}} \\ \end{array}\right]<br /> <br /> *calculate new x*<br /> <br /> .. math::<br /> <br /> x^{(1)} &amp;= x^{(0)} + \Delta x \\ x^{(1)} &amp;= \left[\begin{array}{c} 40 \frac{gCOD}{m^{3}} \\ 20 \frac{gCOD}{m^{3}} \\ \end{array}\right] + \left[\begin{array}{c} -25.7143 \frac{gCOD}{m^{3}} \\ 48.5714 \frac{gCOD}{m^{3}} \\ \end{array}\right]\\ x^{(1)} &amp;= \left[\begin{array}{c} 14.2857 \frac{gCOD}{m^{3}} \\ 68.5714 \frac{gCOD}{m^{3}} \\ \end{array}\right]<br /> <br /> *Calculate relative, absolute, and function error norms*<br /> <br /> We will assume an :math:\epsilon_{tol} of :math:10^{-8} and for this solution we will use the 2-norm.<br /> <br /> Relative error<br /> <br /> .. math::<br /> <br /> \frac{\parallel x^{(1)} - x^{(0)} \parallel}{\parallel x^{(1)} \parallel} &amp; &lt; \epsilon_{tol} \; ? \\ \frac{\parallel \Delta x \parallel}{\parallel x^{(1)} \parallel} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{\sum_{i=1}^{n} \left[ \Delta x_{i} \right]^{2}}{\sum_{i=1}^{n} \left[ x_{i}^{(1)} \right]^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{(-25.7143)^{2} + (48.5714)^{2}}{(14.2857)^{2} + (68.5714)^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ 0.7846 &amp; &lt; \epsilon_{tol} \; ? \; \text{No, so keep going!}<br /> <br /> Absolute error<br /> <br /> .. math::<br /> <br /> \frac{\parallel x^{(1)} - x^{(true)} \parallel}{\parallel x^{(true)} \parallel} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{\sum_{i=1}^{n} \left[ x_{i}^{(1)} - x_{i}^{(true)} \right]^{2}}{\sum_{i=1}^{n} \left[ x_{i}^{(true)} \right]^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{(14.2857 - 33.\overline{3})^{2} + (68.5714 - 53.\overline{3})^{2}}{(33.\overline{3})^{2} + (53.\overline{3})^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ 0.3878 &amp;&lt; \epsilon_{tol} \; ? \; \text{No, so keep going!}<br /> <br /> Function error<br /> <br /> .. math::<br /> <br /> \parallel f(x^{(1)}) \parallel &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\sum_{i=1}^{n} \left[ f(x^{(1)})_{i} \right]^{2}} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{(89.2857)^{2} + (-71.4286)^{2}} &amp; &lt; \epsilon_{tol} \; ? \\ 114.3415 &amp;&lt; \epsilon_{tol} \; ? \; \text{No, so keep going!}<br /> <br /> **ITERATION 2**<br /> <br /> *Calculate Jacobian*<br /> <br /> .. math::<br /> <br /> <br /> J^{(1)}\left(14.2857 \frac{gCOD}{m^{3}},68.5714 \frac{gCOD}{m^{3}}\right) &amp;= \left[\begin{array}{ccc} -10.4167 \frac{1}{day} &amp; \quad , \quad &amp; -2.6042 \frac{1}{day}\\ 5.8333 \frac{1}{day} &amp; \quad , \quad &amp; -1.0417 \frac{1}{day}\\ \end{array}\right]<br /> <br /> *Calculate function values*<br /> <br /> .. math::<br /> <br /> f^{(1)}\left(14.2857 \frac{gCOD}{m^{3}},68.5714 \frac{gCOD}{m^{3}}\right) &amp;= \left[\begin{array}{c} 89.2857 \frac{gCOD/m^{3}}{day} \\ -71.4286 \frac{gCOD/m^{3}}{day} \\ \end{array}\right]<br /> <br /> *Solve the system* :math:J\Delta x = -f (Use MATLAB or Python)<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{ccc} -10.4167 \frac{1}{day} &amp; \quad , \quad &amp; -2.6042 \frac{1}{day}\\ 5.8333 \frac{1}{day} &amp; \quad , \quad &amp; -1.0417 \frac{1}{day}\\ \end{array}\right] \Delta x &amp;= -\left[\begin{array}{c} 89.2857 \frac{gCOD/m^{3}}{day} \\ -71.4286 \frac{gCOD/m^{3}}{day} \\ \end{array}\right]\\ \Delta x &amp;= \left[\begin{array}{c} 10.7143 \frac{gCOD}{m^{3}} \\ -8.5714 \frac{gCOD}{m^{3}} \\ \end{array}\right]<br /> <br /> *Calculate new x*<br /> <br /> .. math::<br /> <br /> x^{(2)} &amp;= x^{(1)} + \Delta x \\ x^{(2)} &amp;= \left[\begin{array}{c} 14.2857 \frac{gCOD}{m^{3}} \\ 68.5714 \frac{gCOD}{m^{3}} \\ \end{array}\right] + \left[\begin{array}{c} 10.7143 \frac{gCOD}{m^{3}} \\ -8.5714 \frac{gCOD}{m^{3}} \\ \end{array}\right]\\ x^{(2)} &amp;= \left[\begin{array}{c} 25.0000 \frac{gCOD}{m^{3}} \\ 60.0000 \frac{gCOD}{m^{3}} \\ \end{array}\right]<br /> <br /> *Calculate relative, absolute, and function error norms*<br /> <br /> We will assume an :math:\epsilon_{tol} of :math:10^{-8} and for this solution we will use the 2-norm.<br /> <br /> Relative error<br /> <br /> .. math::<br /> <br /> \frac{\parallel x^{(2)} - x^{(1)} \parallel}{\parallel x^{(2)} \parallel} &amp; &lt; \epsilon_{tol} \; ? \\ \frac{\parallel \Delta x \parallel}{\parallel x^{(2)} \parallel} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{\sum_{i=1}^{n} \left[ \Delta x_{i} \right]^{2}}{\sum_{i=1}^{n} \left[ x_{i}^{(2)} \right]^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{(10.7143)^{2} + (-8.5714)^{2}}{(25.0000)^{2} + (60.0000)^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ 0.2111 &amp;&lt; \epsilon_{tol} \; ? \; \text{No, so keep going!}<br /> <br /> Absolute error<br /> <br /> .. math::<br /> <br /> \frac{\parallel x^{(2)} - x^{(true)} \parallel}{\parallel x^{(true)} \parallel} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{\sum_{i=1}^{n} \left[ x_{i}^{(2)} - x_{i}^{(true)} \right]^{2}}{\sum_{i=1}^{n} \left[ x_{i}^{(true)} \right]^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{(25.0000 - 33.\overline{3})^{2} + (60.0000 - 53.\overline{3})^{2}}{(33.\overline{3})^{2} + (53.\overline{3})^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ 0.1697 &amp;&lt; \epsilon_{tol} \; ? \; \text{No, so keep going!}<br /> <br /> Function error<br /> <br /> .. math::<br /> <br /> \parallel f(x^{(2)}) \parallel &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\sum_{i=1}^{n} \left[ f(x^{(2)})_{i} \right]^{2}} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{(26.0417)^{2} + (-20.8333)^{2}} &amp; &lt; \epsilon_{tol} \; ? \\ 33.3496 &amp; &lt; \epsilon_{tol} \; ? \; \text{No, so keep going!}<br /> <br /> Note that all three errors appear to be decreasing from step to step so we are fairly confident the algorithm is converging and in fact this is true. If one were to continue the iterative process it would converge to within the specified relative tolerance within 7 iterations.<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.5cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Assignment_4_-_2010_-_Solution&diff=1097 Assignment 4 - 2010 - Solution 2018-09-16T08:54:09Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 08 November 2010<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = <br /> | questions_text_alt = <br /> | solutions_PDF = Assignment-4-Solutions-2010.pdf<br /> | solutions_text_alt = Solutions, prepared by Ali and Kevin<br /> | other_instructions = Hand-in at class.<br /> }}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> .. |m3| replace:: m\ :sup:3<br /> <br /> .. rubric:: Solutions by Kevin and Ali<br /> <br /> Question 1 <br /> ==============<br /> <br /> A new type of thermocouple &lt;http://en.wikipedia.org/wiki/Thermocouple&gt;_ is being investigated by your group. These devices produce an *almost* linear voltage (millivolt) response at different temperatures. In practice though it is used the other way around: use the millivolt reading to predict the temperature. The process of fitting this linear model is called *calibration*. <br /> <br /> #. Use the following data to calibrate a linear model:<br /> <br /> ================= ==== ==== ==== ==== ==== ==== ==== ==== ==== ====<br /> Temperature [K] 273 293 313 333 353 373 393 413 433 453 <br /> ----------------- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----<br /> Reading [mV] 0.01 0.12 0.24 0.38 0.51 0.67 0.84 1.01 1.15 1.31 <br /> ================= ==== ==== ==== ==== ==== ==== ==== ==== ==== ====<br /> <br /> <br /> Show the linear model and provide the predicted temperature when reading 1.00 mV.<br /> <br /> #. Are you satisfied with this model, based on the coefficient of determination (:math:R^2) value? <br /> <br /> #. What is the model's standard error? Now, are you satisfied with the model's prediction ability, given that temperatures can usually be recorded to an accuracy of :math:\pm 0.1 K with most thermocouples.<br /> <br /> Solution<br /> --------<br /> <br /> #. The linear model is used to predict temperature given the reading in millivolts. The reason is that in modelling, in general, we specify as :math:X the variable(s) we always have available, while :math:y is the variable we would like to predict from the :math:X's.<br /> <br /> The model has the form: :math:T = a_0 + a_1V, where :math:T is temperature and :math:V is the voltage reading. Coefficients in the linear model are:<br /> <br /> .. math::<br /> <br /> T = 278.6 + 135.3 V <br /> <br /> indicating that an increase in 0.1 mV leads to an increase, on average, of 13.53 K in the temperature prediction.<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-4/images/voltage-linear-model.png<br /> :scale: 60<br /> :align: center<br /> <br /> The following code was used to fit the model and draw the plot.<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-4/code/voltage_linear_model.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-4/code/voltage_linear_model.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> If you used R to fit the model, you would written something like this::<br /> <br /> &gt; V &lt;- c(0.01, 0.12, 0.24, 0.38, 0.51, 0.67, 0.84, 1.01, 1.15, 1.31)<br /> &gt; T &lt;- c(273, 293, 313, 333, 353, 373, 393, 413, 433, 453)<br /> &gt; model &lt;- lm(T ~ V)<br /> &gt; summary(model)<br /> <br /> Call:<br /> lm(formula = T ~ V)<br /> <br /> Residuals:<br /> Min 1Q Median 3Q Max <br /> -6.9272 -2.1212 -0.1954 2.7480 5.4239 <br /> <br /> Coefficients:<br /> Estimate Std. Error t value Pr(&gt;|t|) <br /> (Intercept) 278.574 2.204 126.39 1.72e-14 ***<br /> V 135.298 2.922 46.30 5.23e-11 ***<br /> ---<br /> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 <br /> <br /> Residual standard error: 3.916 on 8 degrees of freedom<br /> Multiple R-squared: 0.9963, Adjusted R-squared: 0.9958 <br /> F-statistic: 2144 on 1 and 8 DF, p-value: 5.229e-11<br /> <br /> #. The :math:R^2 value from this linear fit is :math:R^2 = 0.996, which being so close to 1.0, implies the model fits the data very well - that's all. However, one cannot be satisfied with only an :math:R^2 value, as explained in class, it has nothing to do with whether the model's prediction accuracy is any good. So we can't tell anything from this number.<br /> <br /> #. The model's standard error is 3.9 K. If we assume the prediction error is normally distributed around the linear fit, this corresponds to one standard deviation. So 95% of our prediction error lies roughly within a range of :math:\pm 2\times 3.92 or :math:\pm 7.8 K. These are the dashed red lines drawn on the figure. (Please note: the true error intervals are not parallel to the regression line, they are curved; however the :math:\pm 2S_E limits are good-enough for this course and most engineering applications.<br /> <br /> This prediction ability of :math:\pm 8 K is probably not satisfying for most engineering applications, since we can predict temperatures far more accurately, over the range from 273K to 453K, using off-the-shelf commercial thermocouples. <br /> <br /> The purpose of this question is to mainly point out the misleading nature of :math:R^2 - this value looks really good: 99.6%, yet the actual purpose of the model, the ability to predict temperature from the millivolt reading has no relationship at all to this :math:R^2 value.<br /> <br /> .. sd(T) = 60.5<br /> .. diff(range(T)) = 180<br /> .. baseline_ratio = 60/180 = 0.3333<br /> .. SE = 3.9<br /> .. model_ratio = 3.9/180 = 0.0216<br /> .. ratio = 1 - 0.0216/0.3333 = 0.935: seems pretty good<br /> <br /> Question 2 <br /> ==============<br /> <br /> Batch reactors are often used to calculate reaction rate data, since the dynamic balances are a direct function of the reaction rate:<br /> <br /> .. math::<br /> <br /> \frac{dC_{\sf A}(t)}{dt} = r_{\sf A}<br /> <br /> assuming constant volume and assuming all energy-related terms are negligible. We are unsure about the order of our reaction rate, :math:\alpha, and we also don't know the reaction rate constant, :math:k in the expression :math:r_A = - k C_{\sf A}^{\alpha}.<br /> <br /> Integrating the above differential equation from time :math:t=0 to time :math:t=t gives:<br /> <br /> .. math::<br /> <br /> \int_{C_{\sf A,0}}^{C_{\sf A}}{ \frac{1}{C_{\sf A}^\alpha} dC_{\sf A}} &amp;= -k \int_{0}^{t}{dt} \\ \left. \frac{C_{\sf A}^{1-\alpha}}{1-\alpha} \right|_{C_{\sf A,0}}^{C_{\sf A}} &amp;= -kt \qquad \text{where}\,\, \alpha \neq 1<br /> <br /> The following concentrations are collected as a function of time<br /> <br /> ====================== ==== ==== ==== ==== ==== ==== ==== ==== ====<br /> Time [hours] 0 0.5 1 2 3 5 7.5 10 17 <br /> ---------------------- ---- ---- ---- ---- ---- ---- ---- ---- ----<br /> Concentration [mol/L] 3.5 1.5 0.92 0.47 0.29 0.16 0.09 0.06 0.03 <br /> ====================== ==== ==== ==== ==== ==== ==== ==== ==== ====<br /> <br /> #. Use these concentration-time data to determine the values of :math:\alpha and :math:k, using a least squares model.<br /> <br /> #. Once you have the rate constants, simulate the batch reactor (use the ode45 or Python integrator) and superimpose the data from the above table on your concentration-time trajectory. Do they agree? Which regions of the plot have the worst agreement (this is where you would collect more data in the future).<br /> <br /> <br /> Solution<br /> --------<br /> <br /> #. There are to approaches to this problem. One is to calculate finite differences :math:\left(\frac{dC_{\sf A}(t)}{dt}\right)_k and plot these against the assumed rate expression :math:\left(- k C_{\sf A}^{\alpha}\right)_k. The other approach is to first integrate the differential equation, as partly shown in the question, and then plot some function of concentration against time. <br /> <br /> The former approach is usually less accurate, because we are using concentration measurement (already measured with error) to estimate finite differences (which will have error), and then fit a linear model (which will have error). The latter approach, called the integral method in reactor design, is more accurate because it works directly with the concentration and time measurements. In this case the integral method is really messy, so we will use the easier, finite-difference method.<br /> <br /> The finite difference approximations are:<br /> <br /> .. math::<br /> <br /> \begin{array}{llll} t &amp; \text{Lowest error method} &amp; \text{Calculation} &amp; \text{Result}\\ \hline 0.5 &amp; \text{Central diff} &amp; (0.92-3.5)/1 &amp; -2.58\\ 1 &amp; \text{Backward diff} &amp; (0.92-1.5)/0.5 &amp; -1.16\\ 2 &amp; \text{Central diff} &amp; (0.29-0.92)/2 &amp; -0.315 \\ 3 &amp; \text{Backward diff} &amp; (0.29 -0.47)/1 &amp; -0.18 \\ 5 &amp; \text{Backward diff} &amp; (0.16-0.29)/2 &amp; -0.065 \\ 7.5 &amp; \text{Central diff} &amp; (0.06-0.16)/5 &amp; -0.02 \\ 10 &amp; \text{Backward diff} &amp; (0.06-0.09)/2.5&amp; -0.012\\ 17 &amp; \text{Backward diff} &amp; (0.03-0.06)/7 &amp; -.0043 \\\hline \end{array}<br /> <br /> The linear model can now be derived as follows:<br /> <br /> .. math::<br /> <br /> \frac{dC_{\sf A}(t)}{dt} &amp;= - k C_{\sf A}^{\alpha} \\ \ln\left(-\frac{dC_{\sf A}(t)}{dt}\right) &amp;= \ln(k) + \alpha \ln(C_{\sf A}) \\ y_k &amp;= a_0 + a_1 x_k <br /> <br /> Then in R one can write::<br /> <br /> &gt; log_dCdt &lt;- log(c(2.58, 1.16, 0.315, 0.18, 0.065, 0.02, 0.012, 0.0043))<br /> &gt; log_C &lt;- log(c(1.5, 0.92, 0.47, 0.29, 0.16, 0.09, 0.06, 0.03))<br /> &gt; model &lt;- lm(log_dCdt ~ log_C)<br /> &gt; summary(model)<br /> <br /> Call:<br /> lm(formula = log_dCdt ~ log_C)<br /> <br /> Residuals:<br /> Min 1Q Median 3Q Max <br /> -0.17327 -0.04749 0.04231 0.06375 0.10477 <br /> <br /> Coefficients:<br /> Estimate Std. Error t value Pr(&gt;|t|) <br /> (Intercept) 0.23971 0.06295 3.808 0.00888 ** <br /> log_C 1.65222 0.03165 52.202 3.32e-09 ***<br /> ---<br /> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 <br /> <br /> Residual standard error: 0.1139 on 6 degrees of freedom<br /> Multiple R-squared: 0.9978, Adjusted R-squared: 0.9974 <br /> F-statistic: 2725 on 1 and 6 DF, p-value: 3.316e-09 <br /> <br /> or in MATLAB and Python we can find the same result:<br /> <br /> .. math::<br /> <br /> \ln\left(-\frac{dC_{\sf A}(t)}{dt}\right) &amp;= \ln(k) + \alpha \ln(C_{\sf A}) \\ \ln\left(-\frac{dC_{\sf A}(t)}{dt}\right) &amp;= 0.2397 + 1.652 \ln(C_{\sf A}) \\ k &amp;= {\bf 1.271\,\,\text{hours}^{-1}}\\ \alpha &amp;= {\bf 1.65}<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-4/images/rate-fitting-linear-model.png<br /> :scale: 48<br /> :align: center<br /> <br /> #. We can simulate this and superimpose it on the given data to give the following plot. The agreement is good, probably OK for the purposes which we want this for: to know the rate constant and reaction rate. There is low agreement as the change in concentration is the greatest (probably due to error in our finite difference approximations). <br /> <br /> We should collect more data in the region between :math:t=0 and :math:t=5 hours, using equally spaced time steps, so we can accurately estimate the derivatives using the central difference approximation.<br /> <br /> The code to calculate the linear model and simulate the system is also provided.<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-4/images/rate-fitting-simulation.png<br /> :scale: 60<br /> :align: center<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-4/code/rate_fitting.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-4/code/rate_fitting.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> Question 3 <br /> ==============<br /> <br /> The yield from your lab-scale bioreactor, :math:y, is a function of reactor temperature, impeller speed and reactor type (one with with baffles and one without). You have collected these data from various experiments.<br /> <br /> .. csv-table:: <br /> :header: Temp = :math:T [°C], Speed = :math:S [RPM], Baffles = :math:B [Yes/No], Yield = :math:y [g]<br /> :widths: 30, 30, 30, 30<br /> <br /> 82, 4300, No, 51<br /> 90, 3700, Yes, 30<br /> 88, 4200, Yes, 40<br /> 86, 3300, Yes, 28<br /> 80, 4300, No, 49<br /> 78, 4300, Yes, 49<br /> 82, 3900, Yes, 44<br /> 83, 4300, No, 59<br /> 65, 4200, No, 61<br /> 73, 4400, No, 59<br /> 60, 4400, No, 57<br /> 60, 4400, No, 62<br /> 101, 4400, No, 42<br /> 92, 4900, Yes, 38<br /> <br /> #. Use a built-in software function in MATLAB or Python (please don't use Excel) to fit a linear model that predicts the bioreactor yield from the above variables. Your model should be of the form:<br /> <br /> .. math::<br /> <br /> \hat{y} = a_0 + a_\text{T} T + a_\text{S} S + a_\text{B} B<br /> <br /> *Hint*: convert the No/Yes variables to integers with 0=No and 1=Yes.<br /> <br /> #. Interpret the meaning of each coefficient in the model and comment on the standard error.<br /> <br /> #. Calculate and show the :math:\mathbf{X}^T\mathbf{X} and :math:\mathbf{X}^T\mathbf{y} matrices for this linear model in order to calculate the 4 coefficients in :math:{\bf a} = \left[a_0, a_T, A_S, A_B\right] yourself. Your answer must include the MATLAB or Python code that computes these matrices and the :math:{\bf a} solution vector.<br /> <br /> Solution<br /> --------<br /> <br /> .. note:: Initially, I assumed the lab computers had the Statistics Toolbox function in MATLAB so you could use the built-in regress.m function in MATLAB. Python users should however use the built-in np.linalg.lstsq(...) function. As a result all questions will be answered together below.<br /> <br /> After converting the Yes/No variable to integers you can fit a linear model of the form :math:\hat{y} = a_0 + a_\text{T} T + a_\text{S} S + a_\text{B} B. In R::<br /> <br /> &gt; y_yield &lt;- c(51,30,40,28,49,49,44,59,61,59,57,62,42,38)<br /> &gt; x_temp &lt;- c(82,90,88,86,80,78,82,83,65,73,60,60,101,92)<br /> &gt; x_speed &lt;- c(4300,3700,4200,3300,4300,4300,3900,4300,4200,4400,4400,4400,4400,4900)<br /> &gt; x_baffles &lt;- c(0,1,1,1,0,1,1,0,0,0,0,0,0,1)<br /> &gt; model = lm(y_yield ~ x_temp + x_speed + x_baffles)<br /> &gt; summary(model)<br /> <br /> Call:<br /> lm(formula = y_yield ~ x_temp + x_speed + x_baffles)<br /> <br /> Residuals:<br /> Min 1Q Median 3Q Max <br /> -6.0790 -3.4265 -0.7974 2.2026 7.9710 <br /> <br /> Coefficients:<br /> Estimate Std. Error t value Pr(&gt;|t|) <br /> (Intercept) 54.804336 18.404567 2.978 0.01386 * <br /> x_temp -0.486872 0.121692 -4.001 0.00251 **<br /> x_speed 0.008520 0.003784 2.252 0.04804 * <br /> x_baffles -9.271748 3.073260 -3.017 0.01296 * <br /> ---<br /> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 <br /> <br /> Residual standard error: 4.708 on 10 degrees of freedom<br /> Multiple R-squared: 0.8647, Adjusted R-squared: 0.8241 <br /> F-statistic: 21.3 on 3 and 10 DF, p-value: 0.0001156 <br /> <br /> which indicates that the model is given by:<br /> <br /> .. math::<br /> <br /> \hat{y} = 54.8 -0.487 T + 0.00852 S + -9.27 B<br /> <br /> The coefficients are interpreted as:<br /> <br /> * :math:a_\text{S} = 0.0085: a 100rpm increase in impeller speed serves to increase yield by 0.85g on average, keeping all other variables constant<br /> * :math:a_\text{B} = -9.3: the use of baffles decreases yield, on average, by 9.1g, keeping all other variables constant<br /> * :math:a_\text{T} = -0.49: each one degree increase in temperature lowers yield by 0.49g on average, keeping all other variables constant<br /> <br /> This information can be quite valuable: we can now decide, using process economics, which is the most cost-effective way to boost the yield from our bioreactor.<br /> <br /> The standard error is 4.7 g. Since we aren't given any information on the accuracy of the yield measurement, it is hard to quantify whether this is reasonable or not. In practice one would need to run several experiments under the same conditions and measure the yield. The standard deviation of those repeated experimental yields would tell us what the noise in our measurement is.<br /> <br /> We can fit the model in MATLAB or Python. Let the coefficient vector be :math:\mathrm{a} = [a_0, a_\text{S}, a_\text{B}, a_\text{T}], then we can write down the following X matrix to estimate it:<br /> <br /> .. math::<br /> \mathrm{X} = \begin{bmatrix} 1 &amp; 82 &amp; 4300 &amp; 0\\ 1 &amp; 90 &amp; 3700 &amp; 1\\ 1 &amp; 88 &amp; 4200 &amp; 1\\ 1 &amp; 86 &amp; 3300 &amp; 1\\ 1 &amp; 80 &amp; 4300 &amp; 0\\ 1 &amp; 78 &amp; 4300 &amp; 1\\ 1 &amp; 82 &amp; 3900 &amp; 1\\ 1 &amp; 83 &amp; 4300 &amp; 0\\ 1 &amp; 65 &amp; 4200 &amp; 0\\ 1 &amp; 73 &amp; 4400 &amp; 0\\ 1 &amp; 60 &amp; 4400 &amp; 0\\ 1 &amp; 60 &amp; 4400 &amp; 0\\ 1 &amp; 101 &amp; 4400 &amp; 0\\ 1 &amp; 92 &amp; 4900 &amp; 1 \end{bmatrix}<br /> <br /> Then we can construct the :math:\mathrm{X}^T\mathrm{X} matrix (called the *correlation matrix*), and the :math:\mathrm{X}^T\mathrm{y} vector (called the *covariance vector*).<br /> <br /> .. math::<br /> \mathrm{X}^T\mathrm{X} = \begin{bmatrix} 14 &amp; 1120 &amp; 59000 &amp; 6 \\ 1120 &amp; 91480 &amp; 4712500 &amp; 516 \\ 59000 &amp; 4712500 &amp; 250480000 &amp; 24300 \\ 6 &amp; 516 &amp; 24300 &amp; 6 \end{bmatrix} \qquad \text{and} \qquad \mathrm{X}^T\mathrm{y} = \begin{bmatrix} 669 \\ 52207 \\ 2847800 \\ 229 \end{bmatrix}<br /> <br /> Using these matrices to solve for :math:\mathrm{a}<br /> <br /> .. math::<br /> \mathrm{a} = \left(\mathrm{X}^T\mathrm{X} \right)^{-1}\mathrm{X}^T\mathrm{y} = \begin{bmatrix} 54.80 \\ -0.4869 \\ 0.00852 \\ -9.271 \end{bmatrix}<br /> <br /> This result matches the results from R. The MATLAB and Python code is shown below.<br /> <br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-4/code/batch_yields.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-4/code/batch_yields.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> Question 4 <br /> ==============<br /> <br /> .. Similar to Tutorial 8, 2009<br /> <br /> The viscosity of sulphuric acid, :math:\nu, varies with purity, :math:p in the following manner:<br /> <br /> ============================= ===== ===== ===== <br /> :math:p [%] 20 60 80<br /> ----------------------------- ----- ----- ----- <br /> :math:\nu [millipascal] 1.40 5.37 17.4<br /> ============================= ===== ===== ===== <br /> <br /> #. Express :math:\nu(p) as a quadratic function using Lagrange interpolating polynomials. Do not simplify the polynomial.<br /> #. Express :math:\nu(p) as a quadratic function using Newton interpolating polynomials. Do not simplify the polynomial.<br /> #. Fit a cubic spline through the data points: clearly show your :math:{\bf Xa = y} linear system of equations, then solve them using computer software; finally report your spline coefficients.<br /> #. Use computer software to plot:<br /> <br /> * the Newton interpolating polynomial <br /> * the cubic spline,<br /> * and the 3 data points on the same graph.<br /> #. What is the estimated viscosity at :math:p = 40\% purity using linear interpolation? <br /> #. Which of the estimation procedures that you used above has the closest estimate to the true value of 2.51 millipascal?<br /> <br /> Solution<br /> ---------<br /> <br /> #. Recall that an :math:n-1 order Lagrange interpolating polynomial, for a set of :math:n data points, is given by the following relation:<br /> <br /> .. math::<br /> <br /> P_{n-1}(x) = y_{1}\ell_{1}(x) + y_{2}\ell_{2}(x) + \ldots + y_{n}\ell_{n}(x) = \sum_{i=1}^{n}y_{i}\ell_{i}(x) \qquad \text{where} \qquad \ell_{i}(x) = \prod_{j=1, j\neq i}^{n} \frac{x - x_{i}}{x_{i} - x_{j}}<br /> <br /> For our system the quadratic Lagrange interpolating polynomial is the following:<br /> <br /> .. math::<br /> <br /> P_{2}(x) &amp;= y_{1}\ell_{1}(x) + y_{2}\ell_{2}(x) + \ldots + y_{n}\ell_{n}(x) \\ &amp;= 1.40 \cdot \frac{(x-60)(x-80)}{(20-60)(20-80)} + 5.37 \cdot \frac{(x-20)(x-80)}{(60-20)(60-80)} + 17.4 \cdot \frac{(x-20)(x-60)}{(80-20)(80-60)}\\<br /> <br /> <br /> #. Recall that an :math:n-1 order Newton interpolating polynomial, for a set of :math:n data points, is given by the following relation:<br /> <br /> .. math::<br /> <br /> P_{n-1}(x) = b_{0}+b_{1}(x-x_{1})+b_{2}(x-x_{1})(x-x_{2})+ \ldots +b_{n-1}(x-x_{1})\cdots(x-x_{n-1}) <br /> <br /> The coefficients, :math:b_i are best calculated in table form, known as &quot;Newton's divided differences&quot;:<br /> <br /> .. math::<br /> <br /> \begin{array}{cccc} \hline \hline p_{i} &amp; \nu_{i} &amp; \nu[p_{i},p_{i-1}] &amp; \nu[p_{i}, p_{i-1}, p_{i-2}] \\ \hline \hline 20 &amp; \textbf{1.40} &amp; \frac{5.37-1.40}{60-20}=\textbf{0.09925} &amp; \frac{0.6015-0.09925}{80-20}=\textbf{0.008371} \\ 60 &amp; 5.37 &amp; \frac{17.4-5.37}{80-60}=0.6015 &amp; \\ 80 &amp; 17.4 &amp; &amp; \\ \hline \hline \end{array}<br /> <br /> <br /> So the Newton interpolating polynomial is:<br /> <br /> .. math::<br /> <br /> P_{2}(x) = 1.40 + 0.09925(x-x_{1}) + 0.008371(x-x_{1})(x-x_{2})<br /> <br /> #. The cubic spline through 3 data points consists of two cubic polynomials -- one connecting points 1 and 2, and another connecting points 2 and 3.<br /> <br /> .. math::<br /> <br /> s_3(x) = \left\{ \begin{array}{l} a_1 + b_1x + c_1x^2 + d_1x^3 \qquad\,\,x_1 \leq x \leq x_2 \\ a_2 + b_2x + c_2x^2 + d_2x^3 \qquad\,\,x_2 \leq x \leq x_3 \end{array} \right.<br /> <br /> So we have 8 unknown coefficients in these 2 polynomials. The 8 linear equations that can be used to solve for the coefficients are:<br /> <br /> .. math::<br /> <br /> \begin{array}{ll} \text{Starting knot} &amp; s_3(x_1) = y_1 = a_1 + b_1x_1 + c_1x_1^2 + d_1x_1^3\\ \text{End knot} &amp; s_3(x_3) = y_3 = a_2 + b_2x_3 + c_2x_3^2 + d_2x_3^3\\ \text{Continuity} &amp; s_3(x_2) = \left\{ \begin{array}{l} y_2 = a_1 + b_1x_2 + c_1x_2^2 + d_1x_2^3 \\ y_2 = a_2 + b_2x_2 + c_2x_2^2 + d_2x_2^3 \end{array} \right.\\ \text{1st derivative continuity} &amp; s'_3(x_2) = b_1 + 2c_1x_2 + 3d_1x_2^2 = b_2 + 2c_2x_2 + 3d_2x_2^2 \\ \text{2nd derivative continuity} &amp; s''_3(x_2) = 2c_1 + 6d_1x_2 = 2c_2 + 6d_2x_2 \\ \text{$s''_3(x)=0$ at the ends} &amp; s''_3(x_1) = 0 = 2c_1 + 6d_1x_1 \\ &amp; s''_3(x_3) = 0 = 2c_2 + 6d_2x_3 \end{array}<br /> <br /> Expressing this as a linear system of equations, :math:{\bf Xa = y}:<br /> <br /> .. math::<br /> <br /> {\bf Xa} &amp;= {\bf y} \\ \left( \begin{array}{cccccccc} 0 &amp; 0 &amp; 2 &amp; 6x_1 \\ 1 &amp; x_1 &amp; x_1^2 &amp; x_1^3 \\ 1 &amp; x_2 &amp; x_2^2 &amp; x_2^3 \\ 0 &amp; 1 &amp; 2x_2 &amp; 3x_2^2 &amp; 0 &amp; -1 &amp; -2x_2 &amp; -3x_2^2 \\ 0 &amp; 0 &amp; 2 &amp; 6x_2 &amp; 0 &amp; 0 &amp; -2 &amp; -6x_2 \\ &amp; &amp; &amp; &amp; 1 &amp; x_2 &amp; x_2^2 &amp; x_2^3 \\ &amp; &amp; &amp; &amp; 1 &amp; x_3 &amp; x_3^2 &amp; x_3^3 \\ &amp; &amp; &amp; &amp; &amp; &amp; 2 &amp; 6x_3 \end{array}\right) \left( \begin{array}{c} a_1 \\ b_1 \\ c_1 \\ d_1 \\ a_2 \\ b_2 \\ c_2 \\ d_2 \end{array} \right) &amp;= \left( \begin{array}{c} 0 \\ y_1 \\ y_2 \\ 0 \\ 0 \\ y_2 \\ y_3 \\ 0 \end{array} \right) \\ \left( \begin{array}{cccccccc} 0 &amp; 0 &amp; 2 &amp; 120 \\ 1 &amp; 20 &amp; 400 &amp; 8000 \\ 1 &amp; 60 &amp; 3600 &amp; 216000 \\ 0 &amp; 1 &amp; 120 &amp; 10800 &amp; 0 &amp; -1 &amp; -120 &amp; -10800 \\ 0 &amp; 0 &amp; 2 &amp; 360 &amp; 0 &amp; 0 &amp; -2 &amp; -360 \\ &amp; &amp; &amp; &amp; 1 &amp; 60 &amp; 3600 &amp; 216000 \\ &amp; &amp; &amp; &amp; 1 &amp; 80 &amp; 6400 &amp; 512000 \\ &amp; &amp; &amp; &amp; 0 &amp; 0 &amp; 2 &amp; 480 \end{array}\right) \left( \begin{array}{c} a_1 \\ b_1 \\ c_1 \\ d_1 \\ a_2 \\ b_2 \\ c_2 \\ d_2 \end{array} \right) &amp;= \left( \begin{array}{c} 0 \\ 1.40 \\ 5.37 \\ 0 \\ 0 \\ 5.37 \\ 17.4 \\ 0 \end{array} \right)<br /> <br /> Solving this system of equations with computer software gives:<br /> <br /> .. math::<br /> <br /> s_3(x) = \left\{ \begin{array}{l} 1.93 + 0.0579x - 6.28\times10^{-3} x^2 + 1.05\times10^{-4}x^3 \qquad\,\,x_1 \leq x \leq x_2 \\ 69.73 - 3.33x + 5.02\times10^{-2} x^2 - 2.09\times10^{-4}x^3 \qquad\,\,x_2 \leq x \leq x_3 \end{array} \right. <br /> <br /> #. A plot of the Lagrange (or Newton) polynomial, the spline, the linear interpolation (not required) superimposed on the 3 original data points is shown below.<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-4/images/q1_plot.png<br /> :scale: 45<br /> :align: center<br /> <br /> The code used to generate this plot is similar to that shown on course software tutorial &lt;http://modelling3e4.connectmv.com/wiki/Software_tutorial/Creating_and_saving_plots&gt;_.<br /> <br /> <br /> #. The linear interpolation for :math:\nu(p=40) has slope = :math:\displaystyle \frac{5.37 - 1.40}{60 - 20} = 0.09925 and intercept = :math:1.40 - 0.09925 \times 20 = -0.585. So :math:\nu(p=40) = 0.09925 \times 40 - 0.585 = 3.385 millipascal.<br /> <br /> #. The question was a bit unclear; the intention was to compare all estimates of :math:\nu(p=40):<br /> <br /> * Lagrange polynomial estimate = :math:1.40 \cdot \frac{(40-60)(40-80)}{(20-60)(20-80)}+ 5.37 \cdot \frac{(40-20)(40-80)}{(60-20)(60-80)} + 17.4 \cdot \frac{(40-20)(40-60)}{(80-20)(80-60)} = 1.40(0.3333) + 5.37(1.0) + 17.4(-0.333) = 0.0366 millipascals.<br /> * Newton polynomial estimate = :math:1.40 + 0.09925(40-20) + 0.008371(40-20)(40-60) = 0.0366 millipascals.<br /> * Cubic spline estimate = :math:1.93 + 0.0579\times 40 - 6.28\times10^{-3} \times 40^2 + 1.05\times10^{-4} \times 40^3 = 0.918 millipascals.<br /> * Linear interpolation = 3.385 millipascal.<br /> <br /> All estimates had large error when compared to the true value of 2.51, because the viscosity is changing fairly rapidly in this neighbourhood. The estimates would be improved if there was another data point close by. The Lagrange/Newton estimates are especially poor, since they are negative in the range from approximately 30% to 40% purity.<br /> <br /> Question 5 <br /> ===============<br /> <br /> The following data are collected from a bioreactor experiment, during the growth phase.<br /> <br /> ================== ===== ===== ===== ===== =====<br /> Time [hours] 0 1.0 2.0 4.0 6.0<br /> ------------------ ----- ----- ----- ----- -----<br /> :math:C [g/L] 0.1 0.341 1.102 4.95 11.24<br /> ================== ===== ===== ===== ===== =====<br /> <br /> Fit a natural cubic spline for these data and use it to estimate the number of cells at time 3, 5, and 7 hours.<br /> <br /> Show your matrix derivation for the linear system of equations, and solve it using computer software. Plot the cubic spline over the time range 0 to 8 hours.<br /> <br /> Solution<br /> ---------<br /> <br /> The cubic spline through 5 data points consists of four cubic polynomials -- connecting points 1 and 2, 2 and 3, 3 and 4, and 4 and 5.<br /> <br /> .. math::<br /> <br /> s_3(x) = \left\{ \begin{array}{l} a_1 + b_1x + c_1x^2 + d_1x^3 \qquad\,\,x_1 \leq x \leq x_2 \\ a_2 + b_2x + c_2x^2 + d_2x^3 \qquad\,\,x_2 \leq x \leq x_3\\ a_3 + b_3x + c_3x^2 + d_3x^3 \qquad\,\,x_3 \leq x \leq x_4\\ a_4 + b_4x + c_4x^2 + d_4x^3 \qquad\,\,x_4 \leq x \leq x_5 \end{array} \right.<br /> <br /> So we have 16 unknown coefficients in these 4 polynomials. The 16 linear equations that can be used to solve for the coefficients are:<br /> <br /> .. math::<br /> <br /> \begin{array}{ll} \text{Starting knot} &amp; s_3(x_1) = y_1 = a_1 + b_1x_1 + c_1x_1^2 + d_1x_1^3\\ \text{End knot} &amp; s_3(x_5) = y_5 = a_2 + b_2x_5 + c_2x_5^2 + d_2x_5^3\\ \text{Continuity} &amp; s_3(x_2) = \left\{ \begin{array}{l} y_2 = a_1 + b_1x_2 + c_1x_2^2 + d_1x_2^3 \\ y_2 = a_2 + b_2x_2 + c_2x_2^2 + d_2x_2^3 \end{array} \right.\\ \text{Continuity} &amp; s_3(x_3) = \left\{ \begin{array}{l} y_3 = a_2 + b_2x_3 + c_2x_3^2 + d_2x_3^3 \\ y_3 = a_3 + b_3x_3 + c_3x_3^2 + d_3x_3^3 \end{array} \right.\\ \text{Continuity} &amp; s_3(x_4) = \left\{ \begin{array}{l} y_4 = a_3 + b_3x_4 + c_3x_4^2 + d_3x_4^3 \\ y_4 = a_4 + b_4x_4 + c_4x_4^2 + d_4x_4^3 \end{array} \right.\\ \text{1st derivative continuity} &amp; s'_3(x_2) = b_1 + 2c_1x_2 + 3d_1x_2^2 = b_2 + 2c_2x_2 + 3d_2x_2^2 \\ \text{1st derivative continuity} &amp; s'_3(x_3) = b_2 + 2c_2x_3 + 3d_2x_3^2 = b_3 + 2c_3x_3 + 3d_3x_3^2 \\ \text{1st derivative continuity} &amp; s'_3(x_4) = b_3 + 2c_3x_4 + 3d_3x_4^2 = b_4 + 2c_4x_4 + 3d_4x_4^2 \\ \text{2nd derivative continuity} &amp; s''_3(x_2) = 2c_1 + 6d_1x_2 = 2c_2 + 6d_2x_2 \\ \text{2nd derivative continuity} &amp; s''_3(x_3) = 2c_2 + 6d_2x_3 = 2c_3 + 6d_3x_3 \\ \text{2nd derivative continuity} &amp; s''_3(x_4) = 2c_3 + 6d_3x_4 = 2c_4 + 6d_4x_4 \\ \text{$s''_3(x)=0$ at the ends} &amp; s''_3(x_1) = 0 = 2c_1 + 6d_1x_1 \\ &amp; s''_3(x_5) = 0 = 2c_2 + 6d_2x_5 \end{array}<br /> <br /> Expressing this as a linear system of equations, :math:{\bf Xa = y} (see the lecture notes for the matrix pattern):<br /> <br /> .. math::<br /> <br /> {\bf Xa} &amp;= {\bf y} \\ \left( \begin{array}{cccccccccccccccc} 0 &amp; 0 &amp; 2 &amp; 6x_1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 1 &amp; x_1 &amp; x_1^2 &amp; x_1^3 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 1 &amp; x_2 &amp; x_2^2 &amp; x_2^3 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 2x_2 &amp; 3x_2^2 &amp; 0 &amp; -1 &amp; -2x_2 &amp; -3x_2^2 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 2 &amp; 6x_2 &amp; 0 &amp; 0 &amp; -2 &amp; -6x_2 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; x_2 &amp; x_2^2 &amp; x_2^3 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; x_3 &amp; x_3^2 &amp; x_3^3 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; 2x_3 &amp; 3x_3^2 &amp; 0 &amp; -1 &amp; -2x_3 &amp; -3x_3^2 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 2 &amp; 6x_3 &amp; 0 &amp; 0 &amp; -2 &amp; -6x_3 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; x_3 &amp; x_3^2 &amp; x_3^3 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; x_4 &amp; x_4^2 &amp; x_4^3 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; 2x_4 &amp; 3x_4^2 &amp; 0 &amp; -1 &amp; -2x_4 &amp; -3x_4^2 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 2 &amp; 6x_4 &amp; 0 &amp; 0 &amp; -2 &amp; -6x_4 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp;0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; x_4 &amp; x_4^2 &amp; x_4^3 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp;0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; x_5 &amp; x_5^2 &amp; x_5^3 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 2 &amp; 6x_5 \end{array}\right) \left( \begin{array}{c} a_1 \\ b_1 \\ c_1 \\ d_1 \\ a_2 \\ b_2 \\ c_2 \\ d_2 \\ a_3 \\ b_3 \\ c_3 \\ d_3 \\ a_4 \\ b_4 \\ c_4 \\ d_4 \end{array} \right) &amp;= \left( \begin{array}{c} 0 \\ y_1 \\ y_2 \\ 0 \\ 0 \\ y_2 \\ y_3 \\ 0 \\ 0 \\ y_3 \\ y_4 \\ 0 \\ 0 \\ y_4 \\ y_5 \\ 0 \\ \end{array} \right) \\ \left( \begin{array}{cccccccccccccccc} 0 &amp; 0 &amp; 2 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 1 &amp; 1 &amp; 1 &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 2 &amp; 3 &amp; 0 &amp; -1 &amp; -2 &amp; -3 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 2 &amp; 6 &amp; 0 &amp; 0 &amp; -2 &amp; -6 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; 1 &amp; 1 &amp; 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; 2 &amp; 4 &amp; 8 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; 4 &amp; 12 &amp; 0 &amp; -1 &amp; -4 &amp; -12 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 2 &amp; 12 &amp; 0 &amp; 0 &amp; -2 &amp; -12 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; 2 &amp; 4 &amp; 8 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; 4 &amp; 16 &amp; 64 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; 8 &amp; 48 &amp; 0 &amp; -1 &amp; -8 &amp; -48 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 2 &amp; 24 &amp; 0 &amp; 0 &amp; -2 &amp; -24 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp;0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; 4 &amp; 16 &amp; 64 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp;0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; 6 &amp; 36 &amp; 216 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 2 &amp; 36 \end{array}\right) \left( \begin{array}{c} a_1 \\ b_1 \\ c_1 \\ d_1 \\ a_2 \\ b_2 \\ c_2 \\ d_2 \\ a_3 \\ b_3 \\ c_3 \\ d_3 \\ a_4 \\ b_4 \\ c_4 \\ d_4 \end{array} \right) &amp;= \left( \begin{array}{c} 0 \\ 0.1 \\ 0.341 \\ 0 \\ 0 \\ 0.341 \\ 1.102 \\ 0 \\ 0 \\ 1.102 \\ 4.95 \\ 0 \\ 0 \\ 4.95 \\ 11.24 \\ 0 \\ \end{array} \right) \\<br /> <br /> Solving this system of equations with computer software gives:<br /> <br /> .. math::<br /> <br /> s_3(x) = \left\{ \begin{array}{l} 0.1000 + 0.1457x + 0\times x^2 + 0.0953x^3 \qquad\,\,0 \leq x \leq 1 \\ 0.1521 -0.0106x + 0.1562 x^2 + 0.0433x^3 \qquad\,\,1 \leq x \leq 2\\ 0.5809 -0.6537x + 0.4778x^2 -0.0103x^3 \qquad\,\,2 \leq x \leq 4\\ 3.6951 + -2.9895x + 1.0617x^2 + -0.0590x^3 \qquad\,\,4 \leq x \leq 6 \end{array} \right. <br /> <br /> Now we can estimate the number of cells at time 3, 5, and 7 hours. All we need is to see which of the above cubic polynomials are appropriate for each point:<br /> <br /> :math:2\leq x=3\leq 4\rightarrow :math:C(3)\approx 0.5809 -0.6537(3) + 0.4778(3)^2 -0.0103(3)^3=2.6411<br /> <br /> :math:4\leq x=5\leq 6\rightarrow :math:C(5)\approx 3.6951 + -2.9895(5) + 1.0617(5)^2 + -0.0590(5)^3=7.9180<br /> <br /> The last point 7 is not within the domain of interpolation for any of the intervals. So, we need to extrapolate the last polynomial (which is the closet to :math:x=7):<br /> <br /> :math:C(7)\approx 3.6951 + -2.9895(7) + 1.0617(7)^2 + -0.0590(7)^3=14.5620<br /> <br /> The cubic spline is plotted below, followed by the code used to generate the estimate values.<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-4/images/spline_q5.png<br /> :scale: 45<br /> :align: center<br /> <br /> **Note**: Polynomial coefficients in MATLAB and Python are sorted from the highest order to the lowest one, which is in the reverse direction we wrote the above polynomials. So, make sure you flip the coefficients (in the decreasing order) when using these polynomials in software.<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-4/code/cubic_spline_5pts.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-4/code/cubic_spline_5pts_python.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> Bonus question [0.5]<br /> ====================<br /> <br /> Use the cubic spline from the previous question and find the time where the cell count was approximately 10.0 g/L. Do not solve the equation by hand, but investigate MATLAB's &lt;http://www.mathworks.com/help/techdoc/ref/roots.html&gt;_ or Python's &lt;http://docs.scipy.org/doc/numpy/reference/generated/numpy.roots.html&gt;_ polynomial root finding function: roots(...)<br /> <br /> Solution<br /> ---------<br /> <br /> The value of :math:C=10 falls between 4.95 (at :math:t=4) and 11.24 (at :math:t=6) in the table of experimental data . So, we use the last spline polynomial that represents the points in this range:<br /> <br /> :math:C(t)\approx 3.6951 + -2.9895(t) + 1.0617(t)^2 + -0.0590(t)^3=10.0 \rightarrow<br /> <br /> :math:C(t)\approx -6.3049 + -2.9895(t) + 1.0617(t)^2 + -0.0590(t)^3=0<br /> <br /> This is a simple polynomial root finding problem which can be solved using either<br /> <br /> * MATLAB: roots([-0.0590, 1.0617, -2.9895, -6.3049])<br /> * Python: np.roots([-0.0590, 1.0617, -2.9895, -6.3049])<br /> <br /> Since it is a cubic function, three roots are returned: :math:t_1 = 13.7417, t_2 = 5.6336, t_3 = -1.3804.<br /> <br /> In order to decide which one is the desired solution, we again look at the tabulated data. Since the :math:C value falls between 4.95 and 11.24, the corresponding :math:t value must also fall between 4.0 and 6.0. So, the desired root is :math:t_2 = 5.6336.<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.5cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Assignment_3_-_2010_-_Solution&diff=1096 Assignment 3 - 2010 - Solution 2018-09-16T08:51:26Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 01 November 2010<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = Assignment-3-2010.pdf<br /> | questions_text_alt = Assignment questions<br /> | solutions_PDF = Assignment-3-2010-Solutions.pdf<br /> | solutions_text_alt = Assignment solutions, by Ali, Elliot and Kevin<br /> | other_instructions = <br /> }}<br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> .. |m3| replace:: m\ :sup:3<br /> <br /> Question 1 <br /> ================<br /> <br /> The Secant method is the most widely used algorithm for solving a nonlinear equation, in chemical engineering, and other areas. Write a MATLAB or Python function that implements the Secant method, using a minimum number of function evaluations. (Python users: please do not just copy the built-in Secant method).<br /> <br /> Your function should accept the following inputs:<br /> <br /> * a function handle to :math:f(x) [see the software tutorial &lt;http://modelling3e4.connectmv.com/wiki/Software_tutorial/Functions_as_objects&gt;_ on the course website for a description of function handles]<br /> * a relaxation coefficient, :math:0 &lt; \alpha \leq 1.0<br /> * the stopping tolerance, :math:\varepsilon_\text{tol}<br /> * the maximum number of iterations, maxiter (default = 200)<br /> * a single starting guess, :math:x^{(0)}<br /> <br /> Your code should automatically calculate :math:x^{(-1)} = 1.001x^{(0)}, and should use a minimum number of function evaluations. The stopping criterion should be as shown in the course notes:<br /> <br /> .. math::<br /> <br /> \left|f(x^{(k)})\right| \leq \epsilon_{\rm tol} \qquad \text{and/or} \qquad \left|\frac{x^{(k)}-x^{(k-1)}}{x^{(k)}}\right| \leq \epsilon_{\rm tol}<br /> <br /> Please print out a table at each iteration that shows these 5 outputs:<br /> <br /> * Iteration :math:k <br /> * :math:x^{(k)}<br /> * :math:f(x^{(k)}) <br /> * :math:\displaystyle \left|\frac{x^{(k)}-x^{(k-1)}}{x^{(k)}}\right| <br /> * Relaxed :math:\Delta x <br /> <br /> Solution<br /> ---------<br /> <br /> See the following MATLAB and Python code:<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-3/code/secant_method.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-3/code/secant.py<br /> :language2: python<br /> :header2: Python code <br /> <br /> Question 2 <br /> ===============<br /> <br /> In your reactor design course you will learn about adiabatic CSTR's exhibiting multiple steady states, depending on the reactor inlet temperature. In practice reactors are sometimes operated at an unstable steady point, due to process economics or improved conversion. The equation below was derived for such a system:<br /> <br /> .. math::<br /> <br /> f(T) = (T-T_\text{feed}) - 150 \times \frac{1.34\times 10^9 e^{-\frac{7554}{T}}}{1+1.34\times 10^9 e^{-\frac{7554}{T}}}<br /> <br /> There is more than one temperature at which :math:f(T) = 0; each :math:f(T) = 0 corresponds to a reactor steady state. Use the secant method, with relaxation factor :math:\alpha = 0.9, and :math:T_\text{feed} = 300 K to find one of these steady state temperatures, using starting values of :math:T^{(0)}=320 K and :math:T^{(-1)}= 1.001 \times 320 K.<br /> <br /> #. Show the first 3 iterations by hand. Explains what happens in the 3rd iteration, by means of a plot of :math:f(x), and marking the iterations used so far on this plot.<br /> #. What could you change to avoid the problem in iteration 3?<br /> #. Compare your manual calculations in step 1 with the computer output from the previous question. Also report the temperature your computer code finds for :math:f(T)=0 at this starting guess. Select a reasonable tolerance value.<br /> #. Use the computer code from question 1, but with a starting value of :math:T = 400 K, and report the steady state operating temperature found.<br /> #. Comment on the solutions found from the two different starting points.<br /> <br /> Solution<br /> ----------<br /> <br /> First we evaluate the function at the two initial guesses (given :math:T_\text{feed}=300): :math:f(T^{(0)}=320)=9.535, and :math:f(T^{(-1)})=1.001\times 320)= 9.623. Now we apply the secant formula to find the first approximation :math:T^{(1)}:<br /> <br /> .. math::<br /> <br /> T^{(1)}=T^{(0)}-\alpha\displaystyle\frac{f(T^{(0)})}{\displaystyle\frac{f(T^{(0)})-f(T^{(-1)})}{T^{(0)}-T^{(-1)}}}= 320.0 - 0.9 \displaystyle \frac{9.535}{\displaystyle \frac{9.535 - 9.623}{320 - 320.32}} = 320.0 - 0.9 \times 34.67 = 288.8<br /> <br /> Evaluate :math:f(T^{(1)}), and repeat the above calculation to obtain :math:T^{(1)}:<br /> <br /> .. math::<br /> <br /> T^{(2)}=T^{(1)}-\alpha\displaystyle\frac{f(T^{(1)})}{\displaystyle\frac{f(T^{(1)})-f(T^{(0)})}{T^{(1)}-T^{(0)}}}= 304.5\\<br /> <br /> Evaluate :math:f(T^{(2)}), and repeat the above calculation to obtain :math:T^{(3)}:<br /> <br /> .. math::<br /> T^{(3)}=T^{(2)}-\alpha\displaystyle\frac{f(T^{(2)})}{\displaystyle\frac{f(T^{(2)})-f(T^{(1)})}{T^{(2)}-T^{(1)}}}=303.2<br /> <br /> The first three iterations (and the initial guess) are plotted below:<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-3/images/secant-iterations.png<br /> :scale: 50<br /> :align: center<br /> <br /> As you can see above, the first three iterations result in reasonable values, meaning that, as a sign of convergence, the difference between successive temperatures are getting less and less. Also, the value of the function itself is continually getting close to zero with the iterations. However, if you got, for example, negative absolute temperatures which is not physically acceptable, or the function value diverged from zero instead of getting closer to it, you could change your initial guesses or the :math:\alpha value to avoid the problem. <br /> <br /> The output from the code in question 1 is as follows::<br /> <br /> Iteration x f(x) Rel step alpha * Delta x<br /> 1 320.00000 9.53534 0.00100 -31.17468<br /> 2 288.82532 -12.04971 0.10794 15.66275<br /> 3 304.48807 1.18307 0.05144 -1.26028<br /> 4 303.22778 0.24013 0.00416 -0.28886<br /> 5 302.93892 0.02004 0.00095 -0.02368<br /> 6 302.91525 0.00194 0.00008 -0.00228<br /> 7 302.91296 0.00019 0.00001 -0.00023<br /> 8 302.91274 0.00002 0.00000 -0.00002<br /> 9 302.91271 0.00000 0.00000 -0.00000<br /> 10 302.91271 0.00000 0.00000 -0.00000<br /> 302.912711645<br /> <br /> The output from the code matches the hand calculations (otherwise something is wrong!). With the tolerance chosen as :math:\varepsilon_\text{tol}=\sqrt{\text{eps}}, the obtained temperature on convergence will be :math:T^{(10)}=302.9 K.<br /> <br /> With a different initial guess as :math:T^{(0)}=400.0, and all other settings being the same, the obtained temperature will be: :math:T^7=447.7. You see that a different initial guess resulted in a far different solution. This is because the given function has multiple solutions. When an iterative method is used to solve the function, the obtained solution will depend on the choice of the initial guess. So, in order to find all solutions to a nonlinear equation, one way is to try different initial guesses. (Now try :math:T^{(0)}=340.0 and see what you get!)<br /> <br /> The following computer code was used to solve this question:<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-3/code/Q2_func.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-3/code/reactor.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> Question 3 <br /> ==============<br /> <br /> In assignment 1 we derived the dynamic balance for a bioreactor system. The steady state nutrient concentration, :math:{\sf \overline{N}} and the steady state biomass concentration, :math:{\sf \overline{B}}, can be shown to be:<br /> <br /> .. math::<br /> <br /> \frac{dN}{dt} &amp;= 0 = {\sf \overline{N}}_\text{in}\frac{F}{V} - {\sf \overline{N}}\frac{F}{V} - \left(\frac{1}{Y_{B}}\right)\left(\mu_{\text{max}}\frac{{\sf \overline{N}}}{K+{\sf \overline{N}}}\right){\sf \overline{B}}\\ \frac{dB}{dt} &amp;= 0 = - {\sf \overline{B}}\frac{F}{V} + \left(\mu_{\text{max}}\frac{{\sf \overline{N}}}{K+{\sf \overline{N}}}\right){\sf \overline{B}}<br /> <br /> Although we showed in assignment 1 that we can solve one equation and substitute it into the other, you should not do that for this question. <br /> <br /> Rather, let :math:\displaystyle \frac{dN}{dt} = f_1({\sf \overline{N}}, {\sf \overline{B}}) = 0 and let :math:\displaystyle \frac{dB}{dt} = f_2({\sf \overline{N}}, {\sf \overline{B}}) = 0. Solve this coupled set of nonlinear equations for :math:{\bf x} = ({\sf \overline{N}}, {\sf \overline{B}}) using Newton's method.<br /> <br /> Use the following approach in your answer:<br /> <br /> #. Construct the Jacobian matrix, :math:{\bf J}, symbolically in terms of the variables used in the equations.<br /> #. Simplify the Jacobian by substituting in these known constants:<br /> <br /> * :math:{\sf \overline{N}}_\text{in} = 100 g/|m3|<br /> * :math:F = 5000 |m3|/day<br /> * :math:V = 1600 |m3|<br /> * :math:Y_B = 0.80 conversion efficiency<br /> * :math:\mu_\text{max} = 5 1/day<br /> * :math:K = 20 g/|m3|<br /> <br /> #. Let your initial guess be :math:{\bf x}^{(0)} = (40 \text{g/m$^3$}, 20 \text{g/m$^3$}). Calculate 2 iterations of the multivariate Newton-Raphson method, to show :math:{\bf x}^{(1)} and :math:{\bf x}^{(2)}. Take a full Newton step at every iteration.<br /> #. Calculate and show the convergence tolerance, using the 2-norm for each iteration. <br /> #. Compare these iterations to the true solution presented by Elliot in assignment 1. Is Newton's method converging to this answer?<br /> <br /> Feel free to code up the Jacobian matrix, and the functions :math:f_1 and :math:f_2 in MATLAB or Python and use your code to evaluate :math:{\bf J}({\sf \overline{N}}, {\sf \overline{B}}), to avoid manual calculation errors.<br /> <br /> Solution<br /> ----------<br /> <br /> #. We start by deriving the Jacobian matrix for our two function system:<br /> <br /> .. math::<br /> <br /> J\left({\sf \overline{N}},{\sf \overline{B}}\right) &amp;= \left[\begin{array}{ll} \frac{\partial f_1({\sf \overline{N}}, {\sf \overline{B}})}{\partial {\sf \overline{N}}} &amp; \frac{\partial f_1({\sf \overline{N}}, {\sf \overline{B}})}{\partial {\sf \overline{B}}} \\ \frac{\partial f_2({\sf \overline{N}}, {\sf \overline{B}})}{\partial {\sf \overline{N}}} &amp; \frac{\partial f_2({\sf \overline{N}}, {\sf \overline{B}})}{\partial {\sf \overline{B}}} \\ \end{array}\right]\\ J\left({\sf \overline{N}},{\sf \overline{B}}\right) &amp;= \left[\begin{array}{ccc} - \frac{F}{V} - \left(\frac{1}{Y_{B}}\right)\left(\mu_{\text{max}}\frac{1}{K+{\sf \overline{N}}}\right){\sf \overline{B}} + \left(\frac{1}{Y_{B}}\right)\left(\mu_{\text{max}}\frac{{\sf \overline{N}}}{\left(K+{\sf \overline{N}}\right)^{2}}\right){\sf \overline{B}} &amp; \quad , \quad &amp; - \left(\frac{1}{Y_{B}}\right)\left(\mu_{\text{max}}\frac{{\sf \overline{N}}}{K+{\sf \overline{N}}}\right) \\ \left(\mu_{\text{max}}\frac{1}{K+{\sf \overline{N}}}\right){\sf \overline{B}} - \left(\mu_{\text{max}}\frac{{\sf \overline{N}}}{\left(K+{\sf \overline{N}}\right)^{2}}\right){\sf \overline{B}} &amp; \quad , \quad &amp; - \frac{F}{V} + \left(\mu_{\text{max}}\frac{{\sf \overline{N}}}{K+{\sf \overline{N}}}\right) \\ \end{array}\right] <br /> <br /> #. Subbing in the constant values provided for this question results in the following simplified Jacobian.<br /> <br /> .. raw:: latex<br /> <br /> \\<br /> {\footnotesize [The PDF version of the solutions is cut off on the right edge of the page; please refer to the course website to get the full solution.]}<br /> <br /> .. math::<br /> <br /> J\left({\sf \overline{N}},{\sf \overline{B}}\right) &amp;= \left[\begin{array}{ccc} - \frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)} - \left(\frac{1}{\left(0.80\right)}\right)\left(\left(5 \frac{1}{day}\right)\frac{1}{\left(20 \frac{gCOD}{m^{3}}\right)+{\sf \overline{N}}}\right){\sf \overline{B}} + \left(\frac{1}{\left(0.80\right)}\right)\left(\left(5 \frac{1}{day}\right)\frac{{\sf \overline{N}}}{\left(\left(20 \frac{gCOD}{m^{3}}\right)+{\sf \overline{N}}\right)^{2}}\right){\sf \overline{B}} &amp; \quad , \quad &amp; - \left(\frac{1}{\left(0.80\right)}\right)\left(\left(5 \frac{1}{day}\right)\frac{{\sf \overline{N}}}{\left(20 \frac{gCOD}{m^{3}}\right)+{\sf \overline{N}}}\right) \\ \left(\left(5 \frac{1}{day}\right)\frac{1}{\left(20 \frac{gCOD}{m^{3}}\right)+{\sf \overline{N}}}\right){\sf \overline{B}} - \left(\left(5 \frac{1}{day}\right)\frac{{\sf \overline{N}}}{\left(\left(20 \frac{gCOD}{m^{3}}\right)+{\sf \overline{N}}\right)^{2}}\right){\sf \overline{B}} &amp; \quad , \quad &amp; - \frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)} + \left(\left(5 \frac{1}{day}\right)\frac{{\sf \overline{N}}}{\left(20 \frac{gCOD}{m^{3}}\right)+{\sf \overline{N}}}\right) \\ \end{array}\right] <br /> <br /> #. In this case we are in the luckiest of situations, we know the exact solution so we can track both relative and absolute errors. The exact solution (taken from Assignment 1) is given by the following equations:<br /> <br /> .. math::<br /> <br /> {\sf \overline{N}} &amp;= \left(\frac{\frac{F}{V_{R}}K}{\left(\mu_{\text{max}}-\frac{F}{V_{R}}\right)}\right)\\ {\sf \overline{B}} &amp;= \frac{\frac{F}{V_{R}}\left(N_{0} - {\sf \overline{N}}\right)}{\left(\frac{1}{Y_{B}}\right)\left(\mu_{\text{max}}\frac{{\sf \overline{N}}}{K+{\sf \overline{N}}}\right)}<br /> <br /> Subbing in the given constants<br /> <br /> .. math::<br /> <br /> {\sf \overline{N}} &amp;= \left(\frac{\left(\frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)}\right)\left(20 \frac{gCOD}{m^{3}}\right)}{\left(\left(5 \frac{1}{day}\right)-\left(\frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)}\right)\right)}\right)\\ {\sf \overline{N}} &amp;= 33.\overline{3} \frac{gCOD}{m^{3}}\\ &amp; \\ {\sf \overline{B}} &amp;= \frac{\left(\frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)}\right)\left(\left(100 \frac{gCOD}{m^{3}}\right) - \left(33.\overline{3} \frac{gCOD}{m^{3}}\right)\right)}{\left(\frac{1}{\left(0.80\right)}\right)\left(\left(5 \frac{1}{day}\right)\frac{\left(33.\overline{3} \frac{gCOD}{m^{3}}\right)}{\left(20 \frac{gCOD}{m^{3}}\right)+\left(33.\overline{3} \frac{gCOD}{m^{3}}\right)}\right)}\\ {\sf \overline{B}} &amp;= 53.\overline{3} \frac{gCOD}{m^{3}}<br /> <br /> **ITERATION 1**<br /> <br /> *Calculate Jacobian*<br /> <br /> .. math::<br /> <br /> J^{(0)}\left(40 \frac{gCOD}{m^{3}},20 \frac{gCOD}{m^{3}}\right) &amp;= \left[\begin{array}{ccc} -3.8194 \frac{1}{day} &amp; \quad , \quad &amp; -4.1667 \frac{1}{day}\\ 0.5556 \frac{1}{day} &amp; \quad , \quad &amp; 0.2083 \frac{1}{day}\\ \end{array}\right]<br /> <br /> *Calculate function values*<br /> <br /> .. raw:: latex<br /> <br /> \\<br /> {\footnotesize [The PDF version of the solutions is cut off on the right edge of the page; please refer to the course website to get the full solution.]}<br /> <br /> .. math::<br /> <br /> f^{(0)}\left(40 \frac{gCOD}{m^{3}},20 \frac{gCOD}{m^{3}}\right) &amp;= \left[\begin{array}{c} \frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)}\left(100 \frac{gCOD}{m^{3}}\right) - \frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)}\left(40 \frac{gCOD}{m^{3}}\right) - \left(\frac{1}{\left(0.80\right)}\right)\left(\left(5 \frac{1}{day}\right)\frac{\left(40 \frac{gCOD}{m^{3}}\right)}{\left(20 \frac{gCOD}{m^{3}}\right)+\left(40 \frac{gCOD}{m^{3}}\right)}\right)\left(20 \frac{gCOD}{m^{3}}\right) \\ - \frac{\left(5000 \frac{m^{3}}{day}\right)}{\left(1600 m^{3}\right)}\left(20 \frac{gCOD}{m^{3}}\right) + \left(\left(5 \frac{1}{day}\right)\frac{\left(40 \frac{gCOD}{m^{3}}\right)}{\left(20 \frac{gCOD}{m^{3}}\right)+\left(40 \frac{gCOD}{m^{3}}\right)}\right)\left(20 \frac{gCOD}{m^{3}}\right) \\ \end{array}\right]\\<br /> &amp;= \left[\begin{array}{c} 104.1667 \frac{gCOD/m^{3}}{day} \\ 4.1667 \frac{gCOD/m^{3}}{day} \\ \end{array}\right]<br /> <br /> *Solve the system* :math:J\Delta x = -f (Use MATLAB or Python)<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{ccc} -3.8194 \frac{1}{day} &amp; \quad , \quad &amp; -4.1667 \frac{1}{day}\\ 0.5556 \frac{1}{day} &amp; \quad , \quad &amp; 0.2083 \frac{1}{day}\\ \end{array}\right] \Delta x &amp;= -\left[\begin{array}{c} 104.1667 \frac{gCOD/m^{3}}{day} \\ 4.1667 \frac{gCOD/m^{3}}{day} \\ \end{array}\right]\\ \Delta x &amp;= \left[\begin{array}{c} -25.7143 \frac{gCOD}{m^{3}} \\ 48.5714 \frac{gCOD}{m^{3}} \\ \end{array}\right]<br /> <br /> *calculate new x*<br /> <br /> .. math::<br /> <br /> x^{(1)} &amp;= x^{(0)} + \Delta x \\ x^{(1)} &amp;= \left[\begin{array}{c} 40 \frac{gCOD}{m^{3}} \\ 20 \frac{gCOD}{m^{3}} \\ \end{array}\right] + \left[\begin{array}{c}<br /> -25.7143 \frac{gCOD}{m^{3}} \\ 48.5714 \frac{gCOD}{m^{3}} \\ \end{array}\right]\\ x^{(1)} &amp;= \left[\begin{array}{c} 14.2857 \frac{gCOD}{m^{3}} \\ 68.5714 \frac{gCOD}{m^{3}} \\ \end{array}\right]<br /> <br /> *Calculate relative, absolute, and function error norms*<br /> <br /> We will assume an :math:\epsilon_{tol} of :math:10^{-8} and for this solution we will use the 2-norm.<br /> <br /> Relative error<br /> <br /> .. math::<br /> <br /> \frac{\parallel x^{(1)} - x^{(0)} \parallel}{\parallel x^{(1)} \parallel} &amp; &lt; \epsilon_{tol} \; ? \\ \frac{\parallel \Delta x \parallel}{\parallel x^{(1)} \parallel} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{\sum_{i=1}^{n} \left[ \Delta x_{i} \right]^{2}}{\sum_{i=1}^{n} \left[ x_{i}^{(1)} \right]^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{(-25.7143)^{2} + (48.5714)^{2}}{(14.2857)^{2} + (68.5714)^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ 0.7846 &amp; &lt; \epsilon_{tol} \; ? \; \text{No, so keep going!}<br /> <br /> Absolute error<br /> <br /> .. math::<br /> <br /> \frac{\parallel x^{(1)} - x^{(true)} \parallel}{\parallel x^{(true)} \parallel} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{\sum_{i=1}^{n} \left[ x_{i}^{(1)} - x_{i}^{(true)} \right]^{2}}{\sum_{i=1}^{n} \left[ x_{i}^{(true)} \right]^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{(14.2857 - 33.\overline{3})^{2} + (68.5714 - 53.\overline{3})^{2}}{(33.\overline{3})^{2} + (53.\overline{3})^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ 0.3878 &amp;&lt; \epsilon_{tol} \; ? \; \text{No, so keep going!}<br /> <br /> Function error<br /> <br /> .. math::<br /> <br /> \parallel f(x^{(1)}) \parallel &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\sum_{i=1}^{n} \left[ f(x^{(1)})_{i} \right]^{2}} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{(89.2857)^{2} + (-71.4286)^{2}} &amp; &lt; \epsilon_{tol} \; ? \\ 114.3415 &amp;&lt; \epsilon_{tol} \; ? \; \text{No, so keep going!}<br /> <br /> **ITERATION 2**<br /> <br /> *Calculate Jacobian*<br /> <br /> .. math::<br /> <br /> <br /> J^{(1)}\left(14.2857 \frac{gCOD}{m^{3}},68.5714 \frac{gCOD}{m^{3}}\right) &amp;= \left[\begin{array}{ccc} -10.4167 \frac{1}{day} &amp; \quad , \quad &amp; -2.6042 \frac{1}{day}\\ 5.8333 \frac{1}{day} &amp; \quad , \quad &amp; -1.0417 \frac{1}{day}\\ \end{array}\right]<br /> <br /> *Calculate function values*<br /> <br /> .. math::<br /> <br /> f^{(1)}\left(14.2857 \frac{gCOD}{m^{3}},68.5714 \frac{gCOD}{m^{3}}\right) &amp;= \left[\begin{array}{c} 89.2857 \frac{gCOD/m^{3}}{day} \\ -71.4286 \frac{gCOD/m^{3}}{day} \\ \end{array}\right]<br /> <br /> *Solve the system* :math:J\Delta x = -f (Use MATLAB or Python)<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{ccc} -10.4167 \frac{1}{day} &amp; \quad , \quad &amp; -2.6042 \frac{1}{day}\\ 5.8333 \frac{1}{day} &amp; \quad , \quad &amp; -1.0417 \frac{1}{day}\\ \end{array}\right] \Delta x &amp;= -\left[\begin{array}{c} 89.2857 \frac{gCOD/m^{3}}{day} \\ -71.4286 \frac{gCOD/m^{3}}{day} \\ \end{array}\right]\\ \Delta x &amp;= \left[\begin{array}{c} 10.7143 \frac{gCOD}{m^{3}} \\ -8.5714 \frac{gCOD}{m^{3}} \\ \end{array}\right]<br /> <br /> *Calculate new x*<br /> <br /> .. math::<br /> <br /> x^{(2)} &amp;= x^{(1)} + \Delta x \\ x^{(2)} &amp;= \left[\begin{array}{c} 14.2857 \frac{gCOD}{m^{3}} \\ 68.5714 \frac{gCOD}{m^{3}} \\ \end{array}\right] + \left[\begin{array}{c} 10.7143 \frac{gCOD}{m^{3}} \\ -8.5714 \frac{gCOD}{m^{3}} \\ \end{array}\right]\\ x^{(2)} &amp;= \left[\begin{array}{c} 25.0000 \frac{gCOD}{m^{3}} \\ 60.0000 \frac{gCOD}{m^{3}} \\ \end{array}\right]<br /> <br /> *Calculate relative, absolute, and function error norms*<br /> <br /> We will assume an :math:\epsilon_{tol} of :math:10^{-8} and for this solution we will use the 2-norm.<br /> <br /> Relative error<br /> <br /> .. math::<br /> <br /> \frac{\parallel x^{(2)} - x^{(1)} \parallel}{\parallel x^{(2)} \parallel} &amp; &lt; \epsilon_{tol} \; ? \\ \frac{\parallel \Delta x \parallel}{\parallel x^{(2)} \parallel} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{\sum_{i=1}^{n} \left[ \Delta x_{i} \right]^{2}}{\sum_{i=1}^{n} \left[ x_{i}^{(2)} \right]^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{(10.7143)^{2} + (-8.5714)^{2}}{(25.0000)^{2} + (60.0000)^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ 0.2111 &amp;&lt; \epsilon_{tol} \; ? \; \text{No, so keep going!}<br /> <br /> Absolute error<br /> <br /> .. math::<br /> <br /> \frac{\parallel x^{(2)} - x^{(true)} \parallel}{\parallel x^{(true)} \parallel} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{\sum_{i=1}^{n} \left[ x_{i}^{(2)} - x_{i}^{(true)} \right]^{2}}{\sum_{i=1}^{n} \left[ x_{i}^{(true)} \right]^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\frac{(25.0000 - 33.\overline{3})^{2} + (60.0000 - 53.\overline{3})^{2}}{(33.\overline{3})^{2} + (53.\overline{3})^{2}}} &amp; &lt; \epsilon_{tol} \; ? \\ 0.1697 &amp;&lt; \epsilon_{tol} \; ? \; \text{No, so keep going!}<br /> <br /> Function error<br /> <br /> .. math::<br /> <br /> \parallel f(x^{(2)}) \parallel &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{\sum_{i=1}^{n} \left[ f(x^{(2)})_{i} \right]^{2}} &amp; &lt; \epsilon_{tol} \; ? \\ \sqrt{(26.0417)^{2} + (-20.8333)^{2}} &amp; &lt; \epsilon_{tol} \; ? \\ 33.3496 &amp; &lt; \epsilon_{tol} \; ? \; \text{No, so keep going!}<br /> <br /> Note that all three errors appear to be decreasing from step to step so we are fairly confident the algorithm is converging and in fact this is true. If one were to continue the iterative process it would converge to within the specified relative tolerance within 7 iterations.<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.5cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Process_Model_Formulation_and_Solution:_3E4:About&diff=1095 Process Model Formulation and Solution: 3E4:About 2018-09-16T08:31:06Z <p>Kevindunn: </p> <hr /> <div>== Disclaimer: Errors, omissions and liability ==<br /> <br /> There might be errors, even though I work hard at making sure there are not. It would be great if you report any issues to me: '''kgdunn@gmail.com'''. Please note however that all material is provided &quot;as-is&quot; and no liability will be accepted for your usage of the material. <br /> <br /> == Copyright ==<br /> <br /> The copyright of all course materials on this site is held by Dr. Benoit Chachuat, now at Imperial College, London. <br /> <br /> The copyright of all other materials is held by Kevin Dunn and the teaching assistants for this course (TA's). If in doubt, please contact me (kevin.dunn@connectmv.com) to clarify.<br /> <br /> == Usage ==<br /> <br /> The materials on this site are generally available under a ''very liberal license'', the [http://creativecommons.org/licenses/by/3.0/ Creative Commons Attribution 3.0 Unported license], except for the course notes. The course notes may not be used in any way, without the permission of Dr. Benoit Chachuat,<br /> <br /> All computer source code (Python code, MATLAB code) is licensed under the [http://en.wikipedia.org/wiki/BSD_license new 3-clause BSD license].<br /> <br /> == Privacy ==<br /> <br /> Google Analytics and other web log handling tools are used to monitor this site's usage. Please feel free to [http://www.ghostery.com/ block these scripts].</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Assignment_2_-_2010_-_Solution/Bonus_question&diff=1094 Assignment 2 - 2010 - Solution/Bonus question 2018-09-16T08:29:32Z <p>Kevindunn: </p> <hr /> <div>{{Navigation|Book=Assignment 2 - 2010 - Solution|previous=Question 5|current=Back to all questions|next=}}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> Bonus question <br /> ===================<br /> <br /> Use the pseudo code developed in the course notes to write a MATLAB or Python function that implements Gauss elimination, without pivoting. The function should take :math:A and :math:b as inputs, and return vector :math:x.<br /> <br /> Use your function to solve this set of equations from question 1, and print the update :math:A matrix and :math:b vector after every pivot step.<br /> <br /> <br /> Solution<br /> -----------<br /> <br /> A function that implements the Gauss elimination without pivoting is provided below.<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-2/code/guass.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-2/code/guass.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> Now we use this function to solve the system of equations in Question 1. The commented lines in the MATLAB code show how the function is used. The results are: :math:x =[1.3333, 3.1667,1.5000,-6.0000].<br /> Also, the :math:A and :math:b (denoted as :math:C=[A\quad b] below) are updated as:<br /> <br /> .. math::<br /> C^1= \left[ \begin{array}{ccccc} 1 &amp; 1 &amp; 1 &amp; 1 &amp; 0\\ 0 &amp; -3 &amp; -3 &amp; -3 &amp; 4\\ 0 &amp; 3 &amp; -5 &amp; 0 &amp; 2\\ 0 &amp; -6 &amp; -6 &amp; -4 &amp; -4 \end{array} \right]<br /> <br /> .. math::<br /> C^2= \left[ \begin{array}{ccccc} 1 &amp; 1 &amp; 1 &amp; 1 &amp; 0\\ 0 &amp; -3 &amp; -3 &amp; -3 &amp; 4\\ 0 &amp; 0 &amp; -8 &amp; -3 &amp; 6\\ 0 &amp; 0 &amp; 0 &amp; 2 &amp; -12 \end{array} \right]<br /> <br /> .. math::<br /> C^3= \left[ \begin{array}{ccccc} 1 &amp; 1 &amp; 1 &amp; 1 &amp; 0\\ 0 &amp; -3 &amp; -3 &amp; -3 &amp; 4\\ 0 &amp; 0 &amp; -8 &amp; -3 &amp; 6\\ 0 &amp; 0 &amp; 0 &amp; 2 &amp; -12 \end{array} \right]<br /> <br /> The final result for :math:x = [1.333, 3.167, 1.5, -6]. <br /> <br /> &lt;/rst&gt;<br /> {{Navigation|Book=Assignment 2 - 2010 - Solution|previous=Question 5|current=Back to all questions|next=}}</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Assignment_2_-_2010_-_Solution/Question_5&diff=1093 Assignment 2 - 2010 - Solution/Question 5 2018-09-16T08:28:56Z <p>Kevindunn: </p> <hr /> <div>{{Navigation|Book=Assignment 2 - 2010 - Solution|previous=Question 4|current=Back to all questions|next=Bonus question}}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> Question 5 <br /> =================<br /> <br /> .. note:: From a previous exam, 5 marks out of 50, 3 hours. This question should be solved by hand, and is a typical question you can expect in the midterm and final.<br /> <br /> For the following system of linear algebraic equations:<br /> <br /> .. math::<br /> -5x_1 + 0x_2 + 12x_3 =&amp;\ 80 \\ 4x_1 -x_2 - x_3 =&amp;\ -2 \\ 6x_1 + 8x_2 +0x_3 =&amp;\ 45<br /> <br /> #. Use the Jacobi method to compute :math:x^{(1)} starting from :math:x^{(0)} = \bf 0.<br /> #. Use the Gauss-Seidel method to compute :math:x^{(1)} starting from :math:x^{(0)} = \bf 0.<br /> #. Use the Gauss-Seidel method with relaxation, :math:\omega=0.9, to compute :math:x^{(1)} starting from :math:x^{(0)} = \bf 0.<br /> <br /> Solution<br /> --------<br /> We start this problem by converting the system to the form :math:Ax = b.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrr} -5 &amp; 0 &amp; 12\\ 4 &amp; -1 &amp; -1\\ 6 &amp; 8 &amp; 0\\ \end{array}\right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ \end{array}\right] = \left[\begin{array}{r} 80\\ -2\\ 45\\ \end{array}\right]<br /> <br /> Next we rearrange the system so that it is diagonally dominant (i.e. :math:|a_{ii}| \geq \sum^{n}_{j=1,j \neq i} |a_{ij}| where ever possible). To achieve this we rearrange the equation order to equation 2, equation 3, equation 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrr} 4 &amp; -1 &amp; -1\\ 6 &amp; 8 &amp; 0\\ -5 &amp; 0 &amp; 12\\ \end{array}\right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ \end{array}\right] = \left[\begin{array}{r} -2\\ 45\\ 80\\ \end{array}\right]<br /> <br /> Now we are ready to attack this problem.<br /> <br /> #. <br /> The Jacobi method uses the following update algorithm:<br /> <br /> .. math::<br /> <br /> x_{i}^{(k+1)} = \frac{1}{a_{ii}} \left[ b_{i} - \sum^{n}_{j=1,j \neq i} a_{ij}x_{j}^{(k)} \right]<br /> <br /> Using a starting value of :math:x^{(0)} = [0,0,0] we calculate the next iterate using the above equation.<br /> <br /> .. math::<br /> <br /> \begin{array}{rcl} x_{1}^{(1)} &amp; = &amp; \frac{1}{a_{11}} \left[ b_{1} - \left( a_{12}x_{2}^{(0)} + a_{13}x_{3}^{(0)} \right) \right]\\ x_{1}^{(1)} &amp; = &amp; \frac{1}{(4)} \left[ (-2) - \left( (-1)(0) + (-1)(0) \right) \right]\\ x_{1}^{(1)} &amp; = &amp; -0.5\\ \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rcl} x_{2}^{(1)} &amp; = &amp; \frac{1}{a_{22}}\left[b_{2} - \left(a_{21}x_{1}^{(0)}+a_{23}x_{3}^{(0)}\right)\right]\\ x_{2}^{(1)} &amp; = &amp; \frac{1}{(8)}\left[(45) - \left((6)(0)+(0)(0)\right)\right]\\ x_{2}^{(1)} &amp; = &amp; 5.625\\ \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rcl} x_{3}^{(1)} &amp; = &amp; \frac{1}{a_{33}}\left[b_{3} - \left(a_{31}x_{1}^{(0)}+a_{32}x_{2}^{(0)}\right)\right]\\ x_{3}^{(1)} &amp; = &amp; \frac{1}{(12)}\left[(80) - \left((-5)(0)+(0)(0)\right)\right]\\ x_{3}^{(1)} &amp; = &amp; 6.6667\\ \end{array}<br /> <br /> Therefore :math:x^{(1)} = [-0.5,5.625,6.6667]<br /> <br /> #. <br /> The Gauss-Seidel method uses the following update algorithm:<br /> <br /> .. math::<br /> <br /> x_{i}^{(k+1)} = \frac{1}{a_{ii}}\left[b_{i} - \sum^{i-1}_{j=1}a_{ij}x_{j}^{(k+1)} - \sum^{n}_{j=i+1}a_{ij}x_{j}^{(k)}\right]<br /> <br /> Using a starting value of :math:x^{(0)} = [0,0,0] we calculate the next iterate using the above equation.<br /> <br /> .. math::<br /> <br /> \begin{array}{rcl} x_{1}^{(1)} &amp; = &amp; \frac{1}{a_{11}}\left[b_{1} - \left(a_{12}x_{2}^{(0)}+a_{13}x_{3}^{(0)}\right)\right]\\ x_{1}^{(1)} &amp; = &amp; \frac{1}{(4)}\left[(-2) - \left((-1)(0)+(-1)(0)\right)\right]\\ x_{1}^{(1)} &amp; = &amp; -0.5\\ \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rcl} x_{2}^{(1)} &amp; = &amp; \frac{1}{a_{22}}\left[b_{2} - \left(a_{21}x_{1}^{(1)}+a_{23}x_{3}^{(0)}\right)\right]\\ x_{2}^{(1)} &amp; = &amp; \frac{1}{(8)}\left[(45) - \left((6)(-0.5)+(0)(0)\right)\right]\\ x_{2}^{(1)} &amp; = &amp; 6\\ \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rcl} x_{3}^{(1)} &amp; = &amp; \frac{1}{a_{33}}\left[b_{3} - \left(a_{31}x_{1}^{(1)}+a_{32}x_{2}^{(1)}\right)\right]\\ x_{3}^{(1)} &amp; = &amp; \frac{1}{(12)}\left[(80) - \left((-5)(-0.5)+(0)(6)\right)\right]\\ x_{3}^{(1)} &amp; = &amp; 6.4583\\ \end{array}<br /> <br /> Therefore :math:x^{(1)} = [-0.5,6,6.4583]<br /> <br /> #. <br /> The Gauss-Seidel method with relaxation uses the following update algorithm:<br /> <br /> .. math::<br /> <br /> x_{i}^{(k+1)} = (1-\omega)x_{i}^{(k)} + \frac{\omega}{a_{ii}}\left[b_{i} - \sum^{i-1}_{j=1}a_{ij}x_{j}^{(k+1)} - \sum^{n}_{j=i+1}a_{ij}x_{j}^{(k)}\right]<br /> <br /> Using a starting value of :math:x^{(0)} = [0,0,0] we calculate the next iterate using the above equation.<br /> <br /> .. math::<br /> <br /> \begin{array}{rcl} x_{1}^{(1)} &amp; = &amp; (1-\omega)x_{1}^{(0)} + \frac{\omega}{a_{11}}\left[b_{1} - \left(a_{12}x_{2}^{(0)}+a_{13}x_{3}^{(0)}\right)\right]\\ x_{1}^{(1)} &amp; = &amp; (1-(0.9))(0) + \frac{(0.9)}{(4)}\left[(-2) - \left((-1)(0)+(-1)(0)\right)\right]\\ x_{1}^{(1)} &amp; = &amp; -0.45\\ \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rcl} x_{2}^{(1)} &amp; = &amp; (1-\omega)x_{2}^{(0)} + \frac{\omega}{a_{22}}\left[b_{2} - \left(a_{21}x_{1}^{(1)}+a_{23}x_{3}^{(0)}\right)\right]\\ x_{2}^{(1)} &amp; = &amp; (1-(0.9))(0) + \frac{(0.9)}{(8)}\left[(45) - \left((6)(-0.45)+(0)(0)\right)\right]\\ x_{2}^{(1)} &amp; = &amp; 5.36625\\ \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rcl} x_{3}^{(1)} &amp; = &amp; (1-\omega)x_{3}^{(0)} + \frac{\omega}{a_{33}}\left[b_{3} - \left(a_{31}x_{1}^{(1)}+a_{32}x_{2}^{(1)}\right)\right]\\ x_{3}^{(1)} &amp; = &amp; (1-(0.9))(0) + \frac{(0.9)}{(12)}\left[(80) - \left((-5)(-0.45)+(0)(5.36625)\right)\right]\\ x_{3}^{(1)} &amp; = &amp; 5.83125\\ \end{array}<br /> <br /> Therefore :math:x^{(1)} = [-0.45,5.36625,5.83125]<br /> &lt;/rst&gt;<br /> {{Navigation|Book=Assignment 2 - 2010 - Solution|previous=Question 4|current=Back to all questions|next=Bonus question}}</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Assignment_2_-_2010_-_Solution/Question_3&diff=1092 Assignment 2 - 2010 - Solution/Question 3 2018-09-16T08:26:50Z <p>Kevindunn: </p> <hr /> <div>{{Navigation|Book=Assignment 2 - 2010 - Solution|previous=Question 2|current=Back to all questions|next=Question 4}}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> Question 3 <br /> ================<br /> <br /> .. note:: This is an example of the type of question you will get in the take-home midterm.<br /> <br /> An impure gas stream can be cleaned, by absorbing the impurity into the liquid phase of an appropriate liquid solvent. For example, carbon dioxide can be removed from flue gas by scrubbing it with monoethanolamine; or NO\ :sub:2 can be absorbed into water to produce nitric acid.<br /> <br /> In this question we will derive the model equations for a sequence of absorber stages (trays), in which a component in the gas phase is absorbed into the liquid phase. <br /> <br /> First consider the :math:n^\text{th} stage in an absorber:<br /> <br /> .. figure:: images/absorber-tray.png<br /> :alt: images/math<br /> :scale: 16<br /> :align: center<br /> <br /> where the molar gas flow is :math:G and the liquid molar flow is :math:L, and assume these are constant throughout the column. Let the number of moles held-up in the liquid phase of the tray be :math:H_n, the hold-up. And let's only consider a binary system, where the species we are interested in is called A, and the absorbing liquid is species B. <br /> <br /> The mol **fraction** of species A, in the gas phase, leaving the tray is :math:y_{A,n}, and we also have :math:y_{B,n}, the mol fraction of B leaving: this gives of course :math:y_{A,n} + y_{B,n} = 1. But since we are interested only in species A in this question, and because we can always calculate B by subtraction from 1.0, we will drop the A and B subscripts, and only write -- for example -- :math:y_n to denote the gas mol fraction of A leaving the :math:n^\text{th} stage of the absorber.<br /> <br /> Similarly, the mol fraction of A, in the liquid phase, leaving the stage is :math:x_n. And we will denote the liquid mol fraction of A entering as :math:x_{n+1}, while the gas-phase mol fraction of A entering is defined as :math:y_{n-1}.<br /> <br /> Using Raoult's Law, and assuming fairly ideal conditions, and that the liquid and vapour phase are well-mixed and in equilibrium, we may relate the concentrations in the gas and liquid phase as:<br /> <br /> .. math::<br /> <br /> y_n P_\text{total} &amp;= x_n P_\text{A} \gamma_A \\ y_n &amp;= \beta x_n<br /> <br /> where :math:P_\text{total} is the total pressure in the absorber, and :math:P_\text{A} is the pure-component vapour pressure of A, at the given conditions, and :math:\gamma_A is an activity coefficient that corrects for non-idealities (cross-reference your thermodynamics course for more background on this equation). In this question you may assume that :math:\beta is constant throughout the absorber.<br /> <br /> #. Write down a **dynamic** mass balance for species A, in terms of the nomenclature above, for this :math:n^\text{th} stage in the absorber tower.<br /> #. Now write down the **steady-state** mass-balance equation for the above, but only in terms of liquid-phase mol fractions. Also divide the steady-state tray balance equations through by :math:\beta G and simplify them using the term :math:\delta = \displaystyle \frac{L}{\beta G}<br /> #. Now repeat this previous step, and write the steady-state mass-balance equations for the five-stage system shown here.<br /> <br /> .. figure:: images/absorber-column.png<br /> :scale: 25<br /> :align: center<br /> <br /> #. Rewrite these mass-balance equations in matrix form, :math:Ax = b, where:<br /> <br /> .. math::<br /> <br /> x = \left[\begin{array}{c} y_F \\ x_1 \\ x_2 \\ x_3 \\ x_4 \\ x_5 \\ x_F \\ y_5 \end{array}\right]<br /> <br /> #. Write a function that will generate the :math:A matrix and :math:b vector, given the variables in the system. Use this function and solve the system of equations for the two cases below:<br /> <br /> ======= ========= ========= ============= =========== ===========<br /> Case :math:G :math:L :math:\beta :math:x_F :math:y_F<br /> ======= ========= ========= ============= =========== ===========<br /> Case I 100 150 0.6 0.05 0.35<br /> ------- --------- --------- ------------- ----------- -----------<br /> Case II 100 50 0.6 0.05 0.35<br /> ======= ========= ========= ============= =========== ===========<br /> <br /> In each case, we are obviously interested in the liquid-phase concentration profile along the column, :math:x_F down to :math:x_1. Report these values and explain whether your answers make physical sense, or not, for the two cases.<br /> <br /> Solution<br /> ---------<br /> <br /> #. The dynamic mass balance for the :math:n^\text{th} tray:<br /> <br /> .. math::<br /> <br /> \frac{H dx_n}{dt} &amp;= L x_{n+1} + Gy_{n-1} - Lx_n - Gy_n \\ H \frac{dx_n}{dt} &amp;= L(x_{n+1} - x_n) + G(y_{n-1} - y_n)<br /> <br /> #. At steady state we have :math:\displaystyle \frac{dx_n}{dt} = 0. Further, using that :math:y_n = \beta x_n, and the definition of :math:\delta = \displaystyle \frac{L}{G\beta}, we can simplify the previous equation to:<br /> <br /> .. math::<br /> <br /> 0 &amp;= L(x_{n+1} - x_n) + G \beta (x_{n-1} - x_n) \\ 0 &amp;= x_{n-1} - \delta x_n - x_n + \delta x_{n+1} \\ 0 &amp;= x_{n-1} - (\delta + 1) x_n + \delta x_{n+1}<br /> <br /> #. For the system with 5 trays, we have:<br /> <br /> #. :math:0 = \displaystyle \frac{y_F}{\beta} - (\delta + 1)x_1 + \delta x_2 <br /> #. :math:0 = x_1 - (\delta + 1)x_2 + \delta x_3 <br /> #. :math:0 = x_2 - (\delta + 1)x_3 + \delta x_4 <br /> #. :math:0 = x_3 - (\delta + 1)x_4 + \delta x_5 <br /> #. :math:0 = x_4 - (\delta + 1)x_5 + \delta x_F <br /> <br /> #. We require a square matrix for :math:A, so we add 3 other equations to the above 5 tray balances. These three equations correspond to the known fractions of :math:A in the inlet gas and liquid streams, as well as the equation that relates :math:y_5 to :math:x_5.<br /> <br /> <br /> .. math::<br /> <br /> Ax &amp;= b \\ \left( \begin{array}{cccccccc} 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ \displaystyle \frac{1}{\beta} &amp; -(\delta + 1) &amp; \delta &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; -(\delta + 1) &amp; \delta &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; -(\delta + 1) &amp; \delta &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 &amp; -(\delta + 1) &amp; \delta &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; -(\delta + 1) &amp; \delta&amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 &amp; -\beta &amp; 0 &amp; 1 \\ \end{array} \right) \left( \begin{array}{c} y_F \\ x_1 \\ x_2 \\ x_3 \\ x_4 \\ x_5 \\ x_F \\ y_5 \end{array} \right) &amp;= \left( \begin{array}{c} y_F \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ x_F \\ 0 \end{array} \right)<br /> <br /> **Note**: Assuming :math:y_5=0 (complete separation) instead of making the equilibrium assumption :math:y_5-\beta x_5=0 will change the last row of the above matrix as having zero element instead of :math:-\beta. This approach only affects the result of :math:y_5 itself because :math:y_5 does not participate in any other equations. **Both solutions will be accepted**.<br /> <br /> #. The code below defines functions for the :math:A matrix and the :math:b vector, and then solves the system for the two cases. The liquid concentration profiles through the column are obtained as follow:<br /> <br /> Case I (:math:L=150): Impurity [%] in the liquid stream, top to bottom: 5.00, 5.33, 6.15, 8.21, 13.3, 26.2; :math:y_5 = 3.20<br /> <br /> Case II (:math:L=50): Impurity [%] in the liquid stream, top to bottom: 5.00, 18.4, 29.5, 38.8, 46.5, 53.0; :math:y_5 = 11.0<br /> <br /> **Discussion**: The only difference between Case I and case II is the amount of the liquid solvent :math:L used in the absorber. One expects that if a lower flow rate of solvent is used, yet the hold-up on each tray is the same, that the concentrations of the impurity in the liquid phase will be higher on each tray. This is indeed shown in the results. However, the outlet concentration of impurity in the gas phase is going to be higher (less impurity is removed) at the lower liquid flow rate, since absorption of the impurity into the liquid phase is inversely proportional to the impurity already in the liquid phase (due to concentration gradients).<br /> <br /> You can verify for yourself that as the liquid flow rate is increased, keeping the gas flow constant, that the limiting outlet concentration in the gas phase is 3.0%, the amount that is in equilibrium with an &quot;infinite&quot; liquid composition of 5%. Also, the outlet liquid-phase concentration tends to 5% across all trays as :math:L \rightarrow \infty.<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-2/code/absorber_question.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-2/code/absorber-question.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> &lt;/rst&gt;<br /> {{Navigation|Book=Assignment 2 - 2010 - Solution|previous=Question 2|current=Back to all questions|next=Question 4}}</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Assignment_2_-_2010_-_Solution/Question_2&diff=1091 Assignment 2 - 2010 - Solution/Question 2 2018-09-16T08:26:24Z <p>Kevindunn: </p> <hr /> <div>{{Navigation|Book=Assignment 2 - 2010 - Solution|previous=Question 1|current=Back to all questions|next=Question 3}}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> Question 2 <br /> ================<br /> <br /> #. Derive, by hand, the lower-triangular matrix :math:L (with ones on the diagonal), and upper triangular matrix :math:U so that :math:A = LU, using the matrix :math:A from question 1. Afterwards, show that :math:A = LU.<br /> <br /> #. Use MATLAB or Python's built-in functions to solve for :math:L and :math:U. Show your code and show the matrices from the software. Explain any disagreement. See the software tutorial on matrix operations &lt;https://learnche.org/3E4/Software_tutorial/Matrix_operations&gt;_ for help.<br /> <br /> #. Use **your** matrices for :math:L and :math:U from the first part of this question, and calculate the inverse of :math:A. Compare your inverse with the inverse calculated by MATLAB or Python - does it agree with the software's values? Also, does :math:A A^{-1} = I for your inverse?<br /> <br /> Solution<br /> ---------<br /> <br /> #. <br /> The algorithm for determining the :math:LU decomposition of a matrix (without pivoting) is covered on slides 27 - 34 of the *Section B: Linear Algebraic Equations* Notes. We start with the matrix :math:A:<br /> <br /> .. math::<br /> <br /> A = \left[\begin{array}{cccc} 1 &amp; 1 &amp; 1 &amp; 1\\ 1 &amp; -2 &amp; -2 &amp; -2\\ 1 &amp; 4 &amp; -4 &amp; 1\\ 1 &amp; -5 &amp; -5 &amp; -3\\ \end{array} \right]<br /> <br /> The basic procedure behind :math:LU decomposition is to perform forward elimination on the matrix :math:A to generate the upper triangular matrix :math:U and, while doing so, also store the row elimination factors used to generate the lower triangular matrix :math:L. <br /> <br /> To eliminate the first column of :math:A we add :math:-\left(\frac{a_{21}}{a_{11}}\right) = -\left(\frac{1}{1}\right) times row 1 to row 2, :math:-\left(\frac{a_{31}}{a_{11}}\right) = -\left(\frac{1}{1}\right) times row 1 to row 3, and :math:-\left(\frac{a_{41}}{a_{11}}\right) = -\left(\frac{1}{1}\right) times row 1 to row 4. Also recall that when we collect these multiplication terms into our :math:L we take the negative of the value (i.e. we multiply by negative 1).<br /> <br /> .. math::<br /> <br /> A^{(1)} = \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 1 + (-1)(1) &amp; -2 + (-1)(1) &amp; -2 + (-1)(1) &amp; -2 + (-1)(1)\\ 1 + (-1)(1) &amp; 4 + (-1)(1) &amp; -4 + (-1)(1) &amp; 1 + (-1)(1)\\ 1 + (-1)(1) &amp; -5 + (-1)(1) &amp; -5 + (-1)(1) &amp; -3 + (-1)(1)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 3 &amp; -5 &amp; 0\\ 0 &amp; -6 &amp; -6 &amp; -4\\ \end{array} \right]<br /> <br /> .. math::<br /> <br /> L^{(1)} = \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ \frac{a_{21}}{a_{11}} &amp; 1 &amp; 0 &amp; 0\\ \frac{a_{31}}{a_{11}} &amp; 0 &amp; 1 &amp; 0\\ \frac{a_{41}}{a_{11}} &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; 0 &amp; 1 &amp; 0\\ 1 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right]<br /> <br /> Next we move to column 2. To remove the elements below the diagonal we add :math:-\left(\frac{a_{32}}{a_{22}}\right) = -\left(\frac{3}{-3}\right) times row 2 to row 3 and :math:-\left(\frac{a_{42}}{a_{22}}\right) = -\left(\frac{-6}{-3}\right) times row 2 to row 4. We also add the negative of these factors to the second column of our :math:L matrix.<br /> <br /> .. math::<br /> <br /> A^{(2)} = \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 3 + (1)(-3) &amp; -5 + (1)(-3) &amp; 0 + (1)(-3)\\ 0 &amp; -6 + (-2)(-3) &amp; -6 + (-2)(-3) &amp; -4 + (-2)(-3)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 0 &amp; -8 &amp; -3\\ 0 &amp; 0 &amp; 0 &amp; 2\\ \end{array} \right]<br /> <br /> .. math::<br /> <br /> L^{(2)} = \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; \frac{a_{32}}{a_{22}} &amp; 1 &amp; 0\\ 1 &amp; \frac{a_{42}}{a_{22}} &amp; 0 &amp; 1\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; -1 &amp; 1 &amp; 0\\ 1 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right]<br /> <br /> Since :math:A^{(2)} is already in upper triangular form we no longer have any reason to continue the forward elimination process. Therefore:<br /> <br /> .. math::<br /> <br /> L = \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; -1 &amp; 1 &amp; 0\\ 1 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \,\,\,\, U = \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 0 &amp; -8 &amp; -3\\ 0 &amp; 0 &amp; 0 &amp; 2\\ \end{array} \right]<br /> <br /> Next we prove that :math:A = LU. Since matrix multiplications takes up alot of space we will do this column by column.<br /> <br /> .. math::<br /> <br /> a_{:1} = \left[\begin{array}{r} l_{11}u_{11} + l_{12}u_{21} + l_{13}u_{31} + l_{14}u_{41}\\ l_{21}u_{11} + l_{22}u_{21} + l_{23}u_{31} + l_{24}u_{41}\\ l_{31}u_{11} + l_{32}u_{21} + l_{33}u_{31} + l_{34}u_{41}\\ l_{41}u_{11} + l_{42}u_{21} + l_{43}u_{31} + l_{44}u_{41}\\ \end{array}\right] = \left[\begin{array}{r} (1)(1) + (0)(0) + (0)(0) + (0)(0)\\ (1)(1) + (1)(0) + (0)(0) + (0)(0)\\ (1)(1) + (-1)(0) + (1)(0) + (0)(0)\\ (1)(1) + (2)(0) + (0)(0) + (1)(0)\\ \end{array}\right] = \left[\begin{array}{r} 1\\ 1\\ 1\\ 1\\ \end{array}\right]<br /> <br /> .. math::<br /> <br /> a_{:2} = \left[\begin{array}{r} l_{11}u_{12} + l_{12}u_{22} + l_{13}u_{32} + l_{14}u_{42}\\ l_{21}u_{12} + l_{22}u_{22} + l_{23}u_{32} + l_{24}u_{42}\\ l_{31}u_{12} + l_{32}u_{22} + l_{33}u_{32} + l_{34}u_{42}\\ l_{41}u_{12} + l_{42}u_{22} + l_{43}u_{32} + l_{44}u_{42}\\ \end{array}\right] = \left[\begin{array}{r} (1)(1) + (0)(-3) + (0)(0) + (0)(0)\\ (1)(1) + (1)(-3) + (0)(0) + (0)(0)\\ (1)(1) + (-1)(-3) + (1)(0) + (0)(0)\\ (1)(1) + (2)(-3) + (0)(0) + (1)(0)\\ \end{array}\right] = \left[\begin{array}{r} 1\\ -2\\ 4\\ -5\\ \end{array}\right]<br /> <br /> .. math::<br /> <br /> a_{:3} = \left[\begin{array}{r} l_{11}u_{13} + l_{12}u_{23} + l_{13}u_{33} + l_{14}u_{43}\\ l_{21}u_{13} + l_{22}u_{23} + l_{23}u_{33} + l_{24}u_{43}\\ l_{31}u_{13} + l_{32}u_{23} + l_{33}u_{33} + l_{34}u_{43}\\ l_{41}u_{13} + l_{42}u_{23} + l_{43}u_{33} + l_{44}u_{43}\\ \end{array}\right] = \left[\begin{array}{r} (1)(1) + (0)(-3) + (0)(-8) + (0)(0)\\ (1)(1) + (1)(-3) + (0)(-8) + (0)(0)\\ (1)(1) + (-1)(-3) + (1)(-8) + (0)(0)\\ (1)(1) + (2)(-3) + (0)(-8) + (1)(0)\\ \end{array}\right] = \left[\begin{array}{r} 1\\ -2\\ -4\\ -5\\ \end{array}\right]<br /> <br /> .. math::<br /> <br /> a_{:4} = \left[\begin{array}{r} l_{11}u_{14} + l_{12}u_{24} + l_{13}u_{34} + l_{14}u_{44}\\ l_{21}u_{14} + l_{22}u_{24} + l_{23}u_{34} + l_{24}u_{44}\\ l_{31}u_{14} + l_{32}u_{24} + l_{33}u_{34} + l_{34}u_{44}\\ l_{41}u_{14} + l_{42}u_{24} + l_{43}u_{34} + l_{44}u_{44}\\ \end{array}\right] = \left[\begin{array}{r} (1)(1) + (0)(-3) + (0)(-3) + (0)(2)\\ (1)(1) + (1)(-3) + (0)(-3) + (0)(2)\\ (1)(1) + (-1)(-3) + (1)(-3) + (0)(2)\\ (1)(1) + (2)(-3) + (0)(-3) + (1)(2)\\ \end{array}\right] = \left[\begin{array}{r} 1\\ -2\\ 1\\ -3\\ \end{array}\right]<br /> <br /> From the above it is easy to see that<br /> <br /> .. math::<br /> <br /> LU = \left[a_{:1},a_{:2},a_{:3},a_{:4}\right] = \left[\begin{array}{cccc} 1 &amp; 1 &amp; 1 &amp; 1\\ 1 &amp; -2 &amp; -2 &amp; -2\\ 1 &amp; 4 &amp; -4 &amp; 1\\ 1 &amp; -5 &amp; -5 &amp; -3\\ \end{array} \right] = A<br /> <br /> Therefore we have proven that :math:A = LU.<br /> <br /> **Extra information - Derivation of Doolittle Method**<br /> <br /> The :math:LU decomposition of a square matrix :math:A is defined as a set of lower triangular (i.e. no elements above the diagonal) and upper triangular (i.e. no elements below the diagonal) matrices that, when multiplied together, produce :math:A. In the simplest case, it is easy to show that if :math:A contains no zero quantities along it's diagonal during a simple forward elimination process (i.e. partial pivoting is not necessary) then matrices :math:L and :math:U exist for :math:A and are directly calculable from :math:A. In the more general case, it is also easy to show that an :math:LU decomposition exists for any square matrix :math:A if partial pivoting is allowed. In this case the :math:LU decomposition will have an associated permutation matrix :math:P, and really you will have solved the sysem :math:PA=LU, but the difference in how these decompositions are used in further calculations (for example to calculate the inverse of a matrix) is minimal (All one must do to use the permuted :math:LU set to solve a linear system is multiply the system by :math:P --&gt; :math:Ax = b --&gt; :math:PAx = Pb --&gt; :math:LUx = Pb. The solution of the system will be exactly the same). Finally, if one specifies that either the :math:L **or** :math:U matrix must have all ones along its diagonal (in the Doolittle method, and in this problem..., we specify that :math:L must have a unit diagonal), then the :math:L and :math:U set is unique to :math:A.<br /> <br /> So how do we derive the :math:LU decomposition of a matrix :math:A. The answer lies in applying elementary row operations. There are three classic elementary row operations in linear algebra:<br /> <br /> * Row Switching:<br /> <br /> Performed by left multiplying a matrix :math:A by an identity matrix that has had its rows :math:i and :math:j switched, where :math:i and :math:j are the rows desired to be switched in :math:A. For example, if :math:A were a 4x4 matrix and you wanted to switch rows 2 and 3, you would left multiply :math:A by the following &quot;row switching&quot; matrix :math:E_{1} (i.e. :math:E_{1}A):<br /> <br /> .. math::<br /> E_{1} = \left[\begin{array}{cccc} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right]<br /> <br /> * Multiplication of a row by a constant:<br /> <br /> This one is pretty straightforward. When you multiply a row by some constant :math:c then you multiply every element of that row by that constant on an element by element basis. This may be represented in elementary matrix form by left multiplying :math:A by an elementary matrix :math:E_{2} that contains only 1s along its diagonal (i.e. it is an identity matrix) except for the :math:i\text{th} element, which contains a value of :math:c (where :math:i is the row we wish to multiply by :math:c). So, for example, if we had an 4x4 matrix :math:A and we wished to multiply row 4 by 5 then we would left multiply :math:A by the following elementary matrix :math:E_{2}:<br /> <br /> .. math::<br /> E_{2} = \left[\begin{array}{cccc} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 5 \\ \end{array}\right]<br /> <br /> * Addition of a multiple of one row to another:<br /> <br /> Many will recognize this as the essential operation in solving a linear system of equations. The addition of one row :math:i, multiplied by some constant :math:c, to another row :math:j may be represented in elementary matrix form by an identity matrix with an off diagonal element equal to :math:c in the :math:i\text{th} column and the :math:j\text{th} row. So, if for example we wanted to add 4 times row 3 to row 1, we would left multiply :math:A by the following elementary matrix :math:E_{3} (go ahead, check for yourself, everything works out):<br /> <br /> .. math::<br /> E_{3} = \left[\begin{array}{cccc} 1 &amp; 0 &amp; 4 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right]<br /> <br /> The final pieces of information we require before we may derive the :math:LU decomposition of :math:A are given below (derivation not provided):<br /> <br /> * The inverse of :math:E_{1} is equal to :math:E_{1}^{T}<br /> * The inverse of :math:E_{2} is equal to a matrix :math:E_{2'} whose diagonal elements are equal to the inverse of those in :math:E_{2} (i.e. 1 over the elements in :math:E_{2})<br /> * The inverse of :math:E_{3} is equal to :math:E_{3} with all its off-diagonal elements multiplied by (-1)<br /> * A lower triangular matrix multplied by a lower triangular matrix is also lower triangular (the opposite is also true)<br /> * The inverse of a lower triangular matrix is also lower triangular (the opposite is also true)<br /> <br /> Now that we have the required background information we may derive the :math:LU decomposition of :math:A. Note that this derivation deals with the simplified case that no intermediate partial pivoting is required during the derivation.<br /> <br /> So let us start by asking a simple question. If one wanted to derive an upper triangular matrix from :math:A, how would one do it? The obvious answer would be to perform forward elimination on the matrix until all elements below the diagonal no longer existed. But how would this look in terms elementary row operations? Essentially we would be continuously multiplying by elementary matrices of form :math:E_{3}. Furthermore, since we are trying to convert matrix :math:A to an upper triangular matrix we would only be concerned with eliminating elements below the diagonal wouldn't we? Therefore all of the elementary matrices applied would be some multiple of row :math:i being added to some row :math:j where :math:i &gt; j. Recall that the off-diagonal element of :math:E_{3} occurs at location :math:e_{ij}. Therefore the forward elimination step would **always** consist of lower triangular matrices no? So in the case of the first column of A we would simply left multiply A by the following three elementary row operation matrices to eliminate the below diagonal elements wouldn't we? (go ahead, try it out by hand)<br /> <br /> .. math::<br /> <br /> A^{(1)} = \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ -\frac{a^{(0)}_{41}}{a^{(0)}_{11}} &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ -\frac{a^{(0)}_{31}}{a^{(0)}_{11}} &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ -\frac{a^{(0)}_{21}}{a^{(0)}_{11}} &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{cccc} 1 &amp; 1 &amp; 1 &amp; 1\\ 1 &amp; -2 &amp; -2 &amp; -2\\ 1 &amp; 4 &amp; -4 &amp; 1\\ 1 &amp; -5 &amp; -5 &amp; -3\\ \end{array} \right] = \left[\begin{array}{cccc} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 3 &amp; -5 &amp; 0\\ 0 &amp; -6 &amp; -6 &amp; -4\\ \end{array} \right]<br /> <br /> But we can't exactly claim :math:A = A^{(1)} can we. So what would we need to do to make the equivalence correct? We would need to multiply by the inverse of the elementary row operations.<br /> <br /> .. math::<br /> <br /> A = \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ \frac{a^{(0)}_{21}}{a^{(0)}_{11}} &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ \frac{a^{(0)}_{31}}{a^{(0)}_{11}} &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ \frac{a^{(0)}_{41}}{a^{(0)}_{11}} &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right]\left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ -\frac{a^{(0)}_{41}}{a^{(0)}_{11}} &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ -\frac{a^{(0)}_{31}}{a^{(0)}_{11}} &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ -\frac{a^{(0)}_{21}}{a^{(0)}_{11}} &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{cccc} 1 &amp; 1 &amp; 1 &amp; 1\\ 1 &amp; -2 &amp; -2 &amp; -2\\ 1 &amp; 4 &amp; -4 &amp; 1\\ 1 &amp; -5 &amp; -5 &amp; -3\\ \end{array} \right]<br /> <br /> At this point it should be stated that when multiplying elementary matrices of type :math:E_{3} where no off-diagonal elements occur in the same position (always the case when performing forward elimination) the result of the multiplication is simply a matrix containing **all** of the off-diagonal elements in the exact same positions they occurred separately (a prove will not be given but the reader is encouraged to investigate the validity of this statement on their own). Therefore if we conduct the multiplication of the row operations and inverse operations respectively then it is easy to see the system simplifies to:<br /> <br /> .. math::<br /> <br /> A = \underbrace{\left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ \frac{a^{(0)}_{21}}{a^{(0)}_{11}} &amp; 1 &amp; 0 &amp; 0 \\ \frac{a^{(0)}_{31}}{a^{(0)}_{11}} &amp; 0 &amp; 1 &amp; 0 \\ \frac{a^{(0)}_{41}}{a^{(0)}_{11}} &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right]}_{(L^{(1)})^{-1}} \underbrace{\left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ -\frac{a^{(0)}_{21}}{a^{(0)}_{11}} &amp; 1 &amp; 0 &amp; 0 \\ -\frac{a^{(0)}_{31}}{a^{(0)}_{11}} &amp; 0 &amp; 1 &amp; 0 \\ -\frac{a^{(0)}_{41}}{a^{(0)}_{11}} &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right]}_{L^{(1)}} \underbrace{\left[\begin{array}{cccc} 1 &amp; 1 &amp; 1 &amp; 1\\ 1 &amp; -2 &amp; -2 &amp; -2\\ 1 &amp; 4 &amp; -4 &amp; 1\\ 1 &amp; -5 &amp; -5 &amp; -3\\ \end{array} \right]}_{A} = (L^{(1)})^{-1}A^{(1)}<br /> <br /> Now what if we wanted to eliminate the second column of :math:A (also :math:A^{(1)})? Well naturally we would follow the exact same process (i.e. we would multiply :math:A^{(1)} by the elementary row operation matrices required to eliminate all elements below the second diagonal, remembering to also multiply by the inverse of the operations to maintain the equivalence to :math:A).<br /> <br /> .. math::<br /> <br /> \begin{array}{rcl} A &amp; = &amp; (L^{(1)})^{-1}(L^{(2)})^{-1}L^{(2)}A^{(1)}\\ A &amp; = &amp; (L^{(1)})^{-1}(L^{(2)})^{-1}A^{(2)} \end{array}<br /> <br /> Are you starting to see the pattern now? If we were to repeat this process :math:n-1 times (where :math:n is the dimension of :math:A) then the forward elimination would be complete and therefore :math:A^{(n-1)} would **have to be upper triangular** (as a quick aside, we repeat the process :math:n-1 times and not :math:n times because we do not have to eliminate the first row).<br /> <br /> .. math::<br /> <br /> \begin{array}{rcl} A &amp; = &amp; (L^{(1)})^{-1}(L^{(2)})^{-1} \cdots (L^{(n-2)})^{-1}(L^{(n-1)})^{-1} L^{(n-1)}L^{(n-2)} \ldots L^{(2)}L^{(1)}A\\ A &amp; = &amp; (L^{(1)})^{-1}(L^{(2)})^{-1} \cdots (L^{(n-2)})^{-1}(L^{(n-1)})^{-1} A^{(n-1)}\\ A &amp; = &amp; (L^{(1)})^{-1}(L^{(2)})^{-1} \cdots (L^{(n-2)})^{-1}(L^{(n-1)})^{-1} U\\ \end{array}<br /> <br /> Now recall the the inverse of a lower triangular matrix is also a lower triangular matrix and the multiplication of two lower triangular matrices is also lower triangular. Therefore all of the terms to the left of :math:U collect to form one lower triangular matrix :math:L.<br /> <br /> .. math::<br /> <br /> A = LU<br /> <br /> But this is exactly what we wanted when we started the LU decomposition process. Furthermore, knowing the simplified multiplication rule for :math:E_{3} type matrices presented earlier, it is easy to see that :math:L equals:<br /> <br /> .. math::<br /> <br /> L = \left[ \begin{array}{rrrrrrr} 1 &amp; 0 &amp; 0 &amp; \cdots &amp; 0 &amp; 0 &amp; 0\\ \frac{a^{(0)}_{21}}{a^{(0)}_{11}} &amp; 1 &amp; 0 &amp; \cdots &amp; 0 &amp; 0 &amp; 0\\ \frac{a^{(0)}_{31}}{a^{(0)}_{11}} &amp; \frac{a^{(1)}_{32}}{a^{(1)}_{22}} &amp; 1 &amp; \cdots &amp; 0 &amp; 0 &amp; 0\\ \vdots &amp; \vdots &amp; \vdots &amp; \ddots &amp; \vdots &amp; \vdots &amp; \vdots\\ \frac{a^{(0)}_{n-2,1}}{a^{(0)}_{11}} &amp; \frac{a^{(1)}_{n-2,2}}{a^{(1)}_{22}} &amp; \frac{a^{(2)}_{n-2,3}}{a^{(2)}_{33}} &amp; \cdots &amp; 1 &amp; 0 &amp; 0\\ \frac{a^{(0)}_{n-1,1}}{a^{(0)}_{11}} &amp; \frac{a^{(1)}_{n-1,2}}{a^{(1)}_{22}} &amp; \frac{a^{(2)}_{n-1,3}}{a^{(2)}_{33}} &amp; \cdots &amp; \frac{a^{(n-2)}_{n-1,n-2}}{a^{(n-2)}_{n-2,n-2}} &amp; 1 &amp; 0\\ \frac{a^{(0)}_{n,1}}{a^{(0)}_{11}} &amp; \frac{a^{(1)}_{n,2}}{a^{(1)}_{22}} &amp; \frac{a^{(2)}_{n,3}}{a^{(2)}_{33}} &amp; \cdots &amp; \frac{a^{(n-2)}_{n,n-2}}{a^{(n-2)}_{n-2,n-2}} &amp; \frac{a^{(n-1)}_{n,n-1}}{a^{(n-1)}_{n-1,n-1}} &amp; 1\\ \end{array}\right]<br /> <br /> This is exactly the general form of L presented in the notes. This is where that form comes from.<br /> <br /> #. To perform LU decomposition in MATLAB we use the lu function. The two most common methods of using lu are:<br /> <br /> * [L,U] = lu(A) : this will return the :math:L and :math:U matrices with the :math:L matrix multiplied by a permutation matrix :math:P (if partial pivoting occurred during the solution procedure).<br /> <br /> * [L,U,P] = lu(A) : this will return the :math:L and :math:U matrices with the implicit understanding that the system solved was :math:PA.<br /> <br /> To perform LU decomposition in Python we use the lu_factor function.<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-2/code/question2_part2_matlab.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-2/code/question2_part2_python.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> We note that the results are different from those we calculated above. This is because MATLAB and Python perform :math:LU decomposition with partial pivoting. Thus the system solved was actually :math:PA = LU.<br /> <br /> #. <br /> To calculate the inverse of a matrix using its :math:LU decomposition we realize that the system :math:AX = B, where :math:X and :math:B are matrices, may be solved by splitting :math:X and :math:B into their individual column pairs (i.e. column 1 of :math:X goes with column 1 of :math:B, column 2 with column 2, etc.) and solving each system of linear equations separately. In this case we know a matrix multiplied by its inverse yields the identity matrix. Therefore:<br /> <br /> .. math::<br /> <br /> B = \left[\begin{array}{cccc} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right]<br /> <br /> Finally, we recall that the :math:LU decomposition of a matrix may be used to simplify the solution process by splitting the system into two separate systems that are easily solved.<br /> <br /> * :math:Ly = b<br /> <br /> * :math:Ux = y<br /> <br /> **Invert column 1**<br /> <br /> We start with the subsystem :math:Ly = b.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; -1 &amp; 1 &amp; 0\\ 1 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{11}\\ y_{21}\\ y_{31}\\ y_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1\\ 0\\ 0\\ 0\\ \end{array} \right]<br /> <br /> We eliminate the elements in the first column under the diagonal (pivot) element by adding (-1) times row 1 to row 2, (-1) times row 1 to row 3, and (-1) times row 1 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 + (-1)(1) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; -1 + (-1)(0) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; 2 + (-1)(0) &amp; 0 + (-1)(0) &amp; 1 + (-1)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{11}\\ y_{21}\\ y_{31}\\ y_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1\\ 0 + (-1)(1)\\ 0 + (-1)(1)\\ 0 + (-1)(1)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 &amp; 1 &amp; 0\\ 0 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{11}\\ y_{21}\\ y_{31}\\ y_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1\\ -1\\ -1\\ -1\\ \end{array} \right] <br /> <br /> We eliminate the elements in the second column under the diagonal (pivot) element by adding (1) times row 2 to row 3, and (-2) times row 2 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 + (1)(1) &amp; 1 + (1)(0) &amp; 0 + (1)(0)\\ 0 &amp; 2 + (-2)(1) &amp; 0 + (-2)(0) &amp; 1 + (-2)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{11}\\ y_{21}\\ y_{31}\\ y_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1\\ -1\\ -1 + (1)(-1)\\ -1 + (-2)(-1)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{11}\\ y_{21}\\ y_{31}\\ y_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1\\ -1\\ -2\\ 1\\ \end{array} \right]<br /> <br /> Now that we have solved for :math:y we move on to solve the second subsystem :math:Ux = y.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 0 &amp; -8 &amp; -3\\ 0 &amp; 0 &amp; 0 &amp; 2\\ \end{array} \right] \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1\\ -1\\ -2\\ 1\\ \end{array} \right]<br /> <br /> We start by dividing row 4 by its diagonal element and then adding 3 times row 4 to row 3, 3 times row 4 to row 2, and (-1) times row 4 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1 + (-1)\left(\frac{2}{(2)}\right)\\ 0 &amp; -3 &amp; -3 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; -8 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; 0 &amp; \frac{2}{(2)}\\ \end{array} \right] \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1 + (-1)\left(\frac{1}{(2)}\right)\\ -1 + (3)\left(\frac{1}{(2)}\right)\\ -2 + (3)\left(\frac{1}{(2)}\right)\\ \frac{1}{(2)}\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 0\\ 0 &amp; -3 &amp; -3 &amp; 0\\ 0 &amp; 0 &amp; -8 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1/2\\ 1/2\\ -1/2\\ 1/2\\ \end{array} \right]<br /> <br /> Next we divide row 3 by its diagonal element and then add 3 times row 3 to row 2, and (-1) times row 3 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 + (-1)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; -3 &amp; -3 + (3)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; 0 &amp; \frac{-8}{(-8)} &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1/2 + (-1)\left(\frac{-1/2}{(-8)}\right)\\ 1/2 + (3)\left(\frac{-1/2}{(-8)}\right)\\ \frac{-1/2}{(-8)}\\ 1/2\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 7/16\\ 11/16\\ 1/16\\ 1/2\\ \end{array} \right]<br /> <br /> Finally we divide row 2 by its diagonal element and then add (-1) times row 2 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 + (-1)\left(\frac{-3}{(-3)}\right) &amp; 0 &amp; 0\\ 0 &amp; \frac{-3}{(-3)} &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 7/16 + (-1)\frac{11/16}{(-3)}\\ \frac{11/16}{(-3)}\\ 1/16\\ 1/2\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 32/48\\ -11/48\\ 3/48\\ 24/48\\ \end{array} \right]<br /> <br /> Therefore the first column of the inverse of :math:A is.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 32/48\\ -11/48\\ 3/48\\ 24/48\\ \end{array} \right]<br /> <br /> **Invert column 2**<br /> <br /> We start with the subsystem :math:Ly = b.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; -1 &amp; 1 &amp; 0\\ 1 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{12}\\ y_{22}\\ y_{32}\\ y_{42}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 1\\ 0\\ 0\\ \end{array} \right]<br /> <br /> We eliminate the elements in the first column under the diagonal (pivot) element by adding (-1) times row 1 to row 2, (-1) times row 1 to row 3, and (-1) times row 1 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 + (-1)(1) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; -1 + (-1)(0) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; 2 + (-1)(0) &amp; 0 + (-1)(0) &amp; 1 + (-1)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{12}\\ y_{22}\\ y_{32}\\ y_{42}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 1 + (-1)(0)\\ 0 + (-1)(0)\\ 0 + (-1)(0)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 &amp; 1 &amp; 0\\ 0 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{12}\\ y_{22}\\ y_{32}\\ y_{42}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 1\\ 0\\ 0\\ \end{array} \right] <br /> <br /> We eliminate the elements in the second column under the diagonal (pivot) element by adding (1) times row 2 to row 3, and (-2) times row 2 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 + (1)(1) &amp; 1 + (1)(0) &amp; 0 + (1)(0)\\ 0 &amp; 2 + (-2)(1) &amp; 0 + (-2)(0) &amp; 1 + (-2)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{12}\\ y_{22}\\ y_{32}\\ y_{42}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 1\\ 0 + (1)(1)\\ 0 + (-2)(1)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{12}\\ y_{22}\\ y_{32}\\ y_{42}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 1\\ 1\\ -2\\ \end{array} \right]<br /> <br /> Now that we have solved for :math:y we move on to solve the second subsystem :math:Ux = y.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 0 &amp; -8 &amp; -3\\ 0 &amp; 0 &amp; 0 &amp; 2\\ \end{array} \right] \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 1\\ 1\\ -2\\ \end{array} \right]<br /> <br /> We start by dividing row 4 by its diagonal element and then adding 3 times row 4 to row 3, 3 times row 4 to row 2, and (-1) times row 4 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1 + (-1)\left(\frac{2}{(2)}\right)\\ 0 &amp; -3 &amp; -3 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; -8 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; 0 &amp; \frac{2}{(2)}\\ \end{array} \right] \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 0 + (-1)\left(\frac{-2}{(2)}\right)\\ 1 + (3)\left(\frac{-2}{(2)}\right)\\ 1 + (3)\left(\frac{-2}{(2)}\right)\\ \frac{-2}{(2)}\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 0\\ 0 &amp; -3 &amp; -3 &amp; 0\\ 0 &amp; 0 &amp; -8 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 1\\ -2\\ -2\\ -1\\ \end{array} \right]<br /> <br /> Next we divide row 3 by its diagonal element and then add 3 times row 3 to row 2, and (-1) times row 3 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 + (-1)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; -3 &amp; -3 + (3)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; 0 &amp; \frac{-8}{(-8)} &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 1 + (-1)\left(\frac{-2}{(-8)}\right)\\ -2 + (3)\left(\frac{-2}{(-8)}\right)\\ \frac{-2}{(-8)}\\ -1\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 3/4\\ -5/4\\ 1/4\\ -1\\ \end{array} \right]<br /> <br /> Finally we divide row 2 by its diagonal element and then add (-1) times row 2 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 + (-1)\left(\frac{-3}{(-3)}\right) &amp; 0 &amp; 0\\ 0 &amp; \frac{-3}{(-3)} &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 3/4 + (-1)\frac{-5/4}{(-3)}\\ \frac{-5/4}{(-3)}\\ 1/4\\ -1\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 1/3\\ 5/12\\ 1/4\\ -1\\ \end{array} \right]<br /> <br /> Therefore the first column of the inverse of :math:A is.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 16/48\\ 20/48\\ 12/48\\ -48\\ \end{array} \right]<br /> <br /> **Invert column 3**<br /> <br /> We start with the subsystem :math:Ly = b.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; -1 &amp; 1 &amp; 0\\ 1 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{13}\\ y_{23}\\ y_{33}\\ y_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 1\\ 0\\ \end{array} \right]<br /> <br /> We eliminate the elements in the first column under the diagonal (pivot) element by adding (-1) times row 1 to row 2, (-1) times row 1 to row 3, and (-1) times row 1 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 + (-1)(1) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; -1 + (-1)(0) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; 2 + (-1)(0) &amp; 0 + (-1)(0) &amp; 1 + (-1)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{13}\\ y_{23}\\ y_{33}\\ y_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0 + (-1)(0)\\ 1 + (-1)(0)\\ 0 + (-1)(0)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 &amp; 1 &amp; 0\\ 0 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{13}\\ y_{23}\\ y_{33}\\ y_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 1\\ 0\\ \end{array} \right] <br /> <br /> We eliminate the elements in the second column under the diagonal (pivot) element by adding (1) times row 2 to row 3, and (-2) times row 2 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 + (1)(1) &amp; 1 + (1)(0) &amp; 0 + (1)(0)\\ 0 &amp; 2 + (-2)(1) &amp; 0 + (-2)(0) &amp; 1 + (-2)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{13}\\ y_{23}\\ y_{33}\\ y_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 1 + (1)(0)\\ 0 + (-2)(0)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{13}\\ y_{23}\\ y_{33}\\ y_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 1\\ 0\\ \end{array} \right]<br /> <br /> Now that we have solved for :math:y we move on to solve the second subsystem :math:Ux = y.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 0 &amp; -8 &amp; -3\\ 0 &amp; 0 &amp; 0 &amp; 2\\ \end{array} \right] \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 1\\ 0\\ \end{array} \right]<br /> <br /> We start by dividing row 4 by its diagonal element and then adding 3 times row 4 to row 3, 3 times row 4 to row 2, and (-1) times row 4 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1 + (-1)\left(\frac{2}{(2)}\right)\\ 0 &amp; -3 &amp; -3 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; -8 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; 0 &amp; \frac{2}{(2)}\\ \end{array} \right] \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0 + (-1)\left(\frac{0}{(2)}\right)\\ 0 + (3)\left(\frac{0}{(2)}\right)\\ 1 + (3)\left(\frac{0}{(2)}\right)\\ \frac{0}{(2)}\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 0\\ 0 &amp; -3 &amp; -3 &amp; 0\\ 0 &amp; 0 &amp; -8 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 1\\ 0\\ \end{array} \right]<br /> <br /> Next we divide row 3 by its diagonal element and then add 3 times row 3 to row 2, and (-1) times row 3 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 + (-1)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; -3 &amp; -3 + (3)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; 0 &amp; \frac{-8}{(-8)} &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0 + (-1)\left(\frac{1}{(-8)}\right)\\ 0 + (3)\left(\frac{1}{(-8)}\right)\\ \frac{1}{(-8)}\\ 0\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 1/8\\ -3/8\\ -1/8\\ 0\\ \end{array} \right]<br /> <br /> Finally we divide row 2 by its diagonal element and then add (-1) times row 2 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 + (-1)\left(\frac{-3}{(-3)}\right) &amp; 0 &amp; 0\\ 0 &amp; \frac{-3}{(-3)} &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 1/8 + (-1)\frac{-3/8}{(-3)}\\ \frac{-3/8}{(-3)}\\ -1/8\\ 0\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 1/8\\ -1/8\\ 0\\ \end{array} \right]<br /> <br /> Therefore the first column of the inverse of :math:A is.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 6/48\\ -6/48\\ 0\\ \end{array} \right]<br /> <br /> **Invert column 4**<br /> <br /> We start with the subsystem :math:Ly = b.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; -1 &amp; 1 &amp; 0\\ 1 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{14}\\ y_{24}\\ y_{34}\\ y_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 0\\ 1\\ \end{array} \right]<br /> <br /> We eliminate the elements in the first column under the diagonal (pivot) element by adding (-1) times row 1 to row 2, (-1) times row 1 to row 3, and (-1) times row 1 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 + (-1)(1) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; -1 + (-1)(0) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; 2 + (-1)(0) &amp; 0 + (-1)(0) &amp; 1 + (-1)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{14}\\ y_{24}\\ y_{34}\\ y_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0 + (-1)(0)\\ 0 + (-1)(0)\\ 1 + (-1)(0)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 &amp; 1 &amp; 0\\ 0 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{14}\\ y_{24}\\ y_{34}\\ y_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 0\\ 1\\ \end{array} \right] <br /> <br /> We eliminate the elements in the second column under the diagonal (pivot) element by adding (1) times row 2 to row 3, and (-2) times row 2 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 + (1)(1) &amp; 1 + (1)(0) &amp; 0 + (1)(0)\\ 0 &amp; 2 + (-2)(1) &amp; 0 + (-2)(0) &amp; 1 + (-2)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{14}\\ y_{24}\\ y_{34}\\ y_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 0 + (1)(0)\\ 1 + (-2)(0)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{14}\\ y_{24}\\ y_{34}\\ y_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 0\\ 1\\ \end{array} \right]<br /> <br /> Now that we have solved for :math:y we move on to solve the second subsystem :math:Ux = y.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 0 &amp; -8 &amp; -3\\ 0 &amp; 0 &amp; 0 &amp; 2\\ \end{array} \right] \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 0\\ 1\\ \end{array} \right]<br /> <br /> We start by dividing row 4 by its diagonal element and then adding 3 times row 4 to row 3, 3 times row 4 to row 2, and (-1) times row 4 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1 + (-1)\left(\frac{2}{(2)}\right)\\ 0 &amp; -3 &amp; -3 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; -8 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; 0 &amp; \frac{2}{(2)}\\ \end{array} \right] \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0 + (-1)\left(\frac{1}{(2)}\right)\\ 0 + (3)\left(\frac{1}{(2)}\right)\\ 0 + (3)\left(\frac{1}{(2)}\right)\\ \frac{1}{(2)}\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 0\\ 0 &amp; -3 &amp; -3 &amp; 0\\ 0 &amp; 0 &amp; -8 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} -1/2\\ 3/2\\ 3/2\\ 1/2\\ \end{array} \right]<br /> <br /> Next we divide row 3 by its diagonal element and then add 3 times row 3 to row 2, and (-1) times row 3 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 + (-1)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; -3 &amp; -3 + (3)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; 0 &amp; \frac{-8}{(-8)} &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} -1/2 + (-1)\left(\frac{3/2}{(-8)}\right)\\ 3/2 + (3)\left(\frac{3/2}{(-8)}\right)\\ \frac{3/2}{(-8)}\\ 1/2\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} -5/16\\ 15/16\\ -3/16\\ 1/2\\ \end{array} \right]<br /> <br /> Finally we divide row 2 by its diagonal element and then add (-1) times row 2 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 + (-1)\left(\frac{-3}{(-3)}\right) &amp; 0 &amp; 0\\ 0 &amp; \frac{-3}{(-3)} &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} -5/16 + (-1)\frac{15/16}{(-3)}\\ \frac{15/16}{(-3)}\\ -3/16\\ 1/2\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ -15/48\\ -3/16\\ 1/2\\ \end{array} \right]<br /> <br /> Therefore the first column of the inverse of :math:A is.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ -15/48\\ -9/48\\ 24/48\\ \end{array} \right]<br /> <br /> Therefore the inverse of :math:A is<br /> <br /> .. math::<br /> <br /> A^{-1} = [x_{:1},x_{:2},x_{:3},x_{:4}] = \left[\begin{array}{rrrr} 32/48 &amp; 16/48 &amp; 0 &amp; 0\\ -11/48 &amp; 20/48 &amp; 6/48 &amp; -15/48\\ 3/48 &amp; 12/48 &amp; -6/48 &amp; -9/48\\ 24/48 &amp; -48/48 &amp; 0 &amp; 24/48\\ \end{array}\right] = \left[\begin{array}{rrrr} 0.6667 &amp; 0.3333 &amp; 0 &amp; 0\\ -0.2292 &amp; 0.4167 &amp; 0.1250 &amp; -0.3125\\ 0.0625 &amp; 0.2500 &amp; -0.1250 &amp; -0.1875\\ 0.5000 &amp; -1.0000 &amp; 0 &amp; 0.5000\\ \end{array}\right]<br /> <br /> **Check the inverse**<br /> <br /> To check the inverse of :math:A in MATLAB and Python we use the inv() function.<br /> <br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-2/code/question2_part3_matlab.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-2/code/question2_part3_python.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> Finally we show that :math:AA^{-1} = I. Since matrix multiplication takes up alot of space we will do this column by column.<br /> <br /> .. math::<br /> <br /> id_{:1} = \left[\begin{array}{r} a_{11}a^{-1}_{11} + a_{12}a^{-1}_{21} + a_{13}a^{-1}_{31} + a_{14}a^{-1}_{41}\\ a_{21}a^{-1}_{11} + a_{22}a^{-1}_{21} + a_{23}a^{-1}_{31} + a_{24}a^{-1}_{41}\\ a_{31}a^{-1}_{11} + a_{32}a^{-1}_{21} + a_{33}a^{-1}_{31} + a_{34}a^{-1}_{41}\\ a_{41}a^{-1}_{11} + a_{42}a^{-1}_{21} + a_{43}a^{-1}_{31} + a_{44}a^{-1}_{41}\\ \end{array}\right] = \left[\begin{array}{r} (1)(32/48) + (1)(-11/48) + (1)(3/48) + (1)(24/48)\\ (1)(32/48) + (-2)(-11/48) + (-2)(3/48) + (-2)(24/48)\\ (1)(32/48) + (4)(-11/48) + (-4)(3/48) + (1)(24/48)\\ (1)(32/48) + (-5)(-11/48) + (-5)(3/48) + (1)(24/48)\\ \end{array}\right] = \left[\begin{array}{r} 1\\ 0\\ 0\\ 0\\ \end{array}\right]<br /> <br /> .. math::<br /> <br /> id_{:2} = \left[\begin{array}{r} a_{11}a^{-1}_{12} + a_{12}a^{-1}_{22} + a_{13}a^{-1}_{32} + a_{14}a^{-1}_{42}\\ a_{21}a^{-1}_{12} + a_{22}a^{-1}_{22} + a_{23}a^{-1}_{32} + a_{24}a^{-1}_{42}\\ a_{31}a^{-1}_{12} + a_{32}a^{-1}_{22} + a_{33}a^{-1}_{32} + a_{34}a^{-1}_{42}\\ a_{41}a^{-1}_{12} + a_{42}a^{-1}_{22} + a_{43}a^{-1}_{32} + a_{44}a^{-1}_{42}\\ \end{array}\right] = \left[\begin{array}{r} (1)(16/48) + (1)(20/48) + (1)(12/48) + (1)(-48/48)\\ (1)(16/48) + (-2)(20/48) + (-2)(12/48) + (-2)(-48/48)\\ (1)(16/48) + (4)(20/48) + (-4)(12/48) + (1)(-48/48)\\ (1)(16/48) + (-5)(20/48) + (-5)(12/48) + (1)(-48/48)\\ \end{array}\right] = \left[\begin{array}{r} 0\\ 1\\ 0\\ 0\\ \end{array}\right]<br /> <br /> .. math::<br /> <br /> id_{:3} = \left[\begin{array}{r} a_{11}a^{-1}_{13} + a_{12}a^{-1}_{23} + a_{13}a^{-1}_{33} + a_{14}a^{-1}_{43}\\ a_{21}a^{-1}_{13} + a_{22}a^{-1}_{23} + a_{23}a^{-1}_{33} + a_{24}a^{-1}_{43}\\ a_{31}a^{-1}_{13} + a_{32}a^{-1}_{23} + a_{33}a^{-1}_{33} + a_{34}a^{-1}_{43}\\ a_{41}a^{-1}_{13} + a_{42}a^{-1}_{23} + a_{43}a^{-1}_{33} + a_{44}a^{-1}_{43}\\ \end{array}\right] = \left[\begin{array}{r} (1)(0) + (1)(6/48) + (1)(-6/48) + (1)(0)\\ (1)(0) + (-2)(6/48) + (-2)(-6/48) + (-2)(0)\\ (1)(0) + (4)(6/48) + (-4)(-6/48) + (1)(0)\\ (1)(0) + (-5)(6/48) + (-5)(-6/48) + (1)(0)\\ \end{array}\right] = \left[\begin{array}{r} 0\\ 0\\ 1\\ 0\\ \end{array}\right]<br /> .. math::<br /> <br /> id_{:4} = \left[\begin{array}{r} a_{11}a^{-1}_{14} + a_{12}a^{-1}_{24} + a_{13}a^{-1}_{34} + a_{14}a^{-1}_{44}\\ a_{21}a^{-1}_{14} + a_{22}a^{-1}_{24} + a_{23}a^{-1}_{34} + a_{24}a^{-1}_{44}\\ a_{31}a^{-1}_{14} + a_{32}a^{-1}_{24} + a_{33}a^{-1}_{34} + a_{34}a^{-1}_{44}\\ a_{41}a^{-1}_{14} + a_{42}a^{-1}_{24} + a_{43}a^{-1}_{34} + a_{44}a^{-1}_{44}\\ \end{array}\right] = \left[\begin{array}{r} (1)(0) + (1)(-15/48) + (1)(-9/48) + (1)(24/48)\\ (1)(0) + (-2)(-15/48) + (-2)(-9/48) + (-2)(24/48)\\ (1)(0) + (4)(-15/48) + (-4)(-9/48) + (1)(24/48)\\ (1)(0) + (-5)(-15/48) + (-5)(-9/48) + (1)(24/48)\\ \end{array}\right] = \left[\begin{array}{r} 0\\ 0\\ 0\\ 1\\ \end{array}\right]<br /> <br /> From the above it is easy to see that<br /> <br /> .. math::<br /> <br /> AA^{-1} = \left[id_{:1},id_{:2},id_{:3},id_{:4}\right] = \left[\begin{array}{cccc} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] = I<br /> <br /> Therefore we have proven that :math:AA^{-1} = I.<br /> <br /> <br /> &lt;/rst&gt;<br /> {{Navigation|Book=Assignment 2 - 2010 - Solution|previous=Question 1|current=Back to all questions|next=Question 3}}</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Assignment_2_-_2010_-_Solution/Question_2&diff=1090 Assignment 2 - 2010 - Solution/Question 2 2018-09-16T08:25:18Z <p>Kevindunn: </p> <hr /> <div>{{Navigation|Book=Assignment 2 - 2010 - Solution|previous=Question 1|current=Back to all questions|next=Question 3}}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> Question 2 <br /> ================<br /> <br /> #. Derive, by hand, the lower-triangular matrix :math:L (with ones on the diagonal), and upper triangular matrix :math:U so that :math:A = LU, using the matrix :math:A from question 1. Afterwards, show that :math:A = LU.<br /> <br /> #. Use MATLAB or Python's built-in functions to solve for :math:L and :math:U. Show your code and show the matrices from the software. Explain any disagreement. See the software tutorial on matrix operations &lt;http://modelling3e4.connectmv.com/wiki/Software_tutorial/Matrix_operations&gt;_ for help.<br /> <br /> #. Use **your** matrices for :math:L and :math:U from the first part of this question, and calculate the inverse of :math:A. Compare your inverse with the inverse calculated by MATLAB or Python - does it agree with the software's values? Also, does :math:A A^{-1} = I for your inverse?<br /> <br /> Solution<br /> ---------<br /> <br /> #. <br /> The algorithm for determining the :math:LU decomposition of a matrix (without pivoting) is covered on slides 27 - 34 of the *Section B: Linear Algebraic Equations* Notes. We start with the matrix :math:A:<br /> <br /> .. math::<br /> <br /> A = \left[\begin{array}{cccc} 1 &amp; 1 &amp; 1 &amp; 1\\ 1 &amp; -2 &amp; -2 &amp; -2\\ 1 &amp; 4 &amp; -4 &amp; 1\\ 1 &amp; -5 &amp; -5 &amp; -3\\ \end{array} \right]<br /> <br /> The basic procedure behind :math:LU decomposition is to perform forward elimination on the matrix :math:A to generate the upper triangular matrix :math:U and, while doing so, also store the row elimination factors used to generate the lower triangular matrix :math:L. <br /> <br /> To eliminate the first column of :math:A we add :math:-\left(\frac{a_{21}}{a_{11}}\right) = -\left(\frac{1}{1}\right) times row 1 to row 2, :math:-\left(\frac{a_{31}}{a_{11}}\right) = -\left(\frac{1}{1}\right) times row 1 to row 3, and :math:-\left(\frac{a_{41}}{a_{11}}\right) = -\left(\frac{1}{1}\right) times row 1 to row 4. Also recall that when we collect these multiplication terms into our :math:L we take the negative of the value (i.e. we multiply by negative 1).<br /> <br /> .. math::<br /> <br /> A^{(1)} = \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 1 + (-1)(1) &amp; -2 + (-1)(1) &amp; -2 + (-1)(1) &amp; -2 + (-1)(1)\\ 1 + (-1)(1) &amp; 4 + (-1)(1) &amp; -4 + (-1)(1) &amp; 1 + (-1)(1)\\ 1 + (-1)(1) &amp; -5 + (-1)(1) &amp; -5 + (-1)(1) &amp; -3 + (-1)(1)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 3 &amp; -5 &amp; 0\\ 0 &amp; -6 &amp; -6 &amp; -4\\ \end{array} \right]<br /> <br /> .. math::<br /> <br /> L^{(1)} = \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ \frac{a_{21}}{a_{11}} &amp; 1 &amp; 0 &amp; 0\\ \frac{a_{31}}{a_{11}} &amp; 0 &amp; 1 &amp; 0\\ \frac{a_{41}}{a_{11}} &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; 0 &amp; 1 &amp; 0\\ 1 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right]<br /> <br /> Next we move to column 2. To remove the elements below the diagonal we add :math:-\left(\frac{a_{32}}{a_{22}}\right) = -\left(\frac{3}{-3}\right) times row 2 to row 3 and :math:-\left(\frac{a_{42}}{a_{22}}\right) = -\left(\frac{-6}{-3}\right) times row 2 to row 4. We also add the negative of these factors to the second column of our :math:L matrix.<br /> <br /> .. math::<br /> <br /> A^{(2)} = \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 3 + (1)(-3) &amp; -5 + (1)(-3) &amp; 0 + (1)(-3)\\ 0 &amp; -6 + (-2)(-3) &amp; -6 + (-2)(-3) &amp; -4 + (-2)(-3)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 0 &amp; -8 &amp; -3\\ 0 &amp; 0 &amp; 0 &amp; 2\\ \end{array} \right]<br /> <br /> .. math::<br /> <br /> L^{(2)} = \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; \frac{a_{32}}{a_{22}} &amp; 1 &amp; 0\\ 1 &amp; \frac{a_{42}}{a_{22}} &amp; 0 &amp; 1\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; -1 &amp; 1 &amp; 0\\ 1 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right]<br /> <br /> Since :math:A^{(2)} is already in upper triangular form we no longer have any reason to continue the forward elimination process. Therefore:<br /> <br /> .. math::<br /> <br /> L = \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; -1 &amp; 1 &amp; 0\\ 1 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \,\,\,\, U = \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 0 &amp; -8 &amp; -3\\ 0 &amp; 0 &amp; 0 &amp; 2\\ \end{array} \right]<br /> <br /> Next we prove that :math:A = LU. Since matrix multiplications takes up alot of space we will do this column by column.<br /> <br /> .. math::<br /> <br /> a_{:1} = \left[\begin{array}{r} l_{11}u_{11} + l_{12}u_{21} + l_{13}u_{31} + l_{14}u_{41}\\ l_{21}u_{11} + l_{22}u_{21} + l_{23}u_{31} + l_{24}u_{41}\\ l_{31}u_{11} + l_{32}u_{21} + l_{33}u_{31} + l_{34}u_{41}\\ l_{41}u_{11} + l_{42}u_{21} + l_{43}u_{31} + l_{44}u_{41}\\ \end{array}\right] = \left[\begin{array}{r} (1)(1) + (0)(0) + (0)(0) + (0)(0)\\ (1)(1) + (1)(0) + (0)(0) + (0)(0)\\ (1)(1) + (-1)(0) + (1)(0) + (0)(0)\\ (1)(1) + (2)(0) + (0)(0) + (1)(0)\\ \end{array}\right] = \left[\begin{array}{r} 1\\ 1\\ 1\\ 1\\ \end{array}\right]<br /> <br /> .. math::<br /> <br /> a_{:2} = \left[\begin{array}{r} l_{11}u_{12} + l_{12}u_{22} + l_{13}u_{32} + l_{14}u_{42}\\ l_{21}u_{12} + l_{22}u_{22} + l_{23}u_{32} + l_{24}u_{42}\\ l_{31}u_{12} + l_{32}u_{22} + l_{33}u_{32} + l_{34}u_{42}\\ l_{41}u_{12} + l_{42}u_{22} + l_{43}u_{32} + l_{44}u_{42}\\ \end{array}\right] = \left[\begin{array}{r} (1)(1) + (0)(-3) + (0)(0) + (0)(0)\\ (1)(1) + (1)(-3) + (0)(0) + (0)(0)\\ (1)(1) + (-1)(-3) + (1)(0) + (0)(0)\\ (1)(1) + (2)(-3) + (0)(0) + (1)(0)\\ \end{array}\right] = \left[\begin{array}{r} 1\\ -2\\ 4\\ -5\\ \end{array}\right]<br /> <br /> .. math::<br /> <br /> a_{:3} = \left[\begin{array}{r} l_{11}u_{13} + l_{12}u_{23} + l_{13}u_{33} + l_{14}u_{43}\\ l_{21}u_{13} + l_{22}u_{23} + l_{23}u_{33} + l_{24}u_{43}\\ l_{31}u_{13} + l_{32}u_{23} + l_{33}u_{33} + l_{34}u_{43}\\ l_{41}u_{13} + l_{42}u_{23} + l_{43}u_{33} + l_{44}u_{43}\\ \end{array}\right] = \left[\begin{array}{r} (1)(1) + (0)(-3) + (0)(-8) + (0)(0)\\ (1)(1) + (1)(-3) + (0)(-8) + (0)(0)\\ (1)(1) + (-1)(-3) + (1)(-8) + (0)(0)\\ (1)(1) + (2)(-3) + (0)(-8) + (1)(0)\\ \end{array}\right] = \left[\begin{array}{r} 1\\ -2\\ -4\\ -5\\ \end{array}\right]<br /> <br /> .. math::<br /> <br /> a_{:4} = \left[\begin{array}{r} l_{11}u_{14} + l_{12}u_{24} + l_{13}u_{34} + l_{14}u_{44}\\ l_{21}u_{14} + l_{22}u_{24} + l_{23}u_{34} + l_{24}u_{44}\\ l_{31}u_{14} + l_{32}u_{24} + l_{33}u_{34} + l_{34}u_{44}\\ l_{41}u_{14} + l_{42}u_{24} + l_{43}u_{34} + l_{44}u_{44}\\ \end{array}\right] = \left[\begin{array}{r} (1)(1) + (0)(-3) + (0)(-3) + (0)(2)\\ (1)(1) + (1)(-3) + (0)(-3) + (0)(2)\\ (1)(1) + (-1)(-3) + (1)(-3) + (0)(2)\\ (1)(1) + (2)(-3) + (0)(-3) + (1)(2)\\ \end{array}\right] = \left[\begin{array}{r} 1\\ -2\\ 1\\ -3\\ \end{array}\right]<br /> <br /> From the above it is easy to see that<br /> <br /> .. math::<br /> <br /> LU = \left[a_{:1},a_{:2},a_{:3},a_{:4}\right] = \left[\begin{array}{cccc} 1 &amp; 1 &amp; 1 &amp; 1\\ 1 &amp; -2 &amp; -2 &amp; -2\\ 1 &amp; 4 &amp; -4 &amp; 1\\ 1 &amp; -5 &amp; -5 &amp; -3\\ \end{array} \right] = A<br /> <br /> Therefore we have proven that :math:A = LU.<br /> <br /> **Extra information - Derivation of Doolittle Method**<br /> <br /> The :math:LU decomposition of a square matrix :math:A is defined as a set of lower triangular (i.e. no elements above the diagonal) and upper triangular (i.e. no elements below the diagonal) matrices that, when multiplied together, produce :math:A. In the simplest case, it is easy to show that if :math:A contains no zero quantities along it's diagonal during a simple forward elimination process (i.e. partial pivoting is not necessary) then matrices :math:L and :math:U exist for :math:A and are directly calculable from :math:A. In the more general case, it is also easy to show that an :math:LU decomposition exists for any square matrix :math:A if partial pivoting is allowed. In this case the :math:LU decomposition will have an associated permutation matrix :math:P, and really you will have solved the sysem :math:PA=LU, but the difference in how these decompositions are used in further calculations (for example to calculate the inverse of a matrix) is minimal (All one must do to use the permuted :math:LU set to solve a linear system is multiply the system by :math:P --&gt; :math:Ax = b --&gt; :math:PAx = Pb --&gt; :math:LUx = Pb. The solution of the system will be exactly the same). Finally, if one specifies that either the :math:L **or** :math:U matrix must have all ones along its diagonal (in the Doolittle method, and in this problem..., we specify that :math:L must have a unit diagonal), then the :math:L and :math:U set is unique to :math:A.<br /> <br /> So how do we derive the :math:LU decomposition of a matrix :math:A. The answer lies in applying elementary row operations. There are three classic elementary row operations in linear algebra:<br /> <br /> * Row Switching:<br /> <br /> Performed by left multiplying a matrix :math:A by an identity matrix that has had its rows :math:i and :math:j switched, where :math:i and :math:j are the rows desired to be switched in :math:A. For example, if :math:A were a 4x4 matrix and you wanted to switch rows 2 and 3, you would left multiply :math:A by the following &quot;row switching&quot; matrix :math:E_{1} (i.e. :math:E_{1}A):<br /> <br /> .. math::<br /> E_{1} = \left[\begin{array}{cccc} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right]<br /> <br /> * Multiplication of a row by a constant:<br /> <br /> This one is pretty straightforward. When you multiply a row by some constant :math:c then you multiply every element of that row by that constant on an element by element basis. This may be represented in elementary matrix form by left multiplying :math:A by an elementary matrix :math:E_{2} that contains only 1s along its diagonal (i.e. it is an identity matrix) except for the :math:i\text{th} element, which contains a value of :math:c (where :math:i is the row we wish to multiply by :math:c). So, for example, if we had an 4x4 matrix :math:A and we wished to multiply row 4 by 5 then we would left multiply :math:A by the following elementary matrix :math:E_{2}:<br /> <br /> .. math::<br /> E_{2} = \left[\begin{array}{cccc} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 5 \\ \end{array}\right]<br /> <br /> * Addition of a multiple of one row to another:<br /> <br /> Many will recognize this as the essential operation in solving a linear system of equations. The addition of one row :math:i, multiplied by some constant :math:c, to another row :math:j may be represented in elementary matrix form by an identity matrix with an off diagonal element equal to :math:c in the :math:i\text{th} column and the :math:j\text{th} row. So, if for example we wanted to add 4 times row 3 to row 1, we would left multiply :math:A by the following elementary matrix :math:E_{3} (go ahead, check for yourself, everything works out):<br /> <br /> .. math::<br /> E_{3} = \left[\begin{array}{cccc} 1 &amp; 0 &amp; 4 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right]<br /> <br /> The final pieces of information we require before we may derive the :math:LU decomposition of :math:A are given below (derivation not provided):<br /> <br /> * The inverse of :math:E_{1} is equal to :math:E_{1}^{T}<br /> * The inverse of :math:E_{2} is equal to a matrix :math:E_{2'} whose diagonal elements are equal to the inverse of those in :math:E_{2} (i.e. 1 over the elements in :math:E_{2})<br /> * The inverse of :math:E_{3} is equal to :math:E_{3} with all its off-diagonal elements multiplied by (-1)<br /> * A lower triangular matrix multplied by a lower triangular matrix is also lower triangular (the opposite is also true)<br /> * The inverse of a lower triangular matrix is also lower triangular (the opposite is also true)<br /> <br /> Now that we have the required background information we may derive the :math:LU decomposition of :math:A. Note that this derivation deals with the simplified case that no intermediate partial pivoting is required during the derivation.<br /> <br /> So let us start by asking a simple question. If one wanted to derive an upper triangular matrix from :math:A, how would one do it? The obvious answer would be to perform forward elimination on the matrix until all elements below the diagonal no longer existed. But how would this look in terms elementary row operations? Essentially we would be continuously multiplying by elementary matrices of form :math:E_{3}. Furthermore, since we are trying to convert matrix :math:A to an upper triangular matrix we would only be concerned with eliminating elements below the diagonal wouldn't we? Therefore all of the elementary matrices applied would be some multiple of row :math:i being added to some row :math:j where :math:i &gt; j. Recall that the off-diagonal element of :math:E_{3} occurs at location :math:e_{ij}. Therefore the forward elimination step would **always** consist of lower triangular matrices no? So in the case of the first column of A we would simply left multiply A by the following three elementary row operation matrices to eliminate the below diagonal elements wouldn't we? (go ahead, try it out by hand)<br /> <br /> .. math::<br /> <br /> A^{(1)} = \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ -\frac{a^{(0)}_{41}}{a^{(0)}_{11}} &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ -\frac{a^{(0)}_{31}}{a^{(0)}_{11}} &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ -\frac{a^{(0)}_{21}}{a^{(0)}_{11}} &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{cccc} 1 &amp; 1 &amp; 1 &amp; 1\\ 1 &amp; -2 &amp; -2 &amp; -2\\ 1 &amp; 4 &amp; -4 &amp; 1\\ 1 &amp; -5 &amp; -5 &amp; -3\\ \end{array} \right] = \left[\begin{array}{cccc} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 3 &amp; -5 &amp; 0\\ 0 &amp; -6 &amp; -6 &amp; -4\\ \end{array} \right]<br /> <br /> But we can't exactly claim :math:A = A^{(1)} can we. So what would we need to do to make the equivalence correct? We would need to multiply by the inverse of the elementary row operations.<br /> <br /> .. math::<br /> <br /> A = \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ \frac{a^{(0)}_{21}}{a^{(0)}_{11}} &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ \frac{a^{(0)}_{31}}{a^{(0)}_{11}} &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ \frac{a^{(0)}_{41}}{a^{(0)}_{11}} &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right]\left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ -\frac{a^{(0)}_{41}}{a^{(0)}_{11}} &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ -\frac{a^{(0)}_{31}}{a^{(0)}_{11}} &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ -\frac{a^{(0)}_{21}}{a^{(0)}_{11}} &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right] \left[\begin{array}{cccc} 1 &amp; 1 &amp; 1 &amp; 1\\ 1 &amp; -2 &amp; -2 &amp; -2\\ 1 &amp; 4 &amp; -4 &amp; 1\\ 1 &amp; -5 &amp; -5 &amp; -3\\ \end{array} \right]<br /> <br /> At this point it should be stated that when multiplying elementary matrices of type :math:E_{3} where no off-diagonal elements occur in the same position (always the case when performing forward elimination) the result of the multiplication is simply a matrix containing **all** of the off-diagonal elements in the exact same positions they occurred separately (a prove will not be given but the reader is encouraged to investigate the validity of this statement on their own). Therefore if we conduct the multiplication of the row operations and inverse operations respectively then it is easy to see the system simplifies to:<br /> <br /> .. math::<br /> <br /> A = \underbrace{\left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ \frac{a^{(0)}_{21}}{a^{(0)}_{11}} &amp; 1 &amp; 0 &amp; 0 \\ \frac{a^{(0)}_{31}}{a^{(0)}_{11}} &amp; 0 &amp; 1 &amp; 0 \\ \frac{a^{(0)}_{41}}{a^{(0)}_{11}} &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right]}_{(L^{(1)})^{-1}} \underbrace{\left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0 \\ -\frac{a^{(0)}_{21}}{a^{(0)}_{11}} &amp; 1 &amp; 0 &amp; 0 \\ -\frac{a^{(0)}_{31}}{a^{(0)}_{11}} &amp; 0 &amp; 1 &amp; 0 \\ -\frac{a^{(0)}_{41}}{a^{(0)}_{11}} &amp; 0 &amp; 0 &amp; 1 \\ \end{array}\right]}_{L^{(1)}} \underbrace{\left[\begin{array}{cccc} 1 &amp; 1 &amp; 1 &amp; 1\\ 1 &amp; -2 &amp; -2 &amp; -2\\ 1 &amp; 4 &amp; -4 &amp; 1\\ 1 &amp; -5 &amp; -5 &amp; -3\\ \end{array} \right]}_{A} = (L^{(1)})^{-1}A^{(1)}<br /> <br /> Now what if we wanted to eliminate the second column of :math:A (also :math:A^{(1)})? Well naturally we would follow the exact same process (i.e. we would multiply :math:A^{(1)} by the elementary row operation matrices required to eliminate all elements below the second diagonal, remembering to also multiply by the inverse of the operations to maintain the equivalence to :math:A).<br /> <br /> .. math::<br /> <br /> \begin{array}{rcl} A &amp; = &amp; (L^{(1)})^{-1}(L^{(2)})^{-1}L^{(2)}A^{(1)}\\ A &amp; = &amp; (L^{(1)})^{-1}(L^{(2)})^{-1}A^{(2)} \end{array}<br /> <br /> Are you starting to see the pattern now? If we were to repeat this process :math:n-1 times (where :math:n is the dimension of :math:A) then the forward elimination would be complete and therefore :math:A^{(n-1)} would **have to be upper triangular** (as a quick aside, we repeat the process :math:n-1 times and not :math:n times because we do not have to eliminate the first row).<br /> <br /> .. math::<br /> <br /> \begin{array}{rcl} A &amp; = &amp; (L^{(1)})^{-1}(L^{(2)})^{-1} \cdots (L^{(n-2)})^{-1}(L^{(n-1)})^{-1} L^{(n-1)}L^{(n-2)} \ldots L^{(2)}L^{(1)}A\\ A &amp; = &amp; (L^{(1)})^{-1}(L^{(2)})^{-1} \cdots (L^{(n-2)})^{-1}(L^{(n-1)})^{-1} A^{(n-1)}\\ A &amp; = &amp; (L^{(1)})^{-1}(L^{(2)})^{-1} \cdots (L^{(n-2)})^{-1}(L^{(n-1)})^{-1} U\\ \end{array}<br /> <br /> Now recall the the inverse of a lower triangular matrix is also a lower triangular matrix and the multiplication of two lower triangular matrices is also lower triangular. Therefore all of the terms to the left of :math:U collect to form one lower triangular matrix :math:L.<br /> <br /> .. math::<br /> <br /> A = LU<br /> <br /> But this is exactly what we wanted when we started the LU decomposition process. Furthermore, knowing the simplified multiplication rule for :math:E_{3} type matrices presented earlier, it is easy to see that :math:L equals:<br /> <br /> .. math::<br /> <br /> L = \left[ \begin{array}{rrrrrrr} 1 &amp; 0 &amp; 0 &amp; \cdots &amp; 0 &amp; 0 &amp; 0\\ \frac{a^{(0)}_{21}}{a^{(0)}_{11}} &amp; 1 &amp; 0 &amp; \cdots &amp; 0 &amp; 0 &amp; 0\\ \frac{a^{(0)}_{31}}{a^{(0)}_{11}} &amp; \frac{a^{(1)}_{32}}{a^{(1)}_{22}} &amp; 1 &amp; \cdots &amp; 0 &amp; 0 &amp; 0\\ \vdots &amp; \vdots &amp; \vdots &amp; \ddots &amp; \vdots &amp; \vdots &amp; \vdots\\ \frac{a^{(0)}_{n-2,1}}{a^{(0)}_{11}} &amp; \frac{a^{(1)}_{n-2,2}}{a^{(1)}_{22}} &amp; \frac{a^{(2)}_{n-2,3}}{a^{(2)}_{33}} &amp; \cdots &amp; 1 &amp; 0 &amp; 0\\ \frac{a^{(0)}_{n-1,1}}{a^{(0)}_{11}} &amp; \frac{a^{(1)}_{n-1,2}}{a^{(1)}_{22}} &amp; \frac{a^{(2)}_{n-1,3}}{a^{(2)}_{33}} &amp; \cdots &amp; \frac{a^{(n-2)}_{n-1,n-2}}{a^{(n-2)}_{n-2,n-2}} &amp; 1 &amp; 0\\ \frac{a^{(0)}_{n,1}}{a^{(0)}_{11}} &amp; \frac{a^{(1)}_{n,2}}{a^{(1)}_{22}} &amp; \frac{a^{(2)}_{n,3}}{a^{(2)}_{33}} &amp; \cdots &amp; \frac{a^{(n-2)}_{n,n-2}}{a^{(n-2)}_{n-2,n-2}} &amp; \frac{a^{(n-1)}_{n,n-1}}{a^{(n-1)}_{n-1,n-1}} &amp; 1\\ \end{array}\right]<br /> <br /> This is exactly the general form of L presented in the notes. This is where that form comes from.<br /> <br /> #. To perform LU decomposition in MATLAB we use the lu function. The two most common methods of using lu are:<br /> <br /> * [L,U] = lu(A) : this will return the :math:L and :math:U matrices with the :math:L matrix multiplied by a permutation matrix :math:P (if partial pivoting occurred during the solution procedure).<br /> <br /> * [L,U,P] = lu(A) : this will return the :math:L and :math:U matrices with the implicit understanding that the system solved was :math:PA.<br /> <br /> To perform LU decomposition in Python we use the lu_factor function.<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-2/code/question2_part2_matlab.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-2/code/question2_part2_python.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> We note that the results are different from those we calculated above. This is because MATLAB and Python perform :math:LU decomposition with partial pivoting. Thus the system solved was actually :math:PA = LU.<br /> <br /> #. <br /> To calculate the inverse of a matrix using its :math:LU decomposition we realize that the system :math:AX = B, where :math:X and :math:B are matrices, may be solved by splitting :math:X and :math:B into their individual column pairs (i.e. column 1 of :math:X goes with column 1 of :math:B, column 2 with column 2, etc.) and solving each system of linear equations separately. In this case we know a matrix multiplied by its inverse yields the identity matrix. Therefore:<br /> <br /> .. math::<br /> <br /> B = \left[\begin{array}{cccc} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right]<br /> <br /> Finally, we recall that the :math:LU decomposition of a matrix may be used to simplify the solution process by splitting the system into two separate systems that are easily solved.<br /> <br /> * :math:Ly = b<br /> <br /> * :math:Ux = y<br /> <br /> **Invert column 1**<br /> <br /> We start with the subsystem :math:Ly = b.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; -1 &amp; 1 &amp; 0\\ 1 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{11}\\ y_{21}\\ y_{31}\\ y_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1\\ 0\\ 0\\ 0\\ \end{array} \right]<br /> <br /> We eliminate the elements in the first column under the diagonal (pivot) element by adding (-1) times row 1 to row 2, (-1) times row 1 to row 3, and (-1) times row 1 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 + (-1)(1) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; -1 + (-1)(0) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; 2 + (-1)(0) &amp; 0 + (-1)(0) &amp; 1 + (-1)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{11}\\ y_{21}\\ y_{31}\\ y_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1\\ 0 + (-1)(1)\\ 0 + (-1)(1)\\ 0 + (-1)(1)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 &amp; 1 &amp; 0\\ 0 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{11}\\ y_{21}\\ y_{31}\\ y_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1\\ -1\\ -1\\ -1\\ \end{array} \right] <br /> <br /> We eliminate the elements in the second column under the diagonal (pivot) element by adding (1) times row 2 to row 3, and (-2) times row 2 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 + (1)(1) &amp; 1 + (1)(0) &amp; 0 + (1)(0)\\ 0 &amp; 2 + (-2)(1) &amp; 0 + (-2)(0) &amp; 1 + (-2)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{11}\\ y_{21}\\ y_{31}\\ y_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1\\ -1\\ -1 + (1)(-1)\\ -1 + (-2)(-1)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{11}\\ y_{21}\\ y_{31}\\ y_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1\\ -1\\ -2\\ 1\\ \end{array} \right]<br /> <br /> Now that we have solved for :math:y we move on to solve the second subsystem :math:Ux = y.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 0 &amp; -8 &amp; -3\\ 0 &amp; 0 &amp; 0 &amp; 2\\ \end{array} \right] \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1\\ -1\\ -2\\ 1\\ \end{array} \right]<br /> <br /> We start by dividing row 4 by its diagonal element and then adding 3 times row 4 to row 3, 3 times row 4 to row 2, and (-1) times row 4 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1 + (-1)\left(\frac{2}{(2)}\right)\\ 0 &amp; -3 &amp; -3 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; -8 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; 0 &amp; \frac{2}{(2)}\\ \end{array} \right] \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1 + (-1)\left(\frac{1}{(2)}\right)\\ -1 + (3)\left(\frac{1}{(2)}\right)\\ -2 + (3)\left(\frac{1}{(2)}\right)\\ \frac{1}{(2)}\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 0\\ 0 &amp; -3 &amp; -3 &amp; 0\\ 0 &amp; 0 &amp; -8 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1/2\\ 1/2\\ -1/2\\ 1/2\\ \end{array} \right]<br /> <br /> Next we divide row 3 by its diagonal element and then add 3 times row 3 to row 2, and (-1) times row 3 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 + (-1)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; -3 &amp; -3 + (3)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; 0 &amp; \frac{-8}{(-8)} &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 1/2 + (-1)\left(\frac{-1/2}{(-8)}\right)\\ 1/2 + (3)\left(\frac{-1/2}{(-8)}\right)\\ \frac{-1/2}{(-8)}\\ 1/2\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 7/16\\ 11/16\\ 1/16\\ 1/2\\ \end{array} \right]<br /> <br /> Finally we divide row 2 by its diagonal element and then add (-1) times row 2 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 + (-1)\left(\frac{-3}{(-3)}\right) &amp; 0 &amp; 0\\ 0 &amp; \frac{-3}{(-3)} &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 7/16 + (-1)\frac{11/16}{(-3)}\\ \frac{11/16}{(-3)}\\ 1/16\\ 1/2\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 32/48\\ -11/48\\ 3/48\\ 24/48\\ \end{array} \right]<br /> <br /> Therefore the first column of the inverse of :math:A is.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{r} x_{11}\\ x_{21}\\ x_{31}\\ x_{41}\\ \end{array} \right] = \left[\begin{array}{r} 32/48\\ -11/48\\ 3/48\\ 24/48\\ \end{array} \right]<br /> <br /> **Invert column 2**<br /> <br /> We start with the subsystem :math:Ly = b.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; -1 &amp; 1 &amp; 0\\ 1 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{12}\\ y_{22}\\ y_{32}\\ y_{42}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 1\\ 0\\ 0\\ \end{array} \right]<br /> <br /> We eliminate the elements in the first column under the diagonal (pivot) element by adding (-1) times row 1 to row 2, (-1) times row 1 to row 3, and (-1) times row 1 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 + (-1)(1) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; -1 + (-1)(0) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; 2 + (-1)(0) &amp; 0 + (-1)(0) &amp; 1 + (-1)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{12}\\ y_{22}\\ y_{32}\\ y_{42}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 1 + (-1)(0)\\ 0 + (-1)(0)\\ 0 + (-1)(0)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 &amp; 1 &amp; 0\\ 0 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{12}\\ y_{22}\\ y_{32}\\ y_{42}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 1\\ 0\\ 0\\ \end{array} \right] <br /> <br /> We eliminate the elements in the second column under the diagonal (pivot) element by adding (1) times row 2 to row 3, and (-2) times row 2 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 + (1)(1) &amp; 1 + (1)(0) &amp; 0 + (1)(0)\\ 0 &amp; 2 + (-2)(1) &amp; 0 + (-2)(0) &amp; 1 + (-2)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{12}\\ y_{22}\\ y_{32}\\ y_{42}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 1\\ 0 + (1)(1)\\ 0 + (-2)(1)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{12}\\ y_{22}\\ y_{32}\\ y_{42}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 1\\ 1\\ -2\\ \end{array} \right]<br /> <br /> Now that we have solved for :math:y we move on to solve the second subsystem :math:Ux = y.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 0 &amp; -8 &amp; -3\\ 0 &amp; 0 &amp; 0 &amp; 2\\ \end{array} \right] \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 1\\ 1\\ -2\\ \end{array} \right]<br /> <br /> We start by dividing row 4 by its diagonal element and then adding 3 times row 4 to row 3, 3 times row 4 to row 2, and (-1) times row 4 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1 + (-1)\left(\frac{2}{(2)}\right)\\ 0 &amp; -3 &amp; -3 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; -8 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; 0 &amp; \frac{2}{(2)}\\ \end{array} \right] \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 0 + (-1)\left(\frac{-2}{(2)}\right)\\ 1 + (3)\left(\frac{-2}{(2)}\right)\\ 1 + (3)\left(\frac{-2}{(2)}\right)\\ \frac{-2}{(2)}\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 0\\ 0 &amp; -3 &amp; -3 &amp; 0\\ 0 &amp; 0 &amp; -8 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 1\\ -2\\ -2\\ -1\\ \end{array} \right]<br /> <br /> Next we divide row 3 by its diagonal element and then add 3 times row 3 to row 2, and (-1) times row 3 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 + (-1)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; -3 &amp; -3 + (3)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; 0 &amp; \frac{-8}{(-8)} &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 1 + (-1)\left(\frac{-2}{(-8)}\right)\\ -2 + (3)\left(\frac{-2}{(-8)}\right)\\ \frac{-2}{(-8)}\\ -1\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 3/4\\ -5/4\\ 1/4\\ -1\\ \end{array} \right]<br /> <br /> Finally we divide row 2 by its diagonal element and then add (-1) times row 2 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 + (-1)\left(\frac{-3}{(-3)}\right) &amp; 0 &amp; 0\\ 0 &amp; \frac{-3}{(-3)} &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 3/4 + (-1)\frac{-5/4}{(-3)}\\ \frac{-5/4}{(-3)}\\ 1/4\\ -1\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 1/3\\ 5/12\\ 1/4\\ -1\\ \end{array} \right]<br /> <br /> Therefore the first column of the inverse of :math:A is.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{r} x_{12}\\ x_{22}\\ x_{32}\\ x_{42}\\ \end{array} \right] = \left[\begin{array}{r} 16/48\\ 20/48\\ 12/48\\ -48\\ \end{array} \right]<br /> <br /> **Invert column 3**<br /> <br /> We start with the subsystem :math:Ly = b.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; -1 &amp; 1 &amp; 0\\ 1 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{13}\\ y_{23}\\ y_{33}\\ y_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 1\\ 0\\ \end{array} \right]<br /> <br /> We eliminate the elements in the first column under the diagonal (pivot) element by adding (-1) times row 1 to row 2, (-1) times row 1 to row 3, and (-1) times row 1 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 + (-1)(1) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; -1 + (-1)(0) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; 2 + (-1)(0) &amp; 0 + (-1)(0) &amp; 1 + (-1)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{13}\\ y_{23}\\ y_{33}\\ y_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0 + (-1)(0)\\ 1 + (-1)(0)\\ 0 + (-1)(0)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 &amp; 1 &amp; 0\\ 0 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{13}\\ y_{23}\\ y_{33}\\ y_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 1\\ 0\\ \end{array} \right] <br /> <br /> We eliminate the elements in the second column under the diagonal (pivot) element by adding (1) times row 2 to row 3, and (-2) times row 2 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 + (1)(1) &amp; 1 + (1)(0) &amp; 0 + (1)(0)\\ 0 &amp; 2 + (-2)(1) &amp; 0 + (-2)(0) &amp; 1 + (-2)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{13}\\ y_{23}\\ y_{33}\\ y_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 1 + (1)(0)\\ 0 + (-2)(0)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{13}\\ y_{23}\\ y_{33}\\ y_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 1\\ 0\\ \end{array} \right]<br /> <br /> Now that we have solved for :math:y we move on to solve the second subsystem :math:Ux = y.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 0 &amp; -8 &amp; -3\\ 0 &amp; 0 &amp; 0 &amp; 2\\ \end{array} \right] \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 1\\ 0\\ \end{array} \right]<br /> <br /> We start by dividing row 4 by its diagonal element and then adding 3 times row 4 to row 3, 3 times row 4 to row 2, and (-1) times row 4 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1 + (-1)\left(\frac{2}{(2)}\right)\\ 0 &amp; -3 &amp; -3 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; -8 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; 0 &amp; \frac{2}{(2)}\\ \end{array} \right] \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0 + (-1)\left(\frac{0}{(2)}\right)\\ 0 + (3)\left(\frac{0}{(2)}\right)\\ 1 + (3)\left(\frac{0}{(2)}\right)\\ \frac{0}{(2)}\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 0\\ 0 &amp; -3 &amp; -3 &amp; 0\\ 0 &amp; 0 &amp; -8 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 1\\ 0\\ \end{array} \right]<br /> <br /> Next we divide row 3 by its diagonal element and then add 3 times row 3 to row 2, and (-1) times row 3 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 + (-1)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; -3 &amp; -3 + (3)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; 0 &amp; \frac{-8}{(-8)} &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0 + (-1)\left(\frac{1}{(-8)}\right)\\ 0 + (3)\left(\frac{1}{(-8)}\right)\\ \frac{1}{(-8)}\\ 0\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 1/8\\ -3/8\\ -1/8\\ 0\\ \end{array} \right]<br /> <br /> Finally we divide row 2 by its diagonal element and then add (-1) times row 2 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 + (-1)\left(\frac{-3}{(-3)}\right) &amp; 0 &amp; 0\\ 0 &amp; \frac{-3}{(-3)} &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 1/8 + (-1)\frac{-3/8}{(-3)}\\ \frac{-3/8}{(-3)}\\ -1/8\\ 0\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 1/8\\ -1/8\\ 0\\ \end{array} \right]<br /> <br /> Therefore the first column of the inverse of :math:A is.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{r} x_{13}\\ x_{23}\\ x_{33}\\ x_{43}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 6/48\\ -6/48\\ 0\\ \end{array} \right]<br /> <br /> **Invert column 4**<br /> <br /> We start with the subsystem :math:Ly = b.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; -1 &amp; 1 &amp; 0\\ 1 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{14}\\ y_{24}\\ y_{34}\\ y_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 0\\ 1\\ \end{array} \right]<br /> <br /> We eliminate the elements in the first column under the diagonal (pivot) element by adding (-1) times row 1 to row 2, (-1) times row 1 to row 3, and (-1) times row 1 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 1 + (-1)(1) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; -1 + (-1)(0) &amp; 1 + (-1)(0) &amp; 0 + (-1)(0)\\ 1 + (-1)(1) &amp; 2 + (-1)(0) &amp; 0 + (-1)(0) &amp; 1 + (-1)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{14}\\ y_{24}\\ y_{34}\\ y_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0 + (-1)(0)\\ 0 + (-1)(0)\\ 1 + (-1)(0)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 &amp; 1 &amp; 0\\ 0 &amp; 2 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{14}\\ y_{24}\\ y_{34}\\ y_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 0\\ 1\\ \end{array} \right] <br /> <br /> We eliminate the elements in the second column under the diagonal (pivot) element by adding (1) times row 2 to row 3, and (-2) times row 2 to row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -1 + (1)(1) &amp; 1 + (1)(0) &amp; 0 + (1)(0)\\ 0 &amp; 2 + (-2)(1) &amp; 0 + (-2)(0) &amp; 1 + (-2)(0)\\ \end{array} \right] \left[\begin{array}{r} y_{14}\\ y_{24}\\ y_{34}\\ y_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 0 + (1)(0)\\ 1 + (-2)(0)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} y_{14}\\ y_{24}\\ y_{34}\\ y_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 0\\ 1\\ \end{array} \right]<br /> <br /> Now that we have solved for :math:y we move on to solve the second subsystem :math:Ux = y.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 0 &amp; -8 &amp; -3\\ 0 &amp; 0 &amp; 0 &amp; 2\\ \end{array} \right] \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 0\\ 0\\ 1\\ \end{array} \right]<br /> <br /> We start by dividing row 4 by its diagonal element and then adding 3 times row 4 to row 3, 3 times row 4 to row 2, and (-1) times row 4 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1 + (-1)\left(\frac{2}{(2)}\right)\\ 0 &amp; -3 &amp; -3 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; -8 &amp; -3 + (3)\left(\frac{2}{(2)}\right)\\ 0 &amp; 0 &amp; 0 &amp; \frac{2}{(2)}\\ \end{array} \right] \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0 + (-1)\left(\frac{1}{(2)}\right)\\ 0 + (3)\left(\frac{1}{(2)}\right)\\ 0 + (3)\left(\frac{1}{(2)}\right)\\ \frac{1}{(2)}\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 0\\ 0 &amp; -3 &amp; -3 &amp; 0\\ 0 &amp; 0 &amp; -8 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} -1/2\\ 3/2\\ 3/2\\ 1/2\\ \end{array} \right]<br /> <br /> Next we divide row 3 by its diagonal element and then add 3 times row 3 to row 2, and (-1) times row 3 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 + (-1)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; -3 &amp; -3 + (3)\left(\frac{-8}{(-8)}\right) &amp; 0\\ 0 &amp; 0 &amp; \frac{-8}{(-8)} &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} -1/2 + (-1)\left(\frac{3/2}{(-8)}\right)\\ 3/2 + (3)\left(\frac{3/2}{(-8)}\right)\\ \frac{3/2}{(-8)}\\ 1/2\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} -5/16\\ 15/16\\ -3/16\\ 1/2\\ \end{array} \right]<br /> <br /> Finally we divide row 2 by its diagonal element and then add (-1) times row 2 to row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 + (-1)\left(\frac{-3}{(-3)}\right) &amp; 0 &amp; 0\\ 0 &amp; \frac{-3}{(-3)} &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} -5/16 + (-1)\frac{15/16}{(-3)}\\ \frac{15/16}{(-3)}\\ -3/16\\ 1/2\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; -3 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ -15/48\\ -3/16\\ 1/2\\ \end{array} \right]<br /> <br /> Therefore the first column of the inverse of :math:A is.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{r} x_{14}\\ x_{24}\\ x_{34}\\ x_{44}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ -15/48\\ -9/48\\ 24/48\\ \end{array} \right]<br /> <br /> Therefore the inverse of :math:A is<br /> <br /> .. math::<br /> <br /> A^{-1} = [x_{:1},x_{:2},x_{:3},x_{:4}] = \left[\begin{array}{rrrr} 32/48 &amp; 16/48 &amp; 0 &amp; 0\\ -11/48 &amp; 20/48 &amp; 6/48 &amp; -15/48\\ 3/48 &amp; 12/48 &amp; -6/48 &amp; -9/48\\ 24/48 &amp; -48/48 &amp; 0 &amp; 24/48\\ \end{array}\right] = \left[\begin{array}{rrrr} 0.6667 &amp; 0.3333 &amp; 0 &amp; 0\\ -0.2292 &amp; 0.4167 &amp; 0.1250 &amp; -0.3125\\ 0.0625 &amp; 0.2500 &amp; -0.1250 &amp; -0.1875\\ 0.5000 &amp; -1.0000 &amp; 0 &amp; 0.5000\\ \end{array}\right]<br /> <br /> **Check the inverse**<br /> <br /> To check the inverse of :math:A in MATLAB and Python we use the inv() function.<br /> <br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-2/code/question2_part3_matlab.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-2/code/question2_part3_python.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> Finally we show that :math:AA^{-1} = I. Since matrix multiplication takes up alot of space we will do this column by column.<br /> <br /> .. math::<br /> <br /> id_{:1} = \left[\begin{array}{r} a_{11}a^{-1}_{11} + a_{12}a^{-1}_{21} + a_{13}a^{-1}_{31} + a_{14}a^{-1}_{41}\\ a_{21}a^{-1}_{11} + a_{22}a^{-1}_{21} + a_{23}a^{-1}_{31} + a_{24}a^{-1}_{41}\\ a_{31}a^{-1}_{11} + a_{32}a^{-1}_{21} + a_{33}a^{-1}_{31} + a_{34}a^{-1}_{41}\\ a_{41}a^{-1}_{11} + a_{42}a^{-1}_{21} + a_{43}a^{-1}_{31} + a_{44}a^{-1}_{41}\\ \end{array}\right] = \left[\begin{array}{r} (1)(32/48) + (1)(-11/48) + (1)(3/48) + (1)(24/48)\\ (1)(32/48) + (-2)(-11/48) + (-2)(3/48) + (-2)(24/48)\\ (1)(32/48) + (4)(-11/48) + (-4)(3/48) + (1)(24/48)\\ (1)(32/48) + (-5)(-11/48) + (-5)(3/48) + (1)(24/48)\\ \end{array}\right] = \left[\begin{array}{r} 1\\ 0\\ 0\\ 0\\ \end{array}\right]<br /> <br /> .. math::<br /> <br /> id_{:2} = \left[\begin{array}{r} a_{11}a^{-1}_{12} + a_{12}a^{-1}_{22} + a_{13}a^{-1}_{32} + a_{14}a^{-1}_{42}\\ a_{21}a^{-1}_{12} + a_{22}a^{-1}_{22} + a_{23}a^{-1}_{32} + a_{24}a^{-1}_{42}\\ a_{31}a^{-1}_{12} + a_{32}a^{-1}_{22} + a_{33}a^{-1}_{32} + a_{34}a^{-1}_{42}\\ a_{41}a^{-1}_{12} + a_{42}a^{-1}_{22} + a_{43}a^{-1}_{32} + a_{44}a^{-1}_{42}\\ \end{array}\right] = \left[\begin{array}{r} (1)(16/48) + (1)(20/48) + (1)(12/48) + (1)(-48/48)\\ (1)(16/48) + (-2)(20/48) + (-2)(12/48) + (-2)(-48/48)\\ (1)(16/48) + (4)(20/48) + (-4)(12/48) + (1)(-48/48)\\ (1)(16/48) + (-5)(20/48) + (-5)(12/48) + (1)(-48/48)\\ \end{array}\right] = \left[\begin{array}{r} 0\\ 1\\ 0\\ 0\\ \end{array}\right]<br /> <br /> .. math::<br /> <br /> id_{:3} = \left[\begin{array}{r} a_{11}a^{-1}_{13} + a_{12}a^{-1}_{23} + a_{13}a^{-1}_{33} + a_{14}a^{-1}_{43}\\ a_{21}a^{-1}_{13} + a_{22}a^{-1}_{23} + a_{23}a^{-1}_{33} + a_{24}a^{-1}_{43}\\ a_{31}a^{-1}_{13} + a_{32}a^{-1}_{23} + a_{33}a^{-1}_{33} + a_{34}a^{-1}_{43}\\ a_{41}a^{-1}_{13} + a_{42}a^{-1}_{23} + a_{43}a^{-1}_{33} + a_{44}a^{-1}_{43}\\ \end{array}\right] = \left[\begin{array}{r} (1)(0) + (1)(6/48) + (1)(-6/48) + (1)(0)\\ (1)(0) + (-2)(6/48) + (-2)(-6/48) + (-2)(0)\\ (1)(0) + (4)(6/48) + (-4)(-6/48) + (1)(0)\\ (1)(0) + (-5)(6/48) + (-5)(-6/48) + (1)(0)\\ \end{array}\right] = \left[\begin{array}{r} 0\\ 0\\ 1\\ 0\\ \end{array}\right]<br /> .. math::<br /> <br /> id_{:4} = \left[\begin{array}{r} a_{11}a^{-1}_{14} + a_{12}a^{-1}_{24} + a_{13}a^{-1}_{34} + a_{14}a^{-1}_{44}\\ a_{21}a^{-1}_{14} + a_{22}a^{-1}_{24} + a_{23}a^{-1}_{34} + a_{24}a^{-1}_{44}\\ a_{31}a^{-1}_{14} + a_{32}a^{-1}_{24} + a_{33}a^{-1}_{34} + a_{34}a^{-1}_{44}\\ a_{41}a^{-1}_{14} + a_{42}a^{-1}_{24} + a_{43}a^{-1}_{34} + a_{44}a^{-1}_{44}\\ \end{array}\right] = \left[\begin{array}{r} (1)(0) + (1)(-15/48) + (1)(-9/48) + (1)(24/48)\\ (1)(0) + (-2)(-15/48) + (-2)(-9/48) + (-2)(24/48)\\ (1)(0) + (4)(-15/48) + (-4)(-9/48) + (1)(24/48)\\ (1)(0) + (-5)(-15/48) + (-5)(-9/48) + (1)(24/48)\\ \end{array}\right] = \left[\begin{array}{r} 0\\ 0\\ 0\\ 1\\ \end{array}\right]<br /> <br /> From the above it is easy to see that<br /> <br /> .. math::<br /> <br /> AA^{-1} = \left[id_{:1},id_{:2},id_{:3},id_{:4}\right] = \left[\begin{array}{cccc} 1 &amp; 0 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] = I<br /> <br /> Therefore we have proven that :math:AA^{-1} = I.<br /> <br /> <br /> &lt;/rst&gt;<br /> {{Navigation|Book=Assignment 2 - 2010 - Solution|previous=Question 1|current=Back to all questions|next=Question 3}}</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Assignment_2_-_2010_-_Solution/Question_1&diff=1089 Assignment 2 - 2010 - Solution/Question 1 2018-09-16T08:17:27Z <p>Kevindunn: </p> <hr /> <div>{{Navigation|Book=Assignment 2 - 2010 - Solution|previous=|current=Back to all questions|next=Question 2}}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> <br /> Question 1 <br /> ================<br /> <br /> The Gauss elimination method is an effective algorithm for solving a set of linear equations, :math:Ax = b. Solve this set of equations by hand, showing the steps taken.<br /> <br /> .. math::<br /> <br /> \left\{\begin{array}{r} x_1 + x_2 + x_3 + x_4 = 0\hphantom{-}\\ x_1 - 2x_2 - 2x_3 - 2x_4 = 4\hphantom{-}\\ x_1 + 4x_2 - 4x_3 + x_4 = 2\hphantom{-}\\ x_1 - 5x_2 - 5x_3 - 3x_4 = -4\\ \end{array}\right.\\<br /> <br /> Solution<br /> ---------<br /> <br /> We start by converting the system of equations to the form :math:Ax = b.<br /> <br /> .. math::<br /> <br /> \begin{array}{ccccccccc} x_{1} &amp; + &amp; x_{2} &amp; + &amp; x_{3} &amp; + &amp; x_{4} &amp; = &amp; 0\hphantom{-}\\ x_{1} &amp; - &amp; 2x_{2} &amp; - &amp; 2x_{3} &amp; - &amp; 2x_{4} &amp; = &amp; 4\hphantom{-}\\ x_{1} &amp; + &amp; 4x_{2} &amp; - &amp; 4x_{3} &amp; + &amp; x_{4} &amp; = &amp; 2\hphantom{-}\\ x_{1} &amp; - &amp; 5x_{2} &amp; - &amp; 5x_{3} &amp; - &amp; 3x_{4} &amp; = &amp; -4\\ \end{array} \;\; \Longrightarrow \;\; \left[\begin{array}{cccc} 1 &amp; 1 &amp; 1 &amp; 1\\ 1 &amp; -2 &amp; -2 &amp; -2\\ 1 &amp; 4 &amp; -4 &amp; 1\\ 1 &amp; -5 &amp; -5 &amp; -3\\ \end{array} \right] \left[\begin{array}{c} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{c} 0\\ 4\\ 2\\ -4\\ \end{array} \right]<br /> <br /> **Forward elimination**<br /> <br /> Recall that the basic algorithm for the forward elimination step of naive Gauss elimination is:<br /> <br /> #. Divide the current row :math:i by the :math:i\text{th} diagonal element.<br /> #. Eliminate all elements in the :math:i\text{th} column below the :math:i\text{th} diagonal element (i.e. rows :math:j &gt; :math:i) by subtracting :math:n times the :math:i\text{th} row from the :math:j\text{th} row (where :math:n is the current value of value in the :math:i\text{th} column of the :math:j\text{th} row, i.e. element ( :math:i , :math:j )). Do not forget to subtract the :math:b vector elements as well.<br /> #. Move to the next row.<br /> #. Repeat steps 1 - 3 until the A matrix is upper triangular (i.e. you have eliminated all elements below the diagonal).<br /> <br /> Therefore our first step is to divide row 1 by the it's diagonal element (in this case the first value), which is equal to 1 (easy enough)<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} \frac{1}{(1)} &amp; \frac{1}{(1)} &amp; \frac{1}{(1)} &amp; \frac{1}{(1)}\\ 1 &amp; -2 &amp; -2 &amp; -2\\ 1 &amp; 4 &amp; -4 &amp; 1\\ 1 &amp; -5 &amp; -5 &amp; -3\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} \frac{0}{(1)}\\ 4\\ 2\\ -4\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 1 &amp; -2 &amp; -2 &amp; -2\\ 1 &amp; 4 &amp; -4 &amp; 1\\ 1 &amp; -5 &amp; -5 &amp; -3\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 4\\ 2\\ -4\\ \end{array} \right]<br /> <br /> Next we subtract row 1 from rows 2, 3, and 4 using :math:n values equal to the coefficients in the first column of each row. In thise case the all the column coefficients are equal to 1, which makes our lives easy again.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 1 - (1)(1) &amp; -2 - (1)(1) &amp; -2 - (1)(1) &amp; -2 - (1)(1)\\ 1 - (1)(1) &amp; 4 - (1)(1) &amp; -4 - (1)(1) &amp; 1 - (1)(1)\\ 1 - (1)(1) &amp; -5 - (1)(1) &amp; -5 - (1)(1) &amp; -3 - (1)(1)\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 4 - (1)(0)\\ 2 - (1)(0)\\ -4 - (1)(0)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; -3 &amp; -3 &amp; -3\\ 0 &amp; 3 &amp; -5 &amp; 0\\ 0 &amp; -6 &amp; -6 &amp; -4\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ 4\\ 2\\ -4\\ \end{array} \right]<br /> <br /> Having eliminated all elements in the first column, we move to the second row/column. Once again we start by dividing the current row (row 2) by its diagonal element.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; \frac{-3}{(-3)} &amp; \frac{-3}{(-3)} &amp; \frac{-3}{(-3)}\\ 0 &amp; 3 &amp; -5 &amp; 0\\ 0 &amp; -6 &amp; -6 &amp; -4\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ \frac{4}{(-3)}\\ 2\\ -4\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; 3 &amp; -5 &amp; 0\\ 0 &amp; -6 &amp; -6 &amp; -4\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ -4/3\\ 2\\ -4\\ \end{array} \right]<br /> <br /> Next we eliminate all elements below row 2 in column 2 by subtracting 3 times row 2 from row 3 and (-6) times row 2 from row 4.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; 3 - (3)(1) &amp; -5 - (3)(1) &amp; 0 - (3)(1)\\ 0 &amp; -6 - (-6)(1) &amp; -6 - (-6)(1) &amp; -4 - (-6)(1)\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ -4/3\\ 2 - (3)(-4/3)\\ -4 - (-6)(-4/3)\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; 0 &amp; -8 &amp; -3\\ 0 &amp; 0 &amp; 0 &amp; 2\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ -4/3\\ 6\\ -12\\ \end{array} \right]<br /> <br /> Moving to row 3 we notice that the previous elimination step removed all values below the 3rd column diagonal. Therefore all we do is divide row 3 by its diagonal element and move on.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; 0 &amp; \frac{-8}{(-8)} &amp; \frac{-3}{(-8)}\\ 0 &amp; 0 &amp; 0 &amp; 2\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ -4/3\\ \frac{6}{(-8)}\\ -12\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; 0 &amp; 1 &amp; 3/8\\ 0 &amp; 0 &amp; 0 &amp; 2\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ -4/3\\ -3/4\\ -12\\ \end{array} \right]<br /> <br /> Similarly, since row 4 is the final row of our matrix, there exist no elements under the diagonal to eliminate so we simply divide through by the row diagonal element and then move on to perform backwards substitution.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; 0 &amp; 1 &amp; 3/8\\ 0 &amp; 0 &amp; 0 &amp; \frac{2}{(2)}\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ -4/3\\ -3/4\\ \frac{-12}{(2)}\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; 1 &amp; 1 &amp; 1\\ 0 &amp; 0 &amp; 1 &amp; 3/8\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 0\\ -4/3\\ -3/4\\ -6\\ \end{array} \right]<br /> <br /> **Backward substitution**<br /> <br /> Recall that the basic algorithm for the backwards substitution step of naive Gauss elimination is:<br /> <br /> #. Eliminate all elements in the :math:i\text{th} column above the :math:i\text{th} diagonal element (i.e. rows :math:j &lt; :math:i) by subtracting :math:n times the :math:i\text{th} row from the :math:j\text{th} row (where :math:n is the current value of value in the :math:i\text{th} column of the :math:j\text{th} row, i.e. element ( :math:i , :math:j )). Do not forget to subtract the :math:b vector elements as well.<br /> #. Move to the next row.<br /> #. Repeat steps 1 - 2 until the A matrix becomes the identity matrix (i.e. you have eliminated all elements above the diagonal).<br /> <br /> Therefore we start by subtracting 1 times row 4 from row 1, 1 times row 4 from row 2, and (3/8) times row 4 from row 3.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 1 - (1)(1)\\ 0 &amp; 1 &amp; 1 &amp; 1 - (1)(1)\\ 0 &amp; 0 &amp; 1 &amp; 3/8 - (3/8)(1)\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 0 - (1)(-6)\\ -4/3 - (1)(-6)\\ -3/4 - (3/8)(-6)\\ -6\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 &amp; 0\\ 0 &amp; 1 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 6\\ 14/3\\ 3/2\\ -6\\ \end{array} \right]<br /> <br /> Moving to next row, in order to eliminate all elements above the 3rd diagonal element we subtract 1 times row 3 from row 1 and 1 times row 3 from row 2.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 1 - (1)(1) &amp; 0\\ 0 &amp; 1 &amp; 1 - (1)(1) &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 6 - (1)(3/2)\\ 14/3 - (1)(3/2)\\ 3/2\\ -6\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 9/2\\ 19/6\\ 3/2\\ -6\\ \end{array} \right]<br /> <br /> Finally, we move to row 2 and eliminate all elements above the 2nd diagonal element by subtracting 1 times row 2 from row 1.<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{rrrr} 1 &amp; 1 - (1)(1) &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 9/2 - (1)(19/6)\\ 19/6\\ 3/2\\ -6\\ \end{array} \right] \;\; \Longrightarrow \;\; \left[\begin{array}{rrrr} 1 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0 &amp; 0\\ 0 &amp; 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 0 &amp; 1\\ \end{array} \right] \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 4/3\\ 19/6\\ 3/2\\ -6\\ \end{array} \right]<br /> <br /> Therefore the solution, arrived at using naive Gauss elimination, is:<br /> <br /> .. math::<br /> <br /> \left[\begin{array}{r} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ \end{array} \right] = \left[\begin{array}{r} 4/3\\ 19/6\\ 3/2\\ -6\\ \end{array} \right]<br /> &lt;/rst&gt;<br /> {{Navigation|Book=Assignment 2 - 2010 - Solution|previous=|current=Back to all questions|next=Question 2}}</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Tutorial_7_-_2010_-_Solution&diff=1088 Tutorial 7 - 2010 - Solution 2018-09-16T08:14:05Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 11 November 2010<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = Tutorial-7-2010.pdf<br /> | questions_text_alt = Tutorial questions<br /> | solutions_PDF = Tutorial-7-2010-Solutions.pdf<br /> | solutions_text_alt = Solutions by Kevin<br /> | other_instructions = Hand-in at class.<br /> }}<br /> <br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> .. |m3| replace:: m\ :sup:3<br /> .. highlight:: python<br /> <br /> .. rubric:: Tutorial objectives: Numerical differentiation and plotting.<br /> <br /> <br /> Question 1 <br /> ================<br /> <br /> This question uses the experimental data from the class notes. A batch system was charged with reactant :math:{\sf A} at time :math:t=0 at a concentration of 1.0 mol/L. As the assumed first-order reaction evolved, the concentrations were recorded:<br /> <br /> .. csv-table::<br /> :header: &quot;Time (minutes)&quot;, &quot;Concentration (mol/L)&quot;<br /> <br /> 0, 1.00<br /> 5, 0.84<br /> 10, 0.72<br /> 20, 0.57<br /> 30, 0.47<br /> 45, 0.37<br /> 60, 0.30<br /> <br /> The dynamic balance for this system can be shown to be :math:\displaystyle \frac{dC_{\sf A}(t)}{dt} = -k C_{\sf A}(t).<br /> <br /> #. Reproduce the above table and add a column that provides an approximation of :math:\displaystyle \frac{dC_{\sf A}(t)}{dt} at each time step.<br /> #. Add a fourth column that lists the order of your approximation listed in part 1.<br /> #. Use a 2nd order Lagrange polynomial to approximate the :math:C_{\sf A}(t) data, and use this polynomial to estimate :math:C_{\sf A}\,'(t) at time :math:t=15 minutes. <br /> #. Can you provide an :math:O(h^2) estimate of :math:\displaystyle \frac{dC_{\sf A}(t)}{dt} at :math:t=0? If so, please calculate it.<br /> <br /> Solution<br /> --------<br /> <br /> .. NOTE: For code that calculates these answers, please see: Notes/Figures/E-Diff-Int/plot_concentrations.py<br /> <br /> A table showing the derivative approximation, and the order:<br /> <br /> .. csv-table::<br /> :header: &quot;Time (minutes)&quot;, &quot;Concentration (mol/L)&quot;, Type of approximation, Approximation, Order<br /> <br /> 0, 1.00, Forward, :math:\frac{0.84 - 1.00}{5 - 0} = -0.032, :math:O(h)<br /> 5, 0.84, Central, :math:\frac{0.72 - 1.00}{2(10 - 5)} = -0.028, :math:O(h^2)<br /> 10, 0.72, Central, :math:\frac{0.57 - 1.00}{2(20 - 10)} = -0.0215, :math:O(h^2)<br /> 20, 0.57, Central, :math:\frac{0.47 - 0.72}{2(30 - 20)} = -0.0125, :math:O(h^2)<br /> 30, 0.47, Backward, :math:\frac{0.47 - 0.57}{30 - 20} = -0.01 , :math:O(h)<br /> 45, 0.37, Central, :math:\frac{0.30 - 0.47}{2(60 - 45)} = -0.005667, :math:O(h^2)<br /> 60, 0.30, Backward, :math:\frac{0.30 - 0.37}{60 - 45)} = -0.004667, :math:O(h)<br /> <br /> Note that the table uses an :math:O(h^2) estimate where possible; you should always prefer an :math:O(h^2) estimate over an :math:O(h) estimate, if it is possible to calculate one.<br /> <br /> In general, one should fit a polynomial using the points closest to where you want to interpolate or use that polynomial. [There are exceptions of course: if one of the points was clearly noisy, relative to the others, you can skip that point and use another instead.] In this case one should fit the polynomial using points :math:t=(5, 10, 20), and their corresponding concentrations, :math:C_{\sf A} = (0.84, 0.72, 0.57). Although in this case it didn't change the answer by much using points at :math:t=(0, 10, 20).<br /> <br /> .. math::<br /> <br /> P_2(x) &amp;= y_{1}\ell_{1}(x) + y_{2}\ell_{2}(x) + y_{3}\ell_{3}(x) \\ P'_2(x) &amp;= y_{1}\ell'_{1}(x) + y_{2}\ell'_{2}(x) + y_{3}\ell'_{3}(x) \\ &amp;= 0.84 \cdot \frac{2x - 10 - 20}{( 5-10)( 5-20)} + 0.72 \cdot \frac{2x - 5 - 20}{(10- 5)(10-20)} + 0.57 \cdot \frac{2x - 5 - 10}{(20- 5)(20-10)}\\ P'_2(15) &amp;= 0.84 \cdot 0 + 0.72 \cdot \frac{5}{-50} + 0.57 \cdot \frac{15}{150} = -0.15 <br /> The polynomial, superimposed on the data points is shown below:<br /> <br /> .. figure:: ../che3e4/Tutorials/Tutorial-7/images/tut7_q1_plot.png<br /> :scale: 50<br /> :align: center<br /> <br /> An :math:O(h^2) estimate can be found at :math:t=0 when using Richardson's extrapolation with :math:h=10 (full step), and :math:h=5 (half step).<br /> <br /> * :math:h = 10: :math:Q_1(h) = \frac{0.72 - 1.00}{10 - 0} = -0.028<br /> * :math:h = 5: :math:Q_1(h/2) = \frac{0.84 - 1.00}{5 - 0} = -0.032<br /> * Combine these to calculate a :math:Q_2(h) = 2Q_1(h/2) - Q_1(h) = 2(-0.032) - (-0.028) = -0.036<br /> <br /> Question 2 [1.5]<br /> ================<br /> <br /> A second order reaction in a constant-volume batch reactor has the following time-dependent behaviour:<br /> <br /> .. math::<br /> <br /> C_{\sf A}(t) = \frac{C_{\sf A}(0)}{C_{\sf A}(0)kt + 1} \qquad\,\text{with}\qquad\, k = 0.002 \frac{\text{m}^3}{\text{mol.s}} \qquad\,\text{and}\qquad\,C_{\sf A}(0) = 2.5 \frac{\text{mol}}{\text{m}^3}<br /> <br /> <br /> Starting with :math:h = 1.0 and decreasing in powers of 10, which step size, :math:h=\Delta t, provides the lowest error estimate of :math:\displaystyle \left.\frac{dC_{\sf A}}{dt}\right|_{t=100} at :math:t=100 seconds? <br /> <br /> Present your results in tabular form with these 5 columns:<br /> <br /> * Step size, :math:h = \Delta t<br /> * Forward difference approximation<br /> * Error in the forward difference approximation<br /> * Central difference approximation<br /> * Error in the central difference approximation<br /> <br /> Solution<br /> ---------<br /> <br /> .. note:: the last column was intended to be the error for the *central difference* approximation; an earlier version of the tutorial asked for the *backward difference* error. Either values will be accepted for the solution.<br /> <br /> Given the above equation for :math:C_{\sf A}(t), the analytical derivative can be found as: <br /> <br /> .. math::<br /> <br /> \frac{dC_{\sf A}(t)}{dt} = -k\frac{C^2_{\sf A}(0)}{\left(C_{\sf A}(0)kt + 1\right)^2} = -k C^2_{\sf A}(t)<br /> <br /> which is expected for a second-order reaction system. So the true derivative at :math:t=100 seconds is:<br /> <br /> .. math::<br /> <br /> \frac{dC_{\sf A}(100)}{dt} = -0.002\frac{2.5^2}{(2.5^2 \times 0.002 \times 100 + 1)^2} = -0.005556<br /> <br /> The required table is shown below. Two extra columns for the relative errors have been added, since that is arguably more useful than the absolute error (which was asked for in the question).<br /> <br /> .. math::<br /> <br /> \begin{array}{l|lll|lll} \hline \hline h &amp;\text{Forward diff} &amp; \text{Fwd diff error} &amp; \text{Fwd relative error[%]} &amp; \text{Central diff} &amp; \text{Central diff error} &amp; \text{Central relative error[%]} \\ \hline \hline 1 &amp; -0.0055371 &amp; 1.8457\times 10^{-5} &amp; -0.33223 &amp; -0.0055556 &amp; 6.1729\times 10^{-8} &amp; -0.0011111\\ 0.1 &amp; -0.0055537 &amp; 1.8512\times 10^{-6} &amp; -0.033322 &amp; -0.0055556 &amp; 6.1728\times 10^{-10} &amp; -1.1111\times 10^{-5}\\ 0.01 &amp; -0.0055554 &amp; 1.8518\times 10^{-7} &amp; -0.0033332 &amp; -0.0055556 &amp; 6.1648\times 10^{-12} &amp; -1.1097\times 10^{-7}\\ 1\times 10^{-3} &amp; -0.0055555 &amp; 1.8518\times 10^{-8} &amp; -0.00033333 &amp; -0.0055556 &amp; 1.4742\times 10^{-13} &amp; {\bf -2.6535\times 10^{-9}}\\ 1\times 10^{-4} &amp; -0.0055556 &amp; 1.8496\times 10^{-9} &amp; -3.3293\times 10^{-5} &amp; -0.0055556 &amp; 1.1466\times 10^{-12} &amp; -2.0639\times 10^{-8}\\ 1\times 10^{-5} &amp; -0.0055556 &amp; 1.6206\times 10^{-10}&amp; -2.917\times 10^{-6} &amp; -0.0055556 &amp; 1.558\times 10^{-11} &amp; -2.8043\times 10^{-7}\\ 1\times 10^{-6} &amp; -0.0055556 &amp; 1.558\times 10^{-11} &amp; {\bf-2.8043\times 10^{-7}} &amp; -0.0055556 &amp; 1.558\times 10^{-11} &amp; -2.8043\times 10^{-7}\\ 1\times 10^{-7} &amp; -0.0055556 &amp; 4.5967\times 10^{-10}&amp; -8.274\times 10^{-6} &amp; -0.0055556 &amp; 4.5967\times 10^{-10} &amp; -8.274\times 10^{-6}\\ 1\times 10^{-8} &amp; -0.0055556 &amp; 4.5967\times 10^{-10}&amp; -8.274\times 10^{-6} &amp; -0.0055556 &amp; 4.5967\times 10^{-10} &amp; -8.274\times 10^{-6}\\ 1\times 10^{-9} &amp; -0.0055556 &amp; 4.5967\times 10^{-10}&amp; -8.274\times 10^{-6} &amp; -0.0055556 &amp; 4.5967\times 10^{-10} &amp; -8.274\times 10^{-6}\\ 1\times 10^{-10} &amp; -0.0055578 &amp; 2.2209\times 10^{-6} &amp; -0.039976 &amp; -0.0055567 &amp; 1.1107\times 10^{-6} &amp; -0.019992\\ 1\times 10^{-11} &amp; -0.0055733 &amp; 1.7764\times 10^{-5} &amp; -0.31975 &amp; -0.0055622 &amp; 6.6618\times 10^{-6} &amp; -0.11991\\ 1\times 10^{-12} &amp; -0.0055511 &amp; 4.4404\times 10^{-6} &amp; -0.079928 &amp; -0.0054401 &amp; 0.00011546 &amp; -2.0783\\ \hline \hline \end{array} <br /> <br /> What's remarkable in the above table is how accurate the central different approximation is, even using very large values of :math:h. The point here is that the central difference should be used instead of the forward or backward difference, wherever possible.<br /> <br /> Furthermore, when using the central difference approximation with very small step sizes we start to accumulate round-off error, but no faster than the error we would have accumulated with the forward difference approximation.<br /> <br /> Bonus question [0.5]<br /> =====================<br /> <br /> Show the two error columns from question 2 in graphical form, using log-axes for both the x- and y-axis. Search for help over the internet if you don't know how to plot log-plots in MATLAB, or with Python's matplotlib library. Add a legend, grid lines and axis labels to the plot.<br /> <br /> Solution<br /> ---------<br /> <br /> .. note:: A clear plot, showing the points, lines connecting the points (different colours or dashed/solid lines), labels, legend on a log-log axis are required to obtain full grade for this question.<br /> <br /> A log-log plot with the required labels, for the absolute errors is shown here. The code to generate the plot is below.<br /> <br /> .. raw:: latex<br /> <br /> \newpage<br /> <br /> .. figure:: ../che3e4/Tutorials/Tutorial-7/images/tut7_qbonus_errors.png<br /> :scale: 60<br /> :align: center<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Tutorials/Tutorial-7/code/table_of_errors.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Tutorials/Tutorial-7/code/table_of_errors.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> <br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Tutorial_6_-_2010_-_Solution&diff=1087 Tutorial 6 - 2010 - Solution 2018-09-16T08:13:02Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 28 October 2010<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = Tutorial-6-2010.pdf<br /> | questions_text_alt = Tutorial questions<br /> | solutions_PDF = Tutorial-6-2010-Solutions.pdf<br /> | solutions_text_alt = Solutions<br /> | other_instructions = Hand-in at class.<br /> }}<br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> <br /> .. |m3| replace:: m\ :sup:3<br /> .. highlight:: python<br /> <br /> .. rubric:: Tutorial objectives: Lagrange and Newton interpolation; due 28 October.<br /> <br /> Question 1 <br /> ================<br /> <br /> Use the heat capacity data in the course slides (slide 5) at :math:T = [300, 500, 900] K, with corresponding heat capacities :math:C_p = [29.85, 30.51, 33.42] \rm J\ mol^{-1}\ K^{-1} <br /> <br /> #. Construct the Lagrange interpolating polynomial of order 2 that passes through all three data points. There is no need to simplify the polynomial.<br /> #. Using this polynomial, compute (i.e. predict) the estimated heat capacity at :math:T = 400 K and :math:T = 800 K. Are the predictions reasonable?<br /> #. Create a computer plot that shows your polynomial, over the range :math:T \in [300, 1600] K. Superimpose on this plot the 14 data points from slide 5 in the course notes. <br /> <br /> Where is the greatest prediction error in your polynomial? Is this expected?<br /> <br /> #. If you were to construct the Newton interpolating polynomial and estimate the heat capacities at :math:T = 400 K and :math:T = 800 K, what answer would you get? Explain.<br /> <br /> Solution<br /> --------<br /> <br /> #. The second order Lagrange polynomial that approximates the heat capacity function, :math:C_p(T) has the form:<br /> <br /> .. math::<br /> <br /> C_p(T) \approx p_2(T) = y_1 \ell_1(T) + y_2 \ell_2(T) + y_3 \ell_3(T) <br /> <br /> where each of the Lagrange cardinal functions, for this case is:<br /> <br /> .. math::<br /> <br /> \ell_1(T) &amp;= \displaystyle \frac{(T-500)(T-900)}{(300-500)(300-900)} \\ \ell_2(T) &amp;= \displaystyle \frac{(T-300)(T-900)}{(500-300)(500-900)} \\ \ell_3(T) &amp;= \displaystyle \frac{(T-300)(T-500)}{(900-300)(900-500)} <br /> <br /> So the Lagrange polynomial approximation is:<br /> <br /> .. math::<br /> <br /> C_p(T) \approx = \displaystyle 29.85 \cdot \frac{(T-500)(T-900)}{120000} + 30.51 \cdot \frac{(T-300)(T-900)}{-80000} + 33.42 \cdot \frac{(T-300)(T-500)}{240000} <br /> <br /> #.<br /> :math:C_p(T=400K) \approx (29.85)(0.4167) + (30.51)(0.625) + (33.42)(-0.04167) = 30.11 J/(mol.K)<br /> <br /> :math:C_p(T=800K) \approx (29.85)(-0.25) + (30.51)(0.625) + (33.42)(0.625) = 32.49 J/(mol.K)<br /> <br /> The predictions are within the range of the neighbouring data points, and fall within the upward trend relationship shown between temperature and heat capacity. Further, they are similar (less than 1% error) to the values shown at these data points in the course slides.<br /> <br /> #. The plot shows the 14 data points (black circles), of which 3 (red asterisks) were used to develop the Lagrange polynomial. The polynomial's accuracy is greatest with the range of these 3 points. Above them the accuracy deteriorates very rapidly, indicating the problem with extrapolation.<br /> <br /> .. figure:: images/tut6-lagrange-polynomial.png<br /> :scale: 60<br /> :align: center<br /> <br /> Importantly, the Lagrange polynomial passes through the 3 source data points.<br /> <br /> #. You get the same answers as in part 2, since the polynomial was created as a second order polynomial passing through 3 data points. This implies the polynomial is unique, and that the Newton form and the Lagrange form of the polynomial, when simplified to :math:p_2(x) = a_0 + a_1x + a_2 x^2, would be identical. There was no need to actually calculate the Newton form for this question.<br /> <br /> Question 2 <br /> ================<br /> <br /> The heat of reaction for a specific reaction depends on temperature as follows:<br /> <br /> ============================================ ====== ===== ===== ===== ===== =====<br /> :math:T \rm[^\circ C] -250 -200 -100 0 100 300 <br /> -------------------------------------------- ------ ----- ----- ----- ----- -----<br /> :math:\Delta H_R \rm[J\ mol^{-1}\ K^{-1}] 160.0 318.0 699.0 870.0 941.0 1040<br /> ============================================ ====== ===== ===== ===== ===== =====<br /> <br /> #. Express :math:\Delta H_R as a 5th-order polynomial using the Newton form of the interpolating polynomials. Report your intermediate results in a chart, as shown in the course material.<br /> #. Estimate :math:\Delta H_R(T=200 \rm ^\circ C) and :math:\Delta H_R(T=400 \rm ^\circ C) using the Newton polynomials. Comment on the results.<br /> <br /> Solution<br /> --------<br /> <br /> #. Newton interpolating polynomials are best determined using a table format: <br /> <br /> .. math::<br /> <br /> \begin{array}{rrrrrrr} \hline \hline ~ T &amp; \Delta H_R &amp; \text{Two terms} &amp; \text{Three terms} &amp; \text{Four terms} &amp; \text{Five terms} &amp; \text{Six terms}\\ -250 &amp; \underline{160.0} &amp; \frac{318-160}{-200-(-250)}=\underline{3.16} &amp; \frac{3.81 - 3.16}{-100-(-250)} = \underline{4.33\times 10^{-3}} &amp; \underline{ -5.933\times 10^{-5}} &amp; \underline{2.219\times 10^{-7}} &amp; \underline{-4.312\times 10^{-10}} \\ -200 &amp; 318.0 &amp; \frac{699-318}{-100-(-200)} = 3.81 &amp; \frac{1.71 - 3.81}{0-(-200)} = -1.05 \times 10^{-2} &amp; 1.833\times 10^{-5} &amp; -1.525\times 10^{-8} &amp; \\ -100 &amp; 699.0 &amp; \frac{870-699}{0-(-100)} = 1.71 &amp; \frac{0.71 - 1.71}{100-(-100)} = -5.0 \times 10^{-3} &amp; 1.071\times 10^{-5} &amp; &amp; \\ 0 &amp; 870.0 &amp; \frac{941-870}{100-0} = 0.71 &amp; \frac{0.495 - 0.71}{300-0} = -7.167 \times 10^{-4} &amp; &amp; &amp; \\ 100 &amp; 941.0 &amp; \frac{1040-941}{300-100} = 0.495 &amp; &amp; &amp; &amp; \\ 300 &amp; 1040 &amp; &amp; &amp; &amp; &amp; \\ \hline \hline \end{array}<br /> <br /> <br /> Therefore the Newton interpolating polynomial for the data set described in this equation is the following.<br /> <br /> .. math::<br /> <br /> f(x) &amp;= b_{0}+b_{1}(x-x_{1})+b_{2}(x-x_{1})(x-x_{2})+ \ldots +b_{5}(x-x_{1})(x-x_{2})(x-x_{3})(x-x_{4})(x-x_{5}) \\ &amp;= {\bf 160.0} + {\bf 3.16} (x+250) + {\bf 4.33\times 10^{-3}} (x+250) (x+200) {\bf -5.933\times 10^{-5}} (x+250) (x+200) (x+100) + \nonumber \\ &amp;{\bf 2.219\times 10^{-7}} (x+250) (x+200) (x+100) (x-0) {\bf -4.312\times 10^{-10}}(x+250) (x+200) (x+100) (x-0) (x-100) \nonumber <br /> <br /> #. The requested estimates are: :math:f(200) = 1088 J/(mol.K) and :math:f(400) = -447 J/(mol.K) <br /> <br /> The estimate for :math:T=200\ \rm ^\circ C is somewhat reasonable as it is close to the :math:\Delta H_R values for the two adjacent temperatures of :math:T=100 and :math:T=300. However, it would be most reasonable if it actually fell between those :math:\Delta H_R values. This is because the raw data shows a monotonic increase in :math:\Delta H_R with respect to temperature.<br /> <br /> The estimate for :math:T=400\ \rm ^\circ C does not make sense at all, as it is *much lower* than the value at :math:T=300\ \rm ^\circ C. Since we see in the raw data that :math:\Delta H_R increases with temperature, this estimate is clearly not very good. This illustrates the fact that an interpolating polynomial is only good for values within its bounds. Extrapolated estimates should not be relied upon. <br /> <br /> The figure illustrates what is happening for this case, where the Newton polynomial has been superimposed on the raw data (circles) and the two predictions (asterisks).<br /> <br /> .. figure:: images/tut6-newton-polynomial.png<br /> :scale: 60<br /> :align: center<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.25cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Tutorial_5_-_2010_-_Solution/Question_1&diff=1086 Tutorial 5 - 2010 - Solution/Question 1 2018-09-16T08:12:15Z <p>Kevindunn: </p> <hr /> <div>{{Navigation|Book=Tutorial 5 - 2010 - Solution|previous=|current=Back to all questions|next=Question 2}}<br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> <br /> Question 1 <br /> ===============<br /> <br /> Show the first 4 iterations of the bisection method to solve for :math:T, justifying your choice for the initial bracket. <br /> <br /> For iteration 2, 3, and 4, please also report these two relative errors:<br /> <br /> .. math::<br /> <br /> \varepsilon_\text{rel,x}^{(k)} = \displaystyle \left|\frac{x_m^{(k)} - x_m^{(k-1)}}{x_m^{(k)}} \right| \qquad\qquad \varepsilon_\text{rel,f}^{(k)} = \displaystyle \left|\frac{f(x_m^{(k)}) - f(x_m^{(k-1)})}{f(x_m^{(k)})} \right|<br /> <br /> Solution<br /> --------<br /> <br /> Since we are attempting to solve for the temperature associated with :math:\Delta H_{r}^{0}(T) = -23505 cal/mol we are essentially asked to solve for the zero of the following function:<br /> <br /> .. math::<br /> <br /> f(T) = - 24097 - 0.26T + 1.69\text{x}10^{-3}T^{2} + \frac{1.5\text{x}10^{5}}{T} - (-23505)<br /> <br /> where<br /> <br /> .. math::<br /> <br /> \Delta H_{r}^{0}(T) = - 24097 - 0.26T + 1.69\text{x}10^{-3}T^{2} + \frac{1.5\text{x}10^{5}}{T}<br /> <br /> To apply the bisection method we first need to select a starting interval that the root is known to fall within. More specifically, we need to also be sure that the root is a &quot;true root&quot; in the sense that it crosses the y-axis (and therefore the function changes sign on either side of the root). **The bisection method is unable to locate roots that only touch the x-axis but do not cross it, e.g. a double root**. Given that this is a physically perceivable system of one variable we can select the lower and upper bounds graphically or based on prior engineering knowledge.<br /> <br /> If we choose to attack the problem based on our previous knowledge then selecting the lower bound is easy since we are dealing with the Kelvin temperature scale. Therefore we know the lowest allowable temperature is 0 K. Selecting the upper bound based on prior knowledge is slightly trickier. If we knew what reaction this equation is describing then we might have some basic knowledge of how the heat of reaction changes with T. In this case we would be able to guess a suitable upper temperature. Other than this we are essentially stuck trying to overguess to ensure we have a root contained in our bounds. This technique, of course, carries with it the critical issue that if the function contains multiple roots (as it will be shown this one does) an over guess could easily contain more than one root and so would either confuse the solver or cause the solver to think that no roots exist (if the function is a quadratic shape, for example, then it is possible that the function could have the same sign on either end of a bounding interval despite containing both roots). For this reason I would recommend the graphical approach.<br /> <br /> Plotting the function in MATLAB/Python is accomplished using the following scripts.<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Tutorials/Tutorial-5/code/Q1_Plot.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Tutorials/Tutorial-5/code/Q1_Plot.py<br /> :language2: python<br /> :header2: Python code <br /> <br /> The result of this script is shown below. From the plot below it is clear that this function has two roots. One between the bounds (200,400) and one between the bounds (400,600).<br /> <br /> .. figure:: ../che3e4/Tutorials/Tutorial-5/images/Q1_Figure.png<br /> :scale: 70<br /> :align: center<br /> <br /> As a final note before getting into the bisection solution, it is believed that some confusion has likely arisen over the function error tolerance equation. In this case f(x) refers to the original :math:\Delta H_{r}^{0}(T) function and not the vertically shifted function you are trying to find the roots of (i.e. :math:\Delta H_{r}^{0}(T) + 23505). Since in your adjusted function you are trying to solve for when the function is 0, it does not make sense to calculate a relative comparison to 0 (the numbers will explode and contain essentially no useful information). This is actually an error that can crop up when a root also happens to be zero, but naturally this is less easy to predict. So to quickly wrap up, when root finding, your best bets for stopping criteria are either the relative error on x from step to step (unless the root is zero), the value of the root finding function (this would be the most versatile, as you are no longer dividing by any value and you know your function will be going to zero), the relative error of the original function from step to step (unless this also is equal to zero at the root...).<br /> <br /> For the bisection test we will consider the lower of the two bounds. Utilizing the procedure given on slide 8 of the *Section C: Nonlinear Algebraic Equations* slide set.<br /> <br /> **ITERATION 1**<br /> <br /> *STEP 0: INITIALIZATION*<br /> <br /> * :math:T_{U}^{(0)} = 400<br /> * :math:T_{L}^{(0)} = 200<br /> * :math:\epsilon_{tol} = 10^{-6} (for the sake of this question the selection of :math:\epsilon_{tol} was arbitrary)<br /> <br /> Calculating the function value at upper and lower bounds.<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} f\left(T_{U}^{(0)}\right) &amp;= - 24097 - 0.26\left(T_{U}^{(0)}\right) + 1.69\text{x}10^{-3}\left(T_{U}^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{U}^{(0)}\right)} - \left(-23505\right)\\ f\left(T_{U}^{(0)}\right) &amp;= - 24097 - 0.26\left(400\right) + 1.69\text{x}10^{-3}\left(400\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(400\right)} - \left(-23505\right)\\ f\left(T_{U}^{(0)}\right) &amp;= -50.6000\\ &amp; \\ f\left(T_{L}^{(0)}\right) &amp;= - 24097 - 0.26\left(T_{L}^{(0)}\right) + 1.69\text{x}10^{-3}\left(T_{L}^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{L}^{(0)}\right)} - \left(-23505\right)\\ f\left(T_{L}^{(0)}\right) &amp;= - 24097 - 0.26\left(200\right) + 1.69\text{x}10^{-3}\left(200\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(200\right)} - \left(-23505\right)\\ f\left(T_{L}^{(0)}\right) &amp;= 173.6000\\ &amp; \\ \Delta H_{r}^{0}\left(T_{U}^{(0)}\right) &amp;= - 24097 - 0.26\left(T_{U}^{(0)}\right) + 1.69\text{x}10^{-3}\left(T_{U}^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{U}^{(0)}\right)}\\ \Delta H_{r}^{0}\left(T_{U}^{(0)}\right) &amp;= - 24097 - 0.26\left(400\right) + 1.69\text{x}10^{-3}\left(400\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(400\right)}\\ \Delta H_{r}^{0}\left(T_{U}^{(0)}\right) &amp;= -23555.6\\ &amp; \\ \Delta H_{r}^{0}\left(T_{L}^{(0)}\right) &amp;= - 24097 - 0.26\left(T_{L}^{(0)}\right) + 1.69\text{x}10^{-3}\left(T_{L}^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{L}^{(0)}\right)}\\ \Delta H_{r}^{0}\left(T_{L}^{(0)}\right) &amp;= - 24097 - 0.26\left(200\right) + 1.69\text{x}10^{-3}\left(200\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(200\right)}\\ \Delta H_{r}^{0}\left(T_{L}^{(0)}\right) &amp;= -23331.4 \end{array}<br /> <br /> Testing if the initial bounds contain a root.<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} Check &amp;= f\left(T_{U}^{(0)}\right)\times f\left(T_{L}^{(0)}\right) \geq 0 ?\\ Check &amp;= \left(-50.6000\right)\times\left(173.6000\right) \geq 0 ?\\ Check &amp;= \left(-8784.16\right) \geq 0 ? \;\; \rightarrow \;\; \text{No, therefore a root is bounded} \end{array}<br /> <br /> *STEP 1: CALCULATE MID-POINT* :math:T_{M}^{(1)}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T_{M}^{(1)} &amp;= \frac{T_{U}^{(0)} + T_{L}^{(0)}}{2}\\ T_{M}^{(1)} &amp;= \frac{\left(400\right) + \left(200\right)}{2}\\ T_{M}^{(1)} &amp;= 300 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} f\left(T_{M}^{(1)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(1)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(1)}\right)^{2} + \frac{1.5\text{x}10^{5}}{T_{M}^{(1)}} - \left(-23505\right)\\ f\left(T_{M}^{(1)}\right) &amp;= - 24097 - 0.26\left(300\right) + 1.69\text{x}10^{-3}\left(300\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(300\right)} - \left(-23505\right)\\ f\left(T_{M}^{(1)}\right) &amp;= -17.9000\\ &amp; \\ \Delta H_{r}^{0}\left(T_{M}^{(1)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(1)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(1)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{M}^{(1)}\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(1)}\right) &amp;= - 24097 - 0.26\left(300\right) + 1.69\text{x}10^{-3}\left(300\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(300\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(1)}\right) &amp;= -23522.9 \end{array}<br /> <br /> *STEP 2: CALCULATE* :math:f(T_{L}^{(0)}) \times f(T_{M}^{(1)})<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} Check &amp;= f\left(T_{L}^{(0)}\right)\times f\left(T_{M}^{(1)}\right) &lt; 0 ?\\ Check &amp;= \left(173.6000\right)\times\left(-17.9000\right) &lt; 0 ?\\ Check &amp;= \left(-3107.44\right) &lt; 0 ? \;\; \rightarrow \;\; \text{Yes, therefore the root falls between }T_{L}^{(0)}\text{ and }T_{M}^{(1)}. \end{array}<br /> <br /> * :math:T_{U}^{(1)} = 300<br /> * :math:T_{L}^{(1)} = 200<br /> * :math:f(T_{U}^{(1)}) = -17.9000<br /> * :math:f(T_{L}^{(1)}) = 173.6000<br /> * :math:\Delta H_{r}^{0}(T_{U}^{(1)}) = -23522.9<br /> * :math:\Delta H_{r}^{0}(T_{L}^{(1)}) = -23331.4<br /> <br /> **ITERATION 2**<br /> <br /> *STEP 1: CALCULATE MID-POINT* :math:T_{M}^{(2)}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T_{M}^{(2)} &amp;= \frac{T_{U}^{(1)} + T_{L}^{(1)}}{2}\\ T_{M}^{(2)} &amp;= \frac{\left(300\right) + \left(200\right)}{2}\\ T_{M}^{(2)} &amp;= 250 \end{array}<br /> <br /> <br /> .. math::<br /> <br /> \begin{array}{rl} f\left(T_{M}^{(2)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(2)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(2)}\right)^{2} + \frac{1.5\text{x}10^{5}}{T_{M}^{(2)}} - \left(-23505\right)\\ f\left(T_{M}^{(2)}\right) &amp;= - 24097 - 0.26\left(250\right) + 1.69\text{x}10^{-3}\left(250\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(250\right)} - \left(-23505\right)\\ f\left(T_{M}^{(2)}\right) &amp;= 48.6250\\ &amp; \\ \Delta H_{r}^{0}\left(T_{M}^{(2)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(2)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(2)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{M}^{(2)}\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(2)}\right) &amp;= - 24097 - 0.26\left(250\right) + 1.69\text{x}10^{-3}\left(250\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(250\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(2)}\right) &amp;= -23456.375 \end{array}<br /> <br /> <br /> *STEP 2: CALCULATE* :math:f(T_{L}^{(1)}) \times f(T_{M}^{(2)})<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} Check &amp;= f\left(T_{L}^{(1)}\right)\times f\left(T_{M}^{(2)}\right) &lt; 0 ?\\ Check &amp;= \left(173.6000\right)\times\left(48.6250\right) &lt; 0 ?\\ Check &amp;= \left(8441.3\right) &lt; 0 ? \;\; \rightarrow \;\; \text{No, therefore the root falls between }T_{M}^{(2)}\text{ and }T_{U}^{(1)}. \end{array}<br /> <br /> * :math:T_{U}^{(2)} = 300<br /> * :math:T_{L}^{(2)} = 250<br /> * :math:f(T_{U}^{(2)}) = -17.9000<br /> * :math:f(T_{L}^{(2)}) = 48.6250<br /> * :math:\Delta H_{r}^{0}(T_{U}^{(2)}) = -23522.9<br /> * :math:\Delta H_{r}^{0}(T_{L}^{(2)}) = -23456.375<br /> <br /> *STEP 3: CHECK STOPPING CRITERION*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(2)} &amp;= \left| \frac{T_{M}^{(2)}-T_{M}^{(1)}}{T_{M}^{(1)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(2)} &amp;= \left| \frac{\left(250\right)-\left(300\right)}{\left(300\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(2)} &amp;= 0.1\bar{6} &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(2)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T_{M}^{(2)}\right)-\Delta H_{r}^{0}\left(T_{M}^{(1)}\right)}{\Delta H_{r}^{0}\left(T_{M}^{(1)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(2)} &amp;= \left| \frac{\left(-23456.375\right)-\left(-23522.9\right)}{\left(-23522.9\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(2)} &amp;= 0.0028 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> **ITERATION 3**<br /> <br /> *STEP 1: CALCULATE MID-POINT* :math:T_{M}^{(3)}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T_{M}^{(3)} &amp;= \frac{T_{U}^{(2)} + T_{L}^{(2)}}{2}\\ T_{M}^{(3)} &amp;= \frac{\left(300\right) + \left(250\right)}{2}\\ T_{M}^{(3)} &amp;= 275 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} f\left(T_{M}^{(3)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(3)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(3)}\right)^{2} + \frac{1.5\text{x}10^{5}}{T_{M}^{(3)}} - \left(-23505\right)\\ f\left(T_{M}^{(3)}\right) &amp;= - 24097 - 0.26\left(275\right) + 1.69\text{x}10^{-3}\left(275\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(275\right)} - \left(-23505\right)\\ f\left(T_{M}^{(3)}\right) &amp;= 9.7608\\ &amp; \\ \Delta H_{r}^{0}\left(T_{M}^{(3)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(3)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(3)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{M}^{(3)}\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(3)}\right) &amp;= - 24097 - 0.26\left(275\right) + 1.69\text{x}10^{-3}\left(275\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(275\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(3)}\right) &amp;= -23495.2392 \end{array}<br /> <br /> *STEP 2: CALCULATE* :math:f(T_{L}^{(2)})\times f(T_{M}^{(3)})<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} Check &amp;= f\left(T_{L}^{(2)}\right)\times f\left(T_{M}^{(3)}\right) &lt; 0 ?\\ Check &amp;= \left(48.6250\right)\times\left(9.7608\right) &lt; 0 ?\\ Check &amp;= \left(474.6189\right) &lt; 0 ? \;\; \rightarrow \;\; \text{No, therefore the root falls between }T_{M}^{(3)}\text{ and }T_{U}^{(2)}. \end{array}<br /> <br /> * :math:T_{U}^{(3)} = 300<br /> * :math:T_{L}^{(3)} = 275<br /> * :math:f(T_{U}^{(3)}) = -17.9000<br /> * :math:f(T_{L}^{(3)}) = 9.7608<br /> * :math:\Delta H_{r}^{0}(T_{U}^{(3)}) = -23522.9<br /> * :math:\Delta H_{r}^{0}(T_{L}^{(3)}) = -23495.2392<br /> <br /> *STEP 3: CHECK STOPPING CRITERION*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(3)} &amp;= \left| \frac{T_{M}^{(3)}-T_{M}^{(2)}}{T_{M}^{(2)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(3)} &amp;= \left| \frac{\left(275\right)-\left(250\right)}{\left(250\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(3)} &amp;= 0.1 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(3)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T_{M}^{(3)}\right)-\Delta H_{r}^{0}\left(T_{M}^{(2)}\right)}{\Delta H_{r}^{0}\left(T_{M}^{(2)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(3)} &amp;= \left| \frac{\left(-23495.2392\right)-\left(-23456.375\right)}{\left(-23456.375\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(3)} &amp;= 0.0017 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> **ITERATION 4**<br /> <br /> *STEP 1: CALCULATE MID-POINT* :math:T_{M}^{(4)}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T_{M}^{(4)} &amp;= \frac{T_{U}^{(3)} + T_{L}^{(3)}}{2}\\ T_{M}^{(4)} &amp;= \frac{\left(300\right) + \left(275\right)}{2}\\ T_{M}^{(4)} &amp;= 287.5 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} f\left(T_{M}^{(4)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(4)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(4)}\right)^{2} + \frac{1.5\text{x}10^{5}}{T_{M}^{(4)}} - \left(-23505\right)\\ f\left(T_{M}^{(4)}\right) &amp;= - 24097 - 0.26\left(287.5\right) + 1.69\text{x}10^{-3}\left(287.5\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(287.5\right)} - \left(-23505\right)\\ f\left(T_{M}^{(4)}\right) &amp;= -5.3218\\ &amp; \\ \Delta H_{r}^{0}\left(T_{M}^{(4)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(4)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(4)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{M}^{(4)}\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(4)}\right) &amp;= - 24097 - 0.26\left(287.5\right) + 1.69\text{x}10^{-3}\left(287.5\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(287.5\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(4)}\right) &amp;= -23510.3218 \end{array}<br /> <br /> *STEP 2: CALCULATE* :math:f(T_{L}^{(3)})\times f(T_{M}^{(4)})<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} Check &amp;= f\left(T_{L}^{(3)}\right)\times f\left(T_{M}^{(4)}\right) &lt; 0 ?\\ Check &amp;= \left(9.7608\right)\times\left(-5.3218\right) &lt; 0 ?\\ Check &amp;= \left(-51.945\right) &lt; 0 ? \;\; \rightarrow \;\; \text{Yes, therefore the root falls between }T_{L}^{(3)}\text{ and }T_{M}^{(4)}. \end{array}<br /> <br /> * :math:T_{U}^{(4)} = 287.5<br /> * :math:T_{L}^{(4)} = 275<br /> * :math:f(T_{U}^{(4)}) = -5.3218<br /> * :math:f(T_{L}^{(4)}) = 9.7608<br /> * :math:\Delta H_{r}^{0}(T_{U}^{(4)}) = -23510.3218<br /> * :math:\Delta H_{r}^{0}(T_{L}^{(4)}) = -23495.2392<br /> <br /> *STEP 3: CHECK STOPPING CRITERION*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(4)} &amp;= \left| \frac{T_{M}^{(4)}-T_{M}^{(3)}}{T_{M}^{(3)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(4)} &amp;= \left| \frac{\left(287.5\right)-\left(275\right)}{\left(275\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(4)} &amp;= 0.0\bar{45} &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(4)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T_{M}^{(4)}\right)-\Delta H_{r}^{0}\left(T_{M}^{(3)}\right)}{\Delta H_{r}^{0}\left(T_{M}^{(3)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(4)} &amp;= \left| \frac{\left(-23510.3218\right)-\left(-23495.2392\right)}{\left(-23495.2392\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(4)} &amp;= 0.00064 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Tutorial_5_-_2010_-_Solution/Question_1&diff=1085 Tutorial 5 - 2010 - Solution/Question 1 2018-09-16T08:11:35Z <p>Kevindunn: </p> <hr /> <div>{{Navigation|Book=Tutorial 5 - 2010 - Solution|previous=|current=Back to all questions|next=Question 2}}<br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> <br /> Question 1 <br /> ===============<br /> <br /> Show the first 4 iterations of the bisection method to solve for :math:T, justifying your choice for the initial bracket. <br /> <br /> For iteration 2, 3, and 4, please also report these two relative errors:<br /> <br /> .. math::<br /> <br /> \varepsilon_\text{rel,x}^{(k)} = \displaystyle \left|\frac{x_m^{(k)} - x_m^{(k-1)}}{x_m^{(k)}} \right| \qquad\qquad \varepsilon_\text{rel,f}^{(k)} = \displaystyle \left|\frac{f(x_m^{(k)}) - f(x_m^{(k-1)})}{f(x_m^{(k)})} \right|<br /> <br /> Solution<br /> --------<br /> <br /> Since we are attempting to solve for the temperature associated with :math:\Delta H_{r}^{0}(T) = -23505 cal/mol we are essentially asked to solve for the zero of the following function:<br /> <br /> .. math::<br /> <br /> f(T) = - 24097 - 0.26T + 1.69\text{x}10^{-3}T^{2} + \frac{1.5\text{x}10^{5}}{T} - (-23505)<br /> <br /> where<br /> <br /> .. math::<br /> <br /> \Delta H_{r}^{0}(T) = - 24097 - 0.26T + 1.69\text{x}10^{-3}T^{2} + \frac{1.5\text{x}10^{5}}{T}<br /> <br /> To apply the bisection method we first need to select a starting interval that the root is known to fall within. More specifically, we need to also be sure that the root is a &quot;true root&quot; in the sense that it crosses the y-axis (and therefore the function changes sign on either side of the root). **The bisection method is unable to locate roots that only touch the x-axis but do not cross it, e.g. a double root**. Given that this is a physically perceivable system of one variable we can select the lower and upper bounds graphically or based on prior engineering knowledge.<br /> <br /> If we choose to attack the problem based on our previous knowledge then selecting the lower bound is easy since we are dealing with the Kelvin temperature scale. Therefore we know the lowest allowable temperature is 0 K. Selecting the upper bound based on prior knowledge is slightly trickier. If we knew what reaction this equation is describing then we might have some basic knowledge of how the heat of reaction changes with T. In this case we would be able to guess a suitable upper temperature. Other than this we are essentially stuck trying to overguess to ensure we have a root contained in our bounds. This technique, of course, carries with it the critical issue that if the function contains multiple roots (as it will be shown this one does) an over guess could easily contain more than one root and so would either confuse the solver or cause the solver to think that no roots exist (if the function is a quadratic shape, for example, then it is possible that the function could have the same sign on either end of a bounding interval despite containing both roots). For this reason I would recommend the graphical approach.<br /> <br /> Plotting the function in MATLAB/Python is accomplished using the following scripts.<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Tutorials/Tutorial-5/code/Q1_Plot.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Tutorials/Tutorial-5/code/Q1_Plot.py<br /> :language2: python<br /> :header2: Python code <br /> <br /> The result of this script is shown below. From the plot below it is clear that this function has two roots. One between the bounds (200,400) and one between the bounds (400,600).<br /> <br /> .. figure:: ../che3e4/Tutorials/Tutorial-5/images/Q1_Figure.png<br /> :scale: 70<br /> :align: center<br /> <br /> As a final note before getting into the bisection solution, it is believed that some confusion has likely arisen over the function error tolerance equation. In this case f(x) refers to the original :math:\Delta H_{r}^{0}(T) function and not the vertically shifted function you are trying to find the roots of (i.e. :math:\Delta H_{r}^{0}(T) + 23505). Since in your adjusted function you are trying to solve for when the function is 0, it does not make sense to calculate a relative comparison to 0 (the numbers will explode and contain essentially no useful information). This is actually an error that can crop up when a root also happens to be zero, but naturally this is less easy to predict. So to quickly wrap up, when root finding, your best bets for stopping criteria are either the relative error on x from step to step (unless the root is zero), the value of the root finding function (this would be the most versatile, as you are no longer dividing by any value and you know your function will be going to zero), the relative error of the original function from step to step (unless this also is equal to zero at the root...).<br /> <br /> For the bisection test we will consider the lower of the two bounds. Utilizing the procedure given on slide 8 of the *Section C: Nonlinear Algebraic Equations* slide set.<br /> <br /> **ITERATION 1**<br /> <br /> *STEP 0: INITIALIZATION*<br /> <br /> * :math:T_{U}^{(0)} = 400<br /> * :math:T_{L}^{(0)} = 200<br /> * :math:\epsilon_{tol} = 10^{-6} (for the sake of this question the selection of :math:\epsilon_{tol} was arbitrary)<br /> <br /> Calculating the function value at upper and lower bounds.<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} f\left(T_{U}^{(0)}\right) &amp;= - 24097 - 0.26\left(T_{U}^{(0)}\right) + 1.69\text{x}10^{-3}\left(T_{U}^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{U}^{(0)}\right)} - \left(-23505\right)\\ f\left(T_{U}^{(0)}\right) &amp;= - 24097 - 0.26\left(400\right) + 1.69\text{x}10^{-3}\left(400\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(400\right)} - \left(-23505\right)\\ f\left(T_{U}^{(0)}\right) &amp;= -50.6000\\ &amp; \\ f\left(T_{L}^{(0)}\right) &amp;= - 24097 - 0.26\left(T_{L}^{(0)}\right) + 1.69\text{x}10^{-3}\left(T_{L}^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{L}^{(0)}\right)} - \left(-23505\right)\\ f\left(T_{L}^{(0)}\right) &amp;= - 24097 - 0.26\left(200\right) + 1.69\text{x}10^{-3}\left(200\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(200\right)} - \left(-23505\right)\\ f\left(T_{L}^{(0)}\right) &amp;= 173.6000\\ &amp; \\ \Delta H_{r}^{0}\left(T_{U}^{(0)}\right) &amp;= - 24097 - 0.26\left(T_{U}^{(0)}\right) + 1.69\text{x}10^{-3}\left(T_{U}^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{U}^{(0)}\right)}\\ \Delta H_{r}^{0}\left(T_{U}^{(0)}\right) &amp;= - 24097 - 0.26\left(400\right) + 1.69\text{x}10^{-3}\left(400\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(400\right)}\\ \Delta H_{r}^{0}\left(T_{U}^{(0)}\right) &amp;= -23555.6\\ &amp; \\ \Delta H_{r}^{0}\left(T_{L}^{(0)}\right) &amp;= - 24097 - 0.26\left(T_{L}^{(0)}\right) + 1.69\text{x}10^{-3}\left(T_{L}^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{L}^{(0)}\right)}\\ \Delta H_{r}^{0}\left(T_{L}^{(0)}\right) &amp;= - 24097 - 0.26\left(200\right) + 1.69\text{x}10^{-3}\left(200\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(200\right)}\\ \Delta H_{r}^{0}\left(T_{L}^{(0)}\right) &amp;= -23331.4<br /> \end{array}<br /> <br /> Testing if the initial bounds contain a root.<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} Check &amp;= f\left(T_{U}^{(0)}\right)\times f\left(T_{L}^{(0)}\right) \geq 0 ?\\ Check &amp;= \left(-50.6000\right)\times\left(173.6000\right) \geq 0 ?\\ Check &amp;= \left(-8784.16\right) \geq 0 ? \;\; \rightarrow \;\; \text{No, therefore a root is bounded} \end{array}<br /> <br /> *STEP 1: CALCULATE MID-POINT* :math:T_{M}^{(1)}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T_{M}^{(1)} &amp;= \frac{T_{U}^{(0)} + T_{L}^{(0)}}{2}\\ T_{M}^{(1)} &amp;= \frac{\left(400\right) + \left(200\right)}{2}\\ T_{M}^{(1)} &amp;= 300 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} f\left(T_{M}^{(1)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(1)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(1)}\right)^{2} + \frac{1.5\text{x}10^{5}}{T_{M}^{(1)}} - \left(-23505\right)\\ f\left(T_{M}^{(1)}\right) &amp;= - 24097 - 0.26\left(300\right) + 1.69\text{x}10^{-3}\left(300\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(300\right)} - \left(-23505\right)\\ f\left(T_{M}^{(1)}\right) &amp;= -17.9000\\ &amp; \\ \Delta H_{r}^{0}\left(T_{M}^{(1)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(1)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(1)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{M}^{(1)}\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(1)}\right) &amp;= - 24097 - 0.26\left(300\right) + 1.69\text{x}10^{-3}\left(300\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(300\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(1)}\right) &amp;= -23522.9 \end{array}<br /> <br /> *STEP 2: CALCULATE* :math:f(T_{L}^{(0)}) \times f(T_{M}^{(1)})<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} Check &amp;= f\left(T_{L}^{(0)}\right)\times f\left(T_{M}^{(1)}\right) &lt; 0 ?\\ Check &amp;= \left(173.6000\right)\times\left(-17.9000\right) &lt; 0 ?\\ Check &amp;= \left(-3107.44\right) &lt; 0 ? \;\; \rightarrow \;\; \text{Yes, therefore the root falls between }T_{L}^{(0)}\text{ and }T_{M}^{(1)}. \end{array}<br /> <br /> * :math:T_{U}^{(1)} = 300<br /> * :math:T_{L}^{(1)} = 200<br /> * :math:f(T_{U}^{(1)}) = -17.9000<br /> * :math:f(T_{L}^{(1)}) = 173.6000<br /> * :math:\Delta H_{r}^{0}(T_{U}^{(1)}) = -23522.9<br /> * :math:\Delta H_{r}^{0}(T_{L}^{(1)}) = -23331.4<br /> <br /> **ITERATION 2**<br /> <br /> *STEP 1: CALCULATE MID-POINT* :math:T_{M}^{(2)}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T_{M}^{(2)} &amp;= \frac{T_{U}^{(1)} + T_{L}^{(1)}}{2}\\ T_{M}^{(2)} &amp;= \frac{\left(300\right) + \left(200\right)}{2}\\ T_{M}^{(2)} &amp;= 250 \end{array}<br /> <br /> <br /> .. math::<br /> <br /> \begin{array}{rl} f\left(T_{M}^{(2)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(2)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(2)}\right)^{2} + \frac{1.5\text{x}10^{5}}{T_{M}^{(2)}} - \left(-23505\right)\\ f\left(T_{M}^{(2)}\right) &amp;= - 24097 - 0.26\left(250\right) + 1.69\text{x}10^{-3}\left(250\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(250\right)} - \left(-23505\right)\\ f\left(T_{M}^{(2)}\right) &amp;= 48.6250\\ &amp; \\ \Delta H_{r}^{0}\left(T_{M}^{(2)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(2)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(2)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{M}^{(2)}\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(2)}\right) &amp;= - 24097 - 0.26\left(250\right) + 1.69\text{x}10^{-3}\left(250\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(250\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(2)}\right) &amp;= -23456.375 \end{array}<br /> <br /> <br /> *STEP 2: CALCULATE* :math:f(T_{L}^{(1)}) \times f(T_{M}^{(2)})<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} Check &amp;= f\left(T_{L}^{(1)}\right)\times f\left(T_{M}^{(2)}\right) &lt; 0 ?\\ Check &amp;= \left(173.6000\right)\times\left(48.6250\right) &lt; 0 ?\\ Check &amp;= \left(8441.3\right) &lt; 0 ? \;\; \rightarrow \;\; \text{No, therefore the root falls between }T_{M}^{(2)}\text{ and }T_{U}^{(1)}. \end{array}<br /> <br /> * :math:T_{U}^{(2)} = 300<br /> * :math:T_{L}^{(2)} = 250<br /> * :math:f(T_{U}^{(2)}) = -17.9000<br /> * :math:f(T_{L}^{(2)}) = 48.6250<br /> * :math:\Delta H_{r}^{0}(T_{U}^{(2)}) = -23522.9<br /> * :math:\Delta H_{r}^{0}(T_{L}^{(2)}) = -23456.375<br /> <br /> *STEP 3: CHECK STOPPING CRITERION*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(2)} &amp;= \left| \frac{T_{M}^{(2)}-T_{M}^{(1)}}{T_{M}^{(1)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(2)} &amp;= \left| \frac{\left(250\right)-\left(300\right)}{\left(300\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(2)} &amp;= 0.1\bar{6} &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(2)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T_{M}^{(2)}\right)-\Delta H_{r}^{0}\left(T_{M}^{(1)}\right)}{\Delta H_{r}^{0}\left(T_{M}^{(1)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(2)} &amp;= \left| \frac{\left(-23456.375\right)-\left(-23522.9\right)}{\left(-23522.9\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(2)} &amp;= 0.0028 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> **ITERATION 3**<br /> <br /> *STEP 1: CALCULATE MID-POINT* :math:T_{M}^{(3)}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T_{M}^{(3)} &amp;= \frac{T_{U}^{(2)} + T_{L}^{(2)}}{2}\\ T_{M}^{(3)} &amp;= \frac{\left(300\right) + \left(250\right)}{2}\\ T_{M}^{(3)} &amp;= 275 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} f\left(T_{M}^{(3)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(3)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(3)}\right)^{2} + \frac{1.5\text{x}10^{5}}{T_{M}^{(3)}} - \left(-23505\right)\\ f\left(T_{M}^{(3)}\right) &amp;= - 24097 - 0.26\left(275\right) + 1.69\text{x}10^{-3}\left(275\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(275\right)} - \left(-23505\right)\\ f\left(T_{M}^{(3)}\right) &amp;= 9.7608\\ &amp; \\ \Delta H_{r}^{0}\left(T_{M}^{(3)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(3)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(3)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{M}^{(3)}\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(3)}\right) &amp;= - 24097 - 0.26\left(275\right) + 1.69\text{x}10^{-3}\left(275\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(275\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(3)}\right) &amp;= -23495.2392 \end{array}<br /> <br /> *STEP 2: CALCULATE* :math:f(T_{L}^{(2)})\times f(T_{M}^{(3)})<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} Check &amp;= f\left(T_{L}^{(2)}\right)\times f\left(T_{M}^{(3)}\right) &lt; 0 ?\\ Check &amp;= \left(48.6250\right)\times\left(9.7608\right) &lt; 0 ?\\ Check &amp;= \left(474.6189\right) &lt; 0 ? \;\; \rightarrow \;\; \text{No, therefore the root falls between }T_{M}^{(3)}\text{ and }T_{U}^{(2)}. \end{array}<br /> <br /> * :math:T_{U}^{(3)} = 300<br /> * :math:T_{L}^{(3)} = 275<br /> * :math:f(T_{U}^{(3)}) = -17.9000<br /> * :math:f(T_{L}^{(3)}) = 9.7608<br /> * :math:\Delta H_{r}^{0}(T_{U}^{(3)}) = -23522.9<br /> * :math:\Delta H_{r}^{0}(T_{L}^{(3)}) = -23495.2392<br /> <br /> *STEP 3: CHECK STOPPING CRITERION*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(3)} &amp;= \left| \frac{T_{M}^{(3)}-T_{M}^{(2)}}{T_{M}^{(2)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(3)} &amp;= \left| \frac{\left(275\right)-\left(250\right)}{\left(250\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(3)} &amp;= 0.1 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(3)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T_{M}^{(3)}\right)-\Delta H_{r}^{0}\left(T_{M}^{(2)}\right)}{\Delta H_{r}^{0}\left(T_{M}^{(2)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(3)} &amp;= \left| \frac{\left(-23495.2392\right)-\left(-23456.375\right)}{\left(-23456.375\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(3)} &amp;= 0.0017 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> **ITERATION 4**<br /> <br /> *STEP 1: CALCULATE MID-POINT* :math:T_{M}^{(4)}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T_{M}^{(4)} &amp;= \frac{T_{U}^{(3)} + T_{L}^{(3)}}{2}\\ T_{M}^{(4)} &amp;= \frac{\left(300\right) + \left(275\right)}{2}\\ T_{M}^{(4)} &amp;= 287.5 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} f\left(T_{M}^{(4)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(4)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(4)}\right)^{2} + \frac{1.5\text{x}10^{5}}{T_{M}^{(4)}} - \left(-23505\right)\\ f\left(T_{M}^{(4)}\right) &amp;= - 24097 - 0.26\left(287.5\right) + 1.69\text{x}10^{-3}\left(287.5\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(287.5\right)} - \left(-23505\right)\\ f\left(T_{M}^{(4)}\right) &amp;= -5.3218\\ &amp; \\ \Delta H_{r}^{0}\left(T_{M}^{(4)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(4)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(4)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{M}^{(4)}\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(4)}\right) &amp;= - 24097 - 0.26\left(287.5\right) + 1.69\text{x}10^{-3}\left(287.5\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(287.5\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(4)}\right) &amp;= -23510.3218 \end{array}<br /> <br /> *STEP 2: CALCULATE* :math:f(T_{L}^{(3)})\times f(T_{M}^{(4)})<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} Check &amp;= f\left(T_{L}^{(3)}\right)\times f\left(T_{M}^{(4)}\right) &lt; 0 ?\\ Check &amp;= \left(9.7608\right)\times\left(-5.3218\right) &lt; 0 ?\\ Check &amp;= \left(-51.945\right) &lt; 0 ? \;\; \rightarrow \;\; \text{Yes, therefore the root falls between }T_{L}^{(3)}\text{ and }T_{M}^{(4)}. \end{array}<br /> <br /> * :math:T_{U}^{(4)} = 287.5<br /> * :math:T_{L}^{(4)} = 275<br /> * :math:f(T_{U}^{(4)}) = -5.3218<br /> * :math:f(T_{L}^{(4)}) = 9.7608<br /> * :math:\Delta H_{r}^{0}(T_{U}^{(4)}) = -23510.3218<br /> * :math:\Delta H_{r}^{0}(T_{L}^{(4)}) = -23495.2392<br /> <br /> *STEP 3: CHECK STOPPING CRITERION*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(4)} &amp;= \left| \frac{T_{M}^{(4)}-T_{M}^{(3)}}{T_{M}^{(3)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(4)} &amp;= \left| \frac{\left(287.5\right)-\left(275\right)}{\left(275\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(4)} &amp;= 0.0\bar{45} &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(4)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T_{M}^{(4)}\right)-\Delta H_{r}^{0}\left(T_{M}^{(3)}\right)}{\Delta H_{r}^{0}\left(T_{M}^{(3)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(4)} &amp;= \left| \frac{\left(-23510.3218\right)-\left(-23495.2392\right)}{\left(-23495.2392\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(4)} &amp;= 0.00064 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Tutorial_5_-_2010_-_Solution/Question_2&diff=1084 Tutorial 5 - 2010 - Solution/Question 2 2018-09-16T08:09:44Z <p>Kevindunn: </p> <hr /> <div>{{Navigation|Book=Tutorial 5 - 2010 - Solution|previous=Question 1|current=Back to all questions|next=Question 3}}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> Question 2 <br /> ===============<br /> <br /> #. Derive a :math:g(x) = x function to use in the fixed-point algorithm.<br /> #. Show the first 3 iterations of using the fixed-point algorithm, starting with an initial guess of :math:T = 380 K.<br /> #. Will the fixed-point method converge for this problem, using your :math:g(x)?<br /> <br /> <br /> Solution<br /> --------<br /> <br /> #. The goal of fixed-point iterative methods is to locate &quot;fixed-point(s)&quot; associated with a given function. A fixed-point is special case of a function where in it maps a value :math:x to itself (what this means in layman's terms is if you put a value :math:x into a function you get :math:x out, i.e. :math:x = g(x)). Knowing that our objective is to find the root(s) of a known objective function (we could have just had a black box function...) we may then construct :math:g(x) in way that gives it the greatest chance of converging. Let us start by looking at our objective function:<br /> <br /> .. math::<br /> <br /> f(T) = 0 = - 24097 - 0.26T + 1.69\text{x}10^{-3}T^{2} + \frac{1.5\text{x}10^{5}}{T} - (-23505)<br /> <br /> Likely the first choice of g(x) that popped to everyone's mind was<br /> <br /> .. math::<br /> <br /> T = \frac{- 24097 + 1.69\text{x}10^{-3}T^{2} + \frac{1.5\text{x}10^{5}}{T} - (-23505)}{0.26} = g(T)_{1}<br /> <br /> Given that we have a function :math:f(T) = 0, another good choice would be one of :math:g(T) = T + f(T) or :math:g(T) = T - f(T). Let's say we were trying to identify the lower of the two roots. In this case we would want :math:f(T) to yield a negative value when :math:T &gt; T_{root} and a positive value when :math:T &lt; T_{root}. Looking at the plot generated in Question 1 we see that the function is positive when :math:T is smaller than the lower root and greater than the upper root and is negative when :math:T is between the two roots. Therefore if we wished to locate the lower root :math:g(T) = T - f(T) would locally satisfy the desired properties and if we wished to find the upper root :math:g(T) = T + f(T) would locally satisfy the desired properties (Note that these are gross generalizations and do not prove stability or ensure convergence. They are merely educated guesses as to potentially good functions). Therefore we will also investigate both:<br /> <br /> .. math::<br /> <br /> g(T)_{2} = T + (- 24097 - 0.26T + 1.69\text{x}10^{-3}T^{2} + \frac{1.5\text{x}10^{5}}{T} - (-23505))<br /> <br /> .. math::<br /> <br /> g(T)_{3} = T - (- 24097 - 0.26T + 1.69\text{x}10^{-3}T^{2} + \frac{1.5\text{x}10^{5}}{T} - (-23505))<br /> <br /> <br /> #. Starting with a value of :math:T^{(0)} = 380 K:<br /> <br /> **FUNCTION** :math:g(T)_{1}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \Delta H_{r}^{0}\left(T^{(0)}\right) &amp;= - 24097 - 0.26\left(T^{(0)}\right) + 1.69\text{x}10^{-3}\left(T^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(0)}\right)}\\ \Delta H_{r}^{0}\left(T^{(0)}\right) &amp;= - 24097 - 0.26\left(380\right) + 1.69\text{x}10^{-3}\left(380\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(380\right)}\\ \Delta H_{r}^{0}\left(T^{(0)}\right) &amp;= -23557.02715 \end{array}<br /> <br /> *ITERATION 1*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T^{(1)} &amp;= \frac{- 24097 + 1.69\text{x}10^{-3}(T^{(0)})^{2} + \frac{1.5\text{x}10^{5}}{T^{(0)}} - (-23505)}{0.26}\\ T^{(1)} &amp;= \frac{- 24097 + 1.69\text{x}10^{-3}(380)^{2} + \frac{1.5\text{x}10^{5}}{(380)} - (-23505)}{0.26}\\ T^{(1)} &amp;= 179.8955\\ &amp; \\ \Delta H_{r}^{0}\left(T^{(1)}\right) &amp;= - 24097 - 0.26\left(T^{(1)}\right) + 1.69\text{x}10^{-3}\left(T^{(1)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(1)}\right)}\\ \Delta H_{r}^{0}\left(T^{(1)}\right) &amp;= - 24097 - 0.26\left(179.8955\right) + 1.69\text{x}10^{-3}\left(179.8955\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(179.8955\right)}\\ \Delta H_{r}^{0}\left(T^{(1)}\right) &amp;= -23255 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(1)} &amp;= \left| \frac{T^{(1)}-T^{(0)}}{T^{(0)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(1)} &amp;= \left| \frac{\left(179.8955\right)-\left(380\right)}{\left(380\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(1)} &amp;= 0.5266 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(1)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T^{(1)}\right)-\Delta H_{r}^{0}\left(T^{(0)}\right)}{\Delta H_{r}^{0}\left(T^{(0)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(1)} &amp;= \left| \frac{\left(-23255\right)-\left(-23557.02715\right)}{\left(-23557.02715\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(1)} &amp;= 0.0128 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> *ITERATION 2*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T^{(2)} &amp;= \frac{- 24097 + 1.69\text{x}10^{-3}(T^{(1)})^{2} + \frac{1.5\text{x}10^{5}}{T^{(1)}} - (-23505)}{0.26}\\ T^{(2)} &amp;= \frac{- 24097 + 1.69\text{x}10^{-3}(179.8955)^{2} + \frac{1.5\text{x}10^{5}}{(179.8955)} - (-23505)}{0.26}\\ T^{(2)} &amp;= 1140.4\\ &amp; \\ \Delta H_{r}^{0}\left(T^{(2)}\right) &amp;= - 24097 - 0.26\left(T^{(2)}\right) + 1.69\text{x}10^{-3}\left(T^{(2)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(2)}\right)}\\ \Delta H_{r}^{0}\left(T^{(2)}\right) &amp;= - 24097 - 0.26\left(1140.4\right) + 1.69\text{x}10^{-3}\left(1140.4\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(1140.4\right)}\\ \Delta H_{r}^{0}\left(T^{(2)}\right) &amp;= -22064 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(2)} &amp;= \left| \frac{T^{(2)}-T^{(1)}}{T^{(1)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(2)} &amp;= \left| \frac{\left(1140.4\right)-\left(179.8955\right)}{\left(179.8955\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(2)} &amp;= 5.3394 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(2)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T^{(2)}\right)-\Delta H_{r}^{0}\left(T^{(1)}\right)}{\Delta H_{r}^{0}\left(T^{(1)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(2)} &amp;= \left| \frac{\left(-22064\right)-\left(-23255\right)}{\left(-23255\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(2)} &amp;= 0.0512 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> *ITERATION 3*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T^{(3)} &amp;= \frac{- 24097 + 1.69\text{x}10^{-3}(T^{(2)})^{2} + \frac{1.5\text{x}10^{5}}{T^{(2)}} - (-23505)}{0.26}\\ T^{(3)} &amp;= \frac{- 24097 + 1.69\text{x}10^{-3}(1140.4)^{2} + \frac{1.5\text{x}10^{5}}{(1140.4)} - (-23505)}{0.26}\\ T^{(2)} &amp;= 6682.6\\ &amp; \\ \Delta H_{r}^{0}\left(T^{(3)}\right) &amp;= - 24097 - 0.26\left(T^{(3)}\right) + 1.69\text{x}10^{-3}\left(T^{(3)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(3)}\right)}\\ \Delta H_{r}^{0}\left(T^{(2)}\right) &amp;= - 24097 - 0.26\left(6682.6\right) + 1.69\text{x}10^{-3}\left(6682.6\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(6682.6\right)}\\ \Delta H_{r}^{0}\left(T^{(3)}\right) &amp;= 49659 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(3)} &amp;= \left| \frac{T^{(3)}-T^{(1)}}{T^{(2)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(3)} &amp;= \left| \frac{\left(6682.6\right)-\left(1140.4\right)}{\left(1140.4\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(3)} &amp;= 4.8598 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(3)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T^{(3)}\right)-\Delta H_{r}^{0}\left(T^{(2)}\right)}{\Delta H_{r}^{0}\left(T^{(2)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(3)} &amp;= \left| \frac{\left(49659\right)-\left(-22064\right)}{\left(-22064\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(3)} &amp;= 3.2507 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> <br /> **FUNCTION** :math:g(T)_{2}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \Delta H_{r}^{0}\left(T^{(0)}\right) &amp;= - 24097 - 0.26\left(T^{(0)}\right) + 1.69\text{x}10^{-3}\left(T^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(0)}\right)}\\ \Delta H_{r}^{0}\left(T^{(0)}\right) &amp;= - 24097 - 0.26\left(380\right) + 1.69\text{x}10^{-3}\left(380\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(380\right)}\\ \Delta H_{r}^{0}\left(T^{(0)}\right) &amp;= -23557.02715 \end{array}<br /> <br /> *ITERATION 1*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T^{(1)} &amp;= T^{(0)} - 24097 - 0.26T^{(0)} + 1.69\text{x}10^{-3}(T^{(0)})^{2} + \frac{1.5\text{x}10^{5}}{T^{(0)}} - (-23505)\\ T^{(1)} &amp;= (380) - 24097 - 0.26(380) + 1.69\text{x}10^{-3}(380)^{2} + \frac{1.5\text{x}10^{5}}{(380)} - (-23505)\\ T^{(1)} &amp;= 283.2531\\ &amp; \\ \Delta H_{r}^{0}\left(T^{(1)}\right) &amp;= - 24097 - 0.26\left(T^{(1)}\right) + 1.69\text{x}10^{-3}\left(T^{(1)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(1)}\right)}\\ \Delta H_{r}^{0}\left(T^{(1)}\right) &amp;= - 24097 - 0.26\left(283.2531\right) + 1.69\text{x}10^{-3}\left(283.2531\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(283.2531\right)}\\ \Delta H_{r}^{0}\left(T^{(1)}\right) &amp;= -23505.49146 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(1)} &amp;= \left| \frac{T^{(1)}-T^{(0)}}{T^{(0)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(1)} &amp;= \left| \frac{\left(283.2531\right)-\left(380\right)}{\left(380\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(1)} &amp;= 0.080 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(1)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T^{(1)}\right)-\Delta H_{r}^{0}\left(T^{(0)}\right)}{\Delta H_{r}^{0}\left(T^{(0)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(1)} &amp;= \left| \frac{\left(-23505.49146\right)-\left(-23557.02715\right)}{\left(-23557.02715\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(1)} &amp;= 0.0022 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> *ITERATION 2*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T^{(2)} &amp;= T^{(1)} - 24097 - 0.26T^{(1)} + 1.69\text{x}10^{-3}(T^{(1)})^{2} + \frac{1.5\text{x}10^{5}}{T^{(1)}} - (-23505)\\ T^{(2)} &amp;= (283.2531) - 24097 - 0.26(283.2531) + 1.69\text{x}10^{-3}(283.2531)^{2} + \frac{1.5\text{x}10^{5}}{(283.2531)} - (-23505)\\ T^{(2)} &amp;= 282.7616\\ &amp; \\ \Delta H_{r}^{0}\left(T^{(2)}\right) &amp;= - 24097 - 0.26\left(T^{(2)}\right) + 1.69\text{x}10^{-3}\left(T^{(2)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(2)}\right)}\\ \Delta H_{r}^{0}\left(T^{(2)}\right) &amp;= - 24097 - 0.26\left(282.7616\right) + 1.69\text{x}10^{-3}\left(282.7616\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(282.7616\right)}\\ \Delta H_{r}^{0}\left(T^{(2)}\right) &amp;= -23504.913333 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(2)} &amp;= \left| \frac{T^{(2)}-T^{(1)}}{T^{(1)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(2)} &amp;= \left| \frac{\left(282.7616\right)-\left(283.2531\right)}{\left(283.2531\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(2)} &amp;= 0.0017 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(2)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T^{(2)}\right)-\Delta H_{r}^{0}\left(T^{(1)}\right)}{\Delta H_{r}^{0}\left(T^{(1)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(2)} &amp;= \left| \frac{\left(-23504.913333\right)-\left(-23505.49146\right)}{\left(-23505.49146\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(2)} &amp;= 0.000026 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> *ITERATION 3*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T^{(3)} &amp;= T^{(2)} - 24097 - 0.26T^{(2)} + 1.69\text{x}10^{-3}(T^{(2)})^{2} + \frac{1.5\text{x}10^{5}}{T^{(2)}} - (-23505)\\ T^{(3)} &amp;= (282.7616) - 24097 - 0.26(282.7616) + 1.69\text{x}10^{-3}(282.7616)^{2} + \frac{1.5\text{x}10^{5}}{(282.7616)} - (-23505)\\ T^{(2)} &amp;= 282.8483\\ &amp; \\ \Delta H_{r}^{0}\left(T^{(3)}\right) &amp;= - 24097 - 0.26\left(T^{(3)}\right) + 1.69\text{x}10^{-3}\left(T^{(3)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(3)}\right)}\\ \Delta H_{r}^{0}\left(T^{(2)}\right) &amp;= - 24097 - 0.26\left(282.8483\right) + 1.69\text{x}10^{-3}\left(282.8483\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(282.8483\right)}\\ \Delta H_{r}^{0}\left(T^{(3)}\right) &amp;= -23505.0156 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(3)} &amp;= \left| \frac{T^{(3)}-T^{(1)}}{T^{(2)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(3)} &amp;= \left| \frac{\left(282.8483\right)-\left(282.7616\right)}{\left(282.7616\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(3)} &amp;= 0.00031 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(3)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T^{(3)}\right)-\Delta H_{r}^{0}\left(T^{(2)}\right)}{\Delta H_{r}^{0}\left(T^{(2)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(3)} &amp;= \left| \frac{\left(-23505.0156\right)-\left(-23504.913333\right)}{\left(-23504.913333\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(3)} &amp;= 0.0000044 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> <br /> **FUNCTION** :math:g(T)_{3}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \Delta H_{r}^{0}\left(T^{(0)}\right) &amp;= - 24097 - 0.26\left(T^{(0)}\right) + 1.69\text{x}10^{-3}\left(T^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(0)}\right)}\\ \Delta H_{r}^{0}\left(T^{(0)}\right) &amp;= - 24097 - 0.26\left(380\right) + 1.69\text{x}10^{-3}\left(380\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(380\right)}\\ \Delta H_{r}^{0}\left(T^{(0)}\right) &amp;= -23557.02715 \end{array}<br /> <br /> *ITERATION 1*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T^{(1)} &amp;= T^{(0)} - \left(- 24097 - 0.26T^{(0)} + 1.69\text{x}10^{-3}(T^{(0)})^{2} + \frac{1.5\text{x}10^{5}}{T^{(0)}} - (-23505) \right)\\ T^{(1)} &amp;= (380) - \left(- 24097 - 0.26(380) + 1.69\text{x}10^{-3}(380)^{2} + \frac{1.5\text{x}10^{5}}{(380)} - (-23505) \right)\\ T^{(1)} &amp;= 432.0272\\ &amp; \\ \Delta H_{r}^{0}\left(T^{(1)}\right) &amp;= - 24097 - 0.26\left(T^{(1)}\right) + 1.69\text{x}10^{-3}\left(T^{(1)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(1)}\right)}\\ \Delta H_{r}^{0}\left(T^{(1)}\right) &amp;= - 24097 - 0.26\left(432.0272\right) + 1.69\text{x}10^{-3}\left(432.0272\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(432.0272\right)}\\ \Delta H_{r}^{0}\left(T^{(1)}\right) &amp;= -23547 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(1)} &amp;= \left| \frac{T^{(1)}-T^{(0)}}{T^{(0)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(1)} &amp;= \left| \frac{\left(432.0272\right)-\left(380\right)}{\left(380\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(1)} &amp;= 0.1369 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(1)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T^{(1)}\right)-\Delta H_{r}^{0}\left(T^{(0)}\right)}{\Delta H_{r}^{0}\left(T^{(0)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(1)} &amp;= \left| \frac{\left(-23547\right)-\left(-23557.02715\right)}{\left(-23557.02715\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(1)} &amp;= 0.00044 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> *ITERATION 2*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T^{(2)} &amp;= T^{(1)} - \left(- 24097 - 0.26T^{(1)} + 1.69\text{x}10^{-3}(T^{(1)})^{2} + \frac{1.5\text{x}10^{5}}{T^{(1)}} - (-23505) \right)\\ T^{(2)} &amp;= (432.0272) - \left(- 24097 - 0.26(432.0272) + 1.69\text{x}10^{-3}(432.0272)^{2} + \frac{1.5\text{x}10^{5}}{(432.0272)} - (-23505) \right)\\ T^{(2)} &amp;= 473.7196\\ &amp; \\ \Delta H_{r}^{0}\left(T^{(2)}\right) &amp;= - 24097 - 0.26\left(T^{(2)}\right) + 1.69\text{x}10^{-3}\left(T^{(2)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(2)}\right)}\\ \Delta H_{r}^{0}\left(T^{(2)}\right) &amp;= - 24097 - 0.26\left(473.7196\right) + 1.69\text{x}10^{-3}\left(473.7196\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(473.7196\right)}\\ \Delta H_{r}^{0}\left(T^{(2)}\right) &amp;= -23524 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(2)} &amp;= \left| \frac{T^{(2)}-T^{(1)}}{T^{(1)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(2)} &amp;= \left| \frac{\left(473.7196\right)-\left(432.0272\right)}{\left(432.0272\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(2)} &amp;= 0.0965 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(2)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T^{(2)}\right)-\Delta H_{r}^{0}\left(T^{(1)}\right)}{\Delta H_{r}^{0}\left(T^{(1)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(2)} &amp;= \left| \frac{\left(-23524\right)-\left(-23547\right)}{\left(-23547\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(2)} &amp;= 0.00095 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> *ITERATION 3*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T^{(3)} &amp;= T^{(2)} - \left(- 24097 - 0.26T^{(2)} + 1.69\text{x}10^{-3}(T^{(2)})^{2} + \frac{1.5\text{x}10^{5}}{T^{(2)}} - (-23505) \right)\\ T^{(3)} &amp;= (473.7196) - 24097 - 0.26(473.7196) + 1.69\text{x}10^{-3}(473.7196)^{2} + \frac{1.5\text{x}10^{5}}{(473.7196)} - (-23505)\\ T^{(2)} &amp;= 492.9904\\ &amp; \\ \Delta H_{r}^{0}\left(T^{(3)}\right) &amp;= - 24097 - 0.26\left(T^{(3)}\right) + 1.69\text{x}10^{-3}\left(T^{(3)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(3)}\right)}\\ \Delta H_{r}^{0}\left(T^{(2)}\right) &amp;= - 24097 - 0.26\left(492.9904\right) + 1.69\text{x}10^{-3}\left(492.9904\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(492.9904\right)}\\ \Delta H_{r}^{0}\left(T^{(3)}\right) &amp;= -23510 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(3)} &amp;= \left| \frac{T^{(3)}-T^{(1)}}{T^{(2)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(3)} &amp;= \left| \frac{\left(492.9904\right)-\left(473.7196\right)}{\left(473.7196\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(3)} &amp;= 0.0407 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(3)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T^{(3)}\right)-\Delta H_{r}^{0}\left(T^{(2)}\right)}{\Delta H_{r}^{0}\left(T^{(2)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(3)} &amp;= \left| \frac{\left(-23510\right)-\left(-23524\right)}{\left(-23524\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(3)} &amp;= 0.00060 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> <br /> #. It would appear that :math:g(T)_{1} is rapidly diverging while :math:g(T)_{2} is converging to the lower root and :math:g(T)_{3} is converging to the upper root.<br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Tutorial_5_-_2010_-_Solution/Question_3&diff=1083 Tutorial 5 - 2010 - Solution/Question 3 2018-09-16T08:06:20Z <p>Kevindunn: </p> <hr /> <div>{{Navigation|Book=Tutorial 5 - 2010 - Solution|previous=Question 2|current=Back to all questions|next=Question 4}}<br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> <br /> Question 3 <br /> ==============<br /> <br /> #. Write the Newton-Raphson iteration formula that you would use to solve this nonlinear equation.<br /> #. Apply 3 iterations of this formula, also starting from :math:T = 380 K, and calculate the error tolerances.<br /> <br /> Solution<br /> --------<br /> <br /> #. The Newton-Raphson algorithm is given on slide 15-17 of the *Section C: Nonlinear Algebraic Equations* slide set.<br /> <br /> To apply the Newton-Raphson method we must first calculate the derivative of our function:<br /> <br /> .. math::<br /> <br /> f'(T) = - 0.26 + 3.38\text{x}10^{-3}T - \frac{1.5\text{x}10^{5}}{T^{2}}<br /> <br /> Therefore the Newton-Raphson iteration formula we use is the following:<br /> <br /> .. math::<br /> <br /> T^{(k+1)} = T^{(k)} - \frac{f(T^{(k)})}{f'(T^{(k)})} = T^{(k)} - \frac{- 24097 - 0.26T + 1.69\text{x}10^{-3}T^{2} + \frac{1.5\text{x}10^{5}}{T} - (-23505)}{- 0.26 + 3.38\text{x}10^{-3}T - \frac{1.5\text{x}10^{5}}{T^{2}}}<br /> <br /> #.<br /> We start with :math:T^{(0)} = 380 K:<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \Delta H_{r}^{0}\left(T^{(0)}\right) &amp;= - 24097 - 0.26\left(T^{(0)}\right) + 1.69\text{x}10^{-3}\left(T^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(0)}\right)}\\ \Delta H_{r}^{0}\left(T^{(0)}\right) &amp;= - 24097 - 0.26\left(380\right) + 1.69\text{x}10^{-3}\left(380\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(380\right)}\\ \Delta H_{r}^{0}\left(T^{(0)}\right) &amp;= -23557.02715 \end{array}<br /> <br /> *ITERATION 1*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T^{(1)} &amp;= T^{(0)} - \frac{- 24097 - 0.26T^{(0)} + 1.69\text{x}10^{-3}(T^{(0)})^{2} + \frac{1.5\text{x}10^{5}}{T^{(0)}} - (-23505)}{- 0.26 + 3.38\text{x}10^{-3}T^{(0)} - \frac{1.5\text{x}10^{5}}{(T^{(0)})^{2}}}\\ T^{(1)} &amp;= (380) - \frac{- 24097 - 0.26(380) + 1.69\text{x}10^{-3}(380)^{2} + \frac{1.5\text{x}10^{5}}{(380)} - (-23505)}{- 0.26 + 3.38\text{x}10^{-3}(380) - \frac{1.5\text{x}10^{5}}{(380)^{2}}}\\ T^{(1)} &amp;= -3237.72940\\ &amp; \\ \Delta H_{r}^{0}\left(T^{(1)}\right) &amp;= - 24097 - 0.26\left(T^{(1)}\right) + 1.69\text{x}10^{-3}\left(T^{(1)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(1)}\right)}\\ \Delta H_{r}^{0}\left(T^{(1)}\right) &amp;= - 24097 - 0.26\left(-3237.72940\right) + 1.69\text{x}10^{-3}\left(-3237.72940\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(-3237.72940\right)}\\ \Delta H_{r}^{0}\left(T^{(1)}\right) &amp;= -5585.4322 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(1)} &amp;= \left| \frac{T^{(1)}-T^{(0)}}{T^{(0)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(1)} &amp;= \left| \frac{\left(-3237.72940\right)-\left(380\right)}{\left(380\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(1)} &amp;= 9.52 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(1)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T^{(1)}\right)-\Delta H_{r}^{0}\left(T^{(0)}\right)}{\Delta H_{r}^{0}\left(T^{(0)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(1)} &amp;= \left| \frac{\left(-5585.4322\right)-\left(-23557.02715\right)}{\left(-23557.02715\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(1)} &amp;= 0.763 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> *ITERATION 2*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T^{(2)} &amp;= T^{(1)} - \frac{- 24097 - 0.26T^{(1)} + 1.69\text{x}10^{-3}(T^{(1)})^{2} + \frac{1.5\text{x}10^{5}}{T^{(1)}} - (-23505)}{- 0.26 + 3.38\text{x}10^{-3}T^{(1)} - \frac{1.5\text{x}10^{5}}{(T^{(1)})^{2}}}\\ T^{(2)} &amp;= (-3237.72940) - \frac{- 24097 - 0.26(-3237.72940) + 1.69\text{x}10^{-3}(-3237.72940)^{2} + \frac{1.5\text{x}10^{5}}{(-3237.72940)} - (-23505)}{- 0.26 + 3.38\text{x}10^{-3}(-3237.72940) - \frac{1.5\text{x}10^{5}}{(-3237.72940)^{2}}}\\ T^{(2)} &amp;= -1640.311\\ &amp; \\ \Delta H_{r}^{0}\left(T^{(2)}\right) &amp;= - 24097 - 0.26\left(T^{(2)}\right) + 1.69\text{x}10^{-3}\left(T^{(2)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(2)}\right)}\\ \Delta H_{r}^{0}\left(T^{(2)}\right) &amp;= - 24097 - 0.26\left(-1640.311\right) + 1.69\text{x}10^{-3}\left(-1640.311\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(-1640.311\right)}\\ \Delta H_{r}^{0}\left(T^{(2)}\right) &amp;= -19214.81711 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(2)} &amp;= \left| \frac{T^{(2)}-T^{(1)}}{T^{(1)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(2)} &amp;= \left| \frac{\left(-1640.311\right)-\left(-3237.72940\right)}{\left(-3237.72940\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(2)} &amp;= 0.493 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(2)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T^{(2)}\right)-\Delta H_{r}^{0}\left(T^{(1)}\right)}{\Delta H_{r}^{0}\left(T^{(1)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(2)} &amp;= \left| \frac{\left(-19214.81711\right)-\left(-5585.4322\right)}{\left(-5585.4322\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(1)} &amp;= 2.44 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> *ITERATION 3*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T^{(3)} &amp;= T^{(2)} - \frac{- 24097 - 0.26T^{(2)} + 1.69\text{x}10^{-3}(T^{(2)})^{2} + \frac{1.5\text{x}10^{5}}{T^{(2)}} - (-23505)}{- 0.26 + 3.38\text{x}10^{-3}T^{(2)} - \frac{1.5\text{x}10^{5}}{(T^{(2)})^{2}}}\\ T^{(3)} &amp;= (-1640.311) - \frac{- 24097 - 0.26(-1640.311) + 1.69\text{x}10^{-3}(-1640.311)^{2} + \frac{1.5\text{x}10^{5}}{(-1640.311)} - (-23505)}{- 0.26 + 3.38\text{x}10^{-3}(-1640.311) - \frac{1.5\text{x}10^{5}}{(-1640.311)^{2}}}\\ T^{(3)} &amp;= -908.1982\\ &amp; \\ \Delta H_{r}^{0}\left(T^{(3)}\right) &amp;= - 24097 - 0.26\left(T^{(3)}\right) + 1.69\text{x}10^{-3}\left(T^{(3)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T^{(3)}\right)}\\ \Delta H_{r}^{0}\left(T^{(3)}\right) &amp;= - 24097 - 0.26\left(-908.1982\right) + 1.69\text{x}10^{-3}\left(-908.1982\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(-908.1982\right)}\\ \Delta H_{r}^{0}\left(T^{(3)}\right) &amp;= -22632.0781 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} \epsilon_{tol,x}^{(3)} &amp;= \left| \frac{T^{(3)}-T^{(2)}}{T^{(2)}} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,x}^{(3)} &amp;= \left| \frac{\left(-908.1982\right)-\left(-1640.311\right)}{\left(-1640.311\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,x}^{(3)} &amp;= 0.45 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\ &amp; \\ \epsilon_{tol,f}^{(3)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T^{(3)}\right)-\Delta H_{r}^{0}\left(T^{(2)}\right)}{\Delta H_{r}^{0}\left(T^{(2)}\right)} \right| &lt; \epsilon_{tol} ? \\ \epsilon_{tol,f}^{(3)} &amp;= \left| \frac{\left(-22632.0781\right)-\left(-19214.81711\right)}{\left(-19214.81711\right)} \right| &lt; \left(10^{-6}\right) ? \\ \epsilon_{tol,f}^{(3)} &amp;= 0.178 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \end{array}<br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Tutorial_5_-_2010_-_Solution/Question_1&diff=1082 Tutorial 5 - 2010 - Solution/Question 1 2018-09-15T10:37:36Z <p>Kevindunn: </p> <hr /> <div>{{Navigation|Book=Tutorial 5 - 2010 - Solution|previous=|current=Back to all questions|next=Question 2}}<br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> <br /> Question 1 <br /> ===============<br /> <br /> Show the first 4 iterations of the bisection method to solve for :math:T, justifying your choice for the initial bracket. <br /> <br /> For iteration 2, 3, and 4, please also report these two relative errors:<br /> <br /> .. math::<br /> <br /> \varepsilon_\text{rel,x}^{(k)} = \displaystyle \left|\frac{x_m^{(k)} - x_m^{(k-1)}}{x_m^{(k)}} \right| \qquad\qquad \varepsilon_\text{rel,f}^{(k)} = \displaystyle \left|\frac{f(x_m^{(k)}) - f(x_m^{(k-1)})}{f(x_m^{(k)})} \right|<br /> <br /> Solution<br /> --------<br /> <br /> Since we are attempting to solve for the temperature associated with :math:\Delta H_{r}^{0}(T) = -23505 cal/mol we are essentially asked to solve for the zero of the following function:<br /> <br /> .. math::<br /> <br /> f(T) = - 24097 - 0.26T + 1.69\text{x}10^{-3}T^{2} + \frac{1.5\text{x}10^{5}}{T} - (-23505)<br /> <br /> where<br /> <br /> .. math::<br /> <br /> \Delta H_{r}^{0}(T) = - 24097 - 0.26T + 1.69\text{x}10^{-3}T^{2} + \frac{1.5\text{x}10^{5}}{T}<br /> <br /> To apply the bisection method we first need to select a starting interval that the root is known to fall within. More specifically, we need to also be sure that the root is a &quot;true root&quot; in the sense that it crosses the y-axis (and therefore the function changes sign on either side of the root). **The bisection method is unable to locate roots that only touch the x-axis but do not cross it, e.g. a double root**. Given that this is a physically perceivable system of one variable we can select the lower and upper bounds graphically or based on prior engineering knowledge.<br /> <br /> If we choose to attack the problem based on our previous knowledge then selecting the lower bound is easy since we are dealing with the Kelvin temperature scale. Therefore we know the lowest allowable temperature is 0 K. Selecting the upper bound based on prior knowledge is slightly trickier. If we knew what reaction this equation is describing then we might have some basic knowledge of how the heat of reaction changes with T. In this case we would be able to guess a suitable upper temperature. Other than this we are essentially stuck trying to overguess to ensure we have a root contained in our bounds. This technique, of course, carries with it the critical issue that if the function contains multiple roots (as it will be shown this one does) an over guess could easily contain more than one root and so would either confuse the solver or cause the solver to think that no roots exist (if the function is a quadratic shape, for example, then it is possible that the function could have the same sign on either end of a bounding interval despite containing both roots). For this reason I would recommend the graphical approach.<br /> <br /> Plotting the function in MATLAB/Python is accomplished using the following scripts.<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Tutorials/Tutorial-5/code/Q1_Plot.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Tutorials/Tutorial-5/code/Q1_Plot.py<br /> :language2: python<br /> :header2: Python code <br /> <br /> The result of this script is shown below. From the plot below it is clear that this function has two roots. One between the bounds (200,400) and one between the bounds (400,600).<br /> <br /> .. figure:: ../che3e4/Tutorials/Tutorial-5/images/Q1_Figure.png<br /> :scale: 70<br /> :align: center<br /> <br /> As a final note before getting into the bisection solution, it is believed that some confusion has likely arisen over the function error tolerance equation. In this case f(x) refers to the original :math:\Delta H_{r}^{0}(T) function and not the vertically shifted function you are trying to find the roots of (i.e. :math:\Delta H_{r}^{0}(T) + 23505). Since in your adjusted function you are trying to solve for when the function is 0, it does not make sense to calculate a relative comparison to 0 (the numbers will explode and contain essentially no useful information). This is actually an error that can crop up when a root also happens to be zero, but naturally this is less easy to predict. So to quickly wrap up, when root finding, your best bets for stopping criteria are either the relative error on x from step to step (unless the root is zero), the value of the root finding function (this would be the most versatile, as you are no longer dividing by any value and you know your function will be going to zero), the relative error of the original function from step to step (unless this also is equal to zero at the root...).<br /> <br /> For the bisection test we will consider the lower of the two bounds. Utilizing the procedure given on slide 8 of the *Section C: Nonlinear Algebraic Equations* slide set.<br /> <br /> **ITERATION 1**<br /> <br /> *STEP 0: INITIALIZATION*<br /> <br /> * :math:T_{U}^{(0)} = 400<br /> * :math:T_{L}^{(0)} = 200<br /> * :math:\epsilon_{tol} = 10^{-6} (for the sake of this question the selection of :math:\epsilon_{tol} was arbitrary)<br /> <br /> Calculating the function value at upper and lower bounds.<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} f\left(T_{U}^{(0)}\right) &amp;= - 24097 - 0.26\left(T_{U}^{(0)}\right) + 1.69\text{x}10^{-3}\left(T_{U}^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{U}^{(0)}\right)} - \left(-23505\right)\\ f\left(T_{U}^{(0)}\right) &amp;= - 24097 - 0.26\left(400\right) + 1.69\text{x}10^{-3}\left(400\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(400\right)} - \left(-23505\right)\\ f\left(T_{U}^{(0)}\right) &amp;= -50.6000\\ &amp; \\ f\left(T_{L}^{(0)}\right) &amp;= - 24097 - 0.26\left(T_{L}^{(0)}\right) + 1.69\text{x}10^{-3}\left(T_{L}^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{L}^{(0)}\right)} - \left(-23505\right)\\ f\left(T_{L}^{(0)}\right) &amp;= - 24097 - 0.26\left(200\right) + 1.69\text{x}10^{-3}\left(200\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(200\right)} - \left(-23505\right)\\ f\left(T_{L}^{(0)}\right) &amp;= 173.6000\\ &amp; \\ \Delta H_{r}^{0}\left(T_{U}^{(0)}\right) &amp;= - 24097 - 0.26\left(T_{U}^{(0)}\right) + 1.69\text{x}10^{-3}\left(T_{U}^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{U}^{(0)}\right)}\\ \Delta H_{r}^{0}\left(T_{U}^{(0)}\right) &amp;= - 24097 - 0.26\left(400\right) + 1.69\text{x}10^{-3}\left(400\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(400\right)}\\ \Delta H_{r}^{0}\left(T_{U}^{(0)}\right) &amp;= -23555.6\\ &amp; \\ \Delta H_{r}^{0}\left(T_{L}^{(0)}\right) &amp;= - 24097 - 0.26\left(T_{L}^{(0)}\right) + 1.69\text{x}10^{-3}\left(T_{L}^{(0)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{L}^{(0)}\right)}\\ \Delta H_{r}^{0}\left(T_{L}^{(0)}\right) &amp;= - 24097 - 0.26\left(200\right) + 1.69\text{x}10^{-3}\left(200\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(200\right)}\\ \Delta H_{r}^{0}\left(T_{L}^{(0)}\right) &amp;= -23331.4<br /> \end{array}<br /> <br /> Testing if the initial bounds contain a root.<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} Check &amp;= f\left(T_{U}^{(0)}\right)\times f\left(T_{L}^{(0)}\right) \geq 0 ?\\ Check &amp;= \left(-50.6000\right)\times\left(173.6000\right) \geq 0 ?\\ Check &amp;= \left(-8784.16\right) \geq 0 ? \;\; \rightarrow \;\; \text{No, therefore a root is bounded} \end{array}<br /> <br /> *STEP 1: CALCULATE MID-POINT* :math:T_{M}^{(1)}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} T_{M}^{(1)} &amp;= \frac{T_{U}^{(0)} + T_{L}^{(0)}}{2}\\ T_{M}^{(1)} &amp;= \frac{\left(400\right) + \left(200\right)}{2}\\ T_{M}^{(1)} &amp;= 300 \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl} f\left(T_{M}^{(1)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(1)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(1)}\right)^{2} + \frac{1.5\text{x}10^{5}}{T_{M}^{(1)}} - \left(-23505\right)\\ f\left(T_{M}^{(1)}\right) &amp;= - 24097 - 0.26\left(300\right) + 1.69\text{x}10^{-3}\left(300\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(300\right)} - \left(-23505\right)\\ f\left(T_{M}^{(1)}\right) &amp;= -17.9000\\ &amp; \\ \Delta H_{r}^{0}\left(T_{M}^{(1)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(1)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(1)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{M}^{(1)}\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(1)}\right) &amp;= - 24097 - 0.26\left(300\right) + 1.69\text{x}10^{-3}\left(300\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(300\right)}\\ \Delta H_{r}^{0}\left(T_{M}^{(1)}\right) &amp;= -23522.9 \end{array}<br /> <br /> *STEP 2: CALCULATE* :math:f(T_{L}^{(0)}) \times f(T_{M}^{(1)})<br /> <br /> .. math::<br /> <br /> \begin{array}{rl}<br /> Check &amp;= f\left(T_{L}^{(0)}\right)\times f\left(T_{M}^{(1)}\right) &lt; 0 ?\\<br /> Check &amp;= \left(173.6000\right)\times\left(-17.9000\right) &lt; 0 ?\\<br /> Check &amp;= \left(-3107.44\right) &lt; 0 ? \;\; \rightarrow \;\; \text{Yes, therefore the root falls between }T_{L}^{(0)}\text{ and }T_{M}^{(1)}.<br /> \end{array}<br /> <br /> * :math:T_{U}^{(1)} = 300<br /> * :math:T_{L}^{(1)} = 200<br /> * :math:f(T_{U}^{(1)}) = -17.9000<br /> * :math:f(T_{L}^{(1)}) = 173.6000<br /> * :math:\Delta H_{r}^{0}(T_{U}^{(1)}) = -23522.9<br /> * :math:\Delta H_{r}^{0}(T_{L}^{(1)}) = -23331.4<br /> <br /> **ITERATION 2**<br /> <br /> *STEP 1: CALCULATE MID-POINT* :math:T_{M}^{(2)}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl}<br /> T_{M}^{(2)} &amp;= \frac{T_{U}^{(1)} + T_{L}^{(1)}}{2}\\<br /> T_{M}^{(2)} &amp;= \frac{\left(300\right) + \left(200\right)}{2}\\<br /> T_{M}^{(2)} &amp;= 250<br /> \end{array}<br /> <br /> <br /> .. math::<br /> <br /> \begin{array}{rl}<br /> f\left(T_{M}^{(2)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(2)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(2)}\right)^{2} + \frac{1.5\text{x}10^{5}}{T_{M}^{(2)}} - \left(-23505\right)\\<br /> f\left(T_{M}^{(2)}\right) &amp;= - 24097 - 0.26\left(250\right) + 1.69\text{x}10^{-3}\left(250\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(250\right)} - \left(-23505\right)\\<br /> f\left(T_{M}^{(2)}\right) &amp;= 48.6250\\<br /> &amp; \\<br /> \Delta H_{r}^{0}\left(T_{M}^{(2)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(2)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(2)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{M}^{(2)}\right)}\\<br /> \Delta H_{r}^{0}\left(T_{M}^{(2)}\right) &amp;= - 24097 - 0.26\left(250\right) + 1.69\text{x}10^{-3}\left(250\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(250\right)}\\<br /> \Delta H_{r}^{0}\left(T_{M}^{(2)}\right) &amp;= -23456.375<br /> \end{array}<br /> <br /> <br /> *STEP 2: CALCULATE* :math:f(T_{L}^{(1)}) \times f(T_{M}^{(2)})<br /> <br /> .. math::<br /> <br /> \begin{array}{rl}<br /> Check &amp;= f\left(T_{L}^{(1)}\right)\times f\left(T_{M}^{(2)}\right) &lt; 0 ?\\<br /> Check &amp;= \left(173.6000\right)\times\left(48.6250\right) &lt; 0 ?\\<br /> Check &amp;= \left(8441.3\right) &lt; 0 ? \;\; \rightarrow \;\; \text{No, therefore the root falls between }T_{M}^{(2)}\text{ and }T_{U}^{(1)}.<br /> \end{array}<br /> <br /> * :math:T_{U}^{(2)} = 300<br /> * :math:T_{L}^{(2)} = 250<br /> * :math:f(T_{U}^{(2)}) = -17.9000<br /> * :math:f(T_{L}^{(2)}) = 48.6250<br /> * :math:\Delta H_{r}^{0}(T_{U}^{(2)}) = -23522.9<br /> * :math:\Delta H_{r}^{0}(T_{L}^{(2)}) = -23456.375<br /> <br /> *STEP 3: CHECK STOPPING CRITERION*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl}<br /> \epsilon_{tol,x}^{(2)} &amp;= \left| \frac{T_{M}^{(2)}-T_{M}^{(1)}}{T_{M}^{(1)}} \right| &lt; \epsilon_{tol} ? \\<br /> \epsilon_{tol,x}^{(2)} &amp;= \left| \frac{\left(250\right)-\left(300\right)}{\left(300\right)} \right| &lt; \left(10^{-6}\right) ? \\<br /> \epsilon_{tol,x}^{(2)} &amp;= 0.1\bar{6} &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\<br /> &amp; \\<br /> \epsilon_{tol,f}^{(2)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T_{M}^{(2)}\right)-\Delta H_{r}^{0}\left(T_{M}^{(1)}\right)}{\Delta H_{r}^{0}\left(T_{M}^{(1)}\right)} \right| &lt; \epsilon_{tol} ? \\<br /> \epsilon_{tol,f}^{(2)} &amp;= \left| \frac{\left(-23456.375\right)-\left(-23522.9\right)}{\left(-23522.9\right)} \right| &lt; \left(10^{-6}\right) ? \\<br /> \epsilon_{tol,f}^{(2)} &amp;= 0.0028 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}.<br /> \end{array}<br /> <br /> **ITERATION 3**<br /> <br /> *STEP 1: CALCULATE MID-POINT* :math:T_{M}^{(3)}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl}<br /> T_{M}^{(3)} &amp;= \frac{T_{U}^{(2)} + T_{L}^{(2)}}{2}\\<br /> T_{M}^{(3)} &amp;= \frac{\left(300\right) + \left(250\right)}{2}\\<br /> T_{M}^{(3)} &amp;= 275<br /> \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl}<br /> f\left(T_{M}^{(3)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(3)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(3)}\right)^{2} + \frac{1.5\text{x}10^{5}}{T_{M}^{(3)}} - \left(-23505\right)\\<br /> f\left(T_{M}^{(3)}\right) &amp;= - 24097 - 0.26\left(275\right) + 1.69\text{x}10^{-3}\left(275\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(275\right)} - \left(-23505\right)\\<br /> f\left(T_{M}^{(3)}\right) &amp;= 9.7608\\<br /> &amp; \\<br /> \Delta H_{r}^{0}\left(T_{M}^{(3)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(3)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(3)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{M}^{(3)}\right)}\\<br /> \Delta H_{r}^{0}\left(T_{M}^{(3)}\right) &amp;= - 24097 - 0.26\left(275\right) + 1.69\text{x}10^{-3}\left(275\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(275\right)}\\<br /> \Delta H_{r}^{0}\left(T_{M}^{(3)}\right) &amp;= -23495.2392<br /> \end{array}<br /> <br /> *STEP 2: CALCULATE* :math:f(T_{L}^{(2)})\times f(T_{M}^{(3)})<br /> <br /> .. math::<br /> <br /> \begin{array}{rl}<br /> Check &amp;= f\left(T_{L}^{(2)}\right)\times f\left(T_{M}^{(3)}\right) &lt; 0 ?\\<br /> Check &amp;= \left(48.6250\right)\times\left(9.7608\right) &lt; 0 ?\\<br /> Check &amp;= \left(474.6189\right) &lt; 0 ? \;\; \rightarrow \;\; \text{No, therefore the root falls between }T_{M}^{(3)}\text{ and }T_{U}^{(2)}.<br /> \end{array}<br /> <br /> * :math:T_{U}^{(3)} = 300<br /> * :math:T_{L}^{(3)} = 275<br /> * :math:f(T_{U}^{(3)}) = -17.9000<br /> * :math:f(T_{L}^{(3)}) = 9.7608<br /> * :math:\Delta H_{r}^{0}(T_{U}^{(3)}) = -23522.9<br /> * :math:\Delta H_{r}^{0}(T_{L}^{(3)}) = -23495.2392<br /> <br /> *STEP 3: CHECK STOPPING CRITERION*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl}<br /> \epsilon_{tol,x}^{(3)} &amp;= \left| \frac{T_{M}^{(3)}-T_{M}^{(2)}}{T_{M}^{(2)}} \right| &lt; \epsilon_{tol} ? \\<br /> \epsilon_{tol,x}^{(3)} &amp;= \left| \frac{\left(275\right)-\left(250\right)}{\left(250\right)} \right| &lt; \left(10^{-6}\right) ? \\<br /> \epsilon_{tol,x}^{(3)} &amp;= 0.1 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\<br /> &amp; \\<br /> \epsilon_{tol,f}^{(3)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T_{M}^{(3)}\right)-\Delta H_{r}^{0}\left(T_{M}^{(2)}\right)}{\Delta H_{r}^{0}\left(T_{M}^{(2)}\right)} \right| &lt; \epsilon_{tol} ? \\<br /> \epsilon_{tol,f}^{(3)} &amp;= \left| \frac{\left(-23495.2392\right)-\left(-23456.375\right)}{\left(-23456.375\right)} \right| &lt; \left(10^{-6}\right) ? \\<br /> \epsilon_{tol,f}^{(3)} &amp;= 0.0017 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}.<br /> \end{array}<br /> <br /> **ITERATION 4**<br /> <br /> *STEP 1: CALCULATE MID-POINT* :math:T_{M}^{(4)}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl}<br /> T_{M}^{(4)} &amp;= \frac{T_{U}^{(3)} + T_{L}^{(3)}}{2}\\<br /> T_{M}^{(4)} &amp;= \frac{\left(300\right) + \left(275\right)}{2}\\<br /> T_{M}^{(4)} &amp;= 287.5<br /> \end{array}<br /> <br /> .. math::<br /> <br /> \begin{array}{rl}<br /> f\left(T_{M}^{(4)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(4)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(4)}\right)^{2} + \frac{1.5\text{x}10^{5}}{T_{M}^{(4)}} - \left(-23505\right)\\<br /> f\left(T_{M}^{(4)}\right) &amp;= - 24097 - 0.26\left(287.5\right) + 1.69\text{x}10^{-3}\left(287.5\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(287.5\right)} - \left(-23505\right)\\<br /> f\left(T_{M}^{(4)}\right) &amp;= -5.3218\\<br /> &amp; \\<br /> \Delta H_{r}^{0}\left(T_{M}^{(4)}\right) &amp;= - 24097 - 0.26\left(T_{M}^{(4)}\right) + 1.69\text{x}10^{-3}\left(T_{M}^{(4)}\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(T_{M}^{(4)}\right)}\\<br /> \Delta H_{r}^{0}\left(T_{M}^{(4)}\right) &amp;= - 24097 - 0.26\left(287.5\right) + 1.69\text{x}10^{-3}\left(287.5\right)^{2} + \frac{1.5\text{x}10^{5}}{\left(287.5\right)}\\<br /> \Delta H_{r}^{0}\left(T_{M}^{(4)}\right) &amp;= -23510.3218<br /> \end{array}<br /> <br /> *STEP 2: CALCULATE* :math:f(T_{L}^{(3)})\times f(T_{M}^{(4)})<br /> <br /> .. math::<br /> <br /> \begin{array}{rl}<br /> Check &amp;= f\left(T_{L}^{(3)}\right)\times f\left(T_{M}^{(4)}\right) &lt; 0 ?\\<br /> Check &amp;= \left(9.7608\right)\times\left(-5.3218\right) &lt; 0 ?\\<br /> Check &amp;= \left(-51.945\right) &lt; 0 ? \;\; \rightarrow \;\; \text{Yes, therefore the root falls between }T_{L}^{(3)}\text{ and }T_{M}^{(4)}.<br /> \end{array}<br /> <br /> * :math:T_{U}^{(4)} = 287.5<br /> * :math:T_{L}^{(4)} = 275<br /> * :math:f(T_{U}^{(4)}) = -5.3218<br /> * :math:f(T_{L}^{(4)}) = 9.7608<br /> * :math:\Delta H_{r}^{0}(T_{U}^{(4)}) = -23510.3218<br /> * :math:\Delta H_{r}^{0}(T_{L}^{(4)}) = -23495.2392<br /> <br /> *STEP 3: CHECK STOPPING CRITERION*<br /> <br /> .. math::<br /> <br /> \begin{array}{rl}<br /> \epsilon_{tol,x}^{(4)} &amp;= \left| \frac{T_{M}^{(4)}-T_{M}^{(3)}}{T_{M}^{(3)}} \right| &lt; \epsilon_{tol} ? \\<br /> \epsilon_{tol,x}^{(4)} &amp;= \left| \frac{\left(287.5\right)-\left(275\right)}{\left(275\right)} \right| &lt; \left(10^{-6}\right) ? \\<br /> \epsilon_{tol,x}^{(4)} &amp;= 0.0\bar{45} &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}. \\<br /> &amp; \\<br /> \epsilon_{tol,f}^{(4)} &amp;= \left| \frac{\Delta H_{r}^{0}\left(T_{M}^{(4)}\right)-\Delta H_{r}^{0}\left(T_{M}^{(3)}\right)}{\Delta H_{r}^{0}\left(T_{M}^{(3)}\right)} \right| &lt; \epsilon_{tol} ? \\<br /> \epsilon_{tol,f}^{(4)} &amp;= \left| \frac{\left(-23510.3218\right)-\left(-23495.2392\right)}{\left(-23495.2392\right)} \right| &lt; \left(10^{-6}\right) ? \\<br /> \epsilon_{tol,f}^{(4)} &amp;= 0.00064 &lt; \left(10^{-6}\right) ? \;\; \rightarrow \;\; \text{No, therefore keep going}.<br /> \end{array}<br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Tutorial_4_-_2010_-_Solution&diff=1081 Tutorial 4 - 2010 - Solution 2018-09-15T10:35:42Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 7 October 2010<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = Tutorial-4-2010.pdf<br /> | questions_text_alt = Tutorial questions<br /> | solutions_PDF = Tutorial-4-2010-Solution.pdf<br /> | solutions_text_alt = Solutions by Ali Sahlodin<br /> | other_instructions = Hand-in at class.<br /> }}<br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> <br /> <br /> .. |m3| replace:: m\ :sup:3<br /> .. highlight:: python<br /> <br /> .. rubric:: Tutorial objectives<br /> <br /> * Derive linear models for actual systems.<br /> * Solve these models by hand, and with computer software.<br /> <br /> .. rubric:: Solutions prepared by Ali Sahlodin.<br /> <br /> Question 1 <br /> ===============<br /> <br /> In the second tutorial you derived the set of linear equations for the reaction:<br /> <br /> .. math::<br /> {\sf P_2I_4} + n\,{\sf P_4} + p\,{\sf H_2O} \longrightarrow 4\,{\sf PH_4I} + q\,{\sf H_3PO_4} <br /> <br /> where :math:n, :math:p and :math:q denote the stoichiometric coefficients for the :math:\sf P_4, :math:\sf H_2O and :math:\sf H_3PO_4 species respectively.<br /> <br /> #. Solve the linear equations using Gauss elimination, by hand.<br /> #. Verify your solution using the mldivide (MATLAB) or solve (Python) function. How long does it take to find the solution? Contrast this with the approach used in tutorial 2.<br /> <br /> Solution<br /> -----------<br /> <br /> Recall from the second tutorial that we have the following balance equations;<br /> <br /> balance for :math:{\sf O}: p- 4q=0,<br /> <br /> balance for :math:{\sf P}: 4n - q=2,<br /> <br /> balance for :math:{\sf H}: 2p-3q = 16.<br /> <br /> <br /> These equations can be written in the form of :math:Ax=b where :math:A is the matrix of coefficients, and :math:b is a column vector containing the right-hand side constants. Also :math:x=[n\quad p\quad q]^T is the vector of unknowns. So, we have:<br /> <br /> .. math::<br /> \left[ \begin{array}{ccc} 0 &amp; 1 &amp; -4 \\ 4 &amp; 0 &amp; -1 \\ 0 &amp; 2 &amp; -3 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 0 \\ 2 \\ 16 \end{array} \right]<br /> <br /> Now we solve this system using the Gauss elimination (:math:R_i denotes the :math:i-th row of the matrix). Note that your solution procedure (not the final results) may be different if you have arranged the equations and unknowns differently so you have a different :math:A and :math:b than the above.<br /> <br /> **Forward Elimination**<br /> <br /> As a first step, :math:A_{2,1} must be eliminated using :math:A_{1,1} in the first row. However, :math:A_{1,1}=0 by which we cannot divide. So, we do a partial pivoting by switching the first and second rows in the above matrix. That is:<br /> <br /> .. math::<br /> \left[ \begin{array}{ccc} 4 &amp; 0 &amp; -1 \\ 0 &amp; 1 &amp; -4 \\ 0 &amp; 2 &amp; -3 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 2 \\ 0 \\ 16 \end{array} \right]<br /> <br /> Interestingly, :math:A_{2,1} is already zero with the new rearrangement. This is a lucky case since nothing needs to be done to the second row of the matrix. Now we eliminate :math:A_{3,2} using :math:A_{2,2}. To do so, multiply :math:R_2 by :math:-2, and add the result to :math:R_3:<br /> <br /> :math:R_3 \leftarrow R_3-2R_2<br /> <br /> which results in<br /> <br /> .. math::<br /> \left[ \begin{array}{ccc} 4 &amp; 0 &amp; -1 \\ 0 &amp; 1 &amp; -4 \\ 0 &amp; 0 &amp; 5 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 2 \\ 0 \\ 16 \end{array} \right]<br /> <br /> The forward elimination is complete.<br /> <br /> **Backward substitution**<br /> <br /> From :math:R_3: :math:5x_3=16\rightarrow x_3=q=3.2. Substituting :math:x_3 into :math:R_2: :math:x_2-4\times 3.2=0\rightarrow x_2=p=12.8.<br /> Finally, substituting :math:x_2 and :math:x_3 into :math:R_1: :math:4x_1-3.2=2\rightarrow x_1=n=1.3.<br /> <br /> *Solution by software*<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Tutorials/Tutorial-4/code/question1.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Tutorials/Tutorial-4/code/question1.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> The time required to solve the above system using built-in :math:Ax=b solvers is much less than that required by the brute-force approach in Tutorial 2.<br /> <br /> Question 2 [1.5]<br /> ================<br /> <br /> When it comes down to the basics, any recipe can be reduced to 4 components: fat, carbohydrates, protein and moisture (usually water). What differentiates each recipe though is the list of ingredients which are combined, and the ratio of fat:carbohydrate:protein:water. Of course how the ingredients are mixed and the reactions that take place during cooking are also important, but that affects mainly taste and texture, not nutritional value.<br /> <br /> Let's consider a biscuit recipe for this question. One can use butter for the fat component, use brown sugar and chocolate for the carbohydrate component (chocolate also contributes to the fat component), flour adds to the carbohydrate component also and somewhat to the protein component, while eggs, milk or water make up the rest of the recipe for protein and moisture.<br /> <br /> We would like to blend raw materials where the ratio of fat:carbohydrate:protein:water of the uncooked ingredients is 20:50:10:20. We have the following ingredients available, broken down on a mass percentage basis.<br /> <br /> ============= ====== ==== ===== ===== ========== === =========<br /> Component Butter Lard Flour Sugar Whole milk Egg Chocolate<br /> ============= ====== ==== ===== ===== ========== === =========<br /> Fat 85 81 0 0 4 10 14<br /> Carbohydrate 0 1 86 98 6 1 75<br /> Protein 1 4 10 0 4 35 6 <br /> Moisture 14 14 4 2 86 54 5 <br /> ============= ====== ==== ===== ===== ========== === =========<br /> <br /> #. Write down the 4 mass balance equations, one for each component, in the form :math:Ax = b, where :math:x is vector that represents the mass of each ingredient to be used, a vector with 7 rows and one column.<br /> #. What is the technical (mathematical) reason why we cannot solve this system of equations using Gauss elimination? Translate your mathematical answer to English, explaining it in terms of the recipe.<br /> #. If we are constrained to using only butter, flour, whole milk and eggs: solve, using LU decomposition on the computer, for the amounts of each to be used, in grams, to obtain 100 grams of uncooked ingredients, in the required ratio.<br /> <br /> If you prefer, you can interpret this question in the context of rubber manufacturing, where various oils, polypropylene and existing rubber materials are blended to create a new rubber with desired physical properties.<br /> <br /> Solution<br /> ----------<br /> <br /> #. For a 100 units of the recipe, we should have 20 units of fat, 50 units of carbohydrate, 10 units of protein, and 20 units of moisture. Given the percentage of these components in each of the ingredients, the four mass balance equations can be written as: <br /> <br /> ============== ===================================================================================<br /> Fat :math:85x_1+ 81x_2+ 0x_3 + 0x_4+ 4x_5+ 10x_6+ 14x_7=20<br /> Carbohydrate :math:0x_1+ 1x_2+ 86x_3+ 98x_4+ 6x_5+ 1x_6+ 75x_7=50<br /> Protein :math:1x_1+ 4x_2+ 10x_3+ 0x_4+ 4x_5+ 35x_6+ 6x_7=10<br /> Moisture :math:14x_1+ 14x_2+ 4x_3+ 2x_4+ 86x_5+ 54x_6+ 5x_7=20<br /> ============== ===================================================================================<br /> <br /> The above equations can be written in form :math:Ax = b as<br /> <br /> .. math::<br /> \left[ \begin{array}{ccccccc} 85&amp;81&amp;0&amp;0&amp;4&amp;10&amp;14 \\ 0&amp;1&amp;86&amp;98&amp;6&amp;1&amp;75 \\ 1&amp;4&amp;10&amp;0&amp;4&amp;35&amp;6\\ 14 &amp; 14 &amp; 4 &amp; 2 &amp; 86 &amp; 54 &amp; 5 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \\ x_{4}\\x_5\\x_6\\x_7 \end{array} \right] = \left[ \begin{array}{c} 20 \\ 50 \\ 10\\ 20 \end{array} \right]<br /> <br /> where :math:x_i represent the mass of the 7 ingredients from butter to chocolate, respectively.<br /> <br /> #. The above system cannot be solved for :math:x because the number of unknowns is **not** equal to the number of equations. In other words, the degrees of freedom is not zero, and we must have either more equations or less unknowns to be able to solve this system. In terms of the recipe, there are infinite combinations of the ingredients that yield the desired component ratio. So, there is no unique recipe given only the above information.<br /> <br /> #. If we limit ourselves to the use of only butter, flour, whole milk and eggs, we imply that the mass of the other three ingredients is zero in our recipe. So, we have eliminated three unknowns by fixing them to zero. Now, we have four unknowns in four equations. This system is now solvable. The new system will only have the coefficients and unknowns corresponding to butter, flour, whole milk and eggs as:<br /> <br /> .. math::<br /> \left[ \begin{array}{ccccccc} 85 &amp; 0 &amp; 4 &amp; 10 \\ 0 &amp; 86 &amp; 6 &amp; 1 \\ 1 &amp; 10 &amp; 4 &amp; 35\\ 14 &amp; 4 &amp; 86 &amp; 54 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{3} \\x_5\\x_6 \end{array} \right] = \left[ \begin{array}{c} 20 \\ 50 \\ 10\\ 20 \end{array} \right]<br /> <br /> <br /> Recall from class that :math:A = LU, but with a permutation or pivot matrix, it becomes :math:PA = LU, or alternatively: :math:A = P^{-1}LU<br /> <br /> :math:Ax = b\rightarrow P^{-1}LUx = b. Let us :math:y=Ux.<br /> <br /> Then solve :math:(P^{-1}L)y = b for :math:y. Finally, solve :math:Ux = y for :math:x. It can be implemented as follows:<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Tutorials/Tutorial-4/code/question2.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Tutorials/Tutorial-4/code/question2.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> .. A = np.array([[85,81,0,0,4,10,14],[0,1,81,98,6,1,75],[1,4,10,0,4,35,6],[14,14,9,1,86,54,5]])<br /> .. A = A[:,[0,2,4,5]]<br /> .. b = np.array([20, 50, 10, 20])<br /> .. linalg.solve(A[:,[0,2,4,5]],b)*100<br /> <br /> <br /> Question 3 [1.5]<br /> ================<br /> <br /> In class we derived the molar balance for a species in a similar flowsheet. Use the slightly modified flowsheet below<br /> <br /> .. figure:: images/reactor-network.png<br /> :scale: 30<br /> :align: center<br /> <br /> and let :math:\alpha be the proportion of :math:m_3 that is sent to reactor RB, and the rest, :math:(1-\alpha)m_3, is sent to reactor RC. Similarly, let :math:\beta represent the proportion of :math:m_8 that leaves the system, and let :math:(1-\beta)m_8 be the amount sent to the recycle stream. Also let :math:X_A be the conversion of the species in reactor RA, and similarly for :math:X_B and :math:X_C.<br /> <br /> #. Write a MATLAB or Python function that will return a matrix :math:A given the five inputs, :math:\alpha, \beta, X_A, X_B, and :math:X_C. This matrix :math:A represents the coefficients in the molar balances on the flowsheet, and these balances can be solved using :math:Ax = b.<br /> #. Given that :math:m_1 = 10 mol/second, and that the molar conversion of the inlet stream to reactor A is 5%, i.e. :math:X_A = 0.05, :math:X_B = 0.60 and :math:X_C = 0.85. What are the molar flow rates, :math:m_6, m_7, m_9, and :math:m_{10} when:<br /> <br /> * :math:\alpha=0.2, :math:\beta=0.7 <br /> * :math:\alpha=0.2, :math:\beta=0.4<br /> <br /> Are your answers reasonable?<br /> <br /> You should use either MATLAB or Python's built-in functions to solve the system of equations.<br /> <br /> Solution<br /> ----------<br /> <br /> #. The feed stream, :math:m_1, is the molar flow rate of the species of interest. A molar balance can be written around each reactor, as well as split and each junction point:<br /> <br /> <br /> :math:m_1 = \text{some given value} mol/s<br /> <br /> :math:m_2 = m_1 + m_9<br /> <br /> :math:m_3 = (1-X_A)m_2<br /> <br /> :math:m_4 = \alpha m_3<br /> <br /> :math:m_5 = (1-\alpha) m_3<br /> <br /> :math:m_6 = (1-X_B) m_4<br /> <br /> :math:m_7 = (1-X_C) m_5<br /> <br /> :math:m_8 = m_6 + m_7<br /> <br /> :math:m_9 = (1-\beta) m_8<br /> <br /> :math:m_{10} = \beta m_8<br /> <br /> Rearranging the above equations to have all unknowns on one side, and all known values on the other side gives:<br /> <br /> :math:m_1 = \text{some given value} <br /> <br /> :math:-m_1 + m_2 - m_9=0<br /> <br /> :math:-(1-X_A)m_2 + m_3=0<br /> <br /> :math:- \alpha m_3+ m_4=0<br /> <br /> :math:- (1-\alpha) m_3 + m_5 =0<br /> <br /> :math:- (1-X_B) m_4+ m_6 =0<br /> <br /> :math:- (1-X_C) m_5 + m_7 =0<br /> <br /> :math:-m_6 - m_7+ m_8 = 0<br /> <br /> :math:- (1-\beta) m_8+ m_9 =0<br /> <br /> :math:-\beta m_8 + m_{10} = 0<br /> <br /> If we have the values of the parameters :math:\alpha, \beta, X_A, X_B, and :math:X_C, then we can write a function that accepts these input arguments and whose output is the matrix :math:A. This function, and the code that calls it later on can be implemented as shown below. With the given parameter values, the molar flow rates can be solved easily. First we have to evaluate the matrix :math:A by calling the function matrix_A, and providing the values of the parameters. Then, the system is solved for :math:m_i. <br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Tutorials/Tutorial-4/code/matrix_A.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Tutorials/Tutorial-4/code/matrix_A.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> The solution for the two scenarios above differ only in terms of the :math:\beta value.<br /> <br /> * :math:\beta = 0.7 is :math:x = [ 10.0, 10.6, 10.1, 2.01, 8.06, 0.806, 1.21, 2.01, 0.604, 1.41]<br /> * :math:\beta = 0.4 is :math:x = [10.0, 11.3, 10.7, 2.14, 8.58, 0.858, 1.29, 2.14, 1.29, 0.859]<br /> <br /> It shows that as :math:\beta decreases (i.e. we recycle more of our stream :math:m_8) that the amount of the species leaving the overall reactor decreases from 1.41 to 0.859. This is expected, because we know that as we increase the recycle rate, that a greater amount of unconverted material will be reacted. So even this relatively simplistic model makes engineering sense.<br /> <br /> Use the above code and see what happens when you let :math:\beta = 0.0 or :math:\beta = 1.0: do the results still make sense?<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.25cm}<br /> \hrule<br /> %\begin{center}END\end{center}<br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Tutorial_3_-_2010_-_Solution&diff=1080 Tutorial 3 - 2010 - Solution 2018-09-15T10:34:14Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 30 September 2010<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = Tutorial-3-2010.pdf<br /> | questions_text_alt = Tutorial questions <br /> | solutions_PDF = Tutorial-3-2010-Solution.pdf<br /> | solutions_text_alt = Solutions, prepared by Elliot Cameron<br /> | other_instructions = Hand-in at class.<br /> }}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> <br /> .. |m3| replace:: m\ :sup:3<br /> .. highlight:: python<br /> <br /> .. rubric:: Tutorial solutions: Elliot Cameron.<br /> <br /> .. rubric:: Tutorial objectives<br /> <br /> * Understand computer representation of decimal numbers and chemical engineering data.<br /> <br /> Question 1 <br /> ===============<br /> <br /> * Convert from binary to decimal: :math:(0.10101)_{2}<br /> * Convert from decimal to binary: :math:(32.625)_{10}<br /> * Convert from decimal to binary: :math:(0.2)_{10} <br /> <br /> Solution<br /> --------<br /> <br /> #.<br /> :math:(0.10101)_{2}<br /> <br /> :math:\underbrace{\begin{array}{ccccccc} \underbrace{0}_{0\text{x}2^{0}} &amp; . &amp; \underbrace{1}_{1\text{x}2^{-1}} &amp; \underbrace{0}_{0\text{x}2^{-2}} &amp; \underbrace{1}_{1\text{x}2^{-3}} &amp; \underbrace{0}_{0\text{x}2^{-4}} &amp; \underbrace{1}_{1\text{x}2^{-5}} \end{array}}_{0.0 + 0.5 + 0.0 + 0.125 + 0.0 + 0.03125 = 0.65625}<br /> <br /> Therefore<br /> <br /> :math:(0.10101)_{2} = (0.65625)_{10}<br /> <br /> #.<br /> :math:(32.625)_{10}<br /> <br /> Start to the left of the decimal point (i.e. Representing 32 in binary)<br /> <br /> :math:log_{2}(32) = 5 \rightarrow \text{This helps us determine how many binary digits are required}<br /> <br /> Rounding 5 up to 5 (obviously)<br /> <br /> :math:2^{5} = 32 \rightarrow (32 - 32 = 0)<br /> <br /> Therefore<br /> <br /> :math:1\text{x}2^{5}+0\text{x}2^{4}+0\text{x}2^{3}+0\text{x}2^{2}+0\text{x}2^{1}+0\text{x}2^{0} = (100000)_{2} = (32)_{10}<br /> <br /> Moving to the right of the decimal place<br /> <br /> :math:2^{-1} = 0.5 \rightarrow (0.625 - 0.5 = 0.125)<br /> <br /> :math:2^{-2} = 0.25 \rightarrow (0.125 &lt; 0.2) : therefore we move to the next digit<br /> <br /> :math:2^{-3} = 0.125 \rightarrow (0.125 - 0.125 = 0.0)<br /> <br /> Solution:<br /> <br /> :math:1\text{x}2^{5}+0\text{x}2^{4}+0\text{x}2^{3}+0\text{x}2^{2}+0\text{x}2^{1}+0\text{x}2^{0}+1\text{x}2^{-1}+0\text{x}2^{-2}+1\text{x}2^{-3} = (100000.101)_{2} = (32.625)_{10}<br /> <br /> #.<br /> :math:(0.2)_{10}<br /> <br /> Start to the left of the decimal point (i.e. Representing 0 in binary)<br /> <br /> :math:(0)_{10} = (0)_{2}<br /> <br /> Moving to the right of the decimal place<br /> <br /> :math:2^{-1} = 0.5 \rightarrow (0.2 &lt; 0.5) : therefore we move to the next digit<br /> <br /> :math:2^{-2} = 0.25 \rightarrow (0.2 &lt; 0.25) : therefore we move to the next digit<br /> <br /> :math:2^{-3} = 0.125 \rightarrow (0.2 - 0.125 = 0.075)<br /> <br /> :math:2^{-4} = 0.0625 \rightarrow (0.075 - 0.0625 = 0.0125)<br /> <br /> :math:2^{-5} = 0.03125 \rightarrow (0.0125 &lt; 0.03125) : therefore we move to the next digit<br /> <br /> :math:2^{-6} = 0.015625 \rightarrow (0.0125 &lt; 0.015625) : therefore we move to the next digit<br /> <br /> :math:2^{-7} = 0.0078125 \rightarrow (0.0125 - 0.0078125 = 0.0046875)<br /> <br /> :math:2^{-8} = 0.00390625 \rightarrow (0.0046875 - 0.00390625 = 0.00078125)<br /> <br /> :math:2^{-9} = 0.001953125 \rightarrow (0.00078125 &lt; 0.001953125) :move to the next digit<br /> <br /> :math:2^{-10} = 0.0009765625 \rightarrow (0.00078125 &lt; 0.0009765625) : move to the next digit<br /> <br /> :math:2^{-11} = 0.00048828125 \rightarrow (0.00078125 - 0.00048828125 = 0.00029296875)<br /> <br /> :math:2^{-12} = 0.000244140625 \rightarrow (0.00029296875 - 0.000244140625 = 0.000048828125)<br /> <br /> :math:\ldots<br /> <br /> Solution<br /> <br /> .. math::<br /> 0\text{x}2^{0}+0\text{x}2^{-1}+0\text{x}2^{-2}+1\text{x}2^{-3}+1\text{x}2^{-4}+0\text{x}2^{-5}+0\text{x}2^{-6}+1\text{x}2^{-7}+1\text{x}2^{-8}+0\text{x}2^{-9}+0\text{x}2^{-10}+1\text{x}2^{-11}+1\text{x}2^{-12}\\<br /> \approx (0.{\bf \overline{0011}})_{2} = (0.2)_{10}<br /> <br /> What this questions aims to show you is, just as in the decimal system, there are numbers that cannot be represented finitely in binary (think 1/3 in decimal notation).<br /> <br /> Question 2 <br /> ===============<br /> <br /> #. Write, in binary form, the representation for the most negative floating point number that can be stored in an 8-bit word, using 1 bit for the sign, 3 bits for the signed exponent and the remainder for the significand (in normalized form). <br /> #. What is the decimal equivalent of this number? <br /> #. What is the machine number next to this one (both in binary and in decimal please)? <br /> #. Calculate the maximal interval-to-value ratio around these two values. Does it agree with the limit for theoretical machine precision?<br /> #. As which machine number would -7.22 be stored if the machine used (a) chopping, or (b) rounding?<br /> <br /> Solution<br /> --------<br /> <br /> #. <br /> We start by recalling that normalized scientific notation assumes that the significand starts with an implied leading digit of 0 (i.e. it falls between the following bounds):<br /> <br /> :math:\frac{1}{b} \leq m &lt; 1<br /> <br /> Where<br /> <br /> :math:m = significand<br /> <br /> :math:b = base<br /> <br /> :math:e = exponent<br /> <br /> Therefore the most negative 8-bit binary number with 1 bit for the sign, 3 bits for the signed exponent, and 4 bits for the normalized significand is:<br /> <br /> .. math::<br /> \begin{array}{ccc} \underbrace{1}_{\text{sign}} &amp; \underbrace{011}_{\text{exponent}} &amp; \underbrace{1111}_{\text{significand}} \rightarrow -\left(1\text{x}2^{-1}+1\text{x}2^{-2}+1\text{x}2^{-3}+1\text{x}2^{-4}\right)\text{x}2^{\left(1\text{x}2^{0}+1\text{x}2^{1}\right)} = -0.9375 \text{x} 2^{3} \end{array}<br /> <br /> #. .. math::<br /> \begin{array}{ccc} \underbrace{1}_{\text{sign}} &amp; \underbrace{011}_{\text{exponent}} &amp; \underbrace{1111}_{\text{significand}} = -0.9375 \text{x} 2^{3} = -0.9375 \text{x} 8 = -7.5 \end{array}<br /> <br /> #. <br /> The next closest machine number is the next physically representable number in the 8-bit floating point system. Since no more space exists &quot;above&quot; the current significand we must go &quot;down&quot; in magnitude. Therefore:<br /> <br /> .. math::<br /> \begin{array}{ccc} \underbrace{1}_{\text{sign}} &amp; \underbrace{011}_{\text{exponent}} &amp; \underbrace{1110}_{\text{significand}} = -0.8750 \text{x} 2^{3} = -7.0 \end{array}<br /> <br /> #. <br /> We start by estimating the theoretical machine precision:<br /> <br /> :math:t = 4 : number of significant digits in the significand<br /> <br /> :math:\beta = 2 : base of number system<br /> <br /> :math:\epsilon_{mach} = \beta^{1-t} = 2^{1-4} = 0.125<br /> <br /> Next we test the maximal interval-to-ratio value on either side of the values from part (a)/(b) and (c)<br /> <br /> :math:\frac{|x_{1}-x_{2}|}{|x_{1}|} = \frac{|(-7.0)-(-7.5)|}{|(-7.0)|} = 0.0714\ldots \leq \epsilon_{mach}<br /> <br /> :math:\frac{|x_{1}-x_{2}|}{|x_{2}|} = \frac{|(-7.0)-(-7.5)|}{|(-7.5)|} = 0.0666\ldots \leq \epsilon_{mach}<br /> <br /> It is easy to see above that the maximal interval-to-value ratios agree with the upper :math:\epsilon_{mach} limit<br /> <br /> #. <br /> As observed in parts (a) - (c) the two closest machine numbers to -7.22 are -7.0 and -7.5. Therefore the effect of chopping (rounding towards 0) and rounding (rounding to the closest avaiable machine number) would result in the same floating point value: **-7.0**.<br /> <br /> Question 3 [1.5]<br /> =================<br /> <br /> #. Increasingly we are seeing cameras being used in chemical processes to monitor and control the process, especially systems that deal with foods and solid products. How much space, in kilobytes, is required to store a digital photo with 640 rows and 480 columns of pixels and 3 layers (red, green and blue) using uint8 integer representation?<br /> #. Computer systems are used to archive data from each electronic measurement, such as temperature, pressure, flow measurements, *etc*. Each measurement is called a tag. At your plant, you wish to store 16,525 tags, recorded once per second and stored in double precision. How much space would be required on the company's server, in terabytes, to store a single copy of 1 year of data? What difference does it make to store the data in single precision?<br /> #. How many data points can you store in double precision in 1500 megabytes of RAM? (For example, MATLAB on a 32-bit Windows Vista machine cannot create arrays greater than 1428 megabytes.)<br /> <br /> Use the fact that:<br /> <br /> * 1024 bytes = 1 kilobyte<br /> * 1024 kilobytes = 1 megabyte<br /> * 1024 megabytes = 1 gigabyte<br /> * 1024 gigabyte = 1 terabyte = 2\ :sup:40 bytes<br /> <br /> Solution<br /> --------<br /> <br /> #. <br /> Recall that the uint8 integer representation refers to an unsigned integer with no exponential term. Therefore it refers to an integer that can store a value from **0 - 255**. As such this question is simply asking us how much memory would be required to store a 640x480 pixel image using the 256/256/256 RGB colour notation. Now, if each pixel in each of the RGB matrices is represented by a uint8 then **each value will logically take up 8 bits of memory**. If each of the three colour matrices contains 640x480 values and we assume a standard 8 bit byte then the total space required to store the photo would be:<br /> <br /> .. math::<br /> Space = \underbrace{3}_{\text{rgb}} \times \underbrace{640*480}_{\text{pixels}} \times \underbrace{8\;\text{bits}}_{\text{uint8}} = 7372800\;\text{bits}*\frac{1\;\text{byte}}{8\;\text{bit}}*\frac{1\;\text{kilobyte}}{1024\;\text{byte}} = 900\;\text{kilobytes}<br /> <br /> <br /> #. <br /> Recall that a standard floating point double precision number takes up 64 bits (8 bytes) of memory. We start this problem by calculating the number of values to be stored in one year.<br /> <br /> .. math::<br /> \text{Values} = 16,525\frac{\text{values}}{s}\times\frac{60\;s}{1\;\text{min}}\times\frac{60\;\text{min}}{1\;\text{hr}}\times\frac{24\;\text{hr}}{1\;\text{day}}\times\frac{365\;\text{day}}{1\;\text{year}} = 521,132,400,000\frac{\text{values}}{\text{year}}<br /> <br /> Therefore the memory requirement is:<br /> <br /> .. math::<br /> Memory = 521,132,400,000\frac{\text{values}}{\text{year}}\cdot8\frac{\text{bytes}}{\text{value}}\cdot\frac{1\;\text{kilobyte}}{1024\;\text{byte}}\cdot\frac{1\;\text{megabyte}}{1024\;\text{kilobyte}}\cdot\frac{1\;\text{gigabyte}}{1024\;\text{megabyte}}\cdot\frac{1\;\text{terabyte}}{1024\;\text{gigabyte}} = 3.79\;\text{terabytes}<br /> <br /> A floating point single precision value takes up exactly half as much space as a floating point double precision number (i.e. 32 bits = 4 bytes). Therefore storing the same tags in single precision would take up :math:1.9\;TB.<br /> <br /> #. <br /> If we take the absolute value of RAM available (i.e. 1500 MB) and the standard definition of a floating point double precision number (i.e. 64 bits = 8 bytes), then<br /> <br /> :math:1,500\;\text{MB}\times\frac{1024\;\text{KB}}{1\;\text{MB}}\times\frac{1024\;\text{B}}{1\;\text{kB}} = 1,572,864,000\;\text{bytes}<br /> <br /> Therefore the number of double precision values that could be stored would be.<br /> <br /> :math:\text{Number} = 1,572,864,000\;\text{bytes}\times\frac{1\;\text{value}}{8\;\text{bytes}} = 196,608,000\;\text{values}<br /> <br /> If we use the maximum MATLAB array size then.<br /> <br /> :math:1,428\;\text{MB}\times\frac{1024\;\text{KB}}{1\;\text{MB}}\times\frac{1024\;\text{bytes}}{1\;\text{kB}} = 1,497,366,528\;\text{bytes}<br /> <br /> Therefore the number of double precision values that could be stored in MATLAB would be.<br /> <br /> :math:\text{Number} = 1,497,366,528\;\text{bytes}\times\frac{1\;\text{value}}{8\;\text{bytes}} = 187,170,816\;\text{values}<br /> <br /> Question 4 <br /> =================<br /> <br /> Consider the following system of linear algebraic equations:<br /> <br /> .. math::<br /> <br /> \left\{\begin{array}{rcl} 2 x_1 -2x_2 +4 x_3 &amp; = &amp; 0 \\ x_1 -3 x_2 + 4x_3 &amp; = &amp; -1 \\ 3x_1 - x_2 +5x_3 &amp;= &amp; 0 \end{array}\right.<br /> <br /> #. Use Gauss elimination (forward elimination and backward substitution) to solve these equations for :math:(x_1,x_2,x_3).<br /> #. Validate your solution in either Python or MATLAB.<br /> <br /> Solution<br /> ---------<br /> <br /> #.<br /> We are asked to solve this system of equations using Gauss Elimination (without partial pivoting).<br /> <br /> .. math::<br /> \begin{array}{ccccc} 2x_{1} &amp; -2x_{2} &amp; +4x_{3} &amp; = &amp; 0\\ x_{1} &amp; -3x_{2} &amp; +4x_{3} &amp; = &amp; -1\\ 3x_{1} &amp; -x_{2} &amp; +5x_{3} &amp; = &amp; 0 \end{array} \rightarrow \left[ \begin{array}{ccc} 2 &amp; -2 &amp; 4 \\ 1 &amp; -3 &amp; 4 \\ 3 &amp; -1 &amp; 5 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 0 \\ -1 \\ 0 \end{array} \right]<br /> <br /> **Forward elimination**<br /> <br /> Divide row 1 by element (1,1)<br /> <br /> .. math::<br /> \left[ \begin{array}{ccc} 1 &amp; -1 &amp; 2 \\ 1 &amp; -3 &amp; 4 \\ 3 &amp; -1 &amp; 5 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 0 \\ -1 \\ 0 \end{array} \right]<br /> <br /> Subtract row 1 from row 2 and 3 times row 1 from row 3 to eliminate all elements in column 1 below the diagonal element<br /> <br /> .. math::<br /> \left[ \begin{array}{ccc} 1 &amp; -1 &amp; 2 \\ 0 &amp; -2 &amp; 2 \\ 0 &amp; 2 &amp; -1 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 0 \\ -1 \\ 0 \end{array} \right]<br /> <br /> Divide row 2 by element (2,2)<br /> <br /> .. math:: <br /> \left[ \begin{array}{ccc} 1 &amp; -1 &amp; 2 \\ 0 &amp; 1 &amp; -1 \\ 0 &amp; 2 &amp; -1 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 0 \\ 0.5 \\ 0 \end{array} \right]<br /> <br /> Subtract 2 times row 2 from row 3 to eliminate all elements in column 2 below the diagonal element<br /> <br /> .. math::<br /> \left[ \begin{array}{ccc} 1 &amp; -1 &amp; 2 \\ 0 &amp; 1 &amp; -1 \\ 0 &amp; 0 &amp; 1 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 0 \\ 0.5 \\ -1 \end{array} \right]<br /> <br /> **Backwards substitution**<br /> <br /> Subtract -1 times row 3 from row 2 and 2 times row 3 from row 1 to eliminate all elements in column 3 above the diagonal element<br /> <br /> .. math::<br /> \left[ \begin{array}{ccc} 1 &amp; -1 &amp; 0 \\ 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 1 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 2 \\ -0.5 \\ -1 \end{array} \right]<br /> <br /> Subtract -1 times row 2 from row 1 to eliminate all elements in column 2 above the diagonal element<br /> <br /> .. math::<br /> \left[ \begin{array}{ccc} 1 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 \\ 0 &amp; 0 &amp; 1 \end{array} \right] \left[ \begin{array}{c} x_{1} \\ x_{2} \\ x_{3} \end{array} \right] = \left[ \begin{array}{c} 1.5 \\ -0.5 \\ -1 \end{array} \right]<br /> <br /> #.<br /> Checking this solution in MATLAB:<br /> <br /> ::<br /> <br /> EDU&gt;&gt; A = [2,-2,4;<br /> 1,-3,4;<br /> 3,-1,5];<br /> EDU&gt;&gt; b = [0;-1;0];<br /> EDU&gt;&gt; x = A\b<br /> <br /> x =<br /> <br /> 1.5000<br /> -0.5000<br /> -1.0000<br /> <br /> Checking this solution in Python:<br /> <br /> ::<br /> <br /> In : A = np.array([[2,-2,4],[1,-3,4],[3,-1,5]])<br /> <br /> In : b = np.array([,[-1],])<br /> <br /> In : x = np.linalg.solve(A,b)<br /> <br /> In : print(x)<br /> <br /> [[ 1.5]<br /> [-0.5]<br /> [-1. ]]<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.25cm}<br /> \hrule<br /> %\begin{center}END\end{center}<br /> <br /> <br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Software_tutorial/About_the_course_software&diff=1079 Software tutorial/About the course software 2018-09-15T10:29:11Z <p>Kevindunn: </p> <hr /> <div>{{Navigation|Book=Software tutorial|previous=|current=Tutorial index|next=Software installation}}<br /> {| class=&quot;wikitable&quot;<br /> |-<br /> ! MATLAB<br /> ! Python<br /> |-<br /> | width=&quot;50%&quot; valign=&quot;top&quot; |<br /> MATLAB (MATrix LABoratory) is a high level computer language/ interactive software package developed and distributed by MathWorks&amp;trade;. Matlab was first developed in the 1970s by Cleve Molar. Cleve was later joined by John N. Little and Steve Bangert and the three went on to found MathWorks&amp;trade;. MATLAB excels at performing matrix operations and can handle large data sets (stored as matrices) very easily. MATLAB was originally designed as a user friendly interface for LINPACK and EISPACK and so was intended for linear algebra application. Since then MATLAB has greatly expanded it's core abilities to encompass a large array of graphic and numeric applications. These core abilities may in turn be expanded further through the addition of specialized &quot;tool boxes&quot;.<br /> | <br /> ''From Wikipedia'': Python is a high-level programming language whose design philosophy emphasizes code readability. Python aims to combine &quot;remarkable power with very clear syntax&quot;, and its standard library of built-in functions is large and comprehensive. <br /> <br /> We will use the [https://www.scipy.org/ NumPy and SciPy] modules (the equivalent of a MATLAB toolbox), to provide scientific computing capabilities to Python. These modules, like MATLAB, allow you to handle large data arrays with little effort. They provide all the tools we require for this course.<br /> <br /> We will also use the [http://matplotlib.sourceforge.net/index.html matplotlib] module, which provides Python with plotting capabilities similar to MATLAB.<br /> <br /> You might not be familiar with Python. Here is a comparison with MATLAB:<br /> <br /> * Many companies already use it as a standard package now <br /> * It can run on Windows, Linux and Mac computers (companies are increasingly using Mac and Linux platforms)<br /> * Commercial software support is available from 3rd parties<br /> * The software can be installed on a local desktop, or in a networked environment and run remotely<br /> * It is free (both for academic and commercial use), so it can used after you graduate<br /> * Installation is straightforward<br /> * The license is not restrictive: you can legally modify and improve the software <br /> * There are excellent add-on libraries for almost anything you need<br /> * It promotes good practice of writing a code file, and then running it (like MATLAB). The code file documents what you have done, and you can always repeat your analysis on a new data set, or share the code with colleagues. Other software packages tend to promote a more point-and-click approach, so you can't always retrace your steps.<br /> * There are multicore and 64-bit versions of Python available to process large data sets, and do parallel data processing.<br /> |}</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Software_tutorial&diff=1078 Software tutorial 2018-09-15T10:28:16Z <p>Kevindunn: </p> <hr /> <div>This tutorial will expand as we progress in the course. Each section has notes for both MATLAB and Python.<br /> <br /> # [[Software tutorial/About the course software | About the course software]]<br /> # [[Software tutorial/Software installation | Software installation]]<br /> # [[Software tutorial/Getting started|Getting started]]<br /> # [[Software tutorial/My first program|My first program]] (required for [[Tutorial_1_-_2010|tutorial 1]])<br /> # [[Software tutorial/Loops|&lt;tt&gt;for&lt;/tt&gt; loops and &lt;tt&gt;while&lt;/tt&gt; loops]] (required for [[Tutorial_1_-_2010|tutorial 1]])<br /> # [[Software tutorial/Scripts and functions|Scripts and functions]] (useful for [[Tutorial_2_-_2010|tutorial 2]])<br /> # [[Software tutorial/Vectors and arrays|Vectors and arrays]] (used for [[Tutorial_3_-_2010|tutorial 3]])<br /> # [[Software_tutorial/Matrix_operations|Matrix operations]] (used for [[Tutorial_4_-_2010|tutorial 4]])<br /> # [[Software_tutorial/Functions as objects|Functions as objects]] (used for [[Tutorial_5_-_2010|tutorial 5]] to find the zero of a function)<br /> # [[Software_tutorial/Creating and saving plots|Creating and saving plots]] : important for all remaining assignments, and the take-home exam.<br /> # [[Software_tutorial/Integration of ODEs|Integrating ODEs]]: a tutorial on integrating ODEs in MATLAB and Python<br /> # [[Software_tutorial/Least squares modelling (linear regression)|Least squares modelling]]: a tutorial on (linear regression) in MATLAB and Python<br /> # [[Software_tutorial/Loading data in MATLAB or Python| Loading data in MATLAB or Python]] from Excel, CSV and XML files<br /> <br /> * Want tutorials on other topics: please [mailto:kevin.dunn@connectmv.com email me] <br /> == Need some more help? ==<br /> <br /> Not every aspect about MATLAB or Python can be covered with our tutorial (above). Here are some additional resources.<br /> <br /> {| class=&quot;wikitable&quot;<br /> |-<br /> ! MATLAB<br /> ! Python<br /> |-<br /> | width=&quot;50%&quot; valign=&quot;top&quot; |<br /> * [[media:MATLAB_primer.pdf | MATLAB primer - a guide to MATLAB]] by Dr. Prashant Mhaskar]]<br /> * [[media:Introduction_to_MATLAB_presentation.pdf | Introduction to MATLAB]] - a presentation to the 2009 CHE 3E04 class by Elliot Cameron and Eric Rodger<br /> * [http://www.mathworks.com/help/pdf_doc/matlab/getstart.pdf Getting started with MATLAB 7] - a guide from the MathWorks.<br /> * [http://www.math.ufl.edu/help/matlab-tutorial/matlab-tutorial.html Online MATLAB tutorial]: An MATLAB tutorial from the University of Florida.<br /> * [http://www.mathworks.com/help/index.html MathWorks documentation]: The official MATLAB documentation<br /> * [http://www.mathworks.com/matlabcentral/fileexchange/ MATLAB Central File Exchange]: User-created MATLAB code that can be freely used.<br /> | width=&quot;50%&quot; valign=&quot;top&quot; |<br /> <br /> * [http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3 The non-programmer's guide to Python]<br /> * Use the money you saved from not buying MATLAB to buy [http://www.amazon.ca/Learning-Python-Powerful-Object-Oriented-Programming/dp/0596158068 this book on Learning Python], or read [http://books.google.com/books?id=1HxWGezDZcgC&amp;printsec=frontcover&amp;dq=%22Learning+Python%22&amp;source=bl&amp;ots=LiMh3Ka7uW&amp;sig=_z8u8gSW7z3hiE7vKrnX8pA3tNo&amp;hl=en&amp;ei=azWWTODQF8qdnAeyjLXDCA&amp;sa=X&amp;oi=book_result&amp;ct=result&amp;resnum=6&amp;ved=0CDoQ6AEwBQ#v=onepage&amp;q&amp;f=false the Google Books version].<br /> * This [http://diveintopython.org/ excellent book - Dive into Python] is available in print, and on the web.<br /> * Are you comfortable with MATLAB? This [http://mathesaurus.sourceforge.net/matlab-numpy.html page shows how to translate] between MATLAB and Python.<br /> * A good [http://www.springerlink.com/content/978-3-540-73915-9 book for computational and engineering work] - free access from McMaster computers.<br /> * We need a link to [http://docs.python.org/tutorial/ the official Python tutorial] - but it is more general.<br /> * And finally, maybe [http://xkcd.com/353/ this cartoon will convince you]?<br /> |}</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Software_tutorial&diff=1077 Software tutorial 2018-09-15T10:27:39Z <p>Kevindunn: </p> <hr /> <div>This tutorial will expand as we progress in the course. Each section has notes for both MATLAB and Python.<br /> <br /> # [[Software tutorial/About the course software | About the course software]]<br /> # [[Software tutorial/Software installation | Software installation]]<br /> # [[Software tutorial/Getting started|Getting started]]<br /> # [[Software tutorial/My first program|My first program]] (required for [[Tutorial_1_-_2010|tutorial 1]])<br /> # [[Software tutorial/Loops|&lt;tt&gt;for&lt;/tt&gt; loops and &lt;tt&gt;while&lt;/tt&gt; loops]] (required for [[Tutorial_1_-_2010|tutorial 1]])<br /> # [[Software tutorial/Scripts and functions|Scripts and functions]] (useful for [[Tutorial_2_-_2010|tutorial 2]])<br /> # [[Software tutorial/Vectors and arrays|Vectors and arrays]] (used for [[Tutorial_3_-_2010|tutorial 3]])<br /> # [[Software_tutorial/Matrix_operations|Matrix operations]] (used for [[Tutorial_4_-_2010|tutorial 4]])<br /> # [[Software_tutorial/Functions as objects|Functions as objects]] (used for [[Tutorial_5_-_2010|tutorial 5]] to find the zero of a function)<br /> # [[Software_tutorial/Creating and saving plots|Creating and saving plots]] : important for all remaining assignments, and the take-home exam.<br /> # [[Software_tutorial/Integration of ODEs|Integrating ODEs]]: a tutorial on integrating ODEs in MATLAB and Python<br /> # [[Software_tutorial/Least squares modelling (linear regression)|Least squares modelling]]: a tutorial on (linear regression) in MATLAB and Python<br /> # [[Software_tutorial/Loading data in MATLAB or Python| Loading data in MATLAB or Python]] from Excel, CSV and XML files<br /> <br /> * Want tutorials on other topics: please [mailto:kevin.dunn@connectmv.com email me] <br /> == Need some more help? ==<br /> <br /> Not every aspect about MATLAB or Python can be covered with our tutorial (above). Here are some additional resources.<br /> <br /> {| class=&quot;wikitable&quot;<br /> |-<br /> ! MATLAB<br /> ! Python<br /> |-<br /> | width=&quot;50%&quot; valign=&quot;top&quot; |<br /> * [[Media:MATLAB_primer.pdf MATLAB primer - a guide to MATLAB] by Dr. Prashant Mhaskar]]<br /> * [[Media:Introduction_to_MATLAB_presentation.pdf Introduction to MATLAB]] - a presentation to the 2009 CHE 3E04 class by Elliot Cameron and Eric Rodger<br /> * [http://www.mathworks.com/help/pdf_doc/matlab/getstart.pdf Getting started with MATLAB 7] - a guide from the MathWorks.<br /> * [http://www.math.ufl.edu/help/matlab-tutorial/matlab-tutorial.html Online MATLAB tutorial]: An MATLAB tutorial from the University of Florida.<br /> * [http://www.mathworks.com/help/index.html MathWorks documentation]: The official MATLAB documentation<br /> * [http://www.mathworks.com/matlabcentral/fileexchange/ MATLAB Central File Exchange]: User-created MATLAB code that can be freely used.<br /> | width=&quot;50%&quot; valign=&quot;top&quot; |<br /> <br /> * [http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3 The non-programmer's guide to Python]<br /> * Use the money you saved from not buying MATLAB to buy [http://www.amazon.ca/Learning-Python-Powerful-Object-Oriented-Programming/dp/0596158068 this book on Learning Python], or read [http://books.google.com/books?id=1HxWGezDZcgC&amp;printsec=frontcover&amp;dq=%22Learning+Python%22&amp;source=bl&amp;ots=LiMh3Ka7uW&amp;sig=_z8u8gSW7z3hiE7vKrnX8pA3tNo&amp;hl=en&amp;ei=azWWTODQF8qdnAeyjLXDCA&amp;sa=X&amp;oi=book_result&amp;ct=result&amp;resnum=6&amp;ved=0CDoQ6AEwBQ#v=onepage&amp;q&amp;f=false the Google Books version].<br /> * This [http://diveintopython.org/ excellent book - Dive into Python] is available in print, and on the web.<br /> * Are you comfortable with MATLAB? This [http://mathesaurus.sourceforge.net/matlab-numpy.html page shows how to translate] between MATLAB and Python.<br /> * A good [http://www.springerlink.com/content/978-3-540-73915-9 book for computational and engineering work] - free access from McMaster computers.<br /> * We need a link to [http://docs.python.org/tutorial/ the official Python tutorial] - but it is more general.<br /> * And finally, maybe [http://xkcd.com/353/ this cartoon will convince you]?<br /> |}</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Software_tutorial&diff=1076 Software tutorial 2018-09-15T10:25:18Z <p>Kevindunn: </p> <hr /> <div>This tutorial will expand as we progress in the course. Each section has notes for both MATLAB and Python.<br /> <br /> # [[Software tutorial/About the course software | About the course software]]<br /> # [[Software tutorial/Software installation | Software installation]]<br /> # [[Software tutorial/Getting started|Getting started]]<br /> # [[Software tutorial/My first program|My first program]] (required for [[Tutorial_1_-_2010|tutorial 1]])<br /> # [[Software tutorial/Loops|&lt;tt&gt;for&lt;/tt&gt; loops and &lt;tt&gt;while&lt;/tt&gt; loops]] (required for [[Tutorial_1_-_2010|tutorial 1]])<br /> # [[Software tutorial/Scripts and functions|Scripts and functions]] (useful for [[Tutorial_2_-_2010|tutorial 2]])<br /> # [[Software tutorial/Vectors and arrays|Vectors and arrays]] (used for [[Tutorial_3_-_2010|tutorial 3]])<br /> # [[Software_tutorial/Matrix_operations|Matrix operations]] (used for [[Tutorial_4_-_2010|tutorial 4]])<br /> # [[Software_tutorial/Functions as objects|Functions as objects]] (used for [[Tutorial_5_-_2010|tutorial 5]] to find the zero of a function)<br /> # [[Software_tutorial/Creating and saving plots|Creating and saving plots]] : important for all remaining assignments, and the take-home exam.<br /> # [[Software_tutorial/Integration of ODEs|Integrating ODEs]]: a tutorial on integrating ODEs in MATLAB and Python<br /> # [[Software_tutorial/Least squares modelling (linear regression)|Least squares modelling]]: a tutorial on (linear regression) in MATLAB and Python<br /> # [[Software_tutorial/Loading data in MATLAB or Python| Loading data in MATLAB or Python]] from Excel, CSV and XML files<br /> <br /> * Want tutorials on other topics: please [mailto:kevin.dunn@connectmv.com email me] <br /> == Need some more help? ==<br /> <br /> Not every aspect about MATLAB or Python can be covered with our tutorial (above). Here are some additional resources.<br /> <br /> {| class=&quot;wikitable&quot;<br /> |-<br /> ! MATLAB<br /> ! Python<br /> |-<br /> | width=&quot;50%&quot; valign=&quot;top&quot; |<br /> * [http://modelling3e4.connectmv.com/mediafiles/mediawiki/7/7c/MATLAB_primer.pdf MATLAB primer - a guide to MATLAB] by Dr. Prashant Mhaskar.<br /> * [http://modelling3e4.connectmv.com/mediafiles/mediawiki/8/80/Introduction_to_MATLAB_presentation.pdf Introduction to MATLAB] - a presentation to the 2009 CHE 3E04 class by Elliot Cameron and Eric Rodger<br /> * [http://www.mathworks.com/help/pdf_doc/matlab/getstart.pdf Getting started with MATLAB 7] - a guide from the MathWorks.<br /> * [http://www.math.ufl.edu/help/matlab-tutorial/matlab-tutorial.html Online MATLAB tutorial]: An MATLAB tutorial from the University of Florida.<br /> * [http://www.mathworks.com/help/index.html MathWorks documentation]: The official MATLAB documentation<br /> * [http://www.mathworks.com/matlabcentral/fileexchange/ MATLAB Central File Exchange]: User-created MATLAB code that can be freely used.<br /> | width=&quot;50%&quot; valign=&quot;top&quot; |<br /> <br /> * [http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3 The non-programmer's guide to Python]<br /> * Use the money you saved from not buying MATLAB to buy [http://www.amazon.ca/Learning-Python-Powerful-Object-Oriented-Programming/dp/0596158068 this book on Learning Python], or read [http://books.google.com/books?id=1HxWGezDZcgC&amp;printsec=frontcover&amp;dq=%22Learning+Python%22&amp;source=bl&amp;ots=LiMh3Ka7uW&amp;sig=_z8u8gSW7z3hiE7vKrnX8pA3tNo&amp;hl=en&amp;ei=azWWTODQF8qdnAeyjLXDCA&amp;sa=X&amp;oi=book_result&amp;ct=result&amp;resnum=6&amp;ved=0CDoQ6AEwBQ#v=onepage&amp;q&amp;f=false the Google Books version].<br /> * This [http://diveintopython.org/ excellent book - Dive into Python] is available in print, and on the web.<br /> * Are you comfortable with MATLAB? This [http://mathesaurus.sourceforge.net/matlab-numpy.html page shows how to translate] between MATLAB and Python.<br /> * A good [http://www.springerlink.com/content/978-3-540-73915-9 book for computational and engineering work] - free access from McMaster computers.<br /> * We need a link to [http://docs.python.org/tutorial/ the official Python tutorial] - but it is more general.<br /> * And finally, maybe [http://xkcd.com/353/ this cartoon will convince you]?<br /> |}</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Software_tutorial&diff=1075 Software tutorial 2018-09-15T10:24:22Z <p>Kevindunn: </p> <hr /> <div>This tutorial will expand as we progress in the course. Each section has notes for both MATLAB and Python.<br /> <br /> # [[Software tutorial/About the course software | About the course software]]<br /> # [[Software tutorial/Software installation | Software installation]]<br /> # [[Software tutorial/Getting started|Getting started]]<br /> # [[Software tutorial/My first program|My first program]] (required for [[Tutorial_1_-_2010|tutorial 1]])<br /> # [[Software tutorial/Loops|&lt;tt&gt;for&lt;/tt&gt; loops and &lt;tt&gt;while&lt;/tt&gt; loops]] (required for [[Tutorial_1_-_2010|tutorial 1]])<br /> # [[Software tutorial/Scripts and functions|Scripts and functions]] (useful for [[Tutorial_2_-_2010|tutorial 2]])<br /> # [[Software tutorial/Vectors and arrays|Vectors and arrays]] (used for [[Tutorial_3_-_2010|tutorial 3]])<br /> # [[Software_tutorial/Matrix_operations|Matrix operations]] (used for [[Tutorial_4_-_2010|tutorial 4]])<br /> # [[Software_tutorial/Functions as objects|Functions as objects]] (used for [[Tutorial_5_-_2010|tutorial 5]] to find the zero of a function)<br /> # [[Software_tutorial/Creating and saving plots|Creating and saving plots]] : important for all remaining assignments, and the take-home exam.<br /> # [[Software_tutorial/Integration of ODEs|Integrating ODEs]]: a tutorial on integrating ODEs in MATLAB and Python<br /> # [[Software_tutorial/Least squares modelling (linear regression)|Least squares modelling]]: a tutorial on (linear regression) in MATLAB and Python<br /> # [[Software_tutorial/Loading data in MATLAB or Python| Loading data in MATLAB or Python]] from Excel, CSV and XML files<br /> <br /> * Want tutorials on other topics: please [mailto:kevin.dunn@connectmv.com email me] <br /> == Need some more help? ==<br /> <br /> Not every aspect about MATLAB or Python can be covered with our tutorial (above). Here are some additional resources.<br /> <br /> {| class=&quot;wikitable&quot;<br /> |-<br /> ! MATLAB<br /> ! Python<br /> |-<br /> | width=&quot;50%&quot; valign=&quot;top&quot; |<br /> * [http://modelling3e4.connectmv.com/mediafiles/mediawiki/7/7c/MATLAB_primer.pdf MATLAB primer - a guide to MATLAB] by Dr. Prashant Mhaskar.<br /> * [http://modelling3e4.connectmv.com/mediafiles/mediawiki/8/80/Introduction_to_MATLAB_presentation.pdf Introduction to MATLAB] - a presentation to the 2009 CHE 3E04 class by Elliot Cameron and Eric Rodger<br /> * [http://www.mathworks.com/help/pdf_doc/matlab/getstart.pdf Getting started with MATLAB 7] - a guide from the MathWorks.<br /> * [http://www.math.ufl.edu/help/matlab-tutorial/matlab-tutorial.html Online MATLAB tutorial]: An MATLAB tutorial from the University of Florida.<br /> * [http://www.mathworks.com/help/index.html MathWorks documentation]: The official MATLAB documentation<br /> * [http://www.mathworks.com/matlabcentral/fileexchange/ MATLAB Central File Exchange]: User-created MATLAB code that can be freely used.<br /> | width=&quot;50%&quot; valign=&quot;top&quot; |<br /> <br /> * [http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3 The non-programmer's guide to Python]<br /> * Use the money you saved from not buying MATLAB to buy [http://www.amazon.ca/Learning-Python-Powerful-Object-Oriented-Programming/dp/0596158068 this book on Learning Python], or read [http://books.google.com/books?id=1HxWGezDZcgC&amp;printsec=frontcover&amp;dq=%22Learning+Python%22&amp;source=bl&amp;ots=LiMh3Ka7uW&amp;sig=_z8u8gSW7z3hiE7vKrnX8pA3tNo&amp;hl=en&amp;ei=azWWTODQF8qdnAeyjLXDCA&amp;sa=X&amp;oi=book_result&amp;ct=result&amp;resnum=6&amp;ved=0CDoQ6AEwBQ#v=onepage&amp;q&amp;f=false the Google Books version].<br /> * This [http://diveintopython.org/ excellent book - Dive into Python] is available in print, and on the web.<br /> * Are you comfortable with MATLAB? This [http://www.scipy.org/NumPy_for_Matlab_Users guide to NumPy for MATLAB users] explains how to transition to Python. This [http://mathesaurus.sourceforge.net/matlab-numpy.html page shows how to translate] between MATLAB and Python.<br /> * A good [http://www.springerlink.com/content/978-3-540-73915-9 book for computational and engineering work] - free access from McMaster computers.<br /> * We need a link to [http://docs.python.org/tutorial/ the official Python tutorial] - but it is more general.<br /> * And finally, maybe [http://xkcd.com/353/ this cartoon will convince you]?<br /> |}</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Assignment_6_-_2010_-_Solutions&diff=1074 Assignment 6 - 2010 - Solutions 2018-09-15T10:23:57Z <p>Kevindunn: </p> <hr /> <div>{{OtherSidebar<br /> | due_dates = 01 December 2010<br /> | dates_alt_text = Due date(s)<br /> | questions_PDF = Assignment-6-2010-Solutions.pdf<br /> | questions_text_alt = Assignment questions and solutions<br /> | solutions_PDF = <br /> | solutions_text_alt = Solutions, prepared by Ali, Elliot and Kevin<br /> | other_instructions = Hand-in at class.<br /> }}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> .. |m3| replace:: m\ :sup:3<br /> <br /> .. rubric:: To integrate ODEs - both by hand and with computer software.<br /> <br /> .. rubric:: Solutions by Ali, Elliot and Kevin.<br /> <br /> .. Potential questions:<br /> <br /> Exothermic reaction in a PFR; does temperature exceed 300C over the distance of the pipe? (See reactor design project)<br /> <br /> Epidemic example, p 557 in Friendly book<br /> <br /> Stiff system?<br /> <br /> <br /> Question 1 <br /> ===============<br /> <br /> In assignment 1 you derived the dynamic balance for a waste water treatment system (aerobic heterotrophic growth using Monod kinetics); and in assignment 3 you used Newton's method to find the steady-state solution to the nutrient (:math:N) and biomass (:math:B) balances:<br /> <br /> .. math::<br /> \frac{dN}{dt} &amp;= N_\text{in}(t)\frac{F(t)}{V} - N(t)\frac{F(t)}{V} - \left(\frac{1}{Y_B}\right)\left(\mu_{\text{max}}\frac{N}{K+N(t)}\right)B(t) \\ \frac{dB}{dt} &amp;= - B(t)\frac{F(t)}{V} + \left(\mu_{\text{max}}\frac{N(t)}{K+N(t)}\right)B(t)<br /> <br /> * The tank volume is assumed constant at :math:V = 1600 |m3|<br /> * The conversion efficiency is constant at :math:Y_B=0.80<br /> * The two Monod kinetics parameters are: :math:\mu_\text{max} = 5 \,\,\text{hr}^{-1} and :math:K = 20 g/|m3|<br /> * The inlet flow rate is :math:F(t) = 5000 |m3|/hr<br /> * The nutrient inflow is :math:N_\text{in}(t) = 100 g/|m3|<br /> <br /> These dynamic balances describe how nutrients in the inlet (i.e. the :math:N_\text{in} = 100 g/|m3| of waste in the wastewater) are converted to biomass, B. The stream leaving the well-mixed CSTR should have lower nutrient levels, :math:N(t).<br /> <br /> #. Code these equations in a MATLAB or Python function and use the built-in integrators in either software package to find the steady state solutions when using starting values of :math:N(t=0) = 100 g/|m3| and :math:B(t) = 20 g/|m3| and show that you obtain the same steady state values as found in assignment 3.<br /> <br /> #. Most real processes are never at steady state. In this reactor a (slightly) more realistic input, to account for the diurnal nature of the flow, is: <br /> <br /> .. math::<br /> <br /> F(t) = 5000+900\sin\left(\displaystyle \frac{2\pi}{24}t -\frac{\pi}{5}\right)<br /> <br /> * Plot 100 hours of process operation at these conditions; use the steady state values from part 1 as your initial conditions. <br /> * Environmental limits prohibit outlet nutrient concentration from exceeding 60 g/|m3|. Does this system meet those standards?<br /> <br /> #. Using the simulation from part 2 (with the sinusoidal input) for the biomass and nutrient profiles, see what happens if your plant accidentally dumps nutrient into the inlet stream for 10 hours when an upstream unit fails. Use an increased nutrient influx of :math:N_\text{in}(t) = 150 g/|m3| between :math:55 \leq t \leq 65 hours.<br /> <br /> * Does the plant still manage to treat this accident without violating the 60 g/|m3| outlet limit on :math:N(t)?<br /> * Would it handle this problem had it occurred between :math:75 \leq t \leq 85 hours?<br /> <br /> .. Solution: see code/wastewater.py file<br /> <br /> Solution<br /> ------------<br /> <br /> #. The code simulating the dynamical system is given below. Note that the same code was used to solve all parts of this problem. So, some of the input values or settings may need to change to suit the conditions given in the different parts of this question.<br /> <br /> Solving the dynamical system with the given initial conditions results in the following plots for :math:N(t) and :math:B(t). The steady-state values of :math:N_s=33.33 and :math:B_s=53.33 are seen to match the results obtained in Assignment 3 using nonlinear solution of the steady-state equations.<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-6/images/Q1P1.png<br /> :scale: 50<br /> :align: center<br /> <br /> #. The steady-state values from Part 1 are used as initial conditions, i.e. :math:N(0)=33.33 and :math:B(0)=53.33. The input flow rate is now fluctuating rather than being constant. This can be seen in code shown earlier. The simulation results given the new conditions are plotted below.<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-6/images/Q1P2.png<br /> :scale: 50<br /> :align: center<br /> <br /> As seen from the plot, the outlet nutrient concentration :math:N(t) does not exceed the maximum allowable limit of 60 g/|m3| (It hits this point once though.)<br /> <br /> 3. In case of an upstream failure where the nutrient influx is suddenly increased to :math:N_\text{in}(t) = 150, we can simulate the system by modelling the nutrient influx as a function of time. It is :math:N_\text{in}(t) = 150 if :math:55 \leq t \leq 65 and :math:N_\text{in}(t) = 100 otherwise (see the code that shows the implementation). Re-integrating the dynamic equations results the following plots below.<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-6/images/Q1P3a.png<br /> :scale: 50<br /> :align: center<br /> <br /> It is seen that due to the sudden increase in the nutrient influx at :math:t=55, the nutrient out flow also spikes and exceeds the allowable limit of 60 g/|m3|. However, this situation does not last as the nutrient outlet decreases as a result of its conversion to :math:B.<br /> <br /> If the accident occurred between :math:75 \leq t \leq 85, the plant would behave as follows, which shows that the nutrient outlet would remain within its limit.<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-6/images/Q1P3b.png<br /> :scale: 50<br /> :align: center<br /> <br /> The following code was used in all 3 parts.<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-6/code/wastemodel.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-6/code/wastewater.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> Question 2 <br /> ===============<br /> <br /> .. Reactor system, with recycle loop; disturbance in middle reactor at t = 10; see the disturbance propagate through the system and the recycle loop<br /> <br /> .. Series of CSTR's with reentry into middle CSTR<br /> <br /> Consider three CSTR's in series, operating at constant volume of :math:V = 100 L. A first order reaction, :math:{\sf A \rightarrow B + C} takes place in the system, with reaction rate = :math:r = 0.09 C_{\sf A}. The inlet conditions to the first reactor are constant with time: inlet concentration = :math:C_{{\sf A},0} = 2.7 mol/L at a flow rate of :math:F_\text{in}^{0} = 15.8 L/min. The outlet from the first reactor, :math:C_{{\sf A},1}, is the inlet to the second reactor, and the outlet from the second reactor, :math:C_{{\sf A},2}, is passed to third reactor. The outlet from the third reactor, :math:C_{{\sf A},3}, can be used to calculate the overall conversion = :math:\displaystyle \frac{C_{{\sf A},0} - C_{{\sf A},3}}{C_{{\sf A},0}}.<br /> <br /> The dynamic balances in each reactor are:<br /> <br /> .. math::<br /> <br /> \frac{d C_{{\sf A},1}(t)}{dt} &amp;= \frac{F}{V_1}\bigg(C_{{\sf A},0} - C_{{\sf A},1} \bigg) - kC_{{\sf A},1} \\ \frac{d C_{{\sf A},2}(t)}{dt} &amp;= \frac{F}{V_2}\bigg(C_{{\sf A},1} - C_{{\sf A},2} \bigg) - kC_{{\sf A},2} \\ \frac{d C_{{\sf A},3}(t)}{dt} &amp;= \frac{F}{V_3}\bigg(C_{{\sf A},2} - C_{{\sf A},3} \bigg) - kC_{{\sf A},3} \\<br /> <br /> 1. Describe how you would ordinarily solve the system of equations :math:f({\bf x}) = {\bf 0} where :math:{\bf x} = \big[C_{{\sf A},1}, C_{{\sf A},2}, C_{{\sf A},3} \big] to find the steady-state values of :math:\big[C_{{\sf A},1}, C_{{\sf A},2}, C_{{\sf A},3} \big]. Now use a computer-based ODE solver to integrate the equations to steady state and report the steady-state values of :math:{\bf x} = \big[C_{{\sf A},1}, C_{{\sf A},2}, C_{{\sf A},3} \big] and the overall conversion.<br /> <br /> .. [1.720, 1.095, 0.698], conversion = (2.7 - 0.698)/2.7 = 74.1%<br /> <br /> You are tasked with increasing the conversion of the overall system. You are investigating two possibilities: adding a recycle stream, and operating at larger tank volumes.<br /> <br /> 2. What are the new steady state values of :math:{\bf x} = \big[C_{{\sf A},1}, C_{{\sf A},2}, C_{{\sf A},3} \big] and the overall conversion when :math:V_1 = V_2 = V_3 = 150 L? Why does operating at a larger tank volumes increase conversion? You can answer this question even if you haven't taken the reactor design course yet - *hint*: interpret the above modelling equations.<br /> <br /> .. [1.456, 0.785, 0.423], conversion = (2.7 - 0.423)/2.7 = 84.3%<br /> <br /> 3. Let's take a look at what happens when a disturbance enters the system. Simulate the case when a step change of +1.2 mol/L in :math:C_{{\sf A},0} is made at time :math:t = 50 (i.e. the inlet concentration is 2.7 mol/L for 50 minutes, then increased to 3.9 mol/L for the rest of the time). Choose suitable initial conditions so that your system is roughly at steady state before :math:t=50, continue the simulation on until the new steady state is reached.<br /> <br /> What do you notice about the shape of the concentration *vs* time trajectories from each reactor (superimpose the 3 trajectories on the same plot).<br /> <br /> <br /> Adding a recycle stream from the final reactor to the first or second reactor is an exercise that you can complete on your own. It leads to a counter-intuitive phenomenon: you'd expect an increase in conversion, but ....<br /> <br /> Solution<br /> ---------<br /> <br /> #. Ordinarily one would use Newton's method to solve a system of :math:N non-linear equations in :math:N unknowns. In this case however, the system of equations are linear, so we could use any of the linear algebra methods, such as Gauss reduction, LU decomposition, Jacobi method, *etc*.<br /> <br /> Using the ode solvers in computer software and integrating for a *long time* from any starting point will get us the steady state values of :math:{\bf x} = \big[C_{{\sf A},1}, C_{{\sf A},2}, C_{{\sf A},3} \big] = \big[1.720, 1.096, 0.6982 \big] mol/L and the overall conversion is <br /> <br /> .. math::<br /> <br /> X_\text{overall} = \frac{C_{{\sf A},0} - C_{{\sf A},3}}{C_{{\sf A},0}} = \frac{2.7 - 0.6982}{2.7} = {\bf74.1\%}<br /> <br /> #. Changing the volumes to 150 L in the code and running the integrator to steady state gives :math:\big[C_{{\sf A},1}, C_{{\sf A},2}, C_{{\sf A},3} \big] = \big[ 1.456, 0.785, 0.423\big] mol/L, and an overall conversion of :math:{\bf 84.3\%}.<br /> <br /> Operating at larger tank volumes can be explained as follows. Consider the first tank at steady state and calculate its conversion. We let :math:\tau = \frac{F}{V} for convenience (but it also has an interpretation, called *residence time*, which you will/have learn about in reactor design).<br /> <br /> .. math::<br /> <br /> 0 &amp;= \frac{F}{V}\bigg(C_{{\sf A},0} - C_{{\sf A},1} \bigg) - kC_{{\sf A},1} \\ \left(k+\tau\right)C_{{\sf A},1} &amp;= \tau C_{{\sf A},0} \\ X_1 = \frac{C_{{\sf A},0} - C_{{\sf A},1}}{C_{{\sf A},0}} &amp;= 1 - \frac{C_{{\sf A},1}}{C_{{\sf A},0}} = 1 - \frac{C_{{\sf A},0}}{C_{{\sf A},0}}\cdot \frac{\tau}{k+\tau}\\ X_1 &amp;= \frac{\tau}{k + \tau}<br /> <br /> And you can show for tanks 2 and 3 that the same equation holds:<br /> <br /> .. math::<br /> <br /> X_2 &amp;= \frac{\tau}{k + \tau} \\ X_3 &amp;= \frac{\tau}{k + \tau} <br /> <br /> So the outlet concentration is the product of all conversions:<br /> <br /> .. math::<br /> <br /> C_{{\sf A},3} = X_3 C_{{\sf A},2} = X_3 X_2 C_{{\sf A},1} = X_3 X_2 X_1 C_{{\sf A},0} = \left( \frac{\tau}{k + \tau} \right)^3 C_{{\sf A},0}<br /> <br /> Overall conversion can be increased in any way that decreases :math:C_{{\sf A},3}: <br /> <br /> * increasing the rate constant, :math:k (maybe with a catalyst?)<br /> * making the value of :math:\tau smaller, which implies using a lower inlet flow rate, :math:F or using increased volumes, :math:V.<br /> <br /> However, notice the diminishing returns for the last bullet point above, because the term appears both in the numerator and denominator. This can be seen in the plot here of reactor volume (for each CSTR) against overall conversion:<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-6/images/volume-vs-conversion.png<br /> :scale: 80<br /> :align: center<br /> <br /> #. The trajectories for this disturbance, which enters the first tank, are shown below.<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-6/images/disturbance-propogation.png<br /> :scale: 80<br /> :align: center<br /> <br /> The key point is that the first CSTR response has a sharp, rising edge (discontinuity) as the trajectory rises immediately when the disturbance hits. The second reactor has a slightly smoother response, while the third reactor responds even slower still. This is typical behaviour of all series reactors -- disturbances in early reactors get &quot;washed out&quot; in later reactors.<br /> <br /> <br /> The following Python code was used to solve this question; MATLAB code (not available) would be similar to this.<br /> <br /> .. literalinclude:: ../che3e4/Assignments/Assignment-6/code/series_of_cstrs.py<br /> <br /> Question 3 <br /> ================<br /> <br /> .. note:: This question is adapted from the textbook by Bradie, listed under the Suggested readings &lt;http://modelling3e4.connectmv.com/wiki/Suggested_readings&gt;_ sections of the course website.<br /> <br /> A genetic switch is a biological mechanism to activate whether or not a particular protein product from a cell is synthesized. For example, under certain conditions a pigment protein can be synthesized, which indicates the presence/absence of another chemical (the switch).<br /> <br /> This model has been proposed for a genetic switch:<br /> <br /> .. math::<br /> <br /> \frac{dg}{dt} = 0.01s - 1.51g + 3.03 \frac{g^2}{1+g^2}\qquad g(0) = 0.0<br /> <br /> In this notation, the pigment protein concentration is :math:g(t) and :math:s(t) is the concentration of the chemical (the switch) that activates the gene that produces the pigment.<br /> <br /> The :math:-1.51g term indicates the protein's natural decay rate is proportional to its concentration, while the last term describes the positive feedback the protein exerts on its own formation.<br /> <br /> A switch has two important characteristics:<br /> <br /> * Below a threshold value of :math:s the gene concentration must be close to zero (i.e. little/no pigment produced means the switch is off), while above a threshold, the gene concentration, :math:g, must be high (i.e. the switch has been turned on).<br /> <br /> * Once turned on (i.e. high values of :math:g), the switch must remain on, even if :math:s is returned below the threshold value. Similarly, if the switch was never on to begin with, it must still remain &quot;off&quot; if :math:s is below the threshold. This phenomenon is known as the hysteresis effect.<br /> <br /> <br /> #. First we wish to find the threshold value of :math:s and interpret what :math:s means. Plot and label four trajectories of :math:g(t) using :math:s=20.0, 20.33, 20.67, 21.0 over a time range of 100 minutes, and describe what the plots are showing. <br /> <br /> * Be specific: how would you use these plots to design your genetic switch? <br /> * What does :math:s mean in your design? (Hint: it has two interpretations)<br /> <br /> #. Now we wish to verify if the hysteresis effect is present, because it is important the switch cannot be turned off. For each of the four values of :math:s in part 1, run a simulation where :math:s(t&gt;150)=0 (i.e. at time :math:t=150 the concentration of :math:s is set from its current value to zero). Show your results over a time horizon of 300 minutes and **comment on the results**.<br /> <br /> .. solution: code/genetic_switch.py<br /> <br /> Solution<br /> -----------<br /> <br /> #. Trajectories, at different levels of :math:s, are simulated over a time scale of 100 minutes in the plot. <br /> <br /> <br /> :math:s must be at least greater than 20.0 for the switch to be activated; values at or below 20.0 never activate the switch, while values above this will activate it, though slowly at first (e.g 20.33 takes a long time to show). So one interpretation of :math:s is that it is the threshold level above which we can detect it. The other interpretation is that the higher the value of :math:s, the faster it will be detected by the gene. So :math:s affects both the threshold of the switch, and the time taken to activate it.<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-6/images/Q3P1.png<br /> :scale: 50<br /> :align: center<br /> <br /> #. The hysteresis effect is present. The simulations show that for all four levels of :math:s, that if the switch is activated (:math:s&gt;20.0) that the gene will remain present, even if :math:s is removed.<br /> <br /> We can set an arbitrary threshold on the gene concentration, e.g. 0.8, so that above this level we can consider the switch to be on, and below it the switch is off.<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-6/images/genetic-switch.png<br /> :scale: 50<br /> :align: center<br /> <br /> The following code was used in both parts of the question.<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-6/code/bioswitch.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-6/code/genetic_switch.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> Question 4 <br /> ==============<br /> <br /> .. note:: This question continues on from question 5, assignment 5. It was adapted from a question in the final exam, 2009, worth 3+1 out of 50 marks.<br /> <br /> Consider a well-mixed tank containing species :math:{\sf A} at concentration :math:C_{\sf A}=1 mol/L. There is no inlet or outlet from the tank, i.e. it is a batch system. A fluid of constant density is considered throughout this question. The reactor is filled to 5.2 m\ :sup:3.<br /> <br /> A catalyst is introduced in the tank at time :math:t=0 minutes to initiate the reaction :math:{\sf A} \rightarrow {\sf B} + {\sf C}. Due to quick catalyst deactivation, the dynamic equation describing the evolution of concentration :math:C_{\sf A} in the tank is given by:<br /> <br /> .. math::<br /> <br /> \frac{{\rm d}C_{\sf A}(t)}{{\rm d}t} = - k\,C_{\sf A}(t)\,e^{-\frac{t}{2}}<br /> <br /> with :math:k=1\ \rm min^{-1}.<br /> <br /> #. Determine the concentration of A in the reactor at time :math:t=20 minutes using the classical Runge-Kutta method with step size :math:h=10 minutes.<br /> <br /> #. Compare and discuss your answer with respect to the analytical solution: <br /> <br /> .. math::<br /> <br /> C_{\sf A}(t) = C_{\sf A}(t=0)\ \exp\left[2\,k\left(e^{-\frac{t}{2}}-1\right)\right]<br /> <br /> <br /> Solution<br /> ----------<br /> <br /> .. Solution: see code/catalyst.py<br /> <br /> For notation simplicity, let us denote :math:y=C_{\sf A} and :math:f(t,y)=\displaystyle\frac{{\rm d}y(t)}{{\rm d}t}=- k\,y(t)\,e^{-\frac{t}{2}}. :math:y_0= y(t=0)=1 and the step size :math:h=10.<br /> <br /> Classical 4th-order Runge-Kutta formula:<br /> <br /> .. math::<br /> <br /> y_1 &amp;= y_0 + \frac{1}{6}[K_1 + 2K_2 + 2K_3 + K_4 ] h \\ K_1 &amp;= f(t_0, y_0)= f(0,1) = -1\times 1e^0 = -1\\ K_2 &amp;= f(t_0+\frac{1}{2}h,y_0 + \frac{1}{2}hK_1)=f(5,-4)=0.328\\ K_3 &amp;= f(t_0+\frac{1}{2}h,y_0 + \frac{1}{2}hK_2)=f(5,2.642)=-0.217\\ K_4 &amp;= f(t_0+h, y_0 + hK_3)=f(10,-1.17)=0.008\\ y_1 &amp;= 1 + \frac{1}{6}\bigg[-1 + 2\times 0.328 + 2\times -0.217 +0.008 \bigg] \times 10={\bf -0.283}<br /> <br /> Does the result make sense? We ended up a negative concentration! Let us double-check with the analytical solution. The actual value of :math:y(t=10) using the analytical solution is 0.1372 [mol/L]. So why did we get a terribly wrong answer using the Runge-Kutta? The answer lies in the stability of ODE methods. Some ODE methods have certain stability restrictions that impose a maximum length on the integration step size. Here we chose :math:h=10 which is too large for our system, thereby leading to an *unstable solution*. A workaround is to choose a very smaller step size and reach :math:t=10 in multi-steps instead. This is not requested in this question though. So, let us continue with :math:h=10 and see what we get as :math:y(t_2=20) using the obtained :math:y(t_1=10)<br /> <br /> .. math::<br /> <br /> y_2 &amp;= y_1 + \frac{1}{6}[K_1 + 2K_2 + 2K_3 + K_4 ] h \\ K_1 &amp;=f(t_1, y_1)=f(10,-0.283)= 0.0019 \\ K_2 &amp;=f(t_1+\frac{1}{2}h, y_1 + \frac{1}{2}hK_1)=f(15,-0.2735)= 1.5127\times 10^{-4}\\ K_3 &amp;=f(t_1+\frac{1}{2}h, y_1 + \frac{1}{2}hK_2)=f(15,-0.2822)= 1.5608\times 10^{-4}\\ K_4 &amp;=f(t_1+h, y_1 + hK_3)=f(20,-0.2814)= 1.2776\times 10^{-5} \\ y_2 &amp;= -0.283 + \frac{1}{6}\bigg[0.0019 + 2\times 1.5127\times 10^{-4} + 2\times 1.5608\times 10^{-4} + 1.2776\times 10^{-5} \bigg] \times 10= {\bf -0.2788}<br /> <br /> The actual value of :math:X(t=20) using the analytical solution is 0.1353 [mol/L] (compare to the above approximate solution!)<br /> <br /> <br /> Question 5 <br /> ==============<br /> <br /> .. From Tutorial 9, 2008<br /> <br /> Consider the CSTR shown below, where an irreversible reaction of the form :math:{\sf A}\rightarrow {\sf B} takes place.<br /> <br /> .. figure:: ../che3e4/Assignments/Assignment-6/images/CSTR_1input.png<br /> :scale: 68<br /> :align: center<br /> <br /> **Model parameters**<br /> <br /> * :math:C_{\sf A}^{\rm in} = 0.79 \rm kmol\ m^{-3}<br /> * :math:T^{\rm in} = 352.6 \rm K<br /> * :math:F = 0.2 \rm m^3\ min^{-1} <br /> * :math:V = 0.1 \rm m^3 <br /> * :math:k_{0} = 7.2\times 10^{10} \rm min^{-1} <br /> * :math:E = 8.314\times 10^{4} \rm kJ\ kmol{-1} <br /> * :math:\Delta H = -4.78\times 10^4 \rm kJ\ kmol^{-1} <br /> * :math:C_{p} = 0.239 \rm kJ\ kg^{-1}\ K^{-1} <br /> * :math:\rho = 1000 \rm kg\ m^{-3} <br /> * :math:R = 8.314 \rm kJ\ kmol^{-1}\ K^{-1} <br /> <br /> #. Derive, by hand, the differential equations describing the evolution of the concentration of species :math:{\sf A}, :math:C_{\sf A}(t), as well as of the temperature of the outlet stream, :math:T(t), based on the following assumptions:<br /> <br /> * The concentrations of :math:{\sf A} in the inlet stream is :math:C_{\sf A}^{\rm in}, the inlet and outlet volumetric flow rates are constant and both equal to :math:F, and the temperature of the inlet stream is :math:T^{\rm in}.<br /> * The reaction has first-order kinetics, with the dependence of the reaction rate :math:r (moles of :math:{\sf A} consumed per unit volume and per unit time) on the temperature given by :math:r=k_0\, e^{-\frac{E}{RT}}\, C_{\sf A}.<br /> <br /> where :math:E is the activation energy; :math:R, the universal gas constant; :math:T, the temperature at which the reaction is taking place; and :math:k_0, a constant.<br /> <br /> * The heat of reaction is :math:\Delta H and no heat is lost or added to the reactor, other than through the liquid streams.<br /> <br /> * The density and specific heat capacity of the fluid are :math:\rho and :math:C_p respectively.<br /> <br /> #. For the values of the process parameters listed in the table and the following initial conditions :math:C_{\sf A}(0) = 0.7\ \rm kmol/m^3 and :math:T(0)=355\ \rm K, compute, by hand, the value of :math:\big[C_{\sf A}(t),\; T(t)\big] at :math:t=0.2, using:<br /> <br /> * Euler's method with :math:h=0.1, and<br /> * Heun's predictor-corrector method with :math:h = 0.1, and<br /> * the fourth-order Runge-Kutta method with :math:h=0.2<br /> <br /> #. Implement these equations in computer software and compare the integration by hand with the computer version at :math:t=0.2.<br /> <br /> Solution<br /> --------<br /> <br /> .. Solution: see code/cstr_integration.py<br /> <br /> <br /> #. First note that the rate of consumption of component :math:{\sf A} is :math:-r_{\sf A} = kC_{\sf A} = k_0 \exp\left(-\frac{E}{RT}\right) C_{\sf A}.<br /> <br /> A mole balance on :math:{\sf A} can be done to determine the evolution of the concentration of species :math:{\sf A}:<br /> <br /> .. math::<br /> <br /> \dfrac{d V C_{\sf A}}{dt} &amp;= F C_{\sf A}^{\rm in} - F C_{\sf A} - r_{\sf A} V \\ V\dfrac{d C_{\sf A}}{dt} &amp;= F C_{\sf A}^{\rm in} - F C_{\sf A} - k_0 \exp\left(-\frac{E}{RT}\right) C_{\sf A} V \\<br /> <br /> On the other hand, an energy balance on the reactor can be done to determine the evolution of the temperature in the reactor:<br /> <br /> .. math::<br /> <br /> \dfrac{d (\rho V C_p (T-T_{\rm ref}))}{dt} &amp;= \rho C_p F (T_{\rm in} - T_{\rm ref}) - \rho C_p F (T - T_{\rm ref}) + (-\Delta H ) r_{\sf A} V \\ \rho V C_p\dfrac{d T}{dt} &amp;= \rho C_p F (T_{\rm in} - T) - \Delta H k_0 \exp\left(-\frac{E}{RT}\right) C_{\sf A} V<br /> <br /> <br /> #. Substituting the values from the table in the dynamic balances gives:<br /> <br /> .. math::<br /> <br /> \frac{dy_1}{dt} =&amp; = f_1(t, {\bf y}) = 1.58 - 2y_1 - 7.2\times 10^{10} \; e^{-\frac{10^4}{y_2}} y_1\\ \frac{dy_2}{dt} =&amp; = f_2(t, {\bf y}) = 705.2 - 2y_2 + 1.44 \times 10^{13} \; e^{-\frac{10^4}{y_2}} y_1,<br /> <br /> with :math:y_1(t)=C_{\sf A}(t)$and$y_2(t)=T(t).<br /> <br /> * **Explicit Euler method**<br /> <br /> From :math:t = 0 to 0.1<br /> <br /> At :math:t=0, :math:\boldsymbol{\phi}={\bf f}(0,{\bf y}^{(0)}) = \begin{bmatrix} 0.1506 \\ 1.0859 \end{bmatrix}. The estimated solution at :math:t=0.1 is:<br /> <br /> .. math::<br /> <br /> {\bf y}^{(1)} &amp;= {\bf y}^{(0)} + h \boldsymbol{\phi} = \begin{bmatrix} 0.7 \\ 355 \end{bmatrix} + 0.1 \begin{bmatrix} 0.1506 \\ 1.0859 \end{bmatrix} = \begin{bmatrix} 0.7151 \\ 355.1086 \end{bmatrix}<br /> <br /> From :math:t = 0.1 to 0.2:<br /> <br /> At :math:t=0.1, :math:\boldsymbol{\phi}={\bf f}(0.1,{\bf y}^{(1)}) = \begin{bmatrix} 0.1196 \\ 1.0473 \end{bmatrix}. The estimated solution at :math:t=0.2 is:<br /> <br /> .. math::<br /> <br /> {\bf y}^{(2)} &amp;= {\bf y}^{(1)} + h \boldsymbol{\phi} \\ &amp;= \begin{bmatrix} 0.727 \\ 355.2 \end{bmatrix}<br /> <br /> Overall, we thus obtain the following solution:<br /> <br /> :math:C_A(0.2) = 0.727\ \rm kmol/m^{3} \quad\text{and}\quad T(0.2) = 355.2\ \rm K<br /> <br /> * **Heun's predictor-corrector method**<br /> <br /> From :math:t = 0 to 0.1<br /> <br /> The first step of Heun's method (predictor) uses the Euler slope, calculated above. The corrector step calculates the slope at :math:t=0.1 and the Heun slope is just the average of the two.<br /> <br /> .. math::<br /> \hat{\bf y}^{(1)} &amp;= \begin{bmatrix} 0.7151 \\ 355.1086 \end{bmatrix} \\ \boldsymbol{\phi}_\text{Heun} &amp;= 0.5 \begin{bmatrix} 0.1506 \\ 1.0859 \end{bmatrix} + 0.5 {\bf f}(0.1, \hat{\bf y}^{(1)}) \\ &amp;= 0.5 \begin{bmatrix} 0.1506 \\ 1.0859 \end{bmatrix} + 0.5 \begin{bmatrix} 0.1196 \\ 1.0473 \end{bmatrix}\\ &amp;= \begin{bmatrix} 0.1351 \\ 1.0667 \end{bmatrix} \\ {\bf y}^{(1)} &amp;= {\bf y}^{(0)} + h \boldsymbol{\phi}_\text{Heun} = \begin{bmatrix} 0.7 \\ 355 \end{bmatrix} + 0.1 \begin{bmatrix} 0.1351 \\ 1.0667 \end{bmatrix} = \begin{bmatrix} 0.7135 \\ 355.107 \end{bmatrix}<br /> <br /> From :math:t = 0.1 to 0.2:<br /> <br /> .. math::<br /> \hat{\bf y}^{(2)} &amp;= \begin{bmatrix} 0.7135 \\ 355.107 \end{bmatrix} + 0.1\begin{bmatrix} 0.1227 \\ 1.0365 \end{bmatrix} \\ \hat{\bf y}^{(2)} &amp;= \begin{bmatrix} 0.72577 \\ 355.210 \end{bmatrix} \\ \boldsymbol{\phi}_\text{Heun} &amp;= 0.5 \begin{bmatrix} 0.1227 \\ 1.0365 \end{bmatrix} + 0.5 {\bf f}(0.2, \hat{\bf y}^{(2)}) \\ &amp;= 0.5 \begin{bmatrix} 0.1227 \\ 1.0365 \end{bmatrix} + 0.5 \begin{bmatrix} 0.097435 \\0.9851 \end{bmatrix}\\ &amp;= \begin{bmatrix} 0.1101 \\ 1.0108 \end{bmatrix} \\ {\bf y}^{(2)} &amp;= {\bf y}^{(1)} + h \boldsymbol{\phi}_\text{Heun} = \begin{bmatrix} 0.7135 \\ 355.107 \end{bmatrix} + 0.1 \begin{bmatrix} 0.1101 \\ 1.0108 \end{bmatrix} = \begin{bmatrix} 0.7245 \\ 355.21 \end{bmatrix}<br /> <br /> Overall, we thus obtain the following solution:<br /> <br /> :math:C_A(0.2) = 0.7245 \ \rm kmol/m^{3} \quad\text{and}\quad T(0.2) = 355.21 \ \rm K<br /> <br /> * **4th-order Runge-Kutta method (known as the classical Runge-Kutta method)**<br /> <br /> From :math:t = 0 to 0.2:<br /> <br /> At :math:t=0, :math:{\bf y}^{(0)} = \begin{bmatrix} 0.7 \\ 355 \end{bmatrix}. The intermediate quantities :math:{\bf K}_1,\ldots,{\bf K}_4 are now 2-dimensional vectors:<br /> <br /> <br /> .. math::<br /> <br /> {\bf K}_1 &amp;= \begin{bmatrix} 0.1506 \\ 1.0859 \end{bmatrix} \\ {\bf K}_2 &amp;= \begin{bmatrix} 0.1196 \\ 1.0473 \end{bmatrix} \\ {\bf K}_3 &amp;= \begin{bmatrix} 0.1259 \\ 1.0269 \end{bmatrix} \\ {\bf K}_4 &amp;= \begin{bmatrix} 0.0986 \\ 0.9870 \end{bmatrix} <br /> <br /> \boldsymbol{\phi} &amp;= \dfrac{1}{6} {\bf K}_1 + \dfrac{1}{3} {\bf K}_2 +\dfrac{1}{3} {\bf K}_3 + \dfrac{1}{6} {\bf K}_4 = \begin{bmatrix} 0.1234 \\ 1.0369 \end{bmatrix}<br /> <br /> The estimated solution at :math:t=0.2 is:<br /> <br /> :math:{\bf y}^{(1)} = {\bf y}^{(0)} + h \boldsymbol{\phi} = \begin{bmatrix} 0.7247 \\ 355.2074 \end{bmatrix}<br /> <br /> Overall, we thus obtain the following solution:<br /> <br /> :math:C_A(0.2) = 0.725\ \rm kmol/m^{3} \quad\text{and}\quad T(0.2) = 355.2\ \rm K, which is in good agreement with the solution found earlier with the explicit Euler method and Heun's method. <br /> <br /> #. Integrating the ODE equations with MATLAB's ode45 function, or Python's integrate function gave the following results at :math:t=0.2: :math:{\bf y} = \left[0.72468, 355.2073\right].<br /> <br /> :math:C_A(0.2) = 0.725\ \rm kmol/m^{3} \quad\text{and}\quad T(0.2) = 355.2\ \rm K, which agrees very well with the Runge-Kutta solution, and less well with the Heun and Euler methods.<br /> <br /> .. raw:: latex<br /> <br /> \vspace{0.25cm}<br /> \hrule<br /> \begin{center}END\end{center}<br /> &lt;/rst&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Modelling_and_scientific_computing&diff=1073 Modelling and scientific computing 2018-08-15T15:39:30Z <p>Kevindunn: </p> <hr /> <div>== Process modelling slides ==<br /> <br /> Please [[Media:A-Modelling-20-Sept-2010.pdf|download the lecture slides]]. 13 September 2010 (slides 1 to 8)&lt;br&gt;15 September 2010 (slides 9 to 15)&lt;br&gt;16 September 2010 (slides 16 to 19)&lt;br&gt;20 September 2010 (slides 20 to the end)<br /> <br /> == Approximation and computer representation ==<br /> <br /> Please [[Media:A-Approximation-and-roundoff-23-Sept-2010.pdf|download the lecture slides]]. 22 and 23 September (updated)<br /> <br /> * More about [http://www.around.com/ariane.html the Ariane 5 rocket] 16-bit overflow<br /> * Python code used in class for ...<br /> {| class=&quot;wikitable&quot;<br /> |-<br /> ! calculating relative error<br /> ! working with integers<br /> |-<br /> | width=&quot;50%&quot; valign=&quot;top&quot; class=&quot;mainpage_hubbox&quot;|<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> y = 13.0<br /> n = 3 # number of significant figures<br /> rel_error = 0.5 * 10 ** (2-n) # relative error calculation<br /> x = y / 2.0<br /> x_prev = 0.0<br /> iter = 0<br /> while abs(x - x_prev)/x &gt; rel_error:<br /> x_prev = x<br /> x = (x + y/x) / 2.0<br /> print(abs(x - x_prev)/x)<br /> iter += 1<br /> <br /> print('Used %d iterations to calculate sqrt(%f) = %.20f; '<br /> 'true value = %.20f\n ' % (iter, y, x, np.sqrt(y)))<br /> &lt;/syntaxhighlight&gt;<br /> | width=&quot;50%&quot; valign=&quot;top&quot; class=&quot;mainpage_hubbox&quot;|<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> <br /> print(np.int16(32767))<br /> print(np.int16(32767+1))<br /> print(np.int16(32767+2))<br /> <br /> # Smallest and largest 16-bit integer<br /> print(np.iinfo(np.int16).min, np.iinfo(np.int16).max)<br /> <br /> # Smallest and largest 32-bit integer<br /> print(np.iinfo(np.int32).min, np.iinfo(np.int32).max)<br /> &lt;/syntaxhighlight&gt;<br /> |-<br /> ! working with floats<br /> ! special numbers<br /> |-<br /> | width=&quot;50%&quot; valign=&quot;top&quot; class=&quot;mainpage_hubbox&quot;|<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> <br /> help(np.finfo) # Read what the np.finfo function does<br /> <br /> f = np.float32 # single precision, 32-bit float, 4 bytes<br /> f = np.float64 # double precision, 64-bit float, 8 bytes<br /> <br /> print('machine precision = eps = %.10g' % np.finfo(f).eps)<br /> print('number of exponent bits = %.10g' % np.finfo(f).iexp)<br /> print('number of significand bits = %.10g' % np.finfo(f).nmant)<br /> print('smallest floating point value = %.10g' % np.finfo(f).min)<br /> print('largest floating point value = %.10g' % np.finfo(f).max)<br /> <br /> # Approximate number of decimal digits to which this kind<br /> # of float is precise.<br /> print('decimal precision = %.10g' % np.finfo(f).precision) <br /> &lt;/syntaxhighlight&gt;<br /> |<br /> &lt;syntaxhighlight lang=&quot;python&quot;&gt;<br /> import numpy as np<br /> <br /> # Infinities<br /> print(np.inf, -np.inf)<br /> print(np.float(1E400)) # inf, number exceeds maximum value that<br /> # is possible with a 64-bit float: overflow<br /> print(np.inf * -4.0) # -inf<br /> print(np.divide(2.4, 0.0)) # inf<br /> <br /> # NaN's<br /> a = np.float(-2.3)<br /> print(np.sqrt(a)) # nan<br /> print(np.log(a)) # nan<br /> <br /> # Negative zeros<br /> a = np.float(0.0)<br /> b = np.float(-4.0)<br /> c = a/b<br /> print(c) # -0.0<br /> print(c * c) # 0.0<br /> <br /> eps = np.finfo(np.float).eps<br /> e = eps/3.0 # create a number smaller than machine precision<br /> # Question: why can we create a number smaller than eps?<br /> print(e)<br /> <br /> # Interesting property: non-commutative operations can occur<br /> # when working with values smaller than eps. Why?<br /> # The print out here should &quot;True&quot;, but it prints &quot;False&quot;<br /> print((1.0 + (e + e)) == (1.0 + e + e)) <br /> &lt;/syntaxhighlight&gt;<br /> |}<br /> <br /> ==Practice questions==<br /> <br /> &lt;ol&gt;<br /> &lt;li&gt;From the [[Suggested readings | Hangos and Cameron]] reference, ([http://macc.mcmaster.ca/sites/default/files/theses/Hangos_Cameron_2001_pp1-161.pdf available here]] - accessible from McMaster computers only)<br /> <br /> * Work through example 2.4.1 on page 33<br /> * Exercise A 2.1 and A 2.2 on page 37<br /> * Exercise A 2.4: which controlling mechanisms would you consider?<br /> <br /> &lt;li&gt;Homework problem, similar to the case presented on slide 18, except<br /> * Use two inlet streams $$\sf F_1$$ and $$\sf F_2$$, and assume they are volumetric flow rates<br /> * An irreversible reaction occurs, $$\sf A + 3B \stackrel{r}{\rightarrow} 2C$$<br /> * The reaction rate for A = $$\sf -r_A = -kC_\text{A} C_\text{B}^3$$<br /> # Derive the time-varying component mass balance for species B.<br /> #* $$V\frac{dC_B}{dt} = F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} C_{\sf B} + 0 - 3 kC_{\sf A} C_{\sf B}^3 V$$<br /> # What is the steady state value of $$\sf C_B$$? Can it be calculated without knowing the steady state value of $$\sf C_A$$?<br /> #* $$F^{\rm in}_1 C^{\rm in}_{\sf B,1} + F^{\rm in}_2 C^{\rm in}_{\sf B,2} - F^{\rm out} \overline{C}_{\sf B} - 3 k \overline{C}_{\sf A} \overline{C}^3_{\sf B} V$$ - we require the steady state value of $$C_{\sf A}$$, denoted as $$\overline{C}_{\sf A}$$, to calculate $$\overline{C}_{\sf B}$$.<br /> &lt;/ol&gt;<br /> <br /> [[Practice questions | More exercises available here]]</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=User:Kevindunn&diff=1072 User:Kevindunn 2017-02-10T07:46:57Z <p>Kevindunn: Replaced content with &quot;&lt;html&gt; &lt;div class=&quot;math&quot;&gt; $\begin{split}C^1= \left[ \begin{array}{ccccc} 1 &amp;amp; 1 &amp;amp; 1 &amp;amp; 1 &amp;amp; 0\\ 0 &amp;amp; -3 &amp;amp; -3 &amp;amp; -3 &amp;amp;...&quot;</p> <hr /> <div>&lt;html&gt;<br /> <br /> &lt;div class=&quot;math&quot;&gt;<br /> \[\begin{split}C^1=<br /> \left[ \begin{array}{ccccc}<br /> 1 &amp;amp; 1 &amp;amp; 1 &amp;amp; 1 &amp;amp; 0\\<br /> 0 &amp;amp; -3 &amp;amp; -3 &amp;amp; -3 &amp;amp; 4\\<br /> 0 &amp;amp; 3 &amp;amp; -5 &amp;amp; 0 &amp;amp; 2\\<br /> 0 &amp;amp; -6 &amp;amp; -6 &amp;amp; -4 &amp;amp; -4 \end{array} \right]\end{split}$&lt;/div&gt;<br /> &lt;script type='text/javascript' src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML-full,local/mwMathJaxConfig'&gt; &lt;/script&gt;<br /> &lt;/html&gt;</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Assignment_2_-_2010_-_Solution/Bonus_question&diff=1071 Assignment 2 - 2010 - Solution/Bonus question 2017-02-10T07:44:18Z <p>Kevindunn: </p> <hr /> <div>{{Navigation|Book=Assignment 2 - 2010 - Solution|previous=Question 5|current=Back to all questions|next=}}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> Bonus question <br /> ===================<br /> <br /> Use the pseudo code developed in the course notes to write a MATLAB or Python function that implements Gauss elimination, without pivoting. The function should take :math:A and :math:b as inputs, and return vector :math:x.<br /> <br /> Use your function to solve this set of equations from question 1, and print the update :math:A matrix and :math:b vector after every pivot step.<br /> <br /> <br /> Solution<br /> -----------<br /> <br /> A function that implements the Gauss elimination without pivoting is provided below.<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-2/code/guass.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-2/code/guass.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> Now we use this function to solve the system of equations in Question 1. The commented lines in the MATLAB code show how the function is used. The results are: :math:x =[1.3333, 3.1667,1.5000,-6.0000].<br /> Also, the :math:A and :math:b (denoted as :math:C=[A\quad b] below) are updated as:<br /> <br /> .. math::<br /> C^1=<br /> \left[ \begin{array}{ccccc}<br /> 1 &amp; 1 &amp; 1 &amp; 1 &amp; 0\\<br /> 0 &amp; -3 &amp; -3 &amp; -3 &amp; 4\\<br /> 0 &amp; 3 &amp; -5 &amp; 0 &amp; 2\\<br /> 0 &amp; -6 &amp; -6 &amp; -4 &amp; -4 \end{array} \right]<br /> <br /> .. math::<br /> C^2=<br /> \left[ \begin{array}{ccccc}<br /> 1 &amp; 1 &amp; 1 &amp; 1 &amp; 0\\<br /> 0 &amp; -3 &amp; -3 &amp; -3 &amp; 4\\<br /> 0 &amp; 0 &amp; -8 &amp; -3 &amp; 6\\<br /> 0 &amp; 0 &amp; 0 &amp; 2 &amp; -12<br /> \end{array} \right]<br /> <br /> .. math::<br /> C^3=<br /> \left[ \begin{array}{ccccc}<br /> 1 &amp; 1 &amp; 1 &amp; 1 &amp; 0\\<br /> 0 &amp; -3 &amp; -3 &amp; -3 &amp; 4\\<br /> 0 &amp; 0 &amp; -8 &amp; -3 &amp; 6\\<br /> 0 &amp; 0 &amp; 0 &amp; 2 &amp; -12<br /> \end{array} \right]<br /> <br /> The final result for :math:x = [1.333, 3.167, 1.5, -6]. <br /> <br /> &lt;/rst&gt;<br /> {{Navigation|Book=Assignment 2 - 2010 - Solution|previous=Question 5|current=Back to all questions|next=}}</div> Kevindunn https://learnche.org/wiki_3E4/index.php?title=Assignment_2_-_2010_-_Solution/Bonus_question&diff=1070 Assignment 2 - 2010 - Solution/Bonus question 2017-02-10T07:43:44Z <p>Kevindunn: </p> <hr /> <div>{{Navigation|Book=Assignment 2 - 2010 - Solution|previous=Question 5|current=Back to all questions|next=}}<br /> <br /> &lt;rst&gt;<br /> &lt;rst-options: 'toc' = False/&gt;<br /> &lt;rst-options: 'reset-figures' = False/&gt;<br /> Bonus question <br /> ===================<br /> <br /> Use the pseudo code developed in the course notes to write a MATLAB or Python function that implements Gauss elimination, without pivoting. The function should take :math:A and :math:b as inputs, and return vector :math:x.<br /> <br /> Use your function to solve this set of equations from question 1, and print the update :math:A matrix and :math:b vector after every pivot step.<br /> <br /> <br /> Solution<br /> -----------<br /> <br /> A function that implements the Gauss elimination without pivoting is provided below.<br /> <br /> .. twocolumncode::<br /> :code1: ../che3e4/Assignments/Assignment-2/code/guass.m<br /> :language1: matlab<br /> :header1: MATLAB code<br /> :code2: ../che3e4/Assignments/Assignment-2/code/guass.py<br /> :language2: python<br /> :header2: Python code<br /> <br /> Now we use this function to solve the system of equations in Question 1. The commented lines in the MATLAB code show how the function is used. The results are: :math:x =[1.3333, 3.1667,1.5000,-6.0000].<br /> Also, the :math:A and :math:b (denoted as :math:C=[A\quad b] below) are updated as:<br /> <br /> .. math::<br /> C^1=<br /> \left[ \begin{array}{ccccc}<br /> 1 &amp; 1 &amp; 1 &amp; 1 &amp; 0\\<br /> 0 &amp; -3 &amp; -3 &amp; -3 &amp; 4\\<br /> 0 &amp; 3 &amp; -5 &amp; 0 &amp; 2\\<br /> 0 &amp; -6 &amp; -6 &amp; -4 &amp; -4 \end{array} \right]<br /> <br /> .. math::<br /> C^2=<br /> \left[ \begin{array}{ccccc}<br /> 1 &amp; 1 &amp; 1 &amp; 1 &amp; 0\\<br /> 0 &amp; -3 &amp; -3 &amp; -3 &amp; 4\\<br /> 0 &amp; 0 &amp; -8 &amp; -3 &amp; 6\\<br /> 0 &amp; 0 &amp; 0 &amp; 2 &amp; -12<br /> \end{array} \right]<br /> <br /> .. math::<br /> C^3=<br /> \left[ \begin{array}{ccccc}<br /> 1 &amp; 1 &amp; 1 &amp; 1 &amp; 0\\<br /> 0 &amp; -3 &amp; -3 &amp; -3 &amp; 4\\<br /> 0 &amp; 0 &amp; -8 &amp; -3 &amp; 6\\<br /> 0 &amp; 0 &amp; 0 &amp; 2 &amp; -12<br /> \end{array} \right]<br /> <br /> The final result for :math:x = [1.333, 3.167, 1.5, -6]. <br /> <br /> &lt;/rst&gt;<br /> {{Navigation|Book=Assignment 2 - 2010 - Solution|previous=Question 5|current=Back to all questions|next=}}</div> Kevindunn