Verifying my code for this problem, are these the right solution and graphs?
Mostrar comentarios más antiguos

clc; clear; close all;
% Parameters
L = 10; % mm
W = 4; % mm
dx = 1; % mm
dy = 1; % mm
T_inf = 22; % Ambient temperature (°C)
Tb = 50; % Base plate temperature (°C)
h = 630; % Convective heat transfer coefficient (W/m^2.K)
k = 20; % Thermal conductivity (W/m.K)
% Grid size
Nx = L/dx + 1;
Ny = W/dy + 1;
% Initialize temperature field
T = ones(Nx, Ny) * T_inf; % Start with ambient temperature
% Set bottom boundary condition
T(:,1) = Tb;
% Convection boundary condition coefficient
conv_coeff = h * dy / k;
% Iteration parameters
tol = 1e-4;
max_iter = 5000;
error = 1;
iter = 0;
% Jacobi Iteration
while error > tol && iter < max_iter
T_old = T;
for i = 2:Nx-1
for j = 2:Ny-1
T(i,j) = (T_old(i+1,j) + T_old(i-1,j) + T_old(i,j+1) + T_old(i,j-1)) / 4;
end
end
% Apply convective boundary conditions
T(1, :) = (conv_coeff * T_inf + T_old(2, :)) / (1 + conv_coeff); % Left boundary
T(Nx, :) = (conv_coeff * T_inf + T_old(Nx-1, :)) / (1 + conv_coeff); % Right boundary
T(:, Ny) = (conv_coeff * T_inf + T_old(:, Ny-1)) / (1 + conv_coeff); % Top boundary
% Compute error
error = max(max(abs(T - T_old)));
iter = iter + 1;
end
fprintf('Converged in %d iterations\n', iter);
% (b) Plot temperature profile along vertical lines at x=1mm, x=4mm, x=6mm
x_positions = [2, 5, 7]; % Corresponding to x=1mm, x=4mm, x=6mm
figure;
hold on;
for x_index = x_positions
plot(T(x_index, :), 'LineWidth', 2);
end
xlabel('Vertical position (y-axis)');
ylabel('Temperature (°C)');
legend('x=1mm', 'x=4mm', 'x=6mm');
title('Temperature Profile along Vertical Lines');
grid on;
hold off;
% (c) Plot temperature contour
figure;
contourf(T', 20);
colorbar;
xlabel('x (mm)');
ylabel('y (mm)');
title('Temperature Contour of the Rib');
% (d) Refine the grid with dx=dy=0.5mm
dx = 0.5; dy = 0.5;
Nx = L/dx + 1;
Ny = W/dy + 1;
T_fine = ones(Nx, Ny) * T_inf;
T_fine(:,1) = Tb;
conv_coeff = h * dy / k;
error = 1;
iter = 0;
while error > tol && iter < max_iter
T_old = T_fine;
for i = 2:Nx-1
for j = 2:Ny-1
T_fine(i,j) = (T_old(i+1,j) + T_old(i-1,j) + T_old(i,j+1) + T_old(i,j-1)) / 4;
end
end
T_fine(1, :) = (conv_coeff * T_inf + T_old(2, :)) / (1 + conv_coeff);
T_fine(Nx, :) = (conv_coeff * T_inf + T_old(Nx-1, :)) / (1 + conv_coeff);
T_fine(:, Ny) = (conv_coeff * T_inf + T_old(:, Ny-1)) / (1 + conv_coeff);
error = max(max(abs(T_fine - T_old)));
iter = iter + 1;
end
fprintf('Refined grid converged in %d iterations\n', iter);
% Compare temperature profile at x=4mm for both grids
x_index_coarse = 5; % Corresponding to x=4mm in the coarse grid
x_index_fine = 9; % Corresponding to x=4mm in the fine grid
figure;
plot(T(x_index_coarse, :), 'r--', 'LineWidth', 2);
hold on;
plot(T_fine(x_index_fine, :), 'b-', 'LineWidth', 2);
xlabel('Vertical position (y-axis)');
ylabel('Temperature (°C)');
legend('Coarse grid (1mm)', 'Fine grid (0.5mm)');
title('Comparison of Temperature Profiles at x=4mm');
grid on;
hold off;
1 comentario
Respuestas (1)
Look at the contour plot. The temperature at x = 0 should be at T_b = 50°C. This is not the case in your graphics. Maybe you interchanged the axes ?
And why do the temperature profiles in your last plot start at 1 and end at 9 ? And why does the red curve end at 5 ? Both curves should start at 0 and end at 4.
I suggest creating
x = linspace(0,L,Nx)
y = linspace(0,W,Ny)
in both cases (coarse and fine) so that you can make plots over the correct x- and y-grids.
Categorías
Más información sobre Mathematics en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





