Plot not showing all results
I need my graph to show the results from all lengths (i.e. all 5 coloured lines) but am only outputting 2 lines for one of the graphs. Both my code and the graphs are below. Please help.
clc; clear; close all;
%% Section 1: Development of Applied Thermal and Solute Concentration Fields
L_values = linspace(0.03, 0.25, 5); % Separation distance [m] (5 samples for clarity)
Nx = 200; % Number of spatial steps
Nt = 5000; % Number of time steps
dt = 0.005; % Time step size
% Thermal diffusivity values for aluminum alloy (m^2/s)
alpha_L = 3.26e-5; % Liquid phase
alpha_S = 6.58e-5; % Solid phase
% Solute diffusivity in liquid (m^2/s)
D_L = 1e-11;
% Interface velocity range
V = linspace(1.67e-5, 1.67e-4, 20); % Velocity range in m/s
% Partition coefficient & solute parameters
k = 0.85; % Partition coefficient
c0 = 7.26; % Initial solute concentration (wt% Al)
m = -3; % Liquidus slope (K/wt% Al)
[G_range, V_mesh] = meshgrid(linspace(1e4, 1e6, 20), V);
delta_c = sqrt(D_L ./ V_mesh); % Solute boundary layer thickness
G_critical = -2 * m * c0 ./ delta_c; % Constitutional gradient
%% Section 2: Initialize Figures
figure(1); hold on; % Temperature distribution plot
figure(2); hold on; % Solute concentration plot
%% Section 3: Loop Over Different L Values
for L = L_values
dx = L / Nx; % Spatial step size
x = linspace(0, L, Nx); % Position array
s = 0.02; % Initial interface position
% Initialize temperature profile
T = zeros(Nx, 1);
T(1:Nx/2) = linspace(1450, 1350, Nx/2); % Melt zone
T(Nx/2+1:end) = linspace(1150, 900, Nx/2); % Cold zone
%% Section 4: Applied Thermal Gradient (Finite Difference)
for t = 1:Nt
T_new = T;
for i = 2:Nx-1
if x(i) < s
T_new(i) = T(i) + alpha_S * dt / dx^2 * (T(i+1) – 2*T(i) + T(i-1));
else
T_new(i) = T(i) + alpha_L * dt / dx^2 * (T(i+1) – 2*T(i) + T(i-1));
end
end
T = T_new;
s = s + V(1) * dt; % Update interface position
end
%% Section 5: Plot Results for Each L
figure(1); % ensuring we’re plotting on the right figure
plot(x, T, ‘LineWidth’, 2, ‘DisplayName’, sprintf(‘L = %.2f m’, L));
figure(2);
plot(x, linspace(c0, c0 * k, Nx), ‘LineWidth’, 2, ‘DisplayName’, sprintf(‘L = %.2f m’, L));
end
%% Section 6: Finalize Temperature Plot
figure(1);
xlabel(‘Position (m)’); ylabel(‘Temperature (C)’);
title(‘Temperature Distribution for Different L Values’);
grid on; legend show; hold off;
%% Section 7: Finalize Solute Concentration Plot
figure(2);
xlabel(‘Position (m)’); ylabel(‘Solute Concentration (wt%)’);
title(‘Solute Concentration Distribution for Different L Values’);
grid on; legend show; hold off;I need my graph to show the results from all lengths (i.e. all 5 coloured lines) but am only outputting 2 lines for one of the graphs. Both my code and the graphs are below. Please help.
clc; clear; close all;
%% Section 1: Development of Applied Thermal and Solute Concentration Fields
L_values = linspace(0.03, 0.25, 5); % Separation distance [m] (5 samples for clarity)
Nx = 200; % Number of spatial steps
Nt = 5000; % Number of time steps
dt = 0.005; % Time step size
% Thermal diffusivity values for aluminum alloy (m^2/s)
alpha_L = 3.26e-5; % Liquid phase
alpha_S = 6.58e-5; % Solid phase
% Solute diffusivity in liquid (m^2/s)
D_L = 1e-11;
% Interface velocity range
V = linspace(1.67e-5, 1.67e-4, 20); % Velocity range in m/s
% Partition coefficient & solute parameters
k = 0.85; % Partition coefficient
c0 = 7.26; % Initial solute concentration (wt% Al)
m = -3; % Liquidus slope (K/wt% Al)
[G_range, V_mesh] = meshgrid(linspace(1e4, 1e6, 20), V);
delta_c = sqrt(D_L ./ V_mesh); % Solute boundary layer thickness
G_critical = -2 * m * c0 ./ delta_c; % Constitutional gradient
%% Section 2: Initialize Figures
figure(1); hold on; % Temperature distribution plot
figure(2); hold on; % Solute concentration plot
%% Section 3: Loop Over Different L Values
for L = L_values
dx = L / Nx; % Spatial step size
x = linspace(0, L, Nx); % Position array
s = 0.02; % Initial interface position
% Initialize temperature profile
T = zeros(Nx, 1);
T(1:Nx/2) = linspace(1450, 1350, Nx/2); % Melt zone
T(Nx/2+1:end) = linspace(1150, 900, Nx/2); % Cold zone
%% Section 4: Applied Thermal Gradient (Finite Difference)
for t = 1:Nt
T_new = T;
for i = 2:Nx-1
if x(i) < s
T_new(i) = T(i) + alpha_S * dt / dx^2 * (T(i+1) – 2*T(i) + T(i-1));
else
T_new(i) = T(i) + alpha_L * dt / dx^2 * (T(i+1) – 2*T(i) + T(i-1));
end
end
T = T_new;
s = s + V(1) * dt; % Update interface position
end
%% Section 5: Plot Results for Each L
figure(1); % ensuring we’re plotting on the right figure
plot(x, T, ‘LineWidth’, 2, ‘DisplayName’, sprintf(‘L = %.2f m’, L));
figure(2);
plot(x, linspace(c0, c0 * k, Nx), ‘LineWidth’, 2, ‘DisplayName’, sprintf(‘L = %.2f m’, L));
end
%% Section 6: Finalize Temperature Plot
figure(1);
xlabel(‘Position (m)’); ylabel(‘Temperature (C)’);
title(‘Temperature Distribution for Different L Values’);
grid on; legend show; hold off;
%% Section 7: Finalize Solute Concentration Plot
figure(2);
xlabel(‘Position (m)’); ylabel(‘Solute Concentration (wt%)’);
title(‘Solute Concentration Distribution for Different L Values’);
grid on; legend show; hold off; I need my graph to show the results from all lengths (i.e. all 5 coloured lines) but am only outputting 2 lines for one of the graphs. Both my code and the graphs are below. Please help.
clc; clear; close all;
%% Section 1: Development of Applied Thermal and Solute Concentration Fields
L_values = linspace(0.03, 0.25, 5); % Separation distance [m] (5 samples for clarity)
Nx = 200; % Number of spatial steps
Nt = 5000; % Number of time steps
dt = 0.005; % Time step size
% Thermal diffusivity values for aluminum alloy (m^2/s)
alpha_L = 3.26e-5; % Liquid phase
alpha_S = 6.58e-5; % Solid phase
% Solute diffusivity in liquid (m^2/s)
D_L = 1e-11;
% Interface velocity range
V = linspace(1.67e-5, 1.67e-4, 20); % Velocity range in m/s
% Partition coefficient & solute parameters
k = 0.85; % Partition coefficient
c0 = 7.26; % Initial solute concentration (wt% Al)
m = -3; % Liquidus slope (K/wt% Al)
[G_range, V_mesh] = meshgrid(linspace(1e4, 1e6, 20), V);
delta_c = sqrt(D_L ./ V_mesh); % Solute boundary layer thickness
G_critical = -2 * m * c0 ./ delta_c; % Constitutional gradient
%% Section 2: Initialize Figures
figure(1); hold on; % Temperature distribution plot
figure(2); hold on; % Solute concentration plot
%% Section 3: Loop Over Different L Values
for L = L_values
dx = L / Nx; % Spatial step size
x = linspace(0, L, Nx); % Position array
s = 0.02; % Initial interface position
% Initialize temperature profile
T = zeros(Nx, 1);
T(1:Nx/2) = linspace(1450, 1350, Nx/2); % Melt zone
T(Nx/2+1:end) = linspace(1150, 900, Nx/2); % Cold zone
%% Section 4: Applied Thermal Gradient (Finite Difference)
for t = 1:Nt
T_new = T;
for i = 2:Nx-1
if x(i) < s
T_new(i) = T(i) + alpha_S * dt / dx^2 * (T(i+1) – 2*T(i) + T(i-1));
else
T_new(i) = T(i) + alpha_L * dt / dx^2 * (T(i+1) – 2*T(i) + T(i-1));
end
end
T = T_new;
s = s + V(1) * dt; % Update interface position
end
%% Section 5: Plot Results for Each L
figure(1); % ensuring we’re plotting on the right figure
plot(x, T, ‘LineWidth’, 2, ‘DisplayName’, sprintf(‘L = %.2f m’, L));
figure(2);
plot(x, linspace(c0, c0 * k, Nx), ‘LineWidth’, 2, ‘DisplayName’, sprintf(‘L = %.2f m’, L));
end
%% Section 6: Finalize Temperature Plot
figure(1);
xlabel(‘Position (m)’); ylabel(‘Temperature (C)’);
title(‘Temperature Distribution for Different L Values’);
grid on; legend show; hold off;
%% Section 7: Finalize Solute Concentration Plot
figure(2);
xlabel(‘Position (m)’); ylabel(‘Solute Concentration (wt%)’);
title(‘Solute Concentration Distribution for Different L Values’);
grid on; legend show; hold off; graphs MATLAB Answers — New Questions