How to find a point within a curve?

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

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

Thank you @Image Analyst, but when I try to find the index I get this error:
0×1 empty double column vector
How can I solve this?
I guess it is because I am trying to find a point that is on the left side of the curve shown, where there are not so many (x,y) pairs defining the curve. Is there a way to solve this?
What's empty? redIndex? Do your x values have any values less 43,210-500? If not, like they're all 50 thousand and above, then the value you're looking for is not in the range of your data, so of course it won't be able to find it. Attach your x and y data if you need more help.
save('answers.mat', 'x', 'y');
Then attach answers.mat with the paperclip icon.
Thank you! Like for example this graph where
bluex = 28700;
But the redIndex is telling me empty double, even when 28200 belongs to the x range.
I attach here the x, and y.
@Image Analyst any news? Thank you!
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)
Wow this is wonderful! Thank you so much @Image Analyst!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!