EME165 -- Heat Transfer

A sample program in C/C++ interpreter Ch

/********************************************************************* * File: example.ch * * This program generates a graph containg two temperature curves. * * To run this program, use computers in CAE lab or * * download C/C++ interpreter Ch Professional Edition from * * http://www.softintegration.com/download * * ******************************************************************/ #include <stdio.h> #include <numeric.h> #include <chplot.h> #define N 51 #define PMAX 100 int main() { double Fo = 0.2, Tinit1 = 400, Tinit2 = 300, L = 0.1; array double x[1:N], A[1:N][1:N], Told[1:N], Tnew[1:N], Texplicit[1:N], Timplicit[1:N]; int i; class CPlot plot; linspace(x, 0, L); for(i=2; i<=(N-1); i++) { A[i][i-1] = 1; A[i][i] = -2; A[i][i+1] = 1; } // Explicit solution Told = Tinit1; Told[N] = Tinit2; for(i=1; i<=PMAX; i++) { Tnew = Told + Fo*A*Told; Told = Tnew; } Texplicit = Tnew; // Implicit solution Told = Tinit1; Told[N] = Tinit2; for(i=1; i<=PMAX; i++) { Tnew = inverse(identitymatrix(N)-Fo*A)*Told; Told = Tnew; } Timplicit = Tnew; // Plot the results plot.data2D(x, Texplicit); plot.data2D(x, Timplicit); plot.title("Implicit and explicit temperature"); plot.label(PLOT_AXIS_X, "x"); plot.label(PLOT_AXIS_Y, "T"); plot.legend("Explicit", 0); plot.legend("Implicit", 1); plot.plotting(); return 0; }

Output

A sample program in MATLAB

clear N = 51; Fo = 0.2; Tinit1 = 400; Tinit2 = 300; Pmax = 100; L = 0.1; dx = L/(N-1); x = [0:dx:L]; A = zeros(N); Told = zeros(N,1); Tnew = zeros(N,1); for m = 2:N-1 A(m,m-1) = 1; A(m,m) = -2; A(m,m+1) = 1; end % Explicit Solution for m = 1:N-1 Told(m) = Tinit1; end Told(N) = Tinit2; for P=1:Pmax Tnew = Told + Fo*A*Told; Told = Tnew; end Texplicit = Tnew; % Implicit Solution for m = 1:N-1 Told(m) = Tinit1; end Told(N) = Tinit2; for P=1:Pmax Tnew = (eye(N) - Fo*A)\Told; Told = Tnew; end Timplicit = Tnew; % Plot the Results plot(x, Texplicit, x, Timplicit), xlabel('x'), ylabel('T'); legend('Explicit','Implicit');