CS 1713 Section 2, Fall 1997
Assignment 4: Loops: Computing a Table of the Error Function

For this assignment, you will write a C program called erf.c that computes and prints a table for the error function, erf(x) we discussed in class Thursday, September 25.

The error function is used in diffusion theory, statistics, and neural networks research. It is the result of finding the area under the curve of a particular exponential function. It can be expressed as an integral like this:

picture of integral
The S-sign means "integral," basically, the area under the curve. So the whole equation means that the error function evaluated at x is two divided by the square root of pi, the whole quantity times the area under the curve from 0 to x of the e raised to the t-squared power. This is a particularly nasty integral, one that doesn't yield to the solution of antidifferentiation and application of the fundamental theorem of calculus (if you don't know what that means, don't worry; all you really need to know is the very last sum in this assignment). The only way to get values for erf(x) is to evaluate the integral numerically. It turns out that, for a continuous function f, the following is true:
picture of sum equation in increments of delta-x.

This means that the integral from a to b of a function f becomes closer and closer to the sum of the function evaluated at intervals between a and b in multiples of delta-x as delta-x gets closer and closer to zero. So if we choose a very small value, like 0.00001, for delta-x, then

picture of sum equation in increments of delta-x
becomes a good approximation for the integral. We can apply this to the error function of x by evaluating
picture of sum equation in increments of delta-t
for a small value of delta-t.

Your job is to write a program that asks the user to enter a step, then prints a table giving the error function from x=0 through 2 in increments of the user's step. You must use the sum shown above to calculate the error function, so you will have nested loops: the outer one for counting through x, and the inner one for approximating the error function. The table should include both value for x and for erf(x). Here is an example output of the program; yours should look similar. If the user enters a step of 0.1, the table should look like this:

  x     erf (x)
------  ------
0.0000  0.0000
0.1000  0.1132
0.2000  0.2288
0.3000  0.3491
0.4000  0.4767
0.5000  0.6151
0.6000  0.7680
0.7000  0.9404
0.8000  1.1388
0.9000  1.3717
1.0000  1.6506
1.1000  1.9914
1.2000  2.4162
1.3000  2.9564
1.4000  3.6574
1.5000  4.5853
1.6000  5.8385
1.7000  7.5652
1.8000  9.9926
1.9000  13.4740
2.0000  18.5679
You can use a for loop for both the inner and outer loop. I recommend first making your program approximate the error function for a single value of x, as we did in class, and then wrapping another for loop around that to print the table for various values of x.

Pi is approximately 3.14159265358979323844. You can find the square root of a float or double in C using the sqrt function, e.g., a = sqrt (b); . You must first include math.h the way you do stdio.h. You can find e raised to a power with the exp function, also in math.h, e.g. a = exp (b); . There's no "squared" function in C, but you can just multiply a number by itself to get the same effect. Important: when using the math library functions from math.h, you must link in the math library when compiling. Compile your program like this:

	cc -o erf erf.c -lm
The -lm tells the compiler to link in the math library. Also if you forget to put #include <math.h> in your program, your program might compile but it will not work correctly.

Your program should work on runner. Make sure to comment the variables and important statements in the program. To turn in your program, e-mail a copy of your C file to the instructor from your runner account.

This assignment is due at midnight on Monday, October 6. You will be writing this program completely from scratch; make sure you understand how to do it and ask the instructor any questions you have well before it is due.