Created
September 10, 2014 18:55
-
-
Save dyjjones/9286f50e2ad88ce32fbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! use f90 or f95 for this | |
program hw2 | |
implicit none | |
! vars | |
double precision :: h, p | |
integer :: loop | |
double precision, dimension(11) :: trueVals | |
h = 1.0 | |
loop = 0 | |
trueVals(1) = -1.2625 | |
trueVals(2) = -0.916 | |
trueVals(3) = -0.912535 | |
trueVals(4) = -0.91250035 | |
trueVals(5) = -0.9125000035 | |
trueVals(6) = -0.912500000035 | |
trueVals(7) = -0.91250000000035 | |
trueVals(8) = -0.9125000000000035 | |
trueVals(9) = -0.912500000000000035 | |
trueVals(10) = -0.91250000000000000035 | |
trueVals(11) = -0.9125000000000000000035 | |
! It looks like fortran mangles the true values. | |
! Compare the above values to the outputted values. | |
write(*, *) | |
1 if (loop /= 11) then | |
p = prime(0.5, h) | |
write(*, *) "h =", h, ": f'(x) =", p | |
write(*, *) "true value =", trueVals(loop + 1) | |
write(*, *) "true error =", abs(trueVals(loop + 1) - p) | |
write(*, *) | |
write(7, *) h, abs(trueVals(loop + 1) - p) | |
h = h * 0.1 | |
loop = loop + 1 | |
goto 1 | |
end if | |
contains | |
function f(x) | |
double precision f, x | |
f = -0.1 * x**4 - 0.15 * x**3 - 0.5 * x**2 | |
f = f - 0.25 * x + 1.2 | |
return | |
end function f | |
function prime(x, h) | |
double precision :: prime | |
real :: x | |
double precision :: h | |
prime = (f(x + h) - f(x - h)) / (2.0 * h) | |
return | |
end function prime | |
end program hw2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment