Exercise 1. Write a subroutine that solves a triangular system of linear equations. Given an n×n upper triangular matrix T and a right-hand side b, your subroutine will compute the solution x. Call your routine as follows:
call trisolve(t,n,x,b,ierr)where on entry:
Example: The system of equations
x + 2y + 3z = 4corresponds to the upper triangular matrix
5y + 6z = 7
8z = 9
1 2 3and right-hand side
T = 5 6
8
b = 4 7 9.Triangular systems of equations are solved by back substitution. Start with the last equation, which gives z=9/8. Then back-substitute the value of z into the second equation and solve for y. Finally, use the computed values of y and z to find x.
Your job is to generalize this approach to an n-dimensional triangular system of equations. Store your code in a file called trisolve.f90. Include only the code for trisolve. (Your main program and other code to test the subroutine will go into another file that you will not hand in.)
Your subroutine should not print anything. Any errors should be noted by setting ierr to an appropriate value and returning. Use double precision (use module precision) for declarations. Here is a code skeleton to get you started.
Exercise 2. Write a manual page, trisolve.man, that documents your routine. For an example of what a manual page should look like, see the manual page for sgesv, which is one of the LAPACK routines.
x1 = x0 - f(x0)/ f '(x0)For example, to find the square root of 2 using only elementary arithmetic, we can define f(x) = x2 -2, f'(x)=2x and take x0=2. Then Newton's method gives the following sequence of values:
x2 = x1 - f(x1)/ f '(x1),
and in general,
xn+1 = xn - f(xn)/ f '(xn).
x0 = 2Notice that the answer is correct to 14 significant digits after only four iterations. (Most calculators use Newton's method to compute square roots.)
x1 = 1.5
x2 = 1.4166666666667
x3 = 1.4142156862745
x4 = 1.4142135623747
x5 = 1.4142135623731
x6 = 1.4142135623731
Exercise 3. Write a Fortran subroutine, newton(f,df,x,tol,ierr) that finds the root of a double-precision function f(x) given its derivative df(x). (Use free source form. Do not store all the x's. On entry, x is the initial guess of the root, and on successful return, x holds the estimate of the root. Stop the iteration after 100 steps, when two successive iterates are less than tol apart, or when the absolute value of any iterate exceeds, say, the square root of the largest representable value, whichever occurs first. (Suggestion: huge(x) is an intrinsic function that returns the largest representable floating-point number whose type is the same as the type of x.)
Set the integer argument ierr to 0 if the routine converges and to a nonzero value of your choosing otherwise.
Here's some code to get you started.
Your subroutine should not print anything. The communication of your subroutine with its caller is solely through the dummy arguments. Handle errors by setting ierr to an appropriate value and returning.
To test your program, you will need to create a separate file containing code for the functions and a main program. Here is a sample program that computes the square root of 2 using Newton's method.
You will turn in only the code for your newton subroutine in a file called roots.f90.
Exercise 4. You'll need to compile three files to test your implementation of Newton's method:
tar cf hw6.tar trisolve.f90 trisolve.man roots.f90 Makefile(Although you will need other files, as mentioned in Exercise 4, you will not turn in precision.f90 or myfuncs.f90. When I test your codes, I will supply versions of these files that will call your Newton routine.)
Mail your tar archive as an attachment to
mat420hw at gmail.comwith a subject line of the form
Your_name MAT 420 HW6
Copyright (c) 2007 by Eric J. Kostelich. All rights reserved.