//		-*- Mode: Java -*-
// Author(s):	J. Robert Buchanan and Zhoude Shao
// Address:	Department of Mathematics
//		Millersville University
//      	P.O. Box 1002
//		Millersville, PA 17551-0302
// Phone:	717-871-7305
// FAX:		717-871-7948
// Email:	Robert.Buchanan@millersville.edu, Zhoude.Shao@millersville.edu
//
// Date:	10/03/2016
//
// Purpose:	Explicit finite difference method for solving the
//		homogeneous heat equation with Dirichlet boundary conditions.
//		This program accompanies Exercise 14.3.3.
//
// To compile:	javac exercise08.java
// To execute:	java exercise08
//
import java.io.*;

public class exercise08
{
    public static double f(double x)	// initial condition
    {
	double result = 100.0*x*Math.sin(2.0*Math.PI*x)	;

	return(result)	;
    }

    public static void main(String args[])
    {
	int i, j;	// counter(s)
	int nelts = 10+1;
	double h;	// spatial discretization step size
	double k;	// temporal discretization step size
	double [][] soln = new double[2][nelts]	;	// solution

	// Set step sizes
	h = 1.0/((double)(nelts-1))	;
	k = 0.001	;
	// Set and output the initial conditions
	for ( i=0 ; i<nelts ; i++ ) {
	    soln[0][i] = f(((double)i)*h)	;
	    System.out.printf("%8.4f ", soln[0][i])	;
	}	// end of for ( i ) loop
	System.out.printf("\n")	;
	// Iterate for t = 0, k, 2k, ..., 10k
	for ( j=1 ; j<10+1 ; j++ ) {
	    soln[j%2][0] = 0.0	;
	    System.out.printf("%8.4f ", soln[j%2][0]);
	    for ( i=1 ; i<(nelts-1) ; i++ ) {
		soln[j%2][i] = k*(1.0-h)*soln[(j-1)%2][i-1]	;
		soln[j%2][i]+= (h*h-2.0*k)*soln[(j-1)%2][i]	;
		soln[j%2][i]+= k*(1.0+h)*soln[(j-1)%2][i+1]	;
		soln[j%2][i]/= h*h	;
		System.out.printf("%8.4f ", soln[j%2][i]);
	    }	// end of for ( i ) loop
	    soln[j%2][nelts-1] = 0.0	;
	    System.out.printf("%8.4f ", soln[j%2][nelts-1]);
	    System.out.printf("\n")	;
	}	// end of for ( j ) loop
    }	// end of function main( )
}

//
//	EOF
//

