Skip to content

Instantly share code, notes, and snippets.

@RustSynx
Created February 1, 2026 08:27
Show Gist options
  • Select an option

  • Save RustSynx/51dbf9489eccb4042a70f5564c263cae to your computer and use it in GitHub Desktop.

Select an option

Save RustSynx/51dbf9489eccb4042a70f5564c263cae to your computer and use it in GitHub Desktop.
Visualizing Mild Steel
% Stress-Strain Curve Simulation
% *Important: The X-axis (strain) scale has been exaggerated
% for educational purposes to enhance graph visibility
%% Visual Data Generation
% [Settings: MPa]
sig_upper_yield = 280; % Upper Yield Point
sig_lower_yield = 240; % Lower Yield Point
sig_uts = 450; % Ultimate Tensile Strength (UTS)
sig_frac = 350; % Fracture Strength
% [Strain Settings per Section (X-axis)]
% Intervals widened to enhance visual slope
eps_1_elastic = linspace(0, 0.05, 30); % 1. Elastic Region
eps_2_drop = linspace(0.05, 0.06, 10); % 2. Yield Drop
eps_3_plateau = linspace(0.06, 0.10, 20); % 3. Yield Plateau
eps_4_harden = linspace(0.10, 0.25, 60); % 4. Strain Hardening
eps_5_necking = linspace(0.25, 0.30, 20); % 5. Necking
% [Stress Calculations per Section (Y-axis)]
% 1. Elastic Region
sig_1_elastic = linspace(0, sig_upper_yield, length(eps_1_elastic));
% 2. Yield Drop Region
sig_2_drop = linspace(sig_upper_yield, sig_lower_yield, length(eps_2_drop));
% 3. Yield Plateau Region
sig_3_plateau = linspace(sig_lower_yield, sig_lower_yield, length(eps_3_plateau));
% 4. Strain Hardening Region
a_val = (sig_lower_yield - sig_uts) / (0.10 - 0.25)^2;
sig_4_harden = a_val * (eps_4_harden - 0.25).^2 + sig_uts;
% 5. Necking Region
sig_5_necking = linspace(sig_uts, sig_frac, length(eps_5_necking));
% Build Strain and Stress Data
strain = [eps_1_elastic, eps_2_drop, eps_3_plateau, eps_4_harden, eps_5_necking];
stress = [sig_1_elastic, sig_2_drop, sig_3_plateau, sig_4_harden, sig_5_necking];
%% Graph Style Settings
figure('Name', 'Stress-Strain Simulation', 'Color', [0,0,0], 'Position', [100, 100, 900, 600]);
hold on; grid on; box on;
% Set Axis Limits and Labels
xlim([0, 0.32]);
ylim([0, 550]);
% X-axis and Y-axis Labels and Title Settings
xlabel('Strain', 'FontSize', 12, 'FontWeight', 'bold');
ylabel('Stress [MPa]', 'FontSize', 12, 'FontWeight', 'bold');
title('Stress-Strain Curve Simulation', 'FontSize', 15, 'Color', [1, 1, 1]);
% Graph Line, Marker, Annotation Text Settings
hLine = plot(strain(1), stress(1), 'b-', 'LineWidth', 3);
hMarker = plot(strain(1), stress(1), 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 10);
hText = text(0.02, 500, 'Start', 'FontSize', 12, 'BackgroundColor', [1,1,1], 'EdgeColor', 'k', 'Color', [0, 0, 0]);
% X,Y Axis Color Settings
ax = gca;
ax.XColor = [1,1,1];
ax.YColor = [1,1,1];
%% Draw Graph
for i = 1:length(strain)
% Graph Line, Marker Data Update
set(hLine, 'XData', strain(1:i), 'YData', stress(1:i));
set(hMarker, 'XData', strain(i), 'YData', stress(i));
curr_eps = strain(i); % Current Strain
% 1. Elastic Region
if curr_eps <= 0.05
msg = '1. Elastic Region';
if i == length(eps_1_elastic)
text(strain(i), stress(i)+20, 'Upper Yield Point', 'Color', [1,1,1], 'FontWeight', 'bold');
pause(0.7);
end
pause(0.05);
% 2. Yield Drop Region
elseif curr_eps <= 0.06
msg = '2. Yield Drop Region';
if i == length(eps_1_elastic) + length(eps_2_drop)
text(strain(i), stress(i)+20, 'Lower Yield Point', 'Color', [1,1,1], 'FontWeight', 'bold');
pause(0.7);
end
pause(0.2);
% 3. Yield Plateau Region
elseif curr_eps <= 0.10
msg = '3. Yield Plateau Region';
if i == length(eps_1_elastic) + length(eps_2_drop)
text(strain(i), stress(i)+20, 'Lower Yield Point', 'Color', [1,1,1], 'FontWeight', 'bold');
pause(0.7);
end
pause(0.05);
% 4. Strain Hardening Region
elseif curr_eps <= 0.25
msg = '4. Strain Hardening Region';
if abs(curr_eps - 0.25) < 0.002
text(strain(i), stress(i)+20, 'UTS', 'Color', [1,1,1], 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
pause(0.7);
end
pause(0.03);
% 5. Necking Region
else
msg = '5. Necking Region';
pause(0.1);
end
set(hText, 'String', msg);
drawnow;
end
% Draw Marker and Text
plot(strain(end), stress(end), 'kx', 'MarkerSize', 25, 'LineWidth', 4, 'Color', [1,1,1]);
text(strain(end), stress(end)+30, 'Fracture!', 'Color', [1,1,1], 'FontSize', 14, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment