### MATLAB Code Examples

1 2 3 4 5 6 7 8 9 10 11 12 13 |
%Truncation example clear all; format long x=3.56e6; y=2.2800e6; a=single(x); b=single(y); z=x*y; c=a*b; fprintf('\nz = %2.8e c= %2.8e',z,c) format short z c |

### Python Code Examples

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# -*- coding: utf-8 -*- """ Created on Thu Dec 15 13:52:27 2016 Truncation example """ import numpy as np x=3.56e15 y=2.2800e22 a=np.float32(x) b=np.float32(y) z=x*y c=a*b print ("z= : %2.8e, c= : %2.8e" % (z,c)) print (z,c) |

### Exercises

- Download the file below and run the glitches. m or glitches.py program file from the book website. Run the code and observe what happens when the order of operations change. Describe the results. If you were doing one of these calculations in a program and wanted the program to branch when the result of the calculation is zero, how would compensate for these truncation errors? Add that code to the program.
- Write a program to simulate the exponential growth model described earlier in this chapter. The initial population is 50 and the growth rate is 0.05. Create the model using the Euler method with a time increment of one and 100 iterations. Calculate the analytical solution to the equation in the same program. Graph both distributions and calculate the root mean squared error for the simulation.
- Use one of the ode solvers in MATLAB or Python to generate a solution to the exponential growth problem above. Compare that result to the analytical solution and calculate the root mean squared error for that comparison.
- You are charged with validating a model of the level of dissolved oxygen (DO) in a river. The oxygen level must remain about 5 parts per million (ppm) in order to maintain a healthy environment. You know that the level of DO can physically go from 0.0 to 14.6 ppm. The model simulates the impact of a sewage treatment plant discharge. As it flows downstream, the organic waste from the plant lowers the DO level as bacteria use oxygen to decompose the waste. Download the file DO_verify from the book website. The file has a series of model runs using different assumptions about the amount of organic waste being released and the efficiency of the environment in adding oxygen back to the stream. Verify the model using these data. Describe any model errors you can find and indicate what you would look for and/or add to the model to fix those problems.