0% found this document useful (0 votes)
7 views5 pages

Code

Uploaded by

11silal04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Code

Uploaded by

11silal04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1: //#include<iostream>

2: #include<stdio.h>
3: #include<math.h>
4: #include<conio.h>
5: #include<io.h>
6:
7:
8: //using namespace std;
9:
10: main()
11: {
12:
13: mkdir("TDMA");
14:
15:
16: int i,step,m,n,nx,q,u,l=0;
17: printf("No.of cells: ");
18: scanf("%d",&m);
19: nx=m;
20: float L,Do,dx,D,beta,dt,Th,Tc,ap,aw,ae,a0,rms=1,sum,residue,x[nx+1],T[nx+1],T
21: a0 = 1.0;
22:
23: printf("Length of the rod: ");
24: scanf("%f",&L);
25: printf("delta time : ");
26: scanf("%f",&dt);
27: printf("time marching steps : ");
28: scanf("%d",&n);
29: printf("Value of Do: ");
30: scanf("%f",&Do);
31: printf("Value of beta: ");
32: scanf("%f",&beta);
33: printf("what is higher temperature ");
34: scanf("%f",&Th);
35: printf("what is lower temperature ");
36: scanf("%f",&Tc);
37: residue = 0.000001; //residue
38: //residue
39:
40: //------------grid generation---------------//
41: dx = L/nx;
42: x[1] = 0.5*dx;
43: x[0] = 0.0;
44: x[nx+1] = L;
45: for(i=2;i<=nx;i++)
46: x[i] = x[i-1] + dx;
47:
48:
49:
50: //-------initialisation----------//
51: for(i=1;i<=nx;i++)
52: {
53:
54: T[i] = 0.0;
55: Told[i]=0.0;
56: }
57: //---------iterative calculation with TDMA-------------//
58:
59: for(step=1;step<=n;step++)
60: {
61:
62:
63: for(i=2;i<=nx+1;i++) //above diagonal
64: {
65: D = Do*(1 + beta*(i-0.5)*dx);
66: ae = (-1)*((D*dt)/(dx*dx));
67: a[i-1] = ae;
68:
69:
70: }
71:
72:
73:
74:
75:
76: for(i=0;i<=nx-1;i++) //below diagonal
77: {
78: D = Do*(1 + beta*(i-0.5)*dx);
79: aw = (-1)*((D*dt)/(dx*dx));
80: b[i+1] = aw;
81: if(i==0)
82: {
83: D=Do;
84: aw = (-1)*((D*dt)/(dx*dx));
85: b[i+1]=aw;
86: }
87:
88: }
89:
90: //first element of main diagonal//
91: D = Do*(1 + beta*(1-0.5)*dx);
92: ap = (1+((2*D*dt)/(dx*dx)));
93: aw = (-1)*((D*dt)/(dx*dx));
94: d[1] = (ap-b[1]);
95: s[1]=d[1];
96: //last element of main diagonal//
97: D = Do*(1 + beta*(nx-0.5)*dx);
98: ap = (1+((2*D*dt)/(dx*dx)));
99: ae = (-1)*((D*dt)/(dx*dx));
100:
101: d[nx] = (ap-a[nx]);
102: s[nx]=d[nx];
103: //middle elements of main diagonal//
104: for(i=2;i<=nx-1;i++)
105: {
106: D = Do*(1 + beta
107: ap = (1+((2*D*dt)/(dx*dx)));
108: d[i] = ap;
109: s[i]=d[i];
110:
111: }
112: //matrix AX=B//
113: //first element of resultant matrix//
114: D = Do*(1 + beta*(1-0.5)*dx);
115: aw = (-1)*((D*dt)/(dx*dx));
116: R[1] = ((a0*T[1])-(2*Th*b[1]));
117: V[1]=R[1];
118: //last element of resultant matrix//
119: D = Do*(1 + beta*(nx-0.5)*dx);
120: ae = (-1)*((D*dt)/(dx*dx));
121: R[nx] = ((a0*T[nx])-(2*Tc*a[nx]));
122: V[nx]=R[nx];
123: //middle elemets of resultant matrix//
124: for(i=2;i<=nx;i++)
125: {
126:
127: R[i] = a0*T[i];
128: V[i]=R[i];
129: }
130:
131:
132: //printing of matrix a//
133: for(u=1;u<=nx;u++)
134: {
135: printf("%f\n",R[u]);
136: }
137:
138: printf(" \n");
139: //forward sweep modification//
140:
141: for(i=2;i<=nx;i++)
142: {
143:
144:
145: d[i]=(s[i]*s[i-1])/(b[i]) -a[i-1];
146: R[i]=(R[i]*s[i-1])/(b[i])-V[i-1];
147:
148: }
149: for(i=2;i<=nx;i++) //modification//
150: {
151: a[i]=a[i]*s[i-1]/b[i];
152: }
153:
154:
155: //printing of modified resultanat//
156:
157: for(u=1;u<=nx;u++)
158: {
159: printf("%f\n",R[u]);
160: }
161:
162:
163:
164: T[nx] = R[nx]/d[nx]; //backword sweep
165:
166: for(i=nx-1;i>0;i--)
167: T[i] = ((R[i] - (a[i]*T[i+1]))/d[i]);
168:
169:
170:
171: sum = 0.0; //rms calulation
172: for(i=1;i<=nx;i++)
173: {
174:
175: sum = sum + pow((Told[i]-T[i]),2);
176:
177: Told[i]=T[i];
178: printf("%d\t%f\t%f\n \n",step,rms,T[i]);
179: }
180:
181: rms = sqrt(sum/nx);
182:
183:
184: }
185:
186: //printf("%d",step);
187:
188: printf("%d",step);
189: return 0;
190: }

You might also like