tailieunhanh - Computational Physics - M. Jensen Episode 1 Part 3

Tham khảo tài liệu 'computational physics - m. jensen episode 1 part 3', kỹ thuật - công nghệ, cơ khí - chế tạo máy phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | . REAL NUMBERS AND NUMERICAL PRECISION 29 and that N 127 just as a mere example which illustrates the kind of problems which can arise when the standard deviation is small compared with 7. Using single precision results in a standard deviation of Ơ for the most used algorithm while the exact answer is Ơ a number which also results from the above two-step algorithm. With double precision the two algorithms result in the same answer. The reason for such a difference resides in the fact that the first algorithm includes the subtraction of two large numbers which are squared. Since the average value for this example is X it is easy to see that computing x i 2 X X ị can give rise to very large numbers with possible loss of precision when we perform the subtraction. To see this consider the case where ỉ 64. Then we have5 i 100352 while the exact answer is atg4 100064 You can even check this by calculating it by hand. The second algorithm computes first the difference between x i and the average value. The difference gets thereafter squared. For the second algorithm we have for Ỉ 64 z64 X 1 and we have no potential for loss of precision. The standard text book algorithm is expressed through the following program program s chap2 program6. cpp program to calculate the mean and standard deviation of a user created data set stored in array x using namespace std include iostream int main int i float sum sumsq2 xbar sigma1 sigma2 array de c lara ti on wi th fixe d dim ension float x 127 in i ti alis e the data set for i 0 i 127 i x i i 100000. The variable sum is just the sum over all elements The variable sumsq2 is the sum over xr 2 sum 0 . sumsq2 0. 5Note that this number may be compiler and machine dependent. 30 CHAPTER 2. INTRODUCTION TO C C AND FORTRAN 90 95 Now we use the text book algorithm for i 0 i 127 i sum x i sumsq2 pow double x i 2 . . calculate the average and sigma xbar sum 12 7. sigma1 s qrt sumsq2 sum xbar 1 2 6 .

TỪ KHÓA LIÊN QUAN