Plot a Line of best fit onto a previously made graph that contains subplots

1 view (last 30 days)
I graph stress vs strain earlier in my code just to verify the data looks good, later on I want to add a line of best fit over the small linear region before failure onto either the same figure or a new figure (either way I want to have stress vs strain and the corresponding line of best fit on one graph).
%% Graph Stress vs Strain
figure(2)
for k=1:n
subplot(1,n,k)
x = epsilon{1,k};
y = sigma{1,k};
plot(x,y,'.')
title('Stress vs Strain'),xlabel('Strain (%)','FontWeight','bold'),ylabel('Stress (MPa)','FontWeight','bold')
end
Above is the code for the stress vs strain graph
below is the code i currently have to try to graph the line of best fit on the same figure
figure(2)
for k=1:n
x = cut_epsilon{1,k}(1:end,1);
hold on
subplot(1,n,k)
plot(x, (slope(k)*x+intercept(k)), 'color','magenta' )
hold off
end
  1 Comment
Dyuman Joshi
Dyuman Joshi on 6 Jul 2023
"(either way I want to have stress vs strain and the corresponding line of best fit on one graph)"
Why not merge the two for loops together?

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 6 Jul 2023
You must make the axes current first, then turn hold on.
subplot(m,n,p) ... If axes exist in the specified position, then this command makes the axes the current axes.
Try modifying the code in your second loop to this (untested)
figure(2)
for k=1:n
x = cut_epsilon{1,k}(1:end,1);
subplot(1,n,k)
hold on
plot(x, (slope(k)*x+intercept(k)), 'color','magenta' )
hold off
end

More Answers (0)

Categories

Find more on Stress and Strain in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!