c - How do I put boundary conditions in 1D Heat Equation? -
i trying solve problem of 1d heat equation, u[x,t] density of energy in uni-dimensional bar, in time t=0 energy concentrated in point x=0. want find solutions given time, t= 64, 128,256, etc.
code (i removed comments because english not 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; { 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 data result in "a" graph:
and want "b".
how put boundary conditions in code give me second graph?
Comments
Post a Comment