I wrote a program called heat.c. The program was this:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <math.h>
 4 #include <string.h>

5

 6 #define K 80
 7 #define T 3.0
 8 #define N 1000
 9 #define NU 0.01

10

11 double u[K+1],un[K+1];

12

13 double f(double x){
14  return fabs(x-0.5);

15 }

16 double a(double t){
17  return 0.5;

18 }

19 double b(double t){
20  return 0.5+sin(t);

21 }

22 void pheat(){
23  int k;
24  double dx=1.0/K;
25  for(k=0;k<=K;k++){
26  double xk=k*dx;
27  printf("%24.15e %24.15e\n",xk,u[k]);

28 } 29 }

30

31 main(){
32  int k;
33  double dx=1.0/K;
34  for(k=0;k<=K;k++){
35  double xk=k*dx;
36  u[k]=f(xk);

37 }

38  pheat();
39  int n;
40  double dt=T/N,dx2=dx*dx;
41  for(n=1;n<=N;n++){
42  double tn=n*dt;
43  un[0]=a(tn);       // eq 1.2.11
44  for(k=1;k<K;k++){  // eq 1.2.9
45  un[k]=u[k]+NU*dt/dx2
46  *(u[k+1]-2*u[k]+u[k-1]);

47 }

48  un[K]=b(tn);       // eq 1.2.12

49  memcpy(u,un,sizeof(u));
50  printf("\n\n#tn=%24.15e\n",tn);
51  pheat();

52 }

53  return 0;

54 }

55

The initial condition was this:

The output at time tn where n=100 was this:

3