I would like to store store the change in temp in each node for each time step

9 views (last 30 days)
So if you run the code. The temperature for each node is changing for the set time step. I would like to store the change of temp in each node for each time step. Could someone help out please.

Answers (1)

Alan Stevens
Alan Stevens on 6 Jul 2020
Edited: Alan Stevens on 6 Jul 2020
I think the following does your desired calculations:
%Parameters
rho = 1; %density, kg/m^3
cp = 1; %specific heat, J/kgK
K = 1; %thermal conductivity, w/mK
h = 1; %heat transfer conefficient, W/(m^2K)
V = 3; %V, voltage
I = 1.5; %A, current
P = V*I; %W, power
%Domain and step
Lx = 7; %length, m
q_gen = P/Lx; %W/m, heat rate per length
A = K/rho/cp; %aplha
Nx = 51; Nt = 100;
dx = Lx/(Nx-1);
%Satisfy CFL condition
c = 1;
C = 0.1; %CFL condition, C<1
dt = 0.5*dx^2;
%Field variables: rows = space, columns = time
Tn = zeros(Nx,Nt); %Temperature
x = linspace(0, Lx, Nx); %x distance
%Initial condition
Tn(:,1) = 25; % Initial temperature at all values of x
Tn(1,:) = 0; % Left-hand temperature for all time
Tn(end,:) = 90; % Right-hand temperature for all time
Tc = Tn(:,1); % initialise previous timestep temperatures
DeltaT = zeros(Nx,Nt-1); %For storing temperature changes
t = 0; % Start time
%Loop
for n = 1:Nt
t = t+dt;
%New temperature
for i = 2:Nx-1
Tn(i, n+1) = Tc(i) + dt*A*((Tc(i+1) -2*Tc(i) + Tc(i-1)))/dx^2;
end
%Source term
Sx = 2: 2: Nx;
if (t<5)
Tn(Sx,:) = Tn(Sx,:) + dt*q_gen/rho/cp;
end
DeltaT(:,n) = Tn(:,n) - Tc; % store temperature changes at each node
Tc = Tn(:,n); %Save the previous timestep temperatures
%Visualize
plot(x, Tn(:,1:n)); set(gca, 'ylim', [0 100]);
xlabel('Distance along rod'); ylabel('Temperature');
title(sprintf('Time = %f seconds', t));
pause(0.01)
end
ts = 20; % look at change from timestep ts-1 to ts
figure
plot(x,DeltaT(:,ts))
  3 Comments
Alan Stevens
Alan Stevens on 7 Jul 2020
Edited: Alan Stevens on 7 Jul 2020
Tn(i, n+1)
This selects the i'th row of the (n+1)'th column of matrix Tn, so is a single value.
Tc(i)
Selects the i'th row of the vector Tc, so is also a single value.
DeltaT(:,n) = Tn(:,n) - Tc;
The colon (:) here means use all rows, so the RHS means select all the rows of the n'th column of matrix Tn, resulting in a 51x1 vector, then subtract, element by element, the values in Tc, which is also a 51x1 vector. The results are stored in the n'th column of the DeltaT matrix. I included this because you asked for a store for the temperature changes from one timestep to the next.
Tc = Tn(:,n);
The RHS selects elements from all the rows of the n'th column of matrix Tn and assigns them to vector Tc. Tc only ever stores the previous timestep value for use in the next timestep; it doesn't store all the previous values (which would be unnecessary as they are stored in Tn.
I hope this clarifies the situation for you.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!