Lab exercise 3 - Numerical Methods 1

advertisement
Algoritmer og Datastrukturer 1
Lab Exercise 2.2 – Numerical Methods
25.01.2006/TFJ
F2006
Lab Exercise 2.2 – Numerical Methods
1. The arithmetic mean value of a set of values is given by
n 1
x
x
i
i0
n
Specify, implement and test an algorithm that returns the mean value of the n first values of a set of values.
The function should have the following prototype:
double mean(double xValues[], int n)
2. The standard deviation of a set of values is given by
 x
n 1
s
i
x

2
i0
n
Specify, implement and test an algorithm that returns standard deviation of the n first values of a set of
values. The function should have the following prototype:
double stdDev(double xValues[], int n)
3. Numerical integration can be accomplished using Composite Simpson’s rule which uses Simpson’s rule
iteratively over a number of intervals (more information on www.wikipedia.org). Like trapezoid integration,
this rule seeks to divide an interval [a;b] into n small sections, each of size (b-a)/n, and then sum each
section’s contribution to the total integral. The rule states that, as a good approximation,

b
a
h
  f ( x 0 )  4 f ( x1 )  2 f ( x 2 )  4 f ( x3 )  ......  4 f ( x n 1 )  f ( x n ) 
3
n 1

h 
  f (a)  f (b) 
(3  (1) k ) f ( x k ) 

3 
k 1

f ( x)dx 

where h 


ba
and xi  a  i  h, i  [0; n] . Specifically, x0=a and xn=b.
n
State pre- and postconditions for a function compositeSimpsonIntegration() that implements
Composite Simpson’s rule.
Design and implement the function algorithm should have the following prototype
double compositeSimpsonIntegration(double (*f)(double),
double a, double b, double n),
where double (*f)(double) is a function pointer to f(x)
4. (Optional) On the ALD-1 web page you will find source files that contain implementations of the trapezoid
rule and the ordinary Simpson’s rule for integration, as well as a test program to test your implementation
from (3) – but you are most welcome to use your own test program. Try to experiment with different f(x) –
which method is the better?
Page 1 of 1
Download