Preliminary Information The factorial function is defined on the natural numbers as follows: Note that the result
Question:
Preliminary Information The factorial function is defined on the natural numbers as follows: Note that the result is an integer and that factorials increase very rapidly. MATLAB has a factorial function: factorial but this function may return a floating-point number, since factorials increase so rapidly. To help you check you calculations a table of the factorials has been included as an Appendix. Program Requirements Write an M-file script in MATLAB that will: Calculate factorials using the above formula from n = 1 to 25 with unsigned, 64-bit integers. You must use unsigned, 64-bit integer; failure to do so will result in a grade of zero (0). Calculate factorials from n = 1 to 25 using MATLAB's factorial function. Output the results by printing on each line: n; the result from (1); and the result from (2). So you will print out 25 lines. Programming Design Notice that you have been given a statement of the requirements of the problem, and it is up to you, the programmer, to turn the requirements into a program. In this assignment you will be provided with some guidance in to how to proceed, but you should understand that normally you will just be given the requirements, and it is up to you to decide how to design your program. To being, recall the basic components of programming: data types and structures. So the first thing you will have to decide is what data type to use. Since factorials are natural numbers, and we would like to calculate as many as possible, it is preferable to use unsigned, 64-bit integers. In addition, you will be required to calculate 25 factorials, so it may be advisable to use an array of 25 unsigned, 64 bit integers. Many programs involve sets of data, so the use of arrays is extremely common. To define an array of this size it is advisable to use the zeros MATLAB function (see reading list below). But since MATLAB default is double precision, it will be necessary to specify that you are using unsigned, 64-bit integer. You can do this using: A = zeros(1,25, 'uint64'); The next thing you need to decide is which structures to use. Factorials are based on a product of integers, so this is an example of a repetition (or loop) structure. In this case since the value of n is known, so a for-loop would be appropriate. Finally you want to display your data. Again, if you are using arrays, you would probably want to use a for-loop. In addition, to control the way the data is displayed, you may want to use the MATLAB function fprintf (see reading list below). So note how we designed the program: Select the data type Decide on the organisation of the program and the structures to use Of course the way the program is designed is not unique, and as you develop experience you will develop you own style and preferences. This program is fairly simple, but you should focus on the way the program is constructed and how the various basic components of programming are used to solve the problem. Notice that the program has several distinct parts in that you have to do things in a certain sequence. Also since the use of arrays is extremely common in programming, extra time spent in mastering this will pay off in the future. Reading Section 4.3: Special Matrices Section 7.2.2: Formatted Outputfprintf Function Section 9.1: For Loops Course Assignment Question Question 1: Explain what happens to your results after 20! What to Submit You are required to upload the following files to Blackboard: Your M-file script. A documentation file. Your software should be properly documented. You should document the various parts of your program, and at the top of the document, you should have the following: File name: Description of what the program does: List and description of program variables Programmer: Student number: Date: You should also include a documentation file. This file is extremely important. It is this information that users of your software will consult when running your software. Too many programmers make extreme efforts writing and testing software and then fail to provide proper documentation. This hinders the use of your software and prevents users from gaining the full benefits of your hard work. Your documentation file should contain the following information: Programmer: Student number: Date: Description of what the program does: Instructions on how to run the program Answer to the question given below
Calculus Early Transcendentals
ISBN: 978-0321947345
2nd edition
Authors: William L. Briggs, Lyle Cochran, Bernard Gillett