Intersection of a line with constant slope and a line with changing slopes

2 visualizaciones (últimos 30 días)
Hi, I have two different lines that represent different river profiles. The green line above sea-level (see jpg at the bottom of this post) represents the modern river channel whereas the yellow line represents what the channel may have looked like in the past. I'm trying to figure out where both lines cross. The code also has to be pretty general as I will be applying it to different river profiles, not just this specific one.
Here's specific informationa about the code:
idx = 2980; % calls upon river profile #2980 out of 10848
channel_height=0:20;
channel_length=channel_len(idx,:); % channel_len comes from dataset
plot(channel_length,channel_height), % plotting the upslope channel profile (green line)
hold on,
plot(-1000*shelf_len(idx,1:7),0:-20:-120), % plotting the shelf profile from dataset (red line, not important in this case), shelf_len comes from dataset
xlabel('distance (m)'),
ylabel('depth (m)')
yline(0,'--','sea-level')
channel_length_nanremoved=rmmissing(channel_length); % removing nans from channel data
positionofnan=find(isnan(channel_length)); % finding positions of nans in channel length
channel_height_lennanremoved=channel_height; % applying the length nan positions to channel height to remove their corresponding heights
channel_height_lennanremoved(positionofnan)=[];
shelf_len_nanremoved=-1000.*rmmissing(shelf_len(idx,1:7)); % removing nans from shelf to combine with channel
% Using a forloop to calculate slope between channel data points
n=2:length(channel_length_nanremoved);
for ii=2:length(channel_length_nanremoved)
slope=(channel_height_lennanremoved(n)-channel_height_lennanremoved(n-1))./(channel_len_nanremoved(n)-channel_len_nanremoved(n-1));
end
% Generating the yellow line representing a previous position of the channel
m=mean(slope(end-9:end))
shelf_len_reversed=flip(shelf_len_nanremoved,2); % flipping shelf lengths as they start off more negative and get more positive
x=horzcat(shelf_len_reversed,channel_length_nanremoved); % combining shelf and channel lengths
x1=channel_length_nanremoved(end); % starting line from start of channel profile
position2=find(channel_length==channel_length_nanremoved(end));
y1=channel_height(position2);
y=(m*(x-x1))+y1; % determining y for given x
hplot=plot(x,y);

Respuesta aceptada

dpb
dpb el 13 de Mzo. de 2020
Editada: dpb el 13 de Mzo. de 2020
idx = 2980; % calls upon river profile #2980 out of 10848
channel_height=0:20;
channel_length=channel_len(idx,:); % channel_len comes from dataset
plot(channel_length,channel_height), % plotting the upslope channel profile (green line)
hold on
plot(-1000*shelf_len(idx,1:7),0:-20:-120), % plot shelf profile from dataset (red line, not important in this case), shelf_len comes from dataset
xlabel('distance (m)'),
ylabel('depth (m)')
yline(0,'--','sea-level')
ixData=~isnan(channel_length); % logical vector nans in channel data
ch_length=channel_length(ixData); % keep good data only -- length
ch_height=channel_height(ixData); % and height
shelf_len_nanremoved=-1000.*rmmissing(shelf_len(idx,1:7)); % removing nans from shelf to combine with channel
% Calculate slope between channel data points (no loop needed)
slope=diff(ch_height)./diff(ch_length);
% Generating the yellow line representing a previous position of the channel
m=mean(slope(end-9:end));
OK, at the above is where I'd probably split ways (besides shortening variable names so much easier to scan the code, anyways)...
Where does the magic number "9" come from and why hardcoded into the code? Can't change anything easily that way.
Why use the average of the slopes instead of fitting a linear trend to the data? Should be nearly the same but a single outlier woudn't be as significant...
b=polyval(ch_length,ch_height,1); % least square linear regression thru ch_length
Then to find the intersection use fsolve on the difference between the two lines.
ADDENDUM POST COMMENT:
OK, I'll concede using the mean of the means if what you really want...I'd suggest investigating the difference a little more though unless this is a respected technique in the application field.
% Generating the yellow line representing a previous position of the channel
nMeansUse=10; % at least make a variable can change
m=mean(slope(end-(nMeansUse_1):end)); % an average mean of means
x=[fliplr(ch_length),ch_length); % combine shelf and channel lengths
x1=ch_length(end); % starting line from start of channel profile
ix2=find(channel_length==ch_length(end),1);
y1=channel_height(ix2);
y=(m*(x-x1))+y1; % determining y for given x
hplot=plot(x,y);
% find intersection of two...
fnUP=@(x) interp1(channel_length,channel_height,x); % interpolated green line (upslope)
fnPR=@(x) polyval([m y1],x-x1); % evaluate yellow line (previous)
x0=fsolve(@(x) fnUP(x)-fnPR(x),x1); % try to find interesection of two
  14 comentarios
Derrick Vaughn
Derrick Vaughn el 23 de Mzo. de 2020
Hi dpb, I got it to return the point today by finding the point when the sign of the difference changes. Thanks for all of your help!
dpb
dpb el 23 de Mzo. de 2020
Yes, that'll be the way to find a good starting point...I'm sorry but I don't have the time at the moment to try to find a more robust solution technique, but either end of the section with the sign change should be reliable I think if not quite as convenient as just throwing a dart somewhere in the general vicinity.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Historical Contests en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by