BSXFUN for use with sinusoids

1 view (last 30 days)
Nicholas
Nicholas on 29 Feb 2012
The question: Make two plots of the seastate: First, plot a reasonable segment of the time history to see the detail of the sea surface. Second, make a plot of a very long segement of the time history (30 minutes).
I completed the problem before this using
A=[Height./2]'; % Needs to be a column vector
w = [omega]'; % Needs to be a column vector of the same size as A
t = 700:0.5:1000; % Needs to be a row vector
waves = bsxfun(@times,A,sin(bsxfun(@times,w,t)));
sea = sum(waves,1);
This is the equivalent of A*sin(w*t) This does not include the random phase angle which I now must use in the next portion.
So essentially I need it to be A*sin(w*t)+(random phase angle value)
I know I need to use the @plus feature in bsxfun and the rand(n) function in order to make this work, but I cannot figure out how to incorporate this into the following equation.
waves = bsxfun(@times,A,sin(bsxfun(@times,w,t)),@plus,B);
sea = sum(waves,1);

Answers (2)

Wayne King
Wayne King on 29 Feb 2012
Hi Nicholas, bsxfun is great function, but it's overkill to create a sine wave with a random phase.
Since this is homework, I do not want to simply give you the solution, but I'll give the following hints:
First create a time vector. See the help for linspace() for example, or read up on the colon operator in MATLAB.
Also, the random phase angle has to be INSIDE the cosine or sine expression, not outside as you have written the equation above.
See the help for rand as a hint and think about how you can get rand to give you a number between [-pi,pi) instead of [0,1]
  1 Comment
Walter Roberson
Walter Roberson on 29 Feb 2012
Nicholas is doing the calculation over a list of wave heights and frequencies, and over a list of times. The alternative to bsxfun() is to use at least one "for" loop.

Sign in to comment.


Wayne King
Wayne King on 1 Mar 2012
Thank you Walter for clearing that I up. Somehow I missed that. Nicholas you are very close.
Work off this simple example (I didn't use a lot of points or frequencies)
A = (1:5)';
omega = 2*pi*[0.1:0.1:1/2];
omega = omega(:);
t = 0:20;
phi = -pi+2*pi*rand(size(omega));
freq = bsxfun(@times,omega,t);
% here was the part you were missing, the phase has
% to be added to the 2*pi*f*t part inside of sin()
waves = sin(bsxfun(@plus,freq,phi));
waves = bsxfun(@times,A,waves);
sea = sum(waves,1);

Community Treasure Hunt

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

Start Hunting!