1D HEAT TRANSFER EXPLICIT USING FINITE DIFFERENCE METHOD
조회 수: 13 (최근 30일)
이전 댓글 표시
Hi! Just wanna ask why our code wont reach equilibrium even we already increase the number of tme.
Here is our code:
clear, clc, close all
%finite difference method
% dT/dt = alpha * dT^2/dx^2
% dT = u(x, t) solve for dt
% u(x , t) = u(x, t-1) + alpha * dt / dx^2 * (u(x+1, t-1) - 2 * u(x, t-1) + u(x-1, t-1))
% Parameters
L = input ('What is the length of the rod? (in m): ');
disp('L = ');
disp(L);
T = input ('How long do you want the heat diffusion to take? (in s): ');
disp('T = ');
disp(T);
nx = input ('How many spatial points? ');
disp('nx = ');
disp(nx);
alpha = input ('What is your thermal diffusitivity? ');
disp('alpha = ');
disp(alpha);
%Thermal Diffusivity of different materials m^2/s
x = linspace(0, L, nx);
dx = x(2) - x(1);
dt = 0.5*(dx^2)/(2*alpha);
t = 0:dt:T;
u = zeros(nx, length(t));
% Initial condition (u(x,0) = initial temperature distribution)
u(:, 1) = input ('What is the constant temperature at the rest of the rod? ');
disp('u(:, 1) = ');
disp(u(:, 1));
% Boundary conditions (u(0, t) = u(L, t) = 0 for insulated ends)
u(1, : ) = input ('What is the thermal boundary on the left side? ');
disp('u(1, : ) = ');
disp(u(1, : ));
u(nx, : ) = input ('What is the thermal boundary on the right side? ');
disp('u(nx, : ) = ');
disp(u(nx, : ));
% Finite difference method (explicit)
for i = 2:length(t)
for j = 2:nx-1
u(j, i) = u(j, i-1) + alpha * dt / dx^2 * (u(j+1, i-1) - 2 * u(j, i-1) + u(j-1, i-1));
end
end
for i = 1:length(t)
u(:, i)
end
% Plotting the temperature distribution
figure(1);
[T, X] = meshgrid(t, x);
surf(X, T, u);
zlim([0 100])
xlabel('Distance');
ylabel('Time');
zlabel('Temperature');
title('1D Heat Equation Solution using Finite Differences');
figure(2);
subplot(2,1,1),
contour(X,u,T);,
colormap;
title('Temperature (Steady State)'), xlabel('Distance'),ylabel('Temperature'), colorbar
subplot(2,1,2),
pcolor(X,T,u);,
shading interp;,
title('Temperature (Steady State)'),xlabel('Distance'), ylabel('Time'), colorbar
댓글 수: 1
Torsten
2023년 12월 2일
Please hardcode the inputs so that we can execute the code and reproduce your problem.
답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!