1. The difference of the rational function and the Taylor series yields the equations: -a = 0 x (1 - b) = 0 x^2 (d - c) = 0 x^3 (-1/6 + e) = 0 x^4 (-1/6 d) = 0 Higher order terms cannot be made to vanish. The solution of the set of equations is a = 0, b = 1, c = 0, d = 0, e = 1/6 Thus the approximation is sin x = x / (1 + x^2 / 6). Let's write a program for testing the accuracy of the approximation: program padetest implicit none real x, & y, & ! sin(x), exactg value taylor, etaylor, & ! Taylor series and relative error pade, epade ! Pade and relative errot integer i do i=0,10 x=0.1*i y=sin(x) taylor=x-x**3/6 pade=x/(1+x**2/6) if (abs(y) > 0.0001) then etaylor=abs((taylor-y)/y) epade=abs((pade-y)/y) else etaylor=0 epade=0 end if write(*,'(F5.2,5F10.5)') x,y,taylor,etaylor,pade,epade end do end program x sin(x) Taylor Pade 0.00 0.00000 0.00000 0.00000 0.00000 0.00000 0.10 0.09983 0.09983 0.00000 0.09983 0.00000 0.20 0.19867 0.19867 0.00001 0.19868 0.00003 0.30 0.29552 0.29550 0.00007 0.29557 0.00016 0.40 0.38942 0.38933 0.00022 0.38961 0.00049 0.50 0.47943 0.47917 0.00054 0.48000 0.00120 0.60 0.56464 0.56400 0.00114 0.56604 0.00247 0.70 0.64422 0.64283 0.00215 0.64715 0.00455 0.80 0.71736 0.71467 0.00375 0.72289 0.00772 0.90 0.78333 0.77850 0.00616 0.79295 0.01229 1.00 0.84147 0.83333 0.00967 0.85714 0.01862 2 and 3. module vectors ! module for handling vector arithmetic implicit none real :: limit=1.0e-7 type vector real x,y,z end type type polar real r, phi, theta end type interface operator (+) module procedure add end interface interface operator (-) module procedure sub end interface interface assignment (=) module procedure recttopolar, polartorect end interface contains function add (a, b) ! a+b type (vector), intent(in) :: a,b type (vector) add add%x = a%x + b%x add%y = a%y + b%y add%z = a%z + b%z end function function sub (a, b) ! a-b type (vector), intent(in) :: a,b type (vector) sub sub%x = a%x - b%x sub%y = a%y - b%y sub%z = a%z - b%z end function subroutine recttopolar (p, r) ! convert rectangular to polar coordinates type (polar), intent(out) :: p type (vector), intent(in) :: r real :: x, y, z, d x = r%x; y=r%y; z=r%z d = sqrt(x*x + y*y + z*z) p%r = d if (d > limit) then p%phi= atan2(y, x) p%theta= asin(z/d) else ! a null vector p%phi= 0.0 p%theta= 0.0 end if end subroutine subroutine polartorect (r, p) ! convert polar to rectangular coordinates type (vector), intent(out) :: r type (polar), intent(in) :: p r%x = p%r * cos(p%phi)*cos(p%theta) r%y = p%r * sin(p%phi)*cos(p%theta) r%z = p%r * sin(p%theta) end subroutine real function sprod (a, b) ! scalar product a.b type (vector), intent(in) :: a,b sprod = a%x * b%x + a%y * b%y + a%z * b%z end function function vprod (a, b) ! vector product axb type (vector), intent(in) :: a,b type (vector) :: vprod vprod%x = a%y * b%z - a%z * b%y vprod%y = a%z * b%x - a%x * b%z vprod%z = a%x * b%y - a%y * b%x end function real function s3prod (a, b, c) ! scalar triple product (axb).c type (vector), intent(in) :: a,b, c s3prod = sprod(vprod(a,b),c) end function function v3prod1 (a, b, c) ! vector triple product (axb)xc type (vector), intent(in) :: a,b, c type (vector) :: v3prod1 v3prod1 = vprod(vprod(a,b),c) end function function v3prod2 (a, b, c) ! vector triple product ax(bxc) type (vector), intent(in) :: a,b, c type (vector) :: v3prod2 v3prod2 = vprod(a,vprod(b,c)) end function end module program vectormain ! test the vector module use vectors implicit none type (vector) :: a,b,c,d, s type (polar) :: pa a = vector(1.0, 1.0, 3.0) b = vector(2.0, 4.0, 6.0) c = vector(2.0, 1.0, 1.0) d = vector(1.0, 0.0, 0.0) write(*,'("a =",3F10.4)') a write(*,'("b =",3F10.4)') b write(*,'("c =",3F10.4)') c ! test addition and subtraction s = a+b write(*,'("a+b =",3F10.4)') s s = a-b write(*,'("a-b =",3F10.4)') s ! test different products write(*,'("a.b =",F10.4)') sprod(a,b) write(*,'("a.c =",F10.4)') sprod(a,c) write(*,'("axb =",3F10.4)') vprod(a,b) write(*,'("axc =",3F10.4)') vprod(a,c) write(*,'("bxc =",3F10.4)') vprod(b,c) write(*,'("axc.b =",F10.4)') s3prod(a,c,b) write(*,'("dxc.a =",F10.4)') s3prod(d,c,a) write(*,'("cxa.d =",F10.4)') s3prod(c,a,d) write(*,'("(axb)xc =",3F10.4)') v3prod1(a,b,c) write(*,'("ax(bxc) =",3F10.4)') v3prod2(a,b,c) ! test conversions pa = a write(*,'("a =",3F10.4)') a write(*,'("pa =",3F10.4)') pa a = pa write(*,'("a =",3F10.4)') a a = vector(0.0, 0.0, 0.0) pa = a write(*,'("a =",3F10.4)') a write(*,'("pa =",3F10.4)') pa a = pa write(*,'("a =",3F10.4)') a a = vector(1.0, 0.0, 0.0) pa = a write(*,'("a =",3F10.4)') a write(*,'("pa =",3F10.4)') pa a = pa write(*,'("a =",3F10.4)') a end program >f95 -c vectormod.f90 >f95 vectormain.f90 vectormod.o >./a.out a = 1.0000 1.0000 3.0000 b = 2.0000 4.0000 6.0000 c = 2.0000 1.0000 1.0000 a+b = 3.0000 5.0000 9.0000 a-b = -1.0000 -3.0000 -3.0000 a.b = 24.0000 a.c = 6.0000 axb = -6.0000 0.0000 2.0000 axc = -2.0000 5.0000 -1.0000 bxc = -2.0000 10.0000 -6.0000 axc.b = 10.0000 dxc.a = 2.0000 cxa.d = 2.0000 (axb)xc = -2.0000 10.0000 -6.0000 ax(bxc) = -36.0000 0.0000 12.0000 a = 1.0000 1.0000 3.0000 pa = 3.3166 0.7854 1.1303 a = 1.0000 1.0000 3.0000 a = 0.0000 0.0000 0.0000 pa = 0.0000 0.0000 0.0000 a = 0.0000 0.0000 0.0000 a = 1.0000 0.0000 0.0000 pa = 1.0000 0.0000 0.0000 a = 1.0000 0.0000 0.0000 4. program interpolate implicit none real, dimension(0: 36) :: sinval real, parameter :: pi=3.141592654 integer :: i ! create the sine table do i=0,36 sinval(i) = sin(10.0*i*pi/180.0) end do write(*,'(2F8.3)') 0.5, interp(0.5) write(*,'(2F8.3)') 30.0, interp(30.0) write(*,'(2F8.3)') 30.1, interp(30.1) write(*,'(2F8.3)') 35.0, interp(35.0) write(*,'(2F8.3)') 90.0, interp(90.0) write(*,'(2F8.3)') 180.0, interp(180.0) write(*,'(2F8.3)') 181.0, interp(181.0) write(*,'(2F8.3)') 355.0, interp(355.0) write(*,'(2F8.3)') -5.0, interp(-5.0) contains real function interp(x) ! find sin(x) by interpolating tabulated values ! here x is in degrees real, intent(in) :: x real:: x1, h, s1, s2, ss integer :: i ! normalize the angle to 0..360 degrees x1 = x if (x1 < 0.0) then do x1 = x1+360.0 if (x1 >= 0.0) exit end do end if if (x1 > 360.0) then do x1 = x1-360.0 if (x1 <= 360.0) exit end do end if ! find the index in the table i = x1/10.0 ! interpolation argument h=0 .. 1 h = (x1-10.0*i)/10.0 s1=sinval(i) ; s2=sinval(i+1) ss = s1 + h*(s2-s1) interp = ss end function end program