Sinusoid plots, XTick

5 visualizaciones (últimos 30 días)
Patrick Star
Patrick Star el 17 de Mzo. de 2011
I've plotted a couple periods of y=5*sqrt(2)*cos(2*pi*t+pi/4) and I would like to use XTick (preferably, if it works) to set the labels on the x-axis to be all of the values of t such that y is at a maximum, minimum, or zero. I could do it manually, but I need it to be generic (i.e. I could change the function and the labels would conform to it). It could do it if there wasn't a phase shift, but there is. Does anyone know how to do this? Thanks.
For clarification: This image is the plot as it is now: http://img684.imageshack.us/img684/7678/whatihaveb.png I drew vertical lines where I want tick marks in this image: http://img838.imageshack.us/img838/1121/whatiwant.png
  5 comentarios
Patrick Star
Patrick Star el 17 de Mzo. de 2011
y will always be a sinusoid. If possible, I would like it to function at any frequency, but leaving it at a frequency of 1 Hz will be fine.
Walter Roberson
Walter Roberson el 18 de Mzo. de 2011
If you push the frequency high enough, the labels will overlap. Higher still and it will be a terrible mess to plot, when you start approaching one cycle per pixel.

Iniciar sesión para comentar.

Respuesta aceptada

Paulo Silva
Paulo Silva el 17 de Mzo. de 2011
clf
t=0:0.01:2*pi;
y=5*sqrt(2)*cos(2*pi*t+pi/4);
%hold on %in case you want to draw your own y lines instead of using GRID
plot(t,y,'r'); 
mx=max(y); %find the maximum value of y
mxm=min(y); %find the minimum value of y
%draw your own lines 
% line(get(gca,'Xlim'),[mx mx],'LineStyle','--','Color',[0 0 0]);
% line(get(gca,'Xlim'),[-mx -mx],'LineStyle','--','Color',[0 0 0]);
% line(get(gca,'Xlim'),[0 0],'LineStyle','--','Color',[0 0 0]);
t1=find(y>=(mx-0.01)); %dirty way to find maximum
t2=find(y>=-0.23 & y<=0.23); %dirty way to find zeros
t3=find(y<=(mxm+0.01)); %dirty way to find minimum
ti=[t1 t2 t3]; %put all the interesiting values on a vector
ti=sort(ti); %put the values in ascending order
v=[find(diff(ti)~=1) numel(ti)]; %remove unwanted values and add last one
%set the ticks and turn on grids
set(gca,'XTick',t(ti(v)))
set(gca,'XTickLabel',t(ti(v)))
set(gca,'XGrid','on')
set(gca,'YTick',[mxm 0 mx])
set(gca,'YTickLabel',[mxm 0 mx])
set(gca,'YGrid','on')
axis([0 max(t(ti(v))) mxm-0.2*abs(mxm) mx+0.2*mx]) %adjust axis
  1 comentario
Patrick Star
Patrick Star el 18 de Mzo. de 2011
Thanks! This works.

Iniciar sesión para comentar.

Más respuestas (1)

the cyclist
the cyclist el 17 de Mzo. de 2011
This code will identify just the maximum (not the minimum or zeros), but you should be able to see how to generalize it.
A cautionary note: you'll see in the plot from my code that it does not identify the second occurrence of the true maximum of the sine wave. This is because when I have sampled at every 0.01 interval of x, it is not a maximum of y there. You may need to code around that in your own data.
x = 0:0.01:4*pi;
y = sin(x);
figure
plot(x,y)
set(gca,'YLim',[-2 2])
[ymax indexToYmax] = max(y);
xAtMaxY = x(indexToYmax);
set(gca,'XTick',xAtMaxY);
set(gca,'XTickLabel',num2str(xAtMaxY));
set(gca,'XGrid','On')

Categorías

Más información sobre Annotations en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by