Tutorial 5 - 2010 - Solution/Bonus question

From Process Model Formulation and Solution: 3E4
Jump to navigation Jump to search
← Question 4 (previous step) Back to all questions

<rst> <rst-options: 'toc' = False/> <rst-options: 'reset-figures' = False/>

Bonus question [1]

=======

A naive code for the bisection algorithm would evaluate the function :math:`f(x)` at the three points, :math:`[x_\ell, x_m, x_u]` in every iteration. Fewer function evaluations can be obtained though.

Write a function, in either MATLAB or Python, that implements the bisection method, that evaluates :math:`f(x)` as few times as possible. You should report the following 8 outputs in each iteration: :math:`[x_\ell^{(k)},\, x_m^{(k)},\, x_u^{(k)},\, f(x_\ell^{(k)}),\, f(x_m^{(k)}),\, f(x_u^{(k)}),\, \varepsilon_\text{rel,x}^{(k)},\, \varepsilon_\text{rel,f}^{(k)}]`.

Use this code to find the solution to the above problem, within a tolerance of :math:`\sqrt{\text{eps}}` based on :math:`\varepsilon_\text{rel,x}^{(k)}`.

Solution


.. twocolumncode:: :code1: ../che3e4/Tutorials/Tutorial-5/code/Bisection.m :language1: matlab :header1: MATLAB code :code2: ../che3e4/Tutorials/Tutorial-5/code/bisection.py :language2: python :header2: Python code


You should run the MATLAB code as follows:

>> func = @(T)(-24097-(0.26.*T)+(0.00169.*T.^2)+(150000./T)-(-23505)) >> root = Bisection(200,400,func,1000,sqrt(eps))

or just run the Python code as shown above.


.. raw:: latex

\vspace{0.25cm} \hrule \begin{center}END\end{center}

</rst>