Find intersection point between two plotted lines
13 次查看(过去 30 天)
显示 更早的评论
Hi all,
I have been trying to find an intersection point between two plotted lines however, I used several functions but it doesnot work.. usually the output is 2*0 empty double matrix ... Actually i need the number of intersection ... ?
here is the code and attached are the data
clearvars
clc;
close all
Data = readmatrix('data_tangent.xlsx')
x = Data(:,1);
y = Data(:,2);
[ymax,idx] = max(y);
Binit = x(1:5) \ y(1:5)
Bymax = x(idx) \ y(idx)
Line_init = x*Binit;
Line_initv = Line_init <= ymax;
Line_ymax = x*Bymax;
Line_ymaxv = Line_ymax <= ymax;
figure
plot(x, y)
hold on
plot([0;x(Line_ymaxv)], [0;Line_ymax(Line_ymaxv)], '-r')
hold off
grid
axis('equal')
xlabel('X')
ylabel('Y')
axis([0 max(x) 0 max(y)])
x1=x(Line_ymaxv)
y1=Line_ymax(Line_ymaxv)
P=InterX([x;y],[x1;y1])
The function i used is from "https://www.mathworks.com/matlabcentral/fileexchange/22441-curve-intersections"
0 个评论
回答(2 个)
Star Strider
2023-4-18
The code I wrote for your previous post, Plot tangent line on part of the curve is designed as requested to have only one intersection, that being at the origin, since both tangent lines were requested to go through the origin, at least as I understood it.
How else would you want to define the two tangent lines?
2 个评论
Star Strider
2023-4-18
编辑:Star Strider
2023-4-18
My pleasure!
The two lines I plotted (only one is shown here) both intersect at the origin, since that is how they were requested and designed. The intersection with the curve is designed to be at ‘ymax’ with the x-coordinate of that intersection being ‘x(idx)’, so nothing further needs to be computed.
% clearvars
% clc;
% close all
format long
Data = readmatrix('data_tangent.xlsx')
x = Data(:,1);
y = Data(:,2);
[ymax,idx] = max(y);
Binit = x(1:fix(idx/4)) \ y(1:fix(idx/4))
Bymax = x(idx) \ y(idx)
Line_init = x*Binit;
Line_initv = Line_init <= ymax;
Line_ymax = x*Bymax;
Line_ymaxv = Line_ymax <= ymax;
Intersection_x = interp1(Line_init-Line_ymax, x, 0, 'linear','extrap')
Intersection_y = interp1(x, Line_init, Intersection_x, 'linear','extrap')
Intersection = [Intersection_x, Intersection_y] % Desired Result
figure
plot(x, y)
hold on
plot([0;x(Line_ymaxv)], [0;Line_ymax(Line_ymaxv)], '-r')
plot([0;x(Line_initv)], [0;Line_init(Line_initv)], '-r')
hold off
grid
axis('equal')
xlabel('X')
ylabel('Y')
axis([0 max(x) 0 max(y)])
% x1=x(Line_ymaxv)
% y1=Line_ymax(Line_ymaxv)
% P=InterX([x;y],[x1;y1])
EDIT — (18 Apr 2023 at 20:03)
.
Cameron
2023-4-18
编辑:Cameron
2023-4-18
Looks like a stress-strain curve. Depending on the material, you could adjust the variable I named cutoff to 0.3*ymax or whatever you want. This code takes the first intersection of the stress-strain curve and interpolates the value for yield.
clearvars
clc;
close all
Data = readmatrix('data_tangent.xlsx')
x = Data(:,1);
y = Data(:,2);
[ymax,idx] = max(y);
Binit = x(1:5) \ y(1:5);
Bymax = x(idx) \ y(idx);
Line_init = x*Binit;
Line_initv = Line_init <= ymax;
Line_ymax = x*Bymax;
Line_ymaxv = Line_ymax <= ymax;
Line_y = Line_ymax(Line_ymaxv);
trunc_x = x(1:find(Line_ymaxv,1,'last'));
trunc_y = y(1:find(Line_ymaxv,1,'last'));
ii = length(trunc_y);
cutoff = 0.5*ymax; %you can adjust this
SaveMe = [];
while trunc_y(ii) > cutoff
if (Line_y(ii) > trunc_y(ii) && Line_y(ii-1) <= trunc_y(ii-1)) | ...
(Line_y(ii) < trunc_y(ii) && Line_y(ii-1) >= trunc_y(ii-1))
SaveMe = [SaveMe;ii];
end
ii = ii - 1;
end
m1 = polyfit(x(SaveMe(end)-1:SaveMe(end)),y(SaveMe(end)-1:SaveMe(end)),1);
m2 = polyfit(x(SaveMe(end)-1:SaveMe(end)),Line_y(SaveMe(end)-1:SaveMe(end)),1);
x_cross = (m2(2) - m1(2))/(m1(1) - m2(1));
y_cross = m1(1)*x_cross + m1(2);
figure
plot(x, y)
hold on
plot([0;x(Line_ymaxv)], [0;Line_ymax(Line_ymaxv)], '-r')
scatter(x_cross,y_cross,'filled')
grid
axis('equal')
xlabel('X')
ylabel('Y')
axis([0 max(x) 0 max(y)])
hold off
3 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!