In this assignment, we will investigate the performance of several root finding routines acting on a number of test functions. We will examine the following test functions:
pi = 3.1415926535897932D0
f1 = 0.8D0 - Pi*x/tan(Pi*x)
f2 = -64 + 336*x - 588*x**2 + 343*x**3
f3 = 0.5D0 - 1.0D0/(1.0D0 + exp(60.0D0 - 100.0D0*x))
f4 = 1.2D0 - 3.0D0*x + 0.3D0*sin(24.0D0*x)
pi = 3.1415926535897932;
return 0.8 - pi*x/tan(pi*x);
return - 64 + 336*x - 588*pow(x,2) + 343*pow(x,3);
return 0.5 - 1.0/(1.0 + exp(60.0 - 100.0*x));
return 1.2 - 3.0*x + 0.3*sin(24.0*x);
EXTERNAL
. In C, you will have to include prototypes of
your four test functions at the beginning of the file. In order
to use the various math functions in C, you will also have to include the
math header file:
#include <math.h>at the beginning of the file.
C C This function returns 0.5 - 1/(exp(60 - 100 x) + 1) and its derivative C double precision function f3(x,fprime) implicit none double precision x,fprime,temp temp = exp(60.0D0 - 100.0D0*x) C C This is the derivative of the function: C fprime = (-100.0D0*temp)/(1.0D0 + temp)**2 C C This is the function itself: C f3 = 0.5D0 - 1.0D0/(1.0D0 + temp) return endIn C, the derivative
fprime
would be passed to the subroutine as
a pointer.
5.0E-16
.
Print out your answer showing 16 digits past the decimal point.
In Fortran, you do this by adding a format statement:
write(*,11)y 11 format('my results for f1=',F20.16)In C, you do this using:
printf("my result for f1=%.16g\n",y);(I have no idea how you do this in C++.) Which cases converged?
zeroin.f
and can be found in the GO (Golden Oldies) package;
you will also need d1mach.f
from the last assignment.
exactvalue = 3.14159265358979324D0 print *,' log10(delta)=',log10(abs(x-exactvalue))in each of the four test function subroutines.
exactvalue
should
be a double precision variable.
The D
in the constant tells the compiler that the
constant is a double precision number.