MATH 2308 Saint Marys University Intro to Numerical Analysis Program Questions

Description

For
computer problems, make sure to submit a copy of the source code you have written
and as well as a copy of the output produced by your program. All computer programs
should be written in Fortran 95.

2 attachmentsSlide 1 of 2attachment_1attachment_1attachment_2attachment_2

Unformatted Attachment Preview

CSCI/MATH2308.1 2020
Assignments should be submitted to the appropriately titled Brightspace Dropbox. For
computer problems, make sure to submit a copy of the source code you have written
and as well as a copy of the output produced by your program. All computer programs
should be written in Fortran 95.
1 Consider the two mathematically equivalent expressions
๐‘“ (๐‘ฅ ) = ๐‘ฅ(โˆš๐‘ฅ + 1 โˆ’ โˆš๐‘ฅ),
and
๐‘”(๐‘ฅ ) =
๐‘ฅ
(โˆš๐‘ฅ+1+โˆš๐‘ฅ)
a) Prove that the two expressions are mathematically equivalent.
b) Which of the two expressions can be evaluated more accurately in floating point
arithmetic? Why?
c) Using 4-digit precision, at ๐‘ฅ = 500, ๐‘“(500) = 10, and ๐‘”(500) = 11.17. Which of
these two evaluations is correct? Explain the discrepancy by performing the
computation and analyzing the interim steps. Use an arbitrary precision calculator
such as https://apfloat.appspot.com/.
2 Assume that you are solving the quadratic equation ๐‘Ž๐‘ฅ 2 + ๐‘๐‘ฅ + ๐‘ = 0, with ๐‘Ž = 1.22,
๐‘ = 3.34, and ๐‘ = 2.28, using the standard quadratic formula, and using 3-digit
arithmetic with rounding. Use an arbitrary precision calculator such as
https://apfloat.appspot.com/.
a. What is the computed value for ๐‘ 2 โˆ’ 4๐‘Ž๐‘ ?
2
b. What is the exact value for ๐‘ โˆ’ 4๐‘Ž๐‘?
c. What is the relative error for the computed value for ๐‘ 2 โˆ’ 4๐‘Ž๐‘ ?
3 When ๐‘Ž and ๐‘ are the same sign, which of the following two formulas is preferable
for computing the midpoint ๐‘š of an interval [๐‘Ž, ๐‘], in floating point arithmetic with
rounding? Why? [Hint: It is possible to devise example(s) in which the midpoint lies
outside the interval [๐‘Ž, ๐‘], for only one the formulas above.]
๐‘š=
๐‘Ž+๐‘
2
๐‘œ๐‘Ÿ
๐‘š=๐‘Ž+
๐‘โˆ’๐‘Ž
2
Programming Questions
1)
Write a program to compute the mathematical constant ๐‘’, the base of natural logarithms
from the definition
1.0 ๐‘›
๐‘’ = lim (1.0 +
) .
๐‘›โ†’โˆž
๐‘›
1.0 ๐‘›
Specifically, compute (1.0 + ๐‘› ) for ๐‘› = 10๐‘˜ , ๐‘˜ = 1, 2, โ€ฆ ,20. Determine the error in your
successive approximations by comparing them with the exact value, ๐‘’. Does the error
always decrease as ๐‘› increases? Explain your results.
2)
a) Consider the function
๐‘“ (๐‘ฅ ) =
Use Lโ€™ Hospitalโ€™s rule to show that
(๐‘’ ๐‘ฅ โˆ’ 1)
.
๐‘ฅ
(๐‘’ ๐‘ฅ โˆ’ 1)
= 1.
๐‘ฅโ†’0
๐‘ฅ
lim ๐‘“ (๐‘ฅ ) = lim
๐‘ฅโ†’0
b) Check this result empirically by writing a program to compute ๐‘“(๐‘ฅ) for
๐‘ฅ = 10.0โˆ’๐‘˜ , ๐‘˜ = 1, โ€ฆ โ€ฆ โ€ฆ . . , 15 . Do your results agree with theoretical
expectations? Explain why.
3)
Write a program to compute the absolute and relative errors in Stirlingโ€™s approximation
๐‘›! โ‰ˆ โˆš2๐œ‹๐‘› (๐‘›โ„๐‘’)๐‘› .
for ๐‘› = 1, 2, โ€ฆ โ€ฆ โ€ฆ ,10. Does the absolute error grow or shrink as ๐‘› increases? Does the
relative error grow or shrink as ๐‘› increases?
CSCI/MATH 2308 Assignment Help
Overview: All of the questions in this assignment exemplify the complications encountered
when numerical computations are carried out in floating point arithmetic.
It is important to realize that often times, computations done with finite precision arithmetic,
produce results which will eventually diverge from theoretical expectations, much like in the
case of the finite difference approximation discussed in class. These assignment questions are
no exception, so you should expect to see some anomalies in the computed solution that will
require some analysis.
When attempting to answer any of these questions, you must put on your numerical analysis
thinking cap and assess the problem in the context of numerical issues such as, machine
precision, underflow, overflow, truncation error, round-off error and catastrophic
cancelation. This is because at least one of these culprits is inevitably responsible for anomalies
in the computed solution.
What to look out for:
Catastrophic cancellation: Beware of expressions or numbers with different or alternate signs
(plus & minus) which are added together. These must be analyzed carefully as a potential
source of the cancellation of significant digits.
Machine precision: This is the smallest positive number in a floating point system whose sum
with 1 is larger than 1, that is,
1 + ๐œ–๐‘€๐ด๐ถ๐ป > 1
The addition of any number ๐œ– < ๐œ–๐‘€๐ด๐ถ๐ป to 1 will have no impact on the sum. In other words, 1+๐œ– = 1 Note that numbers less than ๐œ–๐‘€๐ด๐ถ๐ป can be represented in the given floating point system. Underflow: All numbers less than the smallest number in a floating point system are referred to as underflow. These numbers cannot be represented by the system and are replaced by zero. Overflow: All numbers greater than the largest number in a floating point system are referred to as underflow. These numbers cannot be represented by the system and are replaced by not a number (NAN). Round-off error: This occurs because floating point systems have finite precision and cannot therefore represent all real numbers accurately. Rounding error is an inevitable consequence when computations are performed using floating pint arithmetic. Truncation error: This is concerned with the approximation of an exact mathematical procedure with a numerical technique. E.g. approximation the derivative of a function with a finite difference approximation formula. Question 1: Hint: One of these two formulas is susceptible to a numerical complication that arise when numbers with different signs are added together. Please use 4-digit precision with truncation (no rounding) at each step in the computation process. Question 2: Please use 3-digit precision when required with rounding at each step in the computation process. Note that the computed value is rounded to 3-digit precision and the exact value to infinite precision, which is as many digits as it takes to represent the result of the computation. Question 3: Hint: One of these two formulas is susceptible to one of the numerical complications mentioned above. For example, using two digit precision with rounding at each step, choose,[ ๐‘Ž, ๐‘] = [9.7, 9.9], then ๐‘š= ๐‘š=๐‘Ž+ ๐‘Ž + ๐‘ 9.7 + 9.9 19.6 20 = = = = 10 2 2 2 2 ๐‘โˆ’๐‘Ž 9.9 โˆ’ 9.7 0.2 = 9.7 + = 9.7 + = 9.7 + 0.1 = 9.8 2 2 2 In the first formula, the sum of ๐‘Ž and ๐‘ is 3 digits, which has to be rounded to two digits as required, hence this results in the midpoint being outside the interval [9.7, 9.9]! Note that in this example the midpoint is larger than the range of numbers in the given system. You will have to identify what numerical complication is at play here. Please make sure to devise your own example. Programming Questions 1) Write a program to compute the mathematical constant ๐‘’, the base of natural logarithms from the definition ๐‘’ = lim (1.0 + ๐‘›โ†’โˆž 1.0 ๐‘› ) . ๐‘› Theoretical Result: As n approaches infinity, Eulerโ€™s number e, on the left is exactly equal to the formula on the right. Numerical Reality: We can never reach infinity or the end of the real number line because it has infinitely many numbers, so n can only be numerically represented as a very large integer. Therefore, the theoretical equality must be replaced by an approximation of the form, ๐‘’ โ‰ˆ (1.0 + 1.0 ๐‘› ) , ๐‘Ÿ๐‘’๐‘Ž๐‘™(๐‘›) for some large integer, n. 1.0 ๐‘› Numerical Experiment: For ๐‘› = 10๐‘˜ , ๐‘˜ = 1, 2, โ€ฆ โ€ฆ 20, approximate e with (1.0 + ๐‘Ÿ๐‘’๐‘Ž๐‘™(๐‘›)) . That is: when ๐‘˜ = 1, ๐‘› = 101 = 10 Compute 1) (1.0 + 10 1.0 ) โ‰ˆ๐‘’ ๐‘Ÿ๐‘’๐‘Ž๐‘™(10) 2) Absolute error = |๐‘’ โˆ’ (1.0 + โ‹ฎ โ‹ฎ 10 1.0 ) | ๐‘Ÿ๐‘’๐‘Ž๐‘™(10) When, ๐‘˜ = 20, ๐‘› = 1020 Compute 1) 20 10 1.0 (1.0 + ) ๐‘Ÿ๐‘’๐‘Ž๐‘™(1020 ) 1.0 1020 2) Absolute error = |๐‘’ โˆ’ (1.0 + ๐‘Ÿ๐‘’๐‘Ž๐‘™(1020 )) โ‰ˆ๐‘’ | Program notes: 1) Note that the value of n changes with each different value of k, so a loop must be used to execute this part of the program. 2) Three variables must be declared in this program. Integer type must be declared for variables, ๐‘›, ๐‘˜, and real type for the approximation, (1.0 + 1.0 ๐‘› ๐‘› ) . Use the Fortran intrinsic type, ๐‘’๐‘ฅ๐‘(1.0) for the exact value of ๐‘’. 3) In order to obtain the program you need for this question, make a copy of the finite difference program, and edit it to obtain the program you need rather that writing a new program from scratch. 4) Please observe that the variable n is of integer type, so the key phrase (๐‘Ÿ๐‘’๐‘Ž๐‘™(๐‘›)) is used to protect the computation from mixed mode arithmetic complications. 5) Should you wish to do so, a variable of type real can be declared and used to store the result of the absolute error computation is step 2 above. Hint: As n increases, expect the absolute error to initially decrease down to a minimum, then eventually increase. Revisit the finite difference approximation experiment because the analysis of the error trend is similar to this question. You will have to explain in your analysis, what possible sources of error contribute to the trend in the absolute error. Can you also explain why the value of the approximation equals one after a while? 2) a) Consider the function ๐‘“(๐‘ฅ) = Use Lโ€™ Hospitalโ€™s rule to show that (๐‘’ ๐‘ฅ โˆ’ 1) . ๐‘ฅ lim ๐‘“ (๐‘ฅ) = 1. ๐‘ฅโ†’0 b) Check this result empirically by writing a program to compute ๐‘“(๐‘ฅ) for ๐‘ฅ = 10โˆ’๐‘˜ , ๐‘˜ = 1, โ€ฆ โ€ฆ โ€ฆ . . , 15 . Do your results agree with theoretical expectations? Explain why. Theoretical Result: As ๐‘ฅ approaches zero, ๐‘“(๐‘ฅ) is equals one. (Calculus of limits) Numerical Reality: Because division by zero isnโ€™t allowed, the value of ๐‘ฅ can never be zero, therefore, ๐‘ฅ can only be numerically represented as very small numbers close to zero. Therefore, we expect ๐‘“(๐‘ฅ) to approach one, as ๐‘ฅ becomes increasingly smaller. That is: (๐‘’ ๐‘ฅ โˆ’ 1) โ‰ˆ1 ๐‘ฅ Numerical Experiment: For ๐‘ฅ = 10.0โˆ’๐‘˜ , ๐‘˜ = 1, 2, โ€ฆ โ€ฆ 15, approximate 1.0 with That is: when ๐‘˜ = 1, ๐‘ฅ = 10.0โˆ’1 = 0.1 Compute 1) (๐‘’ 0.1 โˆ’ 1) โ‰ˆ1 0.1 2) Absolute error = |1.0 โˆ’ (๐‘’ 0.1 โˆ’ 1) | 0.1 (๐‘’ ๐‘ฅ โˆ’1) ๐‘ฅ . When ๐‘˜ = 15, ๐‘ฅ = 10.0โˆ’15 Compute 1) โˆ’15 (๐‘’10.0 โˆ’ 1) โ‰ˆ1 10.0โˆ’15 2) โˆ’15 Absolute error = |1.0 โˆ’ (๐‘’10.0 โˆ’ 1) | 10.0โˆ’15 Program notes: 6) Note that the value of x changes with each different value of k, so it should be obvious by now that a loop must be used to execute the statements in this part of the program. 7) At least three variables must be declared in this program. Integer type must be declared for variable, ๐‘˜, and real type for the variables, ๐‘ฅ and ๐‘“(๐‘ฅ). The exact value is 1.0. 8) In order to obtain the program you need for this question, make a copy of the finite difference program, and edit it to obtain the program you need rather that writing a new program from scratch. Hint: Here again as x decreases, expect the absolute error to initially decrease down to a minimum, then eventually increase. However, in addition to the two common sources of computational error, a third numerical complication is also at play here. Note that the expression at the top is a difference of two numbers. You are to identify all the possible sources of error which contribute to the trend in the absolute error 3) Write a program to compute the absolute and relative errors in Stirlingโ€™s approximation ๐‘›! โ‰ˆ โˆš2๐œ‹๐‘› (๐‘›โ„๐‘’)๐‘› . For ๐‘› = 1, 2, โ€ฆ โ€ฆ โ€ฆ ,10. Does the absolute error grow or shrink as ๐‘› increases? Does the relative error grow or shrink as ๐‘› increases? Comments: The explanation of this program is relatively straightforward, the expression on the right is used to approximate n factorial. Numerical Experiment: For ๐‘› = 1, 2, . . . . . . . . . ,10. approximate n! with โˆš2๐œ‹๐‘› (๐‘›โ„๐‘’)๐‘› . That is: when ๐‘› = 1, Compute: 1) 1! 1 2) โˆš2๐œ‹ (1โ„๐‘’) . 1 3) ๐ด๐‘๐‘ ๐‘œ๐‘™๐‘ข๐‘ก๐‘’ ๐‘’๐‘Ÿ๐‘Ÿ๐‘œ๐‘Ÿ = |1! โˆ’ โˆš2๐œ‹ (1โ„๐‘’) |. 1 4) ๐‘…๐‘’๐‘™๐‘Ž๐‘ก๐‘–๐‘ฃ๐‘’ ๐‘’๐‘Ÿ๐‘Ÿ๐‘œ๐‘Ÿ = โ‹ฎ โ‹ฎ โ‹ฎ |1!โˆ’โˆš2๐œ‹ (1โ„๐‘’ ) | 1! When ๐‘› = 10, Compute: 5) 10! 10 6) โˆš2๐œ‹ โˆ— 10 (10โ„๐‘’) . 10 7) ๐ด๐‘๐‘ ๐‘œ๐‘™๐‘ข๐‘ก๐‘’ ๐‘’๐‘Ÿ๐‘Ÿ๐‘œ๐‘Ÿ = |10! โˆ’ โˆš2๐œ‹ โˆ— 10 (10โ„๐‘’) |. 8) ๐‘…๐‘’๐‘™๐‘Ž๐‘ก๐‘–๐‘ฃ๐‘’ ๐‘’๐‘Ÿ๐‘Ÿ๐‘œ๐‘Ÿ = |10!โˆ’โˆš2๐œ‹โˆ—10 (10โ„๐‘’ ) 10 | 10! Program notes: 1) Note that both the values of ๐‘›! and approximation โˆš2๐œ‹๐‘› (๐‘›โ„๐‘’)๐‘› change as ๐‘› loops through the different values, so a loop must be used to execute the statements in this part of the program. 2) This program involves a little more finessing, so I will analyze some parts of the code here. Step 1: Computing n factorial (n!) and โˆš2๐œ‹๐‘› (๐‘›โ„๐‘’)๐‘› . ๐Ÿ! = ๐Ÿ ๐Ÿ! = ๐Ÿ ร— ๐Ÿ = ๐Ÿ! ร— ๐Ÿ ๐Ÿ‘! = (๐Ÿ ร— ๐Ÿ) ร— ๐Ÿ‘ = ๐Ÿ! ร— ๐Ÿ‘ ๐Ÿ’! = (๐Ÿ ร— ๐Ÿ ร— ๐Ÿ‘) ร— ๐Ÿ’ = ๐Ÿ‘! ร— ๐Ÿ’ ๐Ÿ“! = (๐Ÿ ร— ๐Ÿ ร— ๐Ÿ‘ ร— ๐Ÿ’) ร— ๐Ÿ“ = ๐Ÿ’! ร— ๐Ÿ“ โ‹ฎ ๐’! = (๐Ÿ ร— ๐Ÿ ร— ๐Ÿ‘ ร— ๐Ÿ’ ร— ๐Ÿ“ ร— โ€ฆ ร— (๐’ โˆ’ ๐Ÿ)) ร— ๐’ = (๐’ โˆ’ ๐Ÿ)! ร— ๐’ Note that the current factorial, ๐‘›! can be written in terms of the previous (๐‘› โˆ’ 1)! Factorial multiplied by n. 1) Declare two integer type variables, ๐‘› and ๐‘›_๐‘“๐‘Ž๐‘, representing ๐‘› and ๐‘›!, respectively. 2) Declare three real type variables for the approximation formula, โˆš2๐œ‹๐‘› (๐‘›โ„๐‘’)๐‘› , the absolute error and pi. I will use ๐‘Ž๐‘๐‘๐‘Ÿ๐‘œ๐‘ฅ, ๐‘Ž๐‘๐‘ _๐‘’๐‘Ÿ๐‘Ÿ, and ๐‘๐‘– respectively. 3) The intrinsic function ๐‘’๐‘ฅ๐‘(โˆ’๐‘Ÿ๐‘’๐‘Ž๐‘™(๐‘›)) replaces ๐‘’ โˆ’๐‘› . Note that (๐‘Ÿ๐‘’๐‘Ž๐‘™(๐‘›)) is used to protect computation from mixed mode arithmetic complications. 4) The variable pi is defined in Fortran as either: ๐œ‹ = 4 โˆ— ๐‘ก๐‘Ž๐‘›โˆ’1 (1.0) or ๐œ‹ = ๐‘๐‘œ๐‘  โˆ’1 (โˆ’1.0) 5) Use the Fortran intrinsic type, ๐‘’๐‘ฅ๐‘(1.0) for the exact value of ๐‘’. 6) The intrinsic Fortran function ๐‘ ๐‘ž๐‘Ÿ๐‘ก(๐‘ฅ) replaces โˆš๐‘ฅ in the code 7) Initialize, ๐‘๐‘– = ๐‘Ž๐‘๐‘œ๐‘ (โˆ’1.0), ๐‘›_๐‘“๐‘Ž๐‘ = 1 then 8) Construct loop index, n, see code fragment below ๐‘๐‘– = ๐‘Ž๐‘๐‘œ๐‘ (โˆ’1.0) (๐‘Ž๐‘๐‘œ๐‘ (๐‘ฅ)) is the intrinsic Fortran function ๐‘๐‘œ๐‘  โˆ’1 (๐‘ฅ)). ๐‘›_๐‘“๐‘Ž๐‘ = 1 Do ๐‘› = 1, 10 ๐‘›_๐‘“๐‘Ž๐‘ = ๐‘›_๐‘“๐‘Ž๐‘ ร— ๐‘› ๐‘Ž๐‘๐‘๐‘Ÿ๐‘œ๐‘ฅ. = โˆš2๐œ‹๐‘› (๐‘›๐‘› ๐‘’ โˆ’๐‘› ), ๐‘Ž๐‘๐‘ _๐‘’๐‘Ÿ๐‘Ÿ = |๐‘›_๐‘“๐‘Ž๐‘ โˆ’ ๐‘Ž๐‘๐‘๐‘Ÿ๐‘œ๐‘ฅ| print: ๐‘›, ๐‘›_๐‘“๐‘Ž๐‘, ๐‘Ž๐‘๐‘ _๐‘’๐‘Ÿ๐‘Ÿ End Do Important: Exponentiation in Fortran is implemented by the double asterisk, **. For e.g. 2๐‘˜ โ‰ก 2โˆ—โˆ— ๐‘˜ Hint: The questions to this problem are very straightforward. Purchase answer to see full attachment Tags: numerical analysis quadratic equation mathematically equivalent expressions floating point arithmetic arbitrary precision User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Reviews, comments, and love from our customers and community:

This page is having a slideshow that uses Javascript. Your browser either doesn't support Javascript or you have it turned off. To see this page as it is meant to appear please use a Javascript enabled browser.

Peter M.
Peter M.
So far so good! It's safe and legit. My paper was finished on time...very excited!
Sean O.N.
Sean O.N.
Experience was easy, prompt and timely. Awesome first experience with a site like this. Worked out well.Thank you.
Angela M.J.
Angela M.J.
Good easy. I like the bidding because you can choose the writer and read reviews from other students
Lee Y.
Lee Y.
My writer had to change some ideas that she misunderstood. She was really nice and kind.
Kelvin J.
Kelvin J.
I have used other writing websites and this by far as been way better thus far! =)
Antony B.
Antony B.
I received an, "A". Definitely will reach out to her again and I highly recommend her. Thank you very much.
Khadija P.
Khadija P.
I have been searching for a custom book report help services for a while, and finally, I found the best of the best.
Regina Smith
Regina Smith
So amazed at how quickly they did my work!! very happyโ™ฅ.