Clear Filters
Clear Filters

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

9 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
X = flux1.vapa_flx_E
hold on;
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 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', ""))
load(websave('bl', ""))
load(websave('X' , "" ))
% create figure
hold on;
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],...
grid on
title('Logarithmic Y axis')
hold on;
grid on
title('Linear Y axis')
dif1 = rd-bl;
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;
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
Star Strider
Star Strider on 12 Aug 2022
@Karim — Your websave solution to opening .mat files with the online Run feature is brilliant!
I wish I’d thought of that!
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', ""))
ans = 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)


Find more on Specifying Target for Graphics Output 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!