You are on page 1of 20

ME 415: COMPUTATIONAL FLUID DYNAMICS

Assignment 1 07th February 2013

Kushan Gajjar (100100003) Amitosh Dash (100100015)

Q-1: MATLAB code for 1-D Steady conduction without source


k=0.1; qb=200; h=25; L=0.1; Tf=300; Te=50; N=10; dx=L/(N-1); % % % % % % % % thermal conductivity of wall in W/m-K Incoming heat flux on west boundary or left side of wall heat transfer coefficient of fluid in W/m^2-K Wall thickness in m Temperature of fluid in contact with wall on west face(degC) Prescribed temperature on east face of wall in degC No. of grid points distance between grid points

A=zeros(N);

% defining matrix A for storing coefficient values

% defining coefficient matrices for east,west and present grid point ap=zeros(1,N); aw=zeros(1,N); ae=zeros(1,N); b=zeros(1,N); T=zeros(1,N); % assigning values to coefficients of grid point P (ap), west face (aw), east % face (ae) for i=1:N aw(i)=k/dx; ae(i)=k/dx; ap(i)=aw(i)+ae(i); end % boundary condition on leftmost face with heat flux of 200W/m^2 and also % % % convection with the hot fluid ap(1)=(k)/dx+h; b(1)=qb+h*Tf; ae(1)=(k)/dx; aw(1)=0; % precribed temperature boundary condition on rightmost face ap(N)=1; ae(N)=0; T(N)=Te; % assigning values to coefficient matrix A (Tridiagnol matrix in this case) % % to solve for equation AT=b for i=1:N-1 A(i,i)=ap(i); A(i,i+1)=-ae(i); A(i+1,i)=-aw(i+1); end A(N,N)=ap(N); % Applying Thomas Algorithm or TDMA for the tridiagnol matrix

% Forward Elimination for i=2:N A(i,i)=A(i,i)-(A(i,i-1)/A(i-1,i-1))*A(i-1,i); b(i)=b(i)-(A(i,i-1)/A(i-1,i-1))*b(i-1); end % Backward Substitution for i=N-1:(-1):1 T(i)=(b(i)-A(i,i+1)*T(i+1))/A(i,i); end % Plotting the temperature field as a function of distance x x(1)=0; for i=2:N x(i)=x(i-1)+dx; end plot (x,T,'.'); title('Temperature Plot'); xlabel('Distance X (m)'); ylabel('Temperature (degC)'); hold on; % Plotting Analytical Solution u=0:0.001:0.1; for i=1:101 Ta(i)=-2480.8*u(i)+7750.0/26; end plot(u,Ta);

Q-2: MATLAB code for 1-D Steady conduction with constant source
k=0.5; S=8e5; L=0.02; Tw=373.15; Te=473.15; N=100; dx=L/(N-1); % % % % % % % thermal conductivity of wall in W/m-K heat source Wall thickness in m Prescribed temperature on west face of wall in K Prescribed temperature on east face of wall in K No. of grid points distance between grid points

A=zeros(N);

% defining matrix A for storing coefficient values

% coefficient matrices for east (ae),west (aw) and present grid point (ap) ap=zeros(1,N); aw=zeros(1,N); ae=zeros(1,N); b=zeros(1,N); T=zeros(1,N);

b(1,:)=S*dx; ae(1,:)=k/dx; aw(1,:)=k/dx; aw(1)=0; ae(1)=0; ae(N)=0; aw(N)=0; ap=ae+aw; ap(1)=1; ap(N)=1;

% Source Term

% Boundary conditions % Prescribed Temperature boundary conditions on both end faces b(1)=Tw; b(N)=Te; % assigning values to coefficient matrix A to solve for equation AT=b for i=1:N-1 A(i,i)=ap(i); A(i,i+1)=-ae(i); A(i+1,i)=-aw(i+1); end A(N,N)=ap(N); % Applying Thomas Algorithm or TDMA for the tridiagonal matrix % Forward Elimination for i=2:N A(i,i)=A(i,i)-(A(i,i-1)/A(i-1,i-1))*A(i-1,i); b(i)=b(i)-(A(i,i-1)/A(i-1,i-1))*b(i-1); end

% Backward Substitution T(N)=b(N)/A(N,N); for i=N-1:(-1):1 T(i)=(b(i)-A(i,i+1)*T(i+1))/A(i,i); end

