shipping this one.
idfc teach deadass know we'll use chatgpt. im one of them.
This commit is contained in:
1 parent
fa5d845478
commit
45533fd1ff
4 files changed
+158
-9
No files matched your search
@@ -0,0 +1,150 @@
|
||||
%% Project: 1D Heat Transfer Simulation (Numerical Solution of Heat Equation)
|
||||
% This Live Script provides a complete solution to the MATLAB Project focusing
|
||||
% on determining heat transfer through a 1D bar by conduction using the explicit
|
||||
% finite difference method. It addresses all questions from the project document
|
||||
|
||||
%% Define Physical Parameters and Material Properties
|
||||
|
||||
% Bar Dimensions and Discretization
|
||||
L = 0.4; % Length of the bar (m)
|
||||
dx = 0.01; % Spatial step size (m)
|
||||
dt = 0.5; % Time step size (s)
|
||||
|
||||
% Thermal Conditions
|
||||
Tg = 100; % Temperature at left end (x=0) in Celsius
|
||||
Td = 50; % Temperature at right end (x=L) in Celsius
|
||||
T0 = 0; % Initial temperature of the bar (internal nodes) in Celsius
|
||||
|
||||
% Material Properties (Aluminum properties as hinted in the document)
|
||||
rho = 2700; % Density (kg/m^3)
|
||||
Cp = 900; % Specific heat capacity (J/kg/C)
|
||||
lambda = 237; % Thermal conductivity (W/m/C)
|
||||
|
||||
% Simulation End Time (chosen to observe steady-state behavior)
|
||||
t_final_s_sim = 2000; % Total simulation time in seconds
|
||||
|
||||
% Time points at which to capture and plot full temperature profiles
|
||||
% This allows for a denser visualization early on, and sparser later as equilibrium is approached.
|
||||
specific_early_points = [0, 5, 10, 20];
|
||||
mid_range_points = 50:50:750;
|
||||
late_range_points = 750:250:t_final_s_sim;
|
||||
plot_time_points = unique([specific_early_points, mid_range_points, late_range_points]);
|
||||
|
||||
%% Use MATLAB to solve the equation.
|
||||
|
||||
% This section provides the MATLAB implementation based on the algorithm and detailed parameters.
|
||||
|
||||
fprintf('--- Solving 1D Heat Equation Project ---\n');
|
||||
|
||||
% Calculate Derived Constants
|
||||
alpha = lambda / (rho * Cp);
|
||||
r = alpha * dt / (dx^2);
|
||||
fprintf('Thermal Diffusivity (alpha): %.4e m^2/s\n', alpha);
|
||||
fprintf('Fourier Number (r): %.4f\n', r);
|
||||
|
||||
if r > 0.5
|
||||
warning('The explicit method is unstable for r > 0.5. Consider reducing dt or increasing dx.');
|
||||
end
|
||||
|
||||
% Spatial Grid Setup
|
||||
x = 0:dx:L; % Spatial grid points
|
||||
Nx = length(x); % Number of spatial nodes
|
||||
|
||||
% Find the index for the midpoint of the bar for plotting the right pane
|
||||
mid_index = find(abs(x - L/2) < 1e-9, 1); % Find index closest to L/2
|
||||
if isempty(mid_index) % Fallback if L/2 isn't an exact node
|
||||
mid_index = round(Nx / 2);
|
||||
end
|
||||
|
||||
% Initialize Temperature Array
|
||||
T_current = zeros(1, Nx); % Initialize temperature array with zeros
|
||||
T_current(2:Nx-1) = T0; % Internal nodes at initial temperature (0 C)
|
||||
T_current(1) = Tg; % Left boundary condition (100 C)
|
||||
T_current(Nx) = Td; % Right boundary condition (50 C)
|
||||
|
||||
% Time Grid Setup
|
||||
time_vector = 0:dt:t_final_s_sim;
|
||||
Nt = length(time_vector); % Number of time steps
|
||||
|
||||
% Storage for results
|
||||
T_midpoint_over_time = zeros(1, Nt); % Stores temp at midpoint over ALL time steps
|
||||
T_midpoint_over_time(1) = T_current(mid_index); % Initial midpoint temp
|
||||
|
||||
all_profiles = cell(size(plot_time_points)); % Stores full profiles at specific times
|
||||
all_profile_times = cell(size(plot_time_points)); % Stores labels for these profiles
|
||||
|
||||
% Capture initial profile (t=0) if it's one of the requested plot_time_points
|
||||
[is_t0_plot_point, idx_t0_plot_point] = ismembertol(0, plot_time_points, dt/2, 'DataScale', 1);
|
||||
if is_t0_plot_point
|
||||
all_profiles{idx_t0_plot_point} = T_current;
|
||||
all_profile_times{idx_t0_plot_point} = sprintf('t = %.0f s', 0);
|
||||
end
|
||||
|
||||
% Main Time-Stepping Loop
|
||||
fprintf('\nStarting simulation...\n');
|
||||
for n = 1:Nt-1 % Loop from t=0 (n=1) to t_final_s (n=Nt)
|
||||
T_next = T_current; % Initialize T_next. Boundary values from T_current are implicitly carried over.
|
||||
|
||||
% Apply finite difference equation for internal nodes (from index 2 to Nx-1)
|
||||
for i = 2:Nx-1
|
||||
T_next(i) = T_current(i) + r * (T_current(i+1) - 2*T_current(i) + T_current(i-1));
|
||||
end
|
||||
|
||||
T_current = T_next; % Update temperature for the next iteration
|
||||
|
||||
% Store midpoint temperature for time series plot
|
||||
T_midpoint_over_time(n+1) = T_current(mid_index);
|
||||
|
||||
% Store full spatial profiles for plotting at specified plot_time_points
|
||||
[is_plot_time, idx_to_store] = ismembertol(time_vector(n+1), plot_time_points, dt/2, 'DataScale', 1);
|
||||
if is_plot_time
|
||||
all_profiles{idx_to_store} = T_current;
|
||||
all_profile_times{idx_to_store} = sprintf('t = %.0f s', time_vector(n+1));
|
||||
end
|
||||
end
|
||||
fprintf('Simulation finished.\n');
|
||||
%% Visualization
|
||||
|
||||
% Create a figure to display the results in two subplots
|
||||
figure('Color', 'white', 'Position', [100, 100, 1000, 400]); % Adjust figure size for better viewing
|
||||
|
||||
% Left Pane: Temperature Profiles Over Time (T vs x)
|
||||
subplot(1, 2, 1);
|
||||
hold on;
|
||||
grid on;
|
||||
box on; % Add a box around the plot
|
||||
|
||||
% Plot each captured profile
|
||||
colors = lines(length(plot_time_points)); % Generate distinct colors based on number of plot points
|
||||
for k = 1:length(plot_time_points)
|
||||
if ~isempty(all_profiles{k}) % Ensure data exists for this plot point
|
||||
plot(x, all_profiles{k}, 'DisplayName', all_profile_times{k}, 'LineWidth', 1.5, 'Color', colors(k,:));
|
||||
end
|
||||
end
|
||||
|
||||
xlabel('Position along bar, x (m)');
|
||||
ylabel('Temperature, T (^oC)');
|
||||
title('Temperature Profiles Over Time');
|
||||
|
||||
% Conditionally display legend if not too many lines
|
||||
if length(plot_time_points) < 8 % Adjust threshold as needed
|
||||
legend('Location', 'southeast');
|
||||
end
|
||||
hold off;
|
||||
|
||||
% Right Pane: Temperature at Midpoint Over Time (T vs t)
|
||||
subplot(1, 2, 2);
|
||||
plot(time_vector, T_midpoint_over_time, 'LineWidth', 2, 'Color', [0.8500 0.3250 0.0980]);
|
||||
grid on;
|
||||
box on; % Add a box around the plot
|
||||
xlabel('Time, t (s)');
|
||||
ylabel('Temperature at Midpoint (^oC)');
|
||||
title('Temperature at Midpoint (x=L/2) Over Time');
|
||||
ylim([0 100]); % Set y-limits to reasonable temperature range
|
||||
xlim([0 t_final_s_sim]); % Set x-limits to the simulation time
|
||||
|
||||
% Final Output
|
||||
fprintf('\nSimulation and plotting complete. Check the generated figure for results.\n');
|
||||
% To save the figure, use:
|
||||
% exportgraphics(gcf, 'Figure_1.pdf', 'ContentType', 'vector');
|
||||
% (Uncomment and run the above line after the figure appears to save it.)
|
||||
File renamed without changes.
@@ -1,17 +1,18 @@
|
||||
clear; clc; close all;
|
||||
|
||||
material.rho = 2700; material.Cp = 900; material.lambda = 237; material.L = 0.4;
|
||||
thermal_cond.Tg = 100; thermal_cond.Td = 50; thermal_cond.T0 = 0;
|
||||
steps.dx = 0.01; steps.dt = 0.5;
|
||||
|
||||
t_final_s_sim = 2000; % Total simulation time
|
||||
% plot_points = [0, 10, 50, 100, 200, 500, 2000]; % Specific times to capture profiles
|
||||
|
||||
specific_early_points = [0, 5, 10, 20];
|
||||
mid = [50:50:750];
|
||||
bar = [750:250:1250];
|
||||
quux = [t_final_s_sim];
|
||||
plot_points = unique([specific_early_points, mid, bar, quux]);
|
||||
mid_range_points = [50:50:750];
|
||||
late_range_points = [750:250:1250];
|
||||
plot_points = unique([specific_early_points, mid_range_points, late_range_points, t_final_s_sim]);
|
||||
|
||||
% Call the solver function
|
||||
[x_coords, final_T_profile, midpoint_T_series, time_vec, all_temp_profiles, profile_labels] = ...
|
||||
[x_coords, midpoint_T_series, time_vec, all_temp_profiles, profile_labels] = ...
|
||||
solveHeatEquation(material, thermal_cond, steps, t_final_s_sim, plot_points);
|
||||
|
||||
% Create and populate the figure with subplots
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
% solveHeatEquation.m
|
||||
function [x, T_final_profile, T_time_series, time_vector, all_profiles, all_profile_times] = ...
|
||||
function [x, T_time_series, time_vector, all_profiles, all_profile_times] = ...
|
||||
solveHeatEquation(material, thermal_cond, steps, t_final_s, plot_time_points)
|
||||
% SOLVEHEATEQUATION Solves the 1D Heat Equation using Explicit Finite Differences.
|
||||
%
|
||||
@@ -84,6 +84,4 @@ function [x, T_final_profile, T_time_series, time_vector, all_profiles, all_prof
|
||||
all_profile_times{idx_to_store} = sprintf('t = %.0f s', time_vector(n+1));
|
||||
end
|
||||
end
|
||||
|
||||
T_final_profile = T_current; % The profile at the very end of the simulation
|
||||
end
|
||||
Reference in new issue
Block a user