/*
* heat1d-e-glsc.c -- 1次å
ç±ä¼å°æ¹ç¨å¼ã®åæå¤å¢çå¤åé¡
*
* u_t(x,t)=u_{xx}(x,t) xâ(0,1), tâ(0,â)
* u(0,t)=u(1,t)=0 tâ(0,â)
* u(x,0)=f(x) xâ[0,1]
*
* ãé½è§£æ³ã§è§£ãã
*
* ã³ã³ãã¤ã«: ccmg heat1d-e-glsc.c
* æåã³ã¼ãã«æ³¨æï¼ Windows ã§ã¯ qkc -ms heat1d-e-glsc.c ãå¿
è¦ãã
*
* ãªãªã¸ãã«ã¯ fplot ã©ã¤ãã©ãªã£ãå©ç¨ãã
* http://nalab.mind.meiji.ac.jp/~mk/program/fdm/heat1d-e.c
*/
#include
#include
#include
#ifndef G_DOUBLE
#define G_DOUBLE
#endif
#include
int main()
{
int i, n, nMax, N;
double tau, h, lambda, Tmax;
double *u, *newu;
double f(double);
double win_width, win_height, w_margin, h_margin;
char message[100];
/* N, λ ãå
¥åãã */
printf("åºéã®åå²æ° N = "); scanf("%d", &N);
printf("λ (=Ï/h^2) = "); scanf("%lf", &lambda);
/* æéã®å»ã¿å¹
h, Ï ãè¨ç®ãã */
h = 1.0 / N;
tau = lambda * h * h;
printf("Ï=%g\n", tau);
/* æçµæå»ãå
¥åãã */
printf("æçµæå» Tmax = "); scanf("%lf", &Tmax);
/* ãã¯ãã« u, newu ãç¨æãã */
u = malloc(sizeof(double) * (N+1));
newu = malloc(sizeof(double) * (N+1));
if (u == NULL || newu == NULL) {
fprintf(stderr, "ã¡ã¢ãªã¼ãæºåã§ãã¾ããã§ããã\n");
exit(1);
}
/* åæå¤ã®ä»£å
¥ */
for (i = 0; i <= N; i++)
u[i] = f(i * h);
/* ***************** ã°ã©ãã£ãã¯ã¹ã®æºå ***************** */
/* ã¡ã¿ãã¡ã¤ã«å㯠"HEAT1DE",
* ã¦ã£ã³ãã¦ã®ãµã¤ãºã¯ã
横 win_width + 2 * w_margin, 縦 win_height + 2 * h_margin */
win_width = 150.0; win_height = 150.0; w_margin = 10.0; h_margin = 10.0;
g_init("HEAT1DE", win_width + 2 * w_margin, win_height + 2 * h_margin);
/* ç»é¢ã¨ã¡ã¿ãã¡ã¤ã«ã®ä¸¡æ¹ã«è¨é²ãã */
g_device(G_BOTH);
/* 座æ¨ç³»ã®å®ç¾©: [-0.1,1.1]Ã[-0.1,1.1] ã¨ããéé åã表示ãã */
g_def_scale(0,
-0.1, 1.1, -0.1, 1.1,
w_margin, h_margin, win_width, win_height);
/* ç·ãäºç¨®é¡ç¨æãã */
g_def_line(0, G_BLACK, 0, G_LINE_SOLID);
g_def_line(1, G_BLACK, 0, G_LINE_DOTS);
/* 表示ããããã®æååã®å±æ§ãå®ç¾©ãã */
g_def_text(0, G_BLACK, 3);
/* å®ç¾©ãããã®ãé¸æãã */
g_sel_scale(0); g_sel_line(0); g_sel_text(0);
/* ã¿ã¤ãã«ã¨å
¥åãã©ã¡ã¼ã¿ã¼ã表示ãã */
g_text(30.0, 30.0,
"heat equation, homogeneous Dirichlet boundary condition");
snprintf(message, sizeof(message), "N=%d, lambda=%g, Tmax=%g", N, lambda, Tmax);
g_text(30.0, 60.0, message);
/* 座æ¨è»¸ã表示ãã */
g_sel_line(1);
g_move(-0.1, 0.0); g_plot(1.1, 0.0);
g_move(0.0, -0.1); g_plot(0.0, 1.1);
g_sel_line(0);
/* t=0 ã®ç¶æ
ã表示ãã */
g_move(0.0, u[0]);
for (i = 1; i <= N; i++)
g_plot(i * h, u[i]);
/* ã«ã¼ããä½åã¾ãããè¨ç®ãã (åæ¨äºå
¥) */
nMax = rint(Tmax / tau);
/* æéã«é¢ããã¹ããããé²ããã«ã¼ã */
for (n = 0; n < nMax; n++) {
/* å·®åæ¹ç¨å¼ (n -> n+1) */
for (i = 1; i < N; i++)
newu[i] = (1.0 - 2 * lambda) * u[i] + lambda * (u[i+1] + u[i-1]);
/* è¨ç®å¤ãæ´æ° */
for (i = 1; i < N; i++)
u[i] = newu[i];
/* Dirichlet å¢çæ¡ä»¶ */
u[0] = u[N] = 0.0;
/* ãã®æå» (t=(n+1)Ï) ã®ç¶æ
ã表示ãã */
g_move(0.0, u[0]);
for (i = 1; i <= N; i++)
g_plot(i * h, u[i]);
}
printf("çµãã¾ãããX ã®å ´åã¯ã¦ã£ã³ãã¦ãã¯ãªãã¯ãã¦ä¸ããã\n");
g_sleep(-1.0);
/* ã¦ã£ã³ãã¦ãéãã */
g_term();
return 0;
}
/* åææ¡ä»¶ --- ããã§ã¯ min(x,1-x) ã¨ãã山形ã®ã°ã©ããæã¤é¢æ° */
double f(double x)
{
if (x <= 0.5)
return x;
else
return 1.0 - x;
}