Finding intersection of plotted dataset and line
11 views (last 30 days)
Show older comments
I am working with a stress strain curve at the moment and want to find the yield stress at 0.2%. I basically wanna find where the plotted dataset and the plotted line cross each other.
I feel like there should be a really easy way to do this, that is not using graph tools. Help is much appreciated and more info can be given if needed
youngs_modulus = strain([900:1600])\sigma([900:1600]);
offset_line = youngs_modulus*strain - youngs_modulus*0.002;
plot(strain, sigma)
hold on
plot(strain, offset_line)
0 Comments
Answers (1)
Star Strider
on 3 Feb 2019
One approach:
x = 0:20; % Create Data
y1 = 15 - 3*x; % Create Line
y2 = 2*x - 20; % Create Line
yd = y1 - y2; % Subtract
x_cross = interp1(yd, x, 0); % X-Coordinate
y_cross = 15 - 3*x_cross; % Substitute Into One Equation To Get Y-Coordinate
figure
plot(x, y1, x, y2, x_cross, y_cross, '+r')
grid
text(x_cross, y_cross, sprintf('x = %.3f\ny = %.3f', x_cross, y_cross), 'VerticalAlignment','bottom')
This appears to be linear. This code just subtracts the data of one line from the other, interpolates to find the x-intersection, and uses one of the equations to calculate the y-intersection.
If you know the equations for both lines, you can use basic algebra to calculate the intersection from the slopes and intercepts.
8 Comments
Star Strider
on 3 Feb 2019
The lines do not appear to cross in any meaningful way. Testing for the cross using:
yd = sigma - offset_line; % Subtract
produces an irregualr area (‘strain’ ranging from 0 to 0.260) where there appears to be much oscillation. However a low-degree polynomial fit of that region indicates that the fitted line to ‘yd’ never goes below 0, so there do not appear to be any actual zero-crossings:

My full code —
D = dlmread('data.txt',';',3,0);
t = D(:,1);
strain = D(:,2);
force = D(:,3);
load = D(:,4);
sigma = force;
youngs_modulus = strain([900:1600])\sigma([900:1600]);
offset_line = youngs_modulus*strain - youngs_modulus*0.002;
yd = sigma - offset_line; % Subtract
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
xcidx = zci(yd); % Zero-Crossing Indices
p = polyfit(strain(1:max(xcidx)), yd(1:max(xcidx)), 3); % Smoothing Function
fv = polyval(p, strain(1:max(xcidx))); % Smoothing Function Evaluated
figure
plot(strain, sigma)
hold on
plot(strain, offset_line)
plot(strain, yd)
plot(strain(xcidx), yd(xcidx), '+r')
hold off
legend('Sigma', 'Offset Line', 'Location','NW')
xlabel('Strain')
figure
plot(strain, yd)
hold on
plot(strain(xcidx), yd(xcidx), '+r')
plot(strain(1:max(xcidx)), fv, '-g', 'Linewidth',1)
hold off
title('Intersection Region')
xlabel('Strain')
ylabel('Sigma - Offset Line')
legend('yd = sigma - offset_line', 'Zero-Crossings Detected', 'Smoothing Function', 'Location','N')
axis([0 0.3 -0.0001 0.0002])
Look through this to be certain I imported and calculated everything correctly. If I did, there is likely nothing further that I can offer.
See Also
Categories
Find more on Stress and Strain in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!