-
Notifications
You must be signed in to change notification settings - Fork 0
/
1dheatfdm.c
101 lines (74 loc) · 2 KB
/
1dheatfdm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<stdio.h>
#include<math.h>
typedef float newvar[100][100];
float power(float x,float y){
}
void main (){
int i,j,k,n,l;
int xsize, tsize;
newvar a;
float timestep, xstep;
float s;
float x[50], time[50];
float pi = 3.141592653589793238;
float e = 2.718281828459045;
printf("Enter the size of the grid in x direction - \n");
scanf("%d",&xsize);
printf("Enter the size of the grid in t direction - \n");
scanf("%d",&tsize);
//enter x values
printf("Enter the value of xmin \n");
scanf("%f",&x[0]);
printf("Enter the value of xmax \n");
scanf("%f",&x[xsize-1]);
xstep = (x[xsize-1]-x[0])/(xsize -1);
for(i=1;i<xsize-1;i++){
x[i]=xstep+x[i-1];
}
//enter t values
printf("Enter the value of tmin \n");
scanf("%f",&time[0]);
printf("Enter the value of tmax \n");
scanf("%f",&time[xsize-1]);
timestep = (time[tsize-1]-time[0])/(tsize -1);
for(i=1;i<tsize-1;i++){
time[i]=timestep+time[i-1];
}
//initial conditions -
for(i=0;i<xsize;i++){
a[0][i] = (1/sqrt(2*pi*time[0])) * (pow(e,-x[i]*x[i]/(2*time[0])));
}
s = timestep / (xstep * xstep);
printf("s = %f \n",s);
//boundary conditions for the borders -
for(i=1;i<tsize-1;i++){
a[i][0] = 0;
}
for(i=1;i<tsize-1;i++){
a[i][xsize-1] = 0;
}
//printing x values
for(i=0;i<xsize;i++){
printf("%f ",x[i]);
}
printf("\n");
//printing a0 values
for(i=0;i<xsize;i++){
printf("%.3f ",a[0][i]);
}
printf("\n");
// solving in time step
for(i=1;i<tsize;i++){
for(j=1;j<xsize-1;j++){
a[i][j] = 0.5*s*(a[i-1][j+1] - 2 * a[i-1][j] + a[i-1][j+1]) + a[i-1][j] ;
}
}
// printing final matrix
printf("After iterations the matrix is -\n");
for(i=0;i<tsize;i++){
for(j=0;j<xsize;j++){
printf("%.10f ",a[i][j]);
}
printf("\n\n");
}
}