Programs for Assignment 1


Question 6 - Fortran version

c  use of IMSL routines to fit a cubic spline to data and evaluate its derivatives
	implicit real*8(a-h,o-z)
c  (x, y) - data to be splined, b - same as x, c - spline coefficients 
	dimension x(10), f(10), b(10), c(4,10)
c  data for problem 6, ass 1, fw99
	open(unit=5,file='spline.in',status='old')
	open(unit=6,file='spline.out',status='new')
	n = 5
c  input data
	do i=1,n
		read(5,11) x(i), f(i)
	end do
c  IMSL routine to calculate spine coefficients
	call dcsint(n,x,f,b,c)
c  IMSL function to calculate derivatives
	xp = 0.9d0
	d0 = dcsder(0,xp,n-1,b,c)
	d1 = dcsder(1,xp,n-1,b,c)
	d2 = dcsder(2,xp,n-1,b,c)
	write(6,2) xp, d0, d1, d2
	xp = 1.05d0
	d0 = dcsder(0,xp,n-1,b,c)
	d1 = dcsder(1,xp,n-1,b,c)
	d2 = dcsder(2,xp,n-1,b,c)
	write(6,2) xp, d0, d1, d2
1	format(5d16.8)
2	format(' At ',d12.5,' the function is',d12.5,
     1	' the first derivative is ',d12.5,
     2	' the second derivative is ',d12.5)
11	format(f5.2,f6.3)
	stop
	end

Input file:
 0.50 2.473
 0.70 2.061
 0.80 1.797
 1.00 1.375
 1.15 0.988

Output file:

 At  0.90000E+00 the first derivative is -0.20248E+01 the second derivative is  
 0.15732E+01
 At  0.10500E+01 the first derivative is -0.23638E+01 the second derivative is 
-0.60929E+01


Question 6 - C program

#include 
#include 
#include 

#define NDATA    5

main()
{
 int                 i;
    double               fdata[NDATA], xdata[NDATA], x, yp, ypp;
    Imsl_d_ppoly        *pp;
    FILE    *fpi, *fpo;
    fpi = fopen("spline.in","r");
    fpo = fopen("spline.out","w");
                                /* Input data from file */
    for (i = 0;  i < NDATA;  i++) {
        fscanf(fpi, "%lf %lf", &xdata[i], &fdata[i]);
    }
                                /* Compute cubic spline interpolant */
    pp = imsl_d_cub_spline_interp_e_cnd (NDATA, xdata,fdata, 0);

                                /* Calculate derivatives */

    x = (double)0.9;
    yp = imsl_d_cub_spline_value(x, pp,
                                    IMSL_DERIV, 1,
                                    0);       /* Calculate first derivative */
    ypp = imsl_d_cub_spline_value(x, pp,
                                    IMSL_DERIV, 2,
                                    0);       /* Calculate first derivative */
    fprintf(fpo, "  %.3f  %.5f   %.5f  \n", x, yp, ypp );
                                                  
    x = (double)1.05;
    yp = imsl_d_cub_spline_value(x, pp,
                                    IMSL_DERIV, 1,
                                    0);       /* Calculate first derivative */
    ypp = imsl_d_cub_spline_value(x, pp,
                                    IMSL_DERIV, 2,
                                    0);       /* Calculate first derivative */
    fprintf(fpo, "  %.3f  %.5f   %.5f  \n", x, yp, ypp ); 

    fclose(fpi);
    fclose(fpo);
  
}

Input file:

 0.50 2.473
 0.70 2.061
 0.80 1.797
 1.00 1.375
 1.15 0.988

Output file:

  0.900  -2.02482   1.57325  
  1.050  -2.36379   -6.09288