MATLAB: How to extract part of the lines

curved linesgenerating pointsintersectionMATLAB

Hello guys,
I need to somehow extract part of the curved lines on picture below that goes from the red line to the max (or better from the intersection point to the max).
When I try to do it with the code Im running (provided below) I only get this
but I need something similair to picture below. I made this with excel where I generated points and then extracted only points I needed, but all this is done manualy and it's time consuming and not effective.
I'm new to the MATLAB, so if someone has a better idea how to generate the points or if there is an easier way to do this, please explain. Down below is the code that I'm working with. All the help will be appreciated.
%Math for generating the points
clc
rn = 1.3;
f = 0.5;
ap = 0.2;
po = 10;
dx = 0.01;
lambda = 0;
gamma = -8;
Kr = 51;
Kr1 = 51;
xa = (rn*(sin(Kr)))*-1;
xb = rn*(sin(Kr1));
ya = (xa*(tan(Kr)))+(rn/(cos(Kr)))-rn;
%Straight line A
N = 100;
xpA = xa - (0:N-1)*dx;
xpA = xpA';
xpA = sort(xpA);
xpA = round(xpA,2);
ypA = (xpA*tan(Kr))+(rn/(cos(Kr)))-rn;
ypA = sort(ypA);
ypA = round(ypA,2);
%Straight line B
xpB = xb + (0:N-1)*dx;
xpB = xpB';
xpB = round(xpB,2);
ypB = (-xpB*tan(Kr1))+(rn/(cos(Kr1)))-rn;
ypB = round(ypB,2);
%curved line
N1 = 5000;
xk1 = xa + (1:N1)*dx;
xk1 = round(xk1,2);
xk1 = xk1';
[c,d]= intersect(xk1,min(xpB));
xk = xa +(1:d-1)*dx;
xk = round(xk,2);
xk = xk';
yk = (sqrt((rn^2)-(xk.^2)))-rn;
yk = round(yk,2);
%connecting the lines together and creating the second one
X = [xpA;xk;xpB];
Y = [ypA;yk;ypB];
b = po-ap;
x = X(1);
X1 = X-x;
Y1 = Y+b;
X2 = X1+f;
Y2 = Y1;
%Intersection point between two lines
[Y1max,Y1ix] = max(Y1);
[Y2max,Y2ix] = max(Y2);
Xi = linspace(min([X1(Y1ix);X2(Y2ix)]), max([X1(Y1ix);X2(Y2ix)]), 20);
Y1i = interp1(X1,Y1,Xi);
Y2i = interp1(X2,Y2,Xi);
Ydif = sort(Y2i-Y1i);
Xq = interp1(Ydif, Xi, 0);
Yq = interp1(Xi, Y1i, Xq);
%Ploting the lines
figure
plot(X1,Y1)
hold on
plot(X2,Y1)
plot(Xq, Yq, 'sg', 'MarkerSize', 10)
hold off
text(Xq, Yq, sprintf('\\uparrow\nX = %.4f\nY = %.4f',Xq,Yq), 'HorizontalAlignment','center', 'VerticalAlignment','top')
%Extraction of the part of the lines I need (Problematic)
pe = numel(Xi)
Yn1 = linspace(Yq,Y1max,pe/2)';
Yn2 = linspace(Yq,Y1max,pe/2)';
Yn2 = sort(Yn2,'descend');
Yn = [Yn1;Yn2];
xi = Xi(1);
Xi1 = (Xi - xi)';
Xi2 = (Xi1 + f);
figure
plot(Xi1,Yn)
hold on
plot (Xi2,Yn)

Best Answer

hello Robert
I modified a bit your code , first thing was to fix the issue with a relatively coarse rounding
then I added my version of the "extraction" at the end
hope it helps
%Math for generating the points
clc
rounding = 6; % rounding precision
rn = 1.3;
f = 0.5;
ap = 0.2;
po = 10;
dx = 10^(-rounding);
lambda = 0;
gamma = -8;
Kr = 51;
Kr1 = 51;
xa = (rn*(sin(Kr)))*-1;
xb = rn*(sin(Kr1));
ya = (xa*(tan(Kr)))+(rn/(cos(Kr)))-rn;
%Straight line A
N = 10^(rounding);
xpA = xa - (0:N-1)*dx;
xpA = xpA';
xpA = sort(xpA);
xpA = round(xpA,rounding);
ypA = (xpA*tan(Kr))+(rn/(cos(Kr)))-rn;
ypA = sort(ypA);
ypA = round(ypA,rounding);
%Straight line B
xpB = xb + (0:N-1)*dx;
xpB = xpB';
xpB = round(xpB,rounding);
ypB = (-xpB*tan(Kr1))+(rn/(cos(Kr1)))-rn;
ypB = round(ypB,rounding);
%curved line
N1 = 2*N;
xk1 = xa + (1:N1)*dx;
xk1 = round(xk1,rounding);
xk1 = xk1';
[c,d]= intersect(xk1,min(xpB));
xk = xa +(1:d-1)*dx;
xk = round(xk,rounding);
xk = xk';
yk = (sqrt((rn^2)-(xk.^2)))-rn;
yk = round(yk,rounding);
%connecting the lines together and creating the second one
X = [xpA;xk;xpB];
Y = [ypA;yk;ypB];
b = po-ap;
x = X(1);
X1 = X-x;
Y1 = Y+b;
X2 = X1+f;
Y2 = Y1;
%Intersection point between two lines
[Y1max,Y1ix] = max(Y1);
[Y2max,Y2ix] = max(Y2);
% Xi = linspace(min([X1(Y1ix);X2(Y2ix)]), max([X1(Y1ix);X2(Y2ix)]), 100);
Xi = linspace(X1(Y1ix), X2(Y2ix), 100);
Y1i = interp1(X1,Y1,Xi);
Y2i = interp1(X2,Y2,Xi);
Ydif = sort(Y2i-Y1i);
[val,ind1] = min(abs(Y2i-Y1i));
Xq = interp1(Ydif, Xi, 0);
Yq = interp1(Xi, Y1i, Xq);
Xq_distance_to_first_peak = Xq - X1(Y1ix); % x distance between first peak of Y1 curve and Xq
%Ploting the lines
figure(1)
plot(X1,Y1)
hold on
plot(X2,Y1)
plot(Xq, Yq, 'sg', 'MarkerSize', 10)
hold off
text(Xq, Yq, sprintf('\\uparrow\nX = %.4f\nY = %.4f',Xq,Yq), 'HorizontalAlignment','center', 'VerticalAlignment','top')
%Extraction of the part of the lines I need (no more Problematic)
ind1 = find(X1>X1(Y1ix)-Xq_distance_to_first_peak & X1<X1(Y1ix)+Xq_distance_to_first_peak);
Xi1 = X1(ind1);
Y1ii = interp1(X1,Y1,Xi1);
ind2 = find(X2>X2(Y2ix)-Xq_distance_to_first_peak & X2<X2(Y2ix)+Xq_distance_to_first_peak);
Xi2 = X2(ind2);
Y2ii = interp1(X2,Y2,Xi2);
figure(2)
plot(Xi1,Y1ii)
hold on
plot(Xi2,Y2ii)
Related Question