2

I am trying to solve a problem of 1D heat equation, where u[x,t] is the density of energy in a uni-dimensional bar, in the time t=0 all the energy is concentrated in the point x=0. I want to find solutions for a given time, like t= 64, 128,256, etc.

Code (I removed the comments because English is not my native language):

#include <stdio.h>
#include <math.h>

#define D 0.1
#define T 1024
#define tol 0.0000000000000001

int x,t;
double r,DE,u[T+1], ua[T-1];

main() {
    u[0] = 1;
    for(x=1; x<=T;x++)
        u[x] = 0;
    t=0;
    do {
        t++;
        for(x=0; x<=t; x++)
            ua[x] = u[x];
        u[0] = ua[0] + 2*D*(ua[1]-ua[0]);
        for(x=1; x<=t; x++)
            u[x] = ua[x] + D*(ua[x+1]-2*ua[x]+ua[x-1]);
    } while(t<T);
    DE = 0.0;

    for(x=0; x<=t; x++) {
        if(u[x]>tol) {
            printf("\n %d %1.20f", x,u[x]);
            DE += x*x*u[x];
        } else
            break;
    }
    printf("Delta(t=%d) = %20.20f", t, sqrt(2*DE));
}

Plotting the data result in the "A" graph: graphs

And I want something like "B".

How do I put the boundary conditions in the code to give me the second graph?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Viphys
  • 21
  • 3

0 Answers0