% Plotting the temperature field as a function of distance x T=T-273.15; x(1)=0; for i=2:N x(i)=x(i-1)+dx; end plot (x,T,'o'); title('Temperature Plot'); xlabel('Distance X (m)'); ylabel('Temperature (degC)'); hold on; % Plotting Analytical Solution u=0:0.0001:0.02; for i=1:201 Ta(i)=(-8e5*(u(i))^2)+21e3*u(i)+100; end plot(u,Ta);

Q-3: MATLAB code for 1-D unsteady conduction with no source


i) Fully Explicit Scheme (f=0):
alpha=1; Ti=100; L=1; Tf=300; N=21; dx=L/(N-1); dt=0.001; t=0.5; nt=(t/dt)+1; % % % % % % % % % thermal diffusivity of wall in m^2/hr Initial temperature of wall at t=0 Wall thickness in m Wall temperature suddenly increased at x=0 and L No. of grid points distance between grid points time step in hr total time in hr no. of time steps

% coefficient matrices for east,west and present grid point % all coefficient are divided by rho*c*dx ap=ones(1,N); aw=((alpha*dt)/(dx^2))*ones(1,N); ae=((alpha*dt)/(dx^2))*ones(1,N); ap_old=ones(1,N); T=zeros(N,nt); % Boundary conditions T(:,1)=Ti; ae(N)=0; aw(1)=0; T(1,:)=Tf; T(N,:)=Tf; % Initial Temperature % Temperature at leftmost boundary at all times % Temperature at rightmost boundary at all times

% Solving equation for explicit scheme f=0 for j=2:nt for i=2:N-1 T(i,j)=(ae(i)*T(i+1,j-1)+aw(i)*T(i-1,j-1)+(ap_old(i)-ae(i)aw(i))*T(i,j-1))/ap(i); end end % Plotting temperature at t=0.1,0.2,0.3,0.4 and 0.5 hr x=0:dx:L; A1=T(:,101); % Temperature at time t=0.1 hr B1=T(:,201); % Temperature at time t=0.2 hr C1=T(:,301); % Temperature at time t=0.3 hr D1=T(:,401); % Temperature at time t=0.4 hr E1=T(:,501); % Temperature at time t=0.5 hr plot(x,A1,'.'); hold on; plot(x,B1,'.'); plot(x,C1,'.'); plot(x,D1,'.'); plot(x,E1,'.'); title('Temperature Plot (Fully Explicit Method)'); xlabel('Distance X (m)'); ylabel('Temperature (degC)');

ii)

Crank Nicholson Scheme (f=0.5):


% % % % % % % % % thermal diffusivity of wall in m^2/hr Initial temperature of wall at t=0 Wall thickness in m Wall temperature suddenly increased at x=0 and L No. of grid points distance between grid points time step in hr total time in hr no. of time steps

alpha=1; Ti=100; L=1; Tf=300; N=21; dx=L/(N-1); dt=0.001; t=0.5; nt=(t/dt)+1;

% coefficient matrices for east,west and present grid point % all coefficient are divided by rho*c*dx A=zeros(N); Ab=zeros(N); aw=((alpha*dt)/(dx^2))*ones(N,1); ae=((alpha*dt)/(dx^2))*ones(N,1); ap_old=ones(N,1); ap=ae/2+aw/2+ap_old; b=zeros(N,1); T=zeros(N,nt);

T(:,1)=Ti; T(1,:)=Tf; T(N,:)=Tf; ae(N)=0; aw(N)=0; aw(1)=0; ae(1)=0; ap(1)=1; ap(N)=1;

% Initial temperature at t=0 % Temperature at leftmost boundary at all times % Temperature at rightmost boundary at all times

% Evaluation of matrix Ab used in calculation of vector % b=(ae/2)*Te_old+(aw/2)*Tw_old+(ap_old-ae/2-aw/2)*Tp_old = Ab*T_old for i=1:N-1 Ab(i,i)=(ap_old(i)-ae(i)/2-aw(i)/2); Ab(i,i+1)=ae(i)/2; Ab(i+1,i)=aw(i+1)/2; end Ab(N,N)=ap(N);

for j=2:nt % assigning values to coefficient matrix A to solve for equation AT=b for i=1:N-1 A(i,i)=ap(i); A(i,i+1)=-ae(i)/2; A(i+1,i)=-aw(i+1)/2; end A(N,N)=ap(N);

