- Explore Numerical Methods
- Handle non-closed-form functions
- Realize the limitation of the single-program approach

`y`

at a given
`x`

in terms of standard functions and operators. One example is
the For every non-negative integer value of

`m`

, we get a different Bessel
function so the above series defines in fact a family of Bessel functions. These
functions appear as solutions to integral and differential equations in diverse
scientific fields and are considered as natural extensions of
trigonometric functions like `sin`

and `cos`

. In this Lab,
we focus on `J`_{1}(x)

, the Bessel function of the first kind
of order 1 (obtained by substituting `m = 1`

in the above) and seek
to chart it in the range [0,10].

- Rewrite the above sum for
`m = 1`

and write out the first 6 or seven terms in the series. Try to see a pattern that allows one to compute each term from its predecessor; e.g. ask yourself: if I was told that the 4th term is 0.725 and was given the value of x, how do I compute the 5th term? You can, of course, compute the 5th term from scratch but we prefer an incremental approach. - Write a program that prompts for a read the value of
`x`

, a`real*8`

number and compute the sum of the series. The loop should keep computing and accumulating terms until the absolute value of the term to be added becomes less than some`EPSILON`

. Make sure that all computations are done using`real*8`

and/or`integer*2`

; i.e. do not allow`real*4`

round-off errors. - Set
`EPSILON`

to`1.E-3`

. - Run the program and compute
`J`

. The answer should be_{1}(0)`0`

, and if not, debug your program. - Run the program and compute
`J`

. The answer should be about_{1}(10)`0.04`

; if not, debug your program. - Modify the program so it also outputs the term count; i.e. the number of terms that were added before the summation stopped. Re-run the previous two cases and justify the displayed term counts.
- How do your answers change if
`EPSILON`

is changed to`1.E-6`

? - How do your answers change if
`EPSILON`

is changed to`1.E-15`

? - Modify the program so it does not prompt for
`x`

. Instead, it sets up a loop that generates 21 values for`x`

, ranging from`0`

to`10`

in increments of`0.5`

. The output must show the value of`x`

and the corresponding value of`J`

in a tabular fashion; e.g._{1}(x)0.0 0.000000000 0.5 0.242268458 ... ... 9.5 0.161264431 10.0 0.043472746

(use a small value of`EPSILON`

, like 1.E-12, and format the output so that 9 decimals are shown.) - Modify the program so that the tabulation is not done on the screen but on
a disk file named
`bessel.txt`

. - Launch Microsoft Excel and in it, open the file
`bessel.txt`

. Note that Excel can read multi-column text files if they have fixed sizes (the columns are aligned) or if they are delimited (a space or a tab separates the columns). Our file has fixed-size columns that are space-delimited, and hence, Excel can read it either way it sees it. - Once opened, highlight all the shown values and choose
`Chart`

from the`Insert`

menu. Specify that you want an XY (scatter) chart, and choose one which connects the points by smooth lines. - The remaining options can be set based on your own preference but place the chart in a sheet by itself (rather than mixed with the data).
- Deliverables: printed program, printout of the tabulation, a hard copy of the chart, and the answers to the above explorations.

`f(x)`

, and a value `x`_{0}

near
which the function is believed to vanish, Newton's method allows us to quickly determine
the value `x`^{*}

at which `f(x`^{*}) = 0

. The
method works by following the tangent of the curve of `f(x)`

and computing
its x-intercept. Starting with `x`_{0}

, this leads to successive
approximations of the form: `x = x` |

which converges to the sought zero (if one exists). Implementing this algorithm involves a loop that computes successive approximations until the abs value of the function at a computed

`x`

becomes less than `EPSILON`

or the difference
(in abs value) between two successive approximations is less than `EPSILON`

.
To guard against situations in which there is no zero, or the zero is not reachable
from the starting point, we also stop the loop (and declare failure in finding the
zero) if more than 1000 iterations were performed.
We will apply the above technique for solving the following problem: You need to buy a house whose present value is A=$135,000. You don't have that much in cash but you can apply for a 30-year mortgage and make monthly payments. The maximum you can pay each month is $1000. What should the annual interest rate be so that you can afford this house?

We have already seen the mortgage formula, which relates the cash price
`A`

, the amortization period `n`

, the monthly interest
percentage `r`

, and the monthly payment `P`

, but the unknown
now is `r`

. We are therefore interested in finding the zero of
the function `f(r)`

, given by:

- Write a program that implements Newton's method for a function whose
zero is known (e.g.
`(x`

). When run, the program prompts for an initial value^{2}-1)`x`

and then either outputs the sought zero or state that no zero was found._{0} - Set
`EPSILON`

to`1.E-3`

. and run. Did you get the correct zero of the function? If not, debug your program. - How does your answer change if
`EPSILON`

is changed to`1.E-6`

? - Modify the program so it also outputs an iteration count; i.e. the number of
repeated applications of Newton's formula before the iteration stopped. Re-run
the previous two cases (different
`EPSILON`

values) and justify the shown counts. - Does the iteration count depend also on the initial guess
`x`

?_{0} - Modify the function to one that is known not to have zeros;
e.g.
`(x`

. Did your program get stuck in an infinite loop or did it correctly detect the problem?^{2}+1) - Modify the program so it locates the zero of the function
`f(r)`

defined above. Note that the independent variable is now called`r`

, not`x`

, so you need to rename one to the other. - Modify the program so it displays the zero as an annual percent (rather than a monthly percentage) formatted to one decimal.
- Deliverables: printed program and the answers to the above explorations.