Adding zero points on a plot of a damped sine wave - quick question

4 次查看(过去 30 天)
Greetings all,
This will probably be a quick one for you experts, but I have a damped sine wave of:
y1=2*sin(w*t).*exp(-lambda*t);
The user inputs w and lambda, and I have t=0:.001:1;
Now, since the sine wave crosses zero, I would like to add a circle or X where that crossing is just to highlight it. I played around with for and if statements, but not getting it, even though it's kinda trivial.
Any assistance?
Thanks!
-J

采纳的回答

Geoff Hayes
Geoff Hayes 2015-2-13
Jesse - you could compare each element to its neighbour and see if there is a switch in sign i.e. from positive to negative or negative to positive. The easiest way to do this, without a for loop, is to use arrayfun and just check to see if each neighbour is different. Something like
idcs = arrayfun(@(k)sign(y1(k))~=sign(y1(k+1)),1:length(y1)-1);
We use sign to check the sign of the number, and if the sign between two neighbouring elements is different then
sign(y1(k))~=sign(y1(k+1)
is true (or 1). Note how we use the anonymous function
@(k)sign(y1(k))~=sign(y1(k+1))
to take as an input k which will be an index into y1. The resulting logical array, idcs, will have ones where there is a difference in sign which is the zero crossing. Use find to find those indices as
find(idcs==1)
which you can then use to draw your X or circle at the zero crossing. Try the above and see what happens!
  4 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by