%Evaluating right-hand side vector b for each time step j for i=1:N bp=0; for p=1:N bp=bp+Ab(i,p)*T(p,j-1); end b(i)=bp; end % Applying Thomas Algorithm or TDMA for the tridiagnol matrix % Forward Elimination for i=2:N A(i,i)=A(i,i)-(A(i,i-1)/A(i-1,i-1))*A(i-1,i); b(i)=b(i)-(A(i,i-1)/A(i-1,i-1))*b(i-1); end % Backward Substitution T(N,j)=b(N)/A(N,N); for i=N-1:(-1):1 T(i,j)=(b(i)-A(i,i+1)*T(i+1,j))/A(i,i); end end x=0:dx:L; A1=T(:,101); B1=T(:,201); C1=T(:,301); D1=T(:,401); E1=T(:,501);

% % % % %

Temperature Temperature Temperature Temperature Temperature

at at at at at

time time time time time

t=0.1 t=0.2 t=0.3 t=0.4 t=0.5

hr hr hr hr hr

plot(x,A1,'.'); hold on; plot(x,B1,'.'); plot(x,C1,'.'); plot(x,D1,'.'); plot(x,E1,'.'); title('Temperature Plot (Crank-Nicolson Scheme)'); xlabel('Distance X (m)'); ylabel('Temperature (degC)');

iii) Fully Implicit Scheme (f=1):

alpha=1; Ti=100; L=1; Tf=300; N=21; dx=L/(N-1); dt=0.001; t=0.5; nt=(t/dt)+1;

% % % % % % % % %

thermal diffusivity of wall in m^2/hr Initial temperature of wall at t=0 Wall thickness in m Wall temperature suddenly increased at x=0 and L No. of grid points distance between grid points time step in hr total time in hr no. of time steps

% coefficient matrices for east,west and present grid point % all coefficient are divided by rho*c*dx A=zeros(N); aw=((alpha*dt)/(dx^2))*ones(N,1); ae=((alpha*dt)/(dx^2))*ones(N,1); ap_old=ones(N,1); ap=ae+aw+ap_old; b=zeros(N,1); T=zeros(N,nt);

T(:,1)=Ti; T(1,:)=Tf; T(N,:)=Tf; ae(N)=0; aw(N)=0; aw(1)=0; ae(1)=0; ap(1)=1; ap(N)=1; for j=2:nt

% Initial temperature at t=0 % Temperature at leftmost boundary at all times % Temperature at rightmost boundary at all times

% assigning values to coefficient matrix A to solve for equation AT=b for i=1:N-1 A(i,i)=ap(i); A(i,i+1)=-ae(i); A(i+1,i)=-aw(i+1); end A(N,N)=ap(N); b=T(:,j-1); % b=ap_old*T_old = T_old as ap_old=1

% Applying Thomas Algorithm or TDMA for the tridiagnol matrix % Forward Elimination for i=2:N A(i,i)=A(i,i)-(A(i,i-1)/A(i-1,i-1))*A(i-1,i); b(i)=b(i)-(A(i,i-1)/A(i-1,i-1))*b(i-1); end

% Backward Substitution T(N,j)=b(N)/A(N,N); for i=N-1:(-1):1 T(i,j)=(b(i)-A(i,i+1)*T(i+1,j))/A(i,i); end end x=0:dx:L; A1=T(:,101); B1=T(:,201); C1=T(:,301); D1=T(:,401); E1=T(:,501);

% % % % %

Temperature Temperature Temperature Temperature Temperature

at at at at at

time time time time time

t=0.1 t=0.2 t=0.3 t=0.4 t=0.5

hr hr hr hr hr

plot(x,A1,'.'); hold on; plot(x,B1,'.'); plot(x,C1,'.'); plot(x,D1,'.'); plot(x,E1,'.'); title('Temperature Plot (Fully Implicit Method)'); xlabel('Distance X (m)'); ylabel('Temperature (degC)');

Code for Analytical Solution:


% Analytical Solution L=1; ftym=0.5; dx=0.05; dt=0.001; x=0:dx:L; t=0:dt:ftym; Nx=L/dx+1; Nt=ftym/dt+1; Ti=100; Ts=300; alpha=1; eps=10e-4; T=zeros(Nx,Nt); % Wall thickness in m % final time t=0.5 hr % grid size % time steps % Distance vector with increment of dx % Time vector with time step as dt % No. of terms in vector x % No. of terms in vector t % Initial temperature % Temperature at eastmost and westmost boundaries at all times % diffusivity in m^2/hr % tolerance % Defining temperature matrix

for i=1:Nx for j=2:Nt s=1; % Flag variable m=1; while(s~=0) temp=T(i,j); T(i,j)=T(i,j)+2*(Ti-Ts)*exp(-(((m*pi/L)^2)*alpha*(j1)*dt))*((2*sin((m*pi*(i-1)*dx)/L))/(m*pi)); % Analytical Series if(abs(T(i,j)-temp)<eps) s=0; end m=m+2; end T(i,j)=T(i,j)+Ts; p(i,j)=m; end end % Plotting temperature profiles A1=T(:,101); % B1=T(:,201); % C1=T(:,301); % D1=T(:,401); % E1=T(:,501); % plot(x,A1,x,B1,x,C1,x,D1,x,E1); Temperature Temperature Temperature Temperature Temperature at at at at at time time time time time t=0.1 t=0.2 t=0.3 t=0.4 t=0.5 hr hr hr hr hr

Q-4: MATLAB code for 2-D steady conduction with no source


H=2; L=1; Te=0; Tw=0; Tn=0; Ts=100; Ti=33; dx=0.05; dy=0.05; Nx=L/dx+1; Ny=H/dy+1; l=0; Rp=1e-8; e=10e-4; s=1; % % % % % % % % % % % % Wall height in m Wall thickness in m Prescribed temperature on east face (x=L) of wall (degC) Prescribed temperature on west face (x=0) of wall (degC) Prescribed temperature on north face (y=H) of wall (degC) Prescribed temperature on south face (y=0) of wall (degC) Initial guess for temperature grid size in x-direction grid size in y-direction No. of grid points in x-direction No. of grid points in y-direction variable for keeping a count on number of iterations performed during Gauss-Siedel % initial residual error for all grid points % maximum allowable residual error or tolerance epsilon % flag variable

% coefficients for east (ae),west (aw), north (an), south (as) and present % % grid point (ap) aw=(dy/dx); ae=(dy/dx); an=(dx/dy); as=(dx/dy); ap=aw+ae+an+as; % Prescribed temperature boundary condition on all faces of wall T=Ti*ones(Nx,Ny); T(1,:)=Tw; T(Nx,:)=Te; T(:,1)=Ts; T(:,Ny)=Tn; % Gauss-Siedel point by point iterative method while(s~=0) s=0; l=l+1; for j=2:Ny-1 for i=2:Nx-1 T(i,j)=(ae*T(i+1,j)+aw*T(i-1,j)+an*T(i,j+1)+as*T(i,j-1))/ap; end end for j=2:Ny-1 for i=2:Nx-1 % Calculation of Residual error Rp for each point on grid Rp(i,j)=ae*T(i+1,j)+aw*T(i-1,j)+an*T(i,j+1)+as*T(i,j-1)-ap*T(i,j); if(abs(Rp(i,j))>e) s=1; % Convergence criteria end end end end

% Plotting temperature contours x=0:dx:L; y=0:dy:H; Ttrans=T'; [C,h]=contour(x,y,Ttrans); clabel(C,'manual'); title('Temperature Contour Plot'); xlabel('Distance X (m)'); ylabel('Height Y (m)');

Code for Analytical Solution:


% Analytical Solution L=1; H=2; dx=0.05; dy=0.05; x=0:dx:L; y=0:dy:H; Nx=L/dx+1; Ny=H/dy+1; eps=10e-4; T=zeros(Nx,Ny); T(:,1)=100;

% % % % % % % % %

Thickness of wall in m Height of wall in m Grid size in x-direction Grid size in y-direction Distance vector with increment dx Height vector with increment dy No. of terms in vector x No. of terms in vector y tolerance

% Defining temperature matrix % Temperature at y=0

for i=2:Nx-1 for j=2:Ny-1 s=1; % Flag variable n=1; while(s~=0) temp=T(i,j); ana_term=100*(4/pi)*((sinh((n*pi*(H-((j-1)*dy)))/L)*sin((n*pi*(i1)*dx)/L))/(n*sinh((n*pi*H)/L))); % Analytical series T(i,j)=T(i,j)+ana_term; if(abs(T(i,j)-temp)<eps) s=0; end n=n+2; end p(i,j)=n; end end % plotting temperature contours Ttrans=T'; C=contour(x,y,Ttrans); clabel(C,'manual');

You might also like