COSC 1002/1902 Computational Science in C
Practical 4
This practical session involves writing codes to numerically integrate using the trapezoidal rule, and
also includes the use of arrays in C, the procedure for reading data from file using redirection, and
additional practice in the use of gnuplot.
Before you begin these exercises, you should review the material in lecture 4. You should also
download the codes appearing in the lecture notes for lecture 4,
arrays.c
,
scan
fact.c
, and
passing.c
, which are available via WebCT or from the website
http://www.physics.usyd.edu.au/
∼
wheat/cosc1002
.
Compile and run each of the codes, following the steps outlined in the lecture notes, and confirm that
you obtain the results described in the lecture notes. Also try producing the file
sinc.txt
with
arrays.c
and then plotting it using gnuplot, as described in the lecture.
If you succeed with this then you are ready to begin the exercises. If not consult a tutor, who can
provide assistance.
The exercises are for students in both units except where indicated (‘1002’ means that the question
is just for students in COSC 1002, and ‘1902’ means that the question is just for students in COSC
1902). Remember to have a tutor mark off checkpoints as you reach them.
Exercises
1. As explained in lecture 4, the trapezoidal rule gives an approximate value for the integral of a
function
f
(
x
)
between
x
=
a
and
x
=
b
in terms of evaluations
f
i
=
f
(
x
i
)
of the function at
points
x
i
(
i
= 0
,
1
,
2
, ..., N

1
) such that
x
0
=
a
,
x
N

1
=
b
, and
x
0
< x
1
< x
2
< ... < x
N

1
.
Specifically, the extended trapezoidal rule is
b
a
f
(
x
)
dx
≈
N

2
i
=0
1
2
Δ
x
i
(
f
i
+
f
i
+1
)
,
(1)
where
Δ
x
i
=
x
i
+1

x
i
.
The goal of this exercise is to write a code to estimate
I
=
1
0
x
+
1
2
dx
(2)
using the extended trapezoidal rule, i.e. equation (1).
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Three '09
 wheatland

Click to edit the document details