How to find a point within a curve?

24 views (last 30 days)
Hello to all,
How can I calculate a point (the red point) on the same curve but 500 units less than the point indicated in blue? (see graph). The pairs (x,y) of the curve are contained in a cell within an array. I tried with the code that I copy here, but I have problems with the “find" command, because I must be changing the absolute difference manually and since I have to automate the process, this is not an option for me. Thanks for your help!
w=1936 %This is an specific day and the graph shown here is the graph from this day
k = find(abs(Array_demand{w}.Volume- x_blue_point{w}+500) < 70,1)
%Find its correspondance within the curve for X (Volume)
x_red_point=Array_demand{w}.Volume(k);
%Find its correspondance within the curve for Y(Price)
y_red_point=Array_demand{w}.Price(k);

Accepted Answer

Image Analyst
Image Analyst on 20 Oct 2020
Try this:
% Extract the curve for X (Volume)
x = Array_demand{w}.Volume;
% Extract the curve for Y (Price)
y = Array_demand{w}.Price;
% Define blue x
bluex = 43210;
% Define red x
redx = bluex - 500
% Find index of the red x
redIndex = find(x <= redx, 1, 'last');
% Get the y value there
redy = y(redIndex)
  7 Comments
Image Analyst
Image Analyst on 23 Oct 2020
You do not have any data in that range. 28,200 is below the lowest x value in your data. Try this:
s = load('answers.mat')
% Extract the curve for X (Volume)
x = s.x;
% Extract the curve for Y (Price)
y = s.y;
plot(x, y, 'k-', 'LineWidth', 2);
grid on;
xlabel('x', 'FontSize', 15);
ylabel('y', 'FontSize', 15);
% Define blue x
bluex = 28700;
xline(bluex, 'LineWidth', 2, 'Color', 'b');
% Define red x
redx = bluex - 500
% Determine if red x is in the range
if redx < min(x) || redx > max(x)
message = sprintf('Error : red x is %.1f which is outside the range of data we have,\nwhich is %.1f - %.1f', redx, min(x), max(x))
uiwait(errordlg(message));
return;
end
% Find index of the red x
redIndex = find(x <= redx, 1, 'last')
% Get the y value there
redy = y(redIndex)
You'll see
message =
'Error : red x is 28200.0 which is outside the range of data we have,
which is 28263.0 - 35387.0'
You can pull it into the range that you DO have like this:
s = load('answers.mat')
% Extract the curve for X (Volume)
x = s.x;
% Extract the curve for Y (Price)
y = s.y;
plot(x, y, 'k-', 'LineWidth', 2);
grid on;
xlabel('x', 'FontSize', 15);
ylabel('y', 'FontSize', 15);
% Define blue x
bluex = 28700;
xline(bluex, 'LineWidth', 2, 'Color', 'b');
% Define red x
redx = bluex - 500
% Determine if red x is below the range
if redx < min(x)
message = sprintf('Error : red x is %.1f which is outside the range of data we have,\nwhich is %.1f - %.1f. I will set it to %.1f', ...
redx, min(x), max(x), min(x))
uiwait(warndlg(message));
% Set it equal to the min that we do have.
redx = min(x);
end
% Determine if red x is above the range
if redx > max(x)
message = sprintf('Error : red x is %.1f which is outside the range of data we have,\nwhich is %.1f - %.1f. I will set it to %.1f', ...
redx, min(x), max(x), max(x))
uiwait(warndlg(message));
% Set it equal to the max that we do have.
redx = max(x);
end
xline(redx, 'LineWidth', 2, 'Color', 'r');
% Find index of the red x
redIndex = find(x <= redx, 1, 'last')
% Get the y value there
redy = y(redIndex)
Angelavtc
Angelavtc on 23 Oct 2020
Wow this is wonderful! Thank you so much @Image Analyst!

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox 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!