How to find a point within a curve?

26 次查看(过去 30 天)
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);

采纳的回答

Image Analyst
Image Analyst 2020-10-20
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 个评论
Image Analyst
Image Analyst 2020-10-23
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 2020-10-23
Wow this is wonderful! Thank you so much @Image Analyst!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by