Find intersection point between two plotted lines
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Star Strider
el 18 de Abr. de 2023
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 comentarios
Star Strider
el 18 de Abr. de 2023
Editada: Star Strider
el 18 de Abr. de 2023
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
el 18 de Abr. de 2023
Editada: Cameron
el 18 de Abr. de 2023
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 comentarios
Ver también
Categorías
Más información sobre Specifying Target for Graphics Output en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!