Sinusoid plots, XTick
显示 更早的评论
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 个评论
Jan
2011-3-17
What exactly does "generic" mean here? What should be recognized automatically?
Patrick Star
2011-3-17
Walter Roberson
2011-3-17
When you change y, is the function still certain to be sinusoid? Finding all the zeros of an arbitrary "black box" function is impossible.
What is the highest frequency that needs to be dealt with?
Patrick Star
2011-3-17
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.
采纳的回答
更多回答(1 个)
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')
类别
在 帮助中心 和 File Exchange 中查找有关 Annotations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
