Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 13 Feb. 2015
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 Kommentare
Jesse
Jesse am 20 Feb. 2015
Yes that's it. Thanks Geoff!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by