Trouble with my for loop and graph
1 回表示 (過去 30 日間)
古いコメントを表示
The following code is not plotting the temperature distriutuon as desired. The graph should be much smoother. I ran this by my teacher. He said all of my coeficients are good, but was no help in fixing the code. All the math is good, worked out and approved. something is just weird with my code. Hoping someone can see something obvious that I may be missing. Edit: I added an attachment of the i=2:n-2 loop, as well as the governing equations/PDE. The boundary conditions are just the Ri and Rf values with the initial conditions of Ti= 20 degrees C and Tf is 790 C
clear
clc
close all
%Define Constants
rho = 150;
cp = 2300;
k = 0.30;
Ri = 0.0716;
Ro = 0.0970;
Ti = 20;
Tf = 790;
alpha = k/(rho*cp);
q_ppp = -100000;
%Position and Time
N=51;
r=linspace(Ri, Ro, N);
dr=r(2)-r(1);
ti=0;
tf=60;
dt=0.01;
t=ti:dt:tf;
M=length(t);
T=zeros(N,M); %(Row,Column)
T(:,1) = Ti; %Ti
T(N,:)= Tf; %Tf
% % 5 Spatial Discretization
A = zeros(N-1,N-1);
d = zeros(N-1,1);
b1 = -(2*alpha/(dr^2)); % for i=1, T2
c1 = 2*alpha/(dr^2); %i=1, T1
bn = -2*alpha/dr^2; %bn-1, %Tn-1
an = ((alpha/(dr^2))*(1-(dr/(r(N-1))))); %an-1, Tn-2
A(1,1) = b1;
A(1,2) = c1;
A(N-1,N-1) = bn;
A(N-1,N-2) = an;
for j = 2:N-2
A(j,j) = (-2*alpha/(dr^2)); %b, Ti
A(j,j-1) = (alpha/dr^2)-(alpha/(r(j)*dr)); %a, %ti-1
A(j,j+1) = (alpha/dr^2)+(alpha/(r(j)*dr)); %c, Ti+1
end
d(1) = q_ppp/(rho*cp);
d(N-1) = q_ppp/(rho*cp);
for j = 2:N-2
d(j) = q_ppp/(rho*cp);
end
% 6 Time Discretization
I = eye(N-1);
B = I - (dt/2)*A;
C = I + (dt/2)*A;
Binv = inv(B);
for n=1:M-1
T(1:N-1,n+1) = Binv*C*T(1:N-1,n) + Binv*dt*d;
end
disp('Plot')
plot(r,T(:,3000),'b')
title('Temperature Distribution')
xlabel('X')
ylabel('y')
hold on
grid on
grid minor
box on
6 件のコメント
回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!