Sinusoid plots, XTick

조회 수: 6 (최근 30일)
Patrick Star
Patrick Star 2011년 3월 17일
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
Patrick Star
Patrick Star 2011년 3월 17일
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 2011년 3월 18일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Paulo Silva
Paulo Silva 2011년 3월 17일
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
Patrick Star
Patrick Star 2011년 3월 18일
Thanks! This works.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

the cyclist
the cyclist 2011년 3월 17일
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')

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by