You are on page 1of 6

Q.

1 MATLAB Code
clc
clear
la=input('length of the material A in meters=');
ka=input('enter the thermal conductivity of the material A=');
lb=input('length of the material B in meters=');
kb=input('enter the thermal conductivity of the material B=');
lc=input('length of the material C in meters=');
kc=input('enter the thermal conductivity of the material C=');
tf=input('enter the temp of the fluid=');
t2=input('enter the temp at boundary 2=');
h=input('enter heat transfer coefficient between fluid and matl A=');
ma=input('no. of control volumes for material A=');
mb=input('no. of control volumes for material B=');
mc=input('no. of control volumes for material C=');
dxa=la/ma;
dxb=lb/mb;
dxc=lc/mc;
aea=ka/dxa;
awa=aea;
apa=aea+awa;
aea1=2*aea;
awa1=2*awa;
apa1=awa1+aea;
apa2=aea+aea1;
aeb=kb/dxb;
awb=aeb;
apb=aeb+awb;
aeb1=2*aeb;
awb1=2*awb;
apb1=awa1+aeb;
apb2=aeb+aeb1;
aec=kc/dxc;
awc=aec;
apc=aec+awc;
aec1=2*aec;
awc1=2*awc;
apc1=awc1+aec;
apc2=aec+aec1;
apab=awa+aeb;
apbc=awb+aec;
apo=awa1+h;
na=ma+2;
nb=mb+1;
nc=mc+1;

n=na+nb+nc;
old_m=zeros(n,n);
old_m(1,1)=apo;
for i=2:na
old_m(i,(i-1))=-awa;
old_m((i-1),i)=-aea;
old_m(i,i)=apa;
end
old_m(2,1)=-awa1;
old_m(1,2)=-aea1;
old_m((na-1),na)=-aea1;
old_m(2,2)=apa1;
old_m(na,na)=apab;
old_m((na-1),(na-1))=apa2;
for i=(na+1):(na+nb)
old_m(i,i)=apb;
old_m((i-1),i)=-aeb;
old_m(i,(i-1))=-awb;
end
old_m((na+1),(na+1))=apb1;
old_m((na+nb-1),(na+nb-1))=apb2;
old_m((na+1),na)=-awa1;
old_m((na+nb-1),(na+nb))=-aeb1;
old_m((na+nb),(na+nb))=apbc;
for i=(na+nb+1):(n-1)
old_m(i,i)=apc;
old_m((i-1),i)=-aec;
old_m(i,(i-1))=-awc;
end
old_m((na+nb+1),(na+nb+1))=apc1;
old_m((n-1),(n-1))=apc2;
old_m((na+nb+1),(na+nb))=-awc1;
old_m((n-1),n)=-aec1;
old_m(n,n)=1;
old_m
c=zeros(n,1);
c(1,1)=h*tf;
c(n,1)=t2;
T=inv(old_m)*c;
T
N=1:n;
plot(N,T,'o');
grid on;

Q.2 MATLAB Code


clear
clc
l=input('enter the length of the rod in m=');
k=input('enter thermal conductivity of the rod in W/mK=');
t1=input('enter the temp of the left face=');
Su=input('enter the source term Su from Su-SpTp=');
Sp=input('enter the source term Sp from Su-SpTp=');
q=input('enter the flux through the right face in W/m2(assume flux is going
out of the rod. If its entering from right, give ve value)=');
m=input('enter the no. of the CV=');
dx=l/m;
ae=k/dx;
aw=ae;
ae1=2*ae;
aw1=2*aw;
awn=2*aw;
ap=ae+aw+Sp*dx;
ap1=ae1+aw+Sp*dx;
ap2=ae+aw1+Sp*dx;
ap3=ae1-Sp*0.5*dx
n=m+2;
z=Su*dx*0.5;
old_m=zeros(n,n);
old_m(1,1)=1
for i=3:(n-1)
old_m(i,(i-1))=-aw;
old_m((i-1),i)=-ae;
old_m(i,i)=ap;
end
old_m(2,2)=ap1;
old_m((n-1),(n-1))=ap2;
old_m(n,(n-1))=ap3;
old_m(2,1)=-aw1;
old_m((n-1),n)=-ae1;
old_m(n,n)=-ae1;
old_m
c=zeros(n,1);
c(1,1)=100;
for i=2:(n-1)
c(i,1)=z;
end
c(n,1)=q-z;
J=inv(old_m);
T=J*c;
T

Q.3 MATLAB Code


clc
clear
fprintf('Problem with variable k\n');
l=input('lenght of the rod=');
m=input('No.of CV=');
Ta=input('Temp of the left boundary=');
Tb=input('Temp of the right boundary=');
ko=input('Value of ko=');
alpha=input('Value of alpha=');
conv=input('Convergence value=');
n=m+2;
[Told,T1,T2,k]=deal(zeros(n,1));
[ke,kw,c]=deal(zeros(m,1));
Told(1)=Ta;
Told(n)=Tb;
%global for loop
for j=1:10000
%k matrix
for i=2:n-1
k(i)=ko*(1+alpha*(Told(i)));
end
k(1)=ko*(1+alpha*Ta);
k(n)=ko*(1+alpha*Tb);
%ke matrix
for i=1:m-1
ke(i)=2*k(i)*k(i+1)/(k(i)+k(i+1));
end
ke(m)=ko*(1+alpha*Tb);
%kw matrix
for i=2:m
kw(i)=ke(i-1);
end
kw(1)=ko*(1+alpha*Ta);
%old_m Matrix
old_m=zeros(m);
for i=2:m
old_m(i,i)=ke(i)+kw(i);
old_m(i-1,i)=-ke(i-1);
old_m(i,i-1)=-kw(i);
end
old_m(1,1)=ke(1)+2*kw(1);
old_m(m,m)=2*ke(m)+kw(m);

%C matrix
c(1)=2*kw(1)*Ta;
c(m)=2*ke(m)*Tb;

new_m=inv(old_m)*c;
T1(1)=Ta;
T1(n)=Tb;
for i=2:n-1
T1(i)=new_m(i-1);
end
%convergence matrix
for i=1:n
T2(i)=sqrt(((Told(i)-T1(i))^2)/m);
end
A=max(T2);
%check for convergence
if A<conv
break
end
%redefine Told
for i=1:n
Told(i)=T1(i);
end
end
T1
N=1:n;
plot(N,T1);
grid on
hold on
xlabel('Node Points');
ylabel('Temperature');
plot(N,T1,'o');