How to get an x coordinate from a given y?

Hello, I am working a problem that asks me to graph the square of the Amplitude(A) of an oscillation that is a function of omega. I successfully found a way to extract the maximum value of A^2, but have not found a way to find the position omega where A^2 is half of its maximum value. Also, I do not know how to let value x, which is in this case, take all value in real number instead of just the range [0,1000].
x = 0:1000;
y = ((159)./sqrt((1000.^2 - x.^2)+(2*50*x).^2)).^2;
plot(x,y)
xlabel('Omega in rad/second');
ylabel('Amplitude A^2 in meter square(m^2)');
title('Rolling 2 HW7');
max(y);
indexmax = find(max(y) == y);
xmax = x(indexmax);
ymax = y(indexmax);
strmax = ['Max = ',num2str(ymax)];
text(xmax,ymax,strmax,'HorizontalAlignment','right');
[maxY, indexOfMaxY] = max(y);
value = find(y == 0.5*ymax);
display(value);

 Accepted Answer

Guillaume
Guillaume on 24 Oct 2017
Edited: Guillaume on 24 Oct 2017
One usually uses
indexmax = find(max(y) == y);
when there can be several y values that are equal to the max, as indexmax will be a vector of indices of all these max values. e.g.
y = [3 2 1 1 2 3 2 1 1 3 2 1]
indexmax = find(max(y) == y);
returns
indexmax == [1 6 10]
However, later on, your code (the strmax = ...) assumes that indexmax is scalar. In that case, one would use the simpler syntax
[~, indexmax] = max(y);
This will always return just one index. In case of duplicate max, it's the index of the first max value.
As for your question, there's no guarantee that the exact value of half maximum will be present in your array so you can't search for that exact value. Instead you can search for the nearest. The position of that nearest value is the index of the absolute minimum of the difference between y and the half maximum, so:
[~, indexhalfmax] = min(abs(y - max(y)/2));
edit: fixed that last line of code.

7 Comments

Hieu Nguyen
Hieu Nguyen on 24 Oct 2017
Edited: Hieu Nguyen on 24 Oct 2017
I am confused. Here is my graph. Would it be easier if I can find the value of (half ymax) and then find two values that correspond to this half y_max? Is there a way to do that? I am new to Matlab so I am trying my best. Thanks!
Oops, forgot to divide the max value by 2 in my calculation to get the half maximum. Fixed that now. However, my explanation was correct, so you could have worked out the mistake on your own.
Hello, I plot the graph and I cannot find or use the display the desired x value corresponding to half of the y_max. What can I do?
x = -1000:1000;
y = ((159)./sqrt((1000.^2 - x.^2)+(2*50*x).^2)).^2;
plot(x,y)
xlabel('Omega in rad/second');
ylabel('Amplitude A^2 in meter square(m^2)');
title('Rolling 2 HW7');
[~, indexmax] = max(y);
[~, indexhalfmax] = min(abs(y - max(y)/2));
xmax = x(indexmax);
ymax = y(indexmax);
strmax = ['Max = ',num2str(ymax)];
text(xmax,ymax,strmax,'HorizontalAlignment','right');
[maxY, indexOfMaxY] = max(y);
I really don't understand what difficulty you're having. You have all the necessary information in your code above:
text(x(indexhalfmax), y(indexhalfmax), sprintf('half max:%g', y(indexhalfmax)), 'HorizontalAlignment','right');
Hello, I am sorry I did not mean to frustrate you. I am trying to get the x value that corresponds to half ymax and I don’t know how to do that in Matlab. Say my half ymax is 0.01264. I want to find the x values that give me 0.01264 and I don’t know how to do that. I could find the half ymax but I couldn’t not determine the x values. Please help.
Again, isn't
x(indexhalfmax)
the value you're looking for. (i.e. the x that correspond to the y value nearest to the actual half max value)
I don't know why it is giving that but it does not make sense. I plug the number x = 10 into my formula: y = (159^2)/[(1000^2-x^2)^2+4*50^2*x^2. The result is not 0.01264.

Sign in to comment.

More Answers (1)

Categories

Find more on Simulink in Help Center and File Exchange

Asked:

on 24 Oct 2017

Answered:

on 25 Oct 2017

Community Treasure Hunt

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

Start Hunting!