Programming Tips: While Loops and Comparisons 1Daniel Baur / Numerical Methods for Chemical...

4
Programming Tips: While Loops and Comparisons 1 Daniel Baur / Numerical Methods for Chemical Engineerse Daniel Baur ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften ETH Hönggerberg / HCI F128 – Zürich E-Mail: [email protected] http://www.morbidelli-group.ethz.ch/education/index

Transcript of Programming Tips: While Loops and Comparisons 1Daniel Baur / Numerical Methods for Chemical...

Page 1: Programming Tips: While Loops and Comparisons 1Daniel Baur / Numerical Methods for Chemical Engineerse Daniel Baur ETH Zurich, Institut für Chemie- und.

1Daniel Baur / Numerical Methods for Chemical Engineerse

Programming Tips:While Loops and Comparisons

Daniel Baur

ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften

ETH Hönggerberg / HCI F128 – Zürich

E-Mail: [email protected]

http://www.morbidelli-group.ethz.ch/education/index

Page 2: Programming Tips: While Loops and Comparisons 1Daniel Baur / Numerical Methods for Chemical Engineerse Daniel Baur ETH Zurich, Institut für Chemie- und.

2Daniel Baur / Numerical Methods for Chemical Engineerse

While Loops with limited Iterations

In order to keep a while loop from iterating forever, use one of the following two schemes:

Page 3: Programming Tips: While Loops and Comparisons 1Daniel Baur / Numerical Methods for Chemical Engineerse Daniel Baur ETH Zurich, Institut für Chemie- und.

3Daniel Baur / Numerical Methods for Chemical Engineerse

Comparing Entire Vectors and Matrices The easiest way to compare two vectors or matrices is to

do it directly:

Page 4: Programming Tips: While Loops and Comparisons 1Daniel Baur / Numerical Methods for Chemical Engineerse Daniel Baur ETH Zurich, Institut für Chemie- und.

4Daniel Baur / Numerical Methods for Chemical Engineerse

Addendum: Implicit Trapezoid Method

If the Jacobian matrix is constant, there is no need to solve linear systems in the iteration equations:

Where A can be calculated once using A = (I – h/2*J) \ (I + h/2*J);

1

1

1

1

2 2

2 2

n n

n n

n n

h hy y

h hy y

y y

I J I J

I J I J

A