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

Tham khảo tài liệu 'computational physics - m. jensen episode 1 part 4', 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ả | . NUMERICAL DIFFERENTIATION 49 computed_derivative new double number_of_steps compute the second derivative of exp x second_derivative number_of_steps x initial_step h_step computed_derivative Then we print the results to file output h_step computed_derivative x number_of_steps fr e e memory delete h_step delete computed_derivative return 0 end main program We have defined three additional functions one which reads in from screen the value of the initial step length and the number of divisions by 2 of h. This function is called initialise . To calculate the second derivatives we define the function second_derivative . Finally we have a function which writes our results together with a comparison with the exact value to a given file. The results are stored in two arrays one which contains the given step length and another one which contains the computed derivative. These arrays are defined as pointers through the statement double h_step computed_derivative A call in the main function to the function second_derivative looks then like this second_derivative number_of_steps x h_step computed_derivative while the called function is declared in the following way void second_derivative int number_of_steps double x double h_step double computed_deriva indicating that double h_step double computed_derivative are pointers and that we transfer the address of the first elements. The other variables int number_of_steps double x are transferred by value and are not changed in the called function. Another aspect to observe is the possibility of dynamical allocation of memory through the new function. In the included program we reserve space in memory for these three arrays in the following way h_step new double number_of_steps and computed_derivative new double number_of_steps When we no longer need the space occupied by these arrays we free memory through the declarations delete h_step and delete computed_derivative The function initialise Read in from screen the initial and

TỪ KHÓA LIÊN QUAN