Basic Matlab Help...Please

I have 50 wave heights (H) and 50 corresponding wave frequencies (w) and I need to create 50 sine waves over a 30 minute period and then add them up.
I have tried:
S = H.*sin(w.*t) which does not work
and:
S_1 = H(1)*sin(w(1).*t)
which takes too long because I would have to do that 50 times and then later would have to do it 306 times.
I need a quicker way to do it that does element by element multiplication to create 50 sine waves and then add the sin waves up element by element.
Thank you

답변 (3개)

Stephen
Stephen 2012년 2월 28일

0 개 추천

I want to add each point of time together from the two sine waves...
i.e
Amplitude from wave 1 at time 1 sec + Amplitude from wave 2 at time 1 sec
but I need it for 30 mins and 50 waves.
Tom
Tom 2012년 2월 28일

0 개 추천

I think that you have to have a for loop. What about
for t=1:1800
S(t) = sum(H.*sin(w*t))
end
This should give you an array S that is the sum of all the waves at each second, so that (for example) S(61) will be the sum of the waves at 1 minute and 1 second and so on.... You should then be able to graph S as a function of time.
plot(S)

댓글 수: 1

Walter Roberson
Walter Roberson 2012년 2월 28일
A for loop makes things easier and clearer, but see my new answer for a method without a for loop.

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2012년 2월 28일

0 개 추천

Let H, w, and t all be row vectors, and H and w must have the same number of elements but t can be different. Then you want
S = sum(bsxfun(@times,H(:),sin(bsxfun(@times,w(:),t))));

댓글 수: 4

Stephen
Stephen 2012년 2월 29일
I did it your way, but when I try plotting it I get nothing what so ever. Seems like the code should be correct, but idk
Stephen
Stephen 2012년 2월 29일
I have noticed that this way only gives me a single answer instead of an array that allows me to create a SINE wave.
Walter Roberson
Walter Roberson 2012년 2월 29일
Hmmm, I did test before I posted. Are you sure your t is a *row* vector?
Stephen
Stephen 2012년 2월 29일
Ignore my ignorance...I found my mistake it works great. Thank

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 MATLAB Coder에 대해 자세히 알아보기

질문:

2012년 2월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by