Computational Physics - Department of Physics

(Axel Boer) #1

2.6 Exercises 39


INTEGER:: total_states
INTEGER,DIMENSION(:),POINTER:: n, lorb, jang, spin, m_l
CHARACTER(LEN=10),DIMENSION(:),POINTER:: orbit_status
REAL(8),DIMENSION(:),POINTER:: energy
END TYPEsingle_particle_descript

This structure can again be used to define variables likeelectrons,protonsandneutrons
through the statementTYPE (single_particle_descript) :: electrons, protons, neutrons.
More detailed examples on the use of these variable declarations, classes and templates will
be given in subsequent chapters.


2.6 Exercises.


2.1.Set up an algorithm which converts a floating number given in the decimal representa-
tion to the binary representation. You may or may not use a scientific representation. Write
thereafter a program which implements this algorithm.


2.2.Make a program which sums






sup=

N

n= 1

1

n

,

and
sdown=

n= 1

n=N

1

n

.

The program should readNfrom screen and write the final output to screen.


  1. Comparesupogsdownfor differentNusing both single and double precision forNup to
    N= 1010. Which of the above formula is the most realiable one? Try to give an explanation
    of possible differences. One possibility for guiding the eye is for example to make a log-log
    plot of the relative difference as a function ofNin steps of 10 nwithn= 1 , 2 ,..., 10. This
    means you need to computelog 10 (|(sup(N)−sdown(N))/sdown(N)|)as function oflog 10 (N).


2.3.Write a program which computes


f(x) =x−sinx,

for a wide range of values ofx. Make a careful analysis of this function for values ofxnear
zero. Forx≈ 0 you may consider to write out the series expansions ofsinx


sinx=x−
x^3
3!

+

x^5
5!


x^7
7!

+...

Use the loss of precision theorem of Eq. (2.2) to show that theloss of bits can be limited to at
most one bit by restrictingxso that


1 −
sinx
x ≥

1

2.

One finds then thatxmust at least be 1.9, implying that for|x|< 1. 9 we need to carefully
consider the series expansion. For|x|≥ 1. 9 we can use directly the expressionx−sinx.
For|x|< 1. 9 you should device a recurrence relation for the terms in the series expansion
in order to avoid having to compute very large factorials.


2.4.Assume that you do not have access to the intrinsic function forexpx. Write your own
algorithm forexp(−x)for all possible values ofx, with special care on how to avoid the loss of

Free download pdf