A line at maximum value of y ?

want to draw a line at maximum of yth value that will give at what value of x, y is maximum? that value I have to print with legend?
how can I plot perpendicular to this line and want to know value of x, mean at what value of x my y is max coming and i want value of x at which y is maximum and max(y)to be print in legend or somewhere in plot? How can I?

 Respuesta aceptada

Giorgos Papakonstantinou
Giorgos Papakonstantinou el 9 de Ag. de 2013

1 voto

line(get(gca,'Xlim'), [max(y) max(y)],'color', 'red')

9 comentarios

RS
RS el 9 de Ag. de 2013
Editada: RS el 9 de Ag. de 2013
ok thanks how can I plot perpendicular to this line and want to know value of x, mean at what value of x my y is max coming and i want value of x at which y is maximum and max(y)to be print in legend or somewhere in plot? How can I?
Giorgos Papakonstantinou
Giorgos Papakonstantinou el 9 de Ag. de 2013
Editada: Giorgos Papakonstantinou el 9 de Ag. de 2013
x=0:0.1:2*pi;
y=sin(x);
plot(x, y)
hold on
line(get(gca,'Xlim'), [max(y) max(y)],'color', 'red') % line at ymax
xm=find(y==max(y));
legend(num2str(y(xm)))
x(xm)
If you want a perpendicular at which x you have the ymax:
line([x(xm) x(xm)],get(gca, 'Ylim'), 'color','r' )
If you struggle because you have multiple y sets then maybe this will help you:
x=0:0.1:2*pi;
y(:,1)=sin(2.*x);
y(:,2)=2*sin(x);
plot(x, y(:,1),x ,y(:,2))
hold on
line(get(gca,'Xlim'), [max(y(:)) max(y(:))],'color', 'red') % line at ymax
[xm, b]=find(y==max(y(:)));
legend(num2str(y(xm,b)))
x(xm)
line([x(xm) x(xm)],get(gca, 'Ylim'), 'color','r' )
RS
RS el 9 de Ag. de 2013
Editada: RS el 9 de Ag. de 2013
I only need one line which is parallel to y axis and same line for other curve also, and for both curve , line gives the x, y value? In legend I have to print (x at y=0,y at ymax) How can I ? Thanks
Also in plot I want to restrict the range on x axis through command?
Giorgos Papakonstantinou
Giorgos Papakonstantinou el 9 de Ag. de 2013
Editada: Giorgos Papakonstantinou el 9 de Ag. de 2013
You need one line which is parallel to y axis:
line([x(xm) x(xm)],get(gca, 'Ylim'), 'color','r' )
This line is parallel to y axis at x0 which have ymax.
If I understand correctly you need also another line at x0 which you have ymax at a second curve: So that's a code what does what you want (plus the legend you asked for)
x=0:0.01:2*pi;
% Three curves
A=sin(x+2);
B=2*sin(x);
C=-(0.1-x).*(x-5);
% Plotting Three Curves
h=plot(x, A, x, B, x, C);
hold on
% Find the Ymax in each curve
ymaxA=max(A);
ymaxB=max(B);
ymaxC=max(C);
% Find the index of X which you have Ymax
xmaxA=find(A==max(A));
xmaxB=find(B==max(B));
xmaxC=find(C==max(C));
% Find X at Y=0
xA0=x(A==0);
xB0=x(B==0);
xC0=x(C==0);
% Plot a line for each curve PARALLEL to yaxis for each curve
limh=get(gca,'Ylim');
chrom=get(h,'color'); % find the color of each curve
line([x(xmaxA) x(xmaxA)], [limh(1) max(A)], 'color', chrom{1},'Linestyle',':')
line([x(xmaxB) x(xmaxB)], [limh(1) max(B)], 'color', chrom{2},'Linestyle',':')
line([x(xmaxC) x(xmaxC)], [limh(1) max(C)], 'color', chrom{3},'Linestyle',':')
% Add a legend
strA=sprintf(' x=%.1f at y=0\n x=%.1f at ymax', xA0, x(xmaxA));
strB=sprintf(' x=%.1f at y=0\n x=%.1f at ymax', xB0, x(xmaxB));
strC=sprintf(' x=%.1f at y=0\n x=%.1f at ymax', xC0(1), x(xmaxC));
legend(strA, strB, strC)
If you want to display the ymax instead of the string ymax then erase the word ymax from the strA etc. and add %.1f. Also you have to add at the end of the str's the max(A) etc.
If you want to restrict the range of the x axis
set(gca,'Xlim',[2 5])
Read about gca, line, find, legend and sprintf in the mathworks website! Read also about formatspec in sprintf.
RS
RS el 10 de Ag. de 2013
Why this error is coming and how can I overcome?
limh=get(gca,'Ylim');
chrom=get(h,'color'); % find the color of each curve
Undefined function or variable 'h'.
What kind of error do you get?
If you look carefully the variable h is defined at the beginning of the code as:
h=plot(x, A, x, B, x, C);
RS
RS el 10 de Ag. de 2013
Editada: RS el 10 de Ag. de 2013
ok its working; and when I am using subplot command error is something like that
b = subplot(2,1,2); plot(x1,y1(:,1:3)); xlabel('r)'); ylabel('s');
h=b;
limh=get(gca,'Ylim');
chrom=get(h,'color');
line([x1(xmaxA) x1(xmaxA)], [limh(1) max(y1(:,1))], 'color', chrom{1},'Linestyle',':')
Cell contents reference from a non-cell array object.
and after that I want to use combine both plot using
samexaxis('ab','xmt','on','ytac','join','yld',1);
So How can I overcome? At last also I want to put different scale on right y axis and upper x axis? How can I?
Giorgos Papakonstantinou
Giorgos Papakonstantinou el 10 de Ag. de 2013
Editada: Giorgos Papakonstantinou el 10 de Ag. de 2013
For the sameaxis I have no answer. I don't now what it does and who is the creator of it.
Give a new name to this (not to the subplot h=b):
nameplot=plot(x1,y1(:,1:3));
then
chrom=get(nameplot,'color');
line([x1(xmaxA) x1(xmaxA)], [limh(1) max(y1(:,1))], 'color', chrom,'Linestyle',':'
If you only one plot then (i.e. after the subplot) then chrom is no longer a cell. So the indexing chrom{1} makes no sense. crom in this case is a vector with three elements.
If you want 2 y axis (one on the left and one the right) the use plotyy
And S it's better if you have another question to open another thread. Your first question has been fully covered. If you think that your question has been answered please accept it as answer.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by