Trouble with my for loop and graph

2 views (last 30 days)
Kyle Langford
Kyle Langford on 3 May 2023
Commented: Kyle Langford on 3 May 2023
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 Comments
Kyle Langford
Kyle Langford on 3 May 2023
@Torsten Yes, sorry about that. I didn't realize it went back to text format.
I am not sure where to reference the T matrix in my A matrix. I am supposed to be finding dT/dt=A*T+d, but we don't know T besides Ti and Tf.
After developing the time discretization Solving for T gives B^-1*C*T+B^-1*dt*d
I am just confused on how to apply this to the code.
Kyle Langford
Kyle Langford on 3 May 2023
@Torsten That's beautiful. I willt try to manipulate my code to get similar results. that is really clean. I am still pretty new to coding, so I don't know all the best ways to manipulate code.
Thank you for the help!

Sign in to comment.

Answers (0)

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!