Hi all.
I try to solve 1D diffusion problem for diffusion through a rod. ∇⋅(-k∇T) = 0
k is dependent of the temperature
k=k0+alpha*T
Since k (k1) isn't constant and thereby the equation isn't linear I will have to make iterations.
My issue is though that my outer iteration forloop doesn't seem to work. It's like its not overwriting the initial temperature.
Can someone please help me out on this?
clear all
close all
p=4;
Ta=100;
Tb=500;
Area=10*10^(-3);
k=1000;
L=0.5;
N=10;
alpha=10;
x=linspace(0,L,N);
T1=(Ta+(Tb-Ta)/L*x)';
dx=L/N;
a=zeros(N,N);
b=zeros(N,1);
for i=1:p
k1(:,i)=k+T1(:,i)*alpha;
for j=2:1:N-1
ae=k1(j,i)*Area/dx;
aw=k1(j,i)*Area/dx;
ap=ae+aw;
a(j,j-1)=-aw;
a(j,j)=ap;
a(j,j+1)=-ae;
b(j,1)=0;
end
j=1;
aw=2*k1(j,i)*Area/dx;
ae=k1(j,i)*Area/dx;
ap=ae+aw;
a(j,j)=ap;
a(j,j+1)=-ae;
b(j,1)=aw*Ta;
j=N;
aw=k1(j,i)*Area/dx;
ae=2*k1(j,i)*Area/dx;
ap=ae+aw;
a(j,j-1)=-aw;
a(j,j)=ap;
b(j,1)=ae*Tb;
T2=(a^-1*b)';
T1(:,i+1)=T2;
end
xsol=dx/2:dx:L-dx/2;
xexact=0:L/100:L;
Texact=100+800*xexact;
plot(xsol,T1(:,i),'bs',xexact,Texact,'r')
xlabel('x-axis')
ylabel('Temperature')
legend('Numerical','Exact')
2 Comments
Rik (view profile)
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/478511-for-loop-for-iteration#comment_741264
KALYAN ACHARJYA (view profile)
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/478511-for-loop-for-iteration#comment_741420
Sign in to comment.