# 1. Objectives

• To show different ways the result of a computer simulation can be checked

# 2. Motivation

1. Every science model is an approximation of reality.
2. Every mathematical representation of a science model is an approximation of the science model.
3. Every computer representation of a mathematical model is an approximation.

It is important to understand how good the computer representation of a mathematical model is.

# 3. Priming questions

• Why do you think that some of your homework assignments require that you work out answers to your programs on paper first?
• How can you prove that a calculator works properly?

# 4. Notes

## 4.1. Definitions

There are two basic stages in testing computer simulations

• Veriﬁcation - Are you solving the equations correctly?
• Validation - Are you are solving the correct equations?

## 4.2. Verification Approaches

1. Compute a few steps by hand and compare to computer results
2. Compare to analytic solution (if available)
3. Implement mathematical model as a computational model in a different way and compare results

## 4.3. Ex. I: Approach 1.

Compute a few steps by hand and compare to computer results

Plug in numbers for mathematical model and see if computer model gives the same result

• Science model
I gain one pound every year
• Mathematical Model
W(next year) = W(this year) + 1

• A Computational Model (there are many possibilities)
W(1) = 180;
for i = [1:2]
W(i+1) = W(i) + 1;
end


## 4.4. Ex. I: Approach 2.

Compare to analytic solution (if available)

• The mathematical model has an analytic solution
W(year) = W(year = 2010) + (year - 2010)


check to see if equation makes sense

W(2011) = W(2010) + 2011 - 2010


which equals

W(2011) = W(2010) + 1


## 4.5. Ex. I: Approach 2. cont.

What will you be in 2050 if you started at 180 in 2010?

W(2050) = W(2010) + 2050 - 2010
W(2050) = 180 + 40 = 220


Compare with computer model

W(1) = 180;
for i = [1:40]
W(i+1) = W(i) + 1;
end


## 4.6. Ex. I: Approach 3.

We can check that we get the same answer by implementing the program in a different way:

w = 180;
for i = [1:2]
w = w + 1;
end


# 5. Activities

## 5.1. Seemingly simple addition I.

Write a computer program that implements the statement

The balance of a bank account after 10 days is 1/1+ 1/2+ 1/3+ ...+ 1/10.

and use the three approaches to verification to check your result.

## 5.2. Seemingly simple addition II.

Consider the following program:

b = 0;
for i = [10:-1:1]
b = b + 1/i;
end


Write the program out long-hand to convince yourself that it should give the same answer as the program that you wrote for the previous activity.

See Ethics.

# 6. Questions

## 6.1. Big numbers

What if your initial weight was 10308 pounds and you gained 10307 pounds per year? How much will you weigh in 10 years? Compare this to the answer given after running:

W(1) = 10^308
for i = [1:10]
W(i+1) = W(i) + 10^307
end


Explain the result.

Two different ways of computing a sum were considered in the activity:

a = 0;
single(a);
for i = [1:100]
a = a + 1/i;
end
a

b = 0;
single(b);
for i = [100:-1:1]
b = b + 1/i;
end


It was argued that mathematically a and b should be the same. Computationally, the results may be different. How many iterations are required before the difference between a and b is 1.0?

Note that the statement single(a) tells MATLAB to store numbers as single precision values. To see the difference, type

format long % display all digits of a number
a = pi;
a % 3.141592653589793 is displayed

single(a); % Tell MATLAB to store the value of pi rounded 1/2 as many digits.
a % 3.1415927 is displayed


## 6.3. Definition

What are the three steps of verification? Give an example of a problem for which the second step cannot be performed.