Sinusoid plots, XTick

8 次查看(过去 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 个)

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 中查找有关 2-D and 3-D Plots 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by