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: