This commit is contained in:
lda committed 2025-06-26 10:57:21 +00:00
1 parent 1cadeecff2
commit 14d38cc163
3 files changed
+24 -38

No files matched your search

+3 -1
View File
@@ -20,7 +20,9 @@ function plotProfiles(x_coords, profiles_data, labels)
xlabel('Position along bar, x (m)'); xlabel('Position along bar, x (m)');
ylabel('Temperature, T (^oC)'); ylabel('Temperature, T (^oC)');
title('Temperature Profiles Over Time'); title('Temperature Profiles Over Time');
legend('Location', 'southeast'); if length(profiles_data) <= 15
legend('Location', 'ne');
end
ylim([0 100]); % Set y-limits to reasonable temperature range ylim([0 100]); % Set y-limits to reasonable temperature range
hold off; hold off;
end end
+7 -2
View File
@@ -3,14 +3,19 @@ thermal_cond.Tg = 100; thermal_cond.Td = 50; thermal_cond.T0 = 0;
steps.dx = 0.01; steps.dt = 0.5; steps.dx = 0.01; steps.dt = 0.5;
t_final_s_sim = 2000; % Total simulation time t_final_s_sim = 2000; % Total simulation time
plot_points = [0, 10, 50, 100, 200, 500, 1000, 2000]; % Specific times to capture profiles % 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]);
% Call the solver function % Call the solver function
[x_coords, final_T_profile, midpoint_T_series, time_vec, all_temp_profiles, profile_labels] = ... [x_coords, final_T_profile, midpoint_T_series, time_vec, all_temp_profiles, profile_labels] = ...
solveHeatEquation(material, thermal_cond, steps, t_final_s_sim, plot_points); solveHeatEquation(material, thermal_cond, steps, t_final_s_sim, plot_points);
% Create and populate the figure with subplots % Create and populate the figure with subplots
figure('Color', 'white', 'Position', [100, 100, 1000, 400]); figure('Color', 'white', 'Position', [100, 100, 1920, 1080]);
subplot(1, 2, 1); subplot(1, 2, 1);
plotProfiles(x_coords, all_temp_profiles, profile_labels); plotProfiles(x_coords, all_temp_profiles, profile_labels);
+14 -35
View File
@@ -49,20 +49,15 @@ function [x, T_final_profile, T_time_series, time_vector, all_profiles, all_prof
T_time_series = zeros(1, Nt); T_time_series = zeros(1, Nt);
T_time_series(1) = T_current(mid_index); T_time_series(1) = T_current(mid_index);
% Store profiles for plotting (initialization, before the main loop) % Store profiles for plotting
all_profiles = cell(size(plot_time_points)); % Pre-allocate all_profiles = cell(length(plot_time_points), 1);
all_profile_times = cell(size(plot_time_points)); all_profile_times = cell(length(plot_time_points), 1);
% Track if a profile for a given plot_time_point has been captured % Capture initial profile (t=0)
% This ensures each target time is captured only once and correctly. if ismembertol(0, plot_time_points, dt/2, 'DataScale', 1)
captured_flags = false(size(plot_time_points)); all_profiles{1} = T_current;
all_profile_times{1} = sprintf('t = %.0f s', 0); % Using sprintf for consistent formatting
% Capture initial profile (t=0) - this should always be the first plot point end
% Assuming plot_time_points(1) is 0
all_profiles{1} = T_current;
all_profile_times{1} = sprintf('t = %.0f s', plot_time_points(1));
captured_flags(1) = true; % Mark t=0 as captured
% Main Time Loop % Main Time Loop
for n = 1:Nt-1 for n = 1:Nt-1
T_next = T_current; % Initialize T_next. Boundary conditions are retained. T_next = T_current; % Initialize T_next. Boundary conditions are retained.
@@ -77,30 +72,14 @@ function [x, T_final_profile, T_time_series, time_vector, all_profiles, all_prof
% Store midpoint temperature % Store midpoint temperature
T_time_series(n+1) = T_current(mid_index); T_time_series(n+1) = T_current(mid_index);
% Store full profiles at specified plot times % Store full profiles at specified plot times
% Iterate through plot_time_points to find matches % Use a small tolerance for floating point comparison to match plot_time_points
for p_idx = 1:length(plot_time_points) [is_plot_time, idx_to_store] = ismembertol(time_vector(n+1), plot_time_points, dt/2, 'DataScale', 1);
if ~captured_flags(p_idx) % Only try to capture if not already captured if is_plot_time
% Check if current simulation time (time_vector(n+1)) is at or just past the target plot time all_profiles{idx_to_store} = T_current;
% Use a small tolerance relative to dt to account for floating point all_profile_times{idx_to_store} = sprintf('t = %.0f s', time_vector(n+1));
if time_vector(n+1) >= plot_time_points(p_idx) - dt/2 && time_vector(n+1) < plot_time_points(p_idx) + dt/2
all_profiles{p_idx} = T_current;
all_profile_times{p_idx} = sprintf('t = %.0f s', plot_time_points(p_idx)); % Use the *target time* for label
captured_flags(p_idx) = true; % Mark as captured
end
end end
end end
% Add a check at the very end of the simulation to ensure the final plot_time_point is captured
% if t_final_s matches one of the plot_time_points and it wasn't captured exactly during the loop
[is_final_plot_time, idx_final_plot] = ismembertol(t_final_s, plot_time_points, dt/2);
if is_final_plot_time && ~captured_flags(idx_final_plot)
all_profiles{idx_final_plot} = T_current;
all_profile_times{idx_final_plot} = sprintf('t = %.0f s', t_final_s);
captured_flags(idx_final_plot) = true;
end
end
T_final_profile = T_current; % The profile at the very end of the simulation T_final_profile = T_current; % The profile at the very end of the simulation
end end