How to find X and Y coordinates of maximum gap between curves?

7 views (last 30 days)
Please see the attached figure. In this figure, I want to find the values of X and Y coordinates where the two curve has maximum separation in Y-direction. In this figure, we can see that maximum gap is somewhere around 0.7 in the x-scale.
To do so, I simply take the subtraction of red data and blue data and obtain the gap value and corresponding X-value.
The gap value is delta Y is 6.7*10^7 and X-value is 0.027451.
However, the gap is not maximum at X = 0.027451. So, is there any way I can find the X-Y values of maximum gap between curves?
The data is also attached here with. It may not be same dataset as shown in figure, but a similar data.
I attach fig file for the same data for refrence.
Here is the code to obtain this type of plot.
But how to find maximum gap between curves?
clear; close all
load('rd.mat');
load('bl.mat');
X = flux1.vapa_flx_E
plot(X,rd,'-or');
hold on;
plot(X,bl,'-ob');
set(gca,'YScale','log','YTick',[10^2 10^3 10^4 10^5 10^6 10^7 10^8 10^9 10^10 10^11],'LineWidth',1.5,'FontWeight','bold',...
'FontSize',14, 'YTicklabel',{'10^2','','10^4','','10^6','','10^8','','10^1^0',''});
dif1 = rd-bl;
[val_maxdif1, idx_maxdif1] = max(dif1);
delta_y = val_maxdif1;
x1 = X(idx_maxdif1);
Any help is greatly appriciated.

Accepted Answer

Karim
Karim on 10 Aug 2022
Edited: Karim on 11 Aug 2022
It depends on what you mean with the gap, notice that you plot it on a logarithmic axis. As a result from a graphical point of view the gap seems larger at 0.7 however if you make the axis linear you will see that the gap is bigger at the small X values
% load mat files
load(websave('rd', "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1093100/rd.mat"))
load(websave('bl', "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1093105/bl.mat"))
load(websave('X' , "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1093090/X.mat" ))
% create figure
figure
plot(X,rd,'-or');
hold on;
plot(X,bl,'-ob');
set(gca,'YScale','log','YTick',[10^2 10^3 10^4 10^5 10^6 10^7 10^8 10^9 10^10 10^11],...
'LineWidth',1.5,'FontWeight','bold','FontSize',14,...
'YTicklabel',{'10^2','','10^4','','10^6','','10^8','','10^1^0',''});
grid on
title('Logarithmic Y axis')
figure
plot(X,rd,'-or');
hold on;
plot(X,bl,'-ob');
set(gca,'LineWidth',1.5,'FontWeight','bold','FontSize',14);
grid on
title('Linear Y axis')
dif1 = rd-bl;
figure
plot(X,dif1)
grid on
update after extra comment of the OP
here i made an attempt to identify the 'gap' that the OP looks for
dif2 = (rd-bl)./bl;
figure
plot(X,dif2)
grid on
[maxGap, ixd] = max( dif2 );
maxGap_X = X(ixd);
disp(['max gap is found at X = ', num2str(maxGap_X)])
max gap is found at X = 0.692
  4 Comments
Karim
Karim on 12 Aug 2022
Thank you, I had some issues a while back because we are limited to the amount of files we can upload to the answers per day. Hence I looked a bit for an workaround.
For completness, I also use the matfile command to see which variables are inside the mat file.
load(websave('rd', "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1093100/rd.mat"))
matfile('rd.mat')
ans =
matlab.io.MatFile Properties: Properties.Source: '/users/mss.system.KkVnZh/rd.mat' Properties.Writable: false Properties.ProtectedLoading: false rd: [64x1 double] Methods

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance 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!