Is this correct approach for a single summation in time domain?

1 vue (au cours des 30 derniers jours)
Sand Man
Sand Man le 15 Jan 2014
Commenté : Sand Man le 16 Jan 2014
Hey there,
I am trying to do a single summation of a function in the time domain. I got my code working, but I would feel more confident if someone would verify the correctness or point out my mistakes.
Here is also the formula for what I am trying to achieve:
Here is the code:
h = 100;
t=[1:400];
rho_w = 1025;
g = 9.81;
Ohm = [0.01:0.01:4]
Phase = rand(1,length(Ohm))*2*pi;
Amp = [1:1:400];
for i = 1:length(t)
P(i) = rho_w*g*sum(Amp.*Ohm.*cos(Ohm*t(i)+Phase))
end
Thanks a bunch!
  1 commentaire
Skye
Skye le 15 Jan 2014
Modifié(e) : Skye le 15 Jan 2014
it looks correct for the above equation, though you could make some general improvements:
replace (Ohm*t(i)+Phase)) with (Ohm*i+Phase)), because t(i) = i at all times; replace Amp = [1:1:400] with Amp = t, since they're the same, unless you to have amp different from t at some points (e.g amp = [1:0.5:200]); replace Ohm = [0.01:0.01:4] with Ohm = t/100, unless same reason as amp

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 15 Jan 2014
Looks fine to me. I'll just point out that you could avoid the for-loop by doing
P = cos(bsxfun(@plus, t(:)*Ohm,Phase) )*(rho_w*g*Amp.*Ohm).';

Plus de réponses (3)

Mark
Mark le 15 Jan 2014
looks like P is going to return as a an vector of vectors. The dot operator will take the dot product of your ohm and amp vectors, and then throw in your function of t in those multiplications. When you run your for loop you are creating an indexed value for that time t, but for every amp and ohm value. I also don't see a sum() function so I doubt this code alone is accomplishing what you want done.
  1 commentaire
Sand Man
Sand Man le 15 Jan 2014
Thanks for the quick replies.
Maybe you missed it, but I did include the sum() function, which is inside the P equation.
To my understanding the code does the following: For each t, it calculates all the values for P from n=1 to N and then the sum function adds all those values together and the same procedure is repeated for the next t etc. Thus it is a superposition, where for each time all the waveform components are added together.

Connectez-vous pour commenter.


Sand Man
Sand Man le 16 Jan 2014
But how should I proceed if I would like to take the double summation of the following formula:
Simply doing sum(sum()) will probably not solve the case. Im pretty stuck here.
  1 commentaire
Matt J
Matt J le 16 Jan 2014
Modifié(e) : Matt J le 16 Jan 2014
There are no variables in the above summand that depend simultaneously on n and j. You can therefore factor the double summation into the product of 2 single summations. It is basically just the square of what you calculated before.

Connectez-vous pour commenter.


Sand Man
Sand Man le 16 Jan 2014
You are right about that.
Still keeping to my more time consuming coding due to my beginners level in matlab, but my code for the double summation would be:
for i = 1:length(t)
P(i) = rho_w*g*(sum(Amp.*Ohm.*cos(Ohm*t(i)+Phase))*sum(Amp.*cos(Ohm*t(i)+Phase)))
end
right?
  2 commentaires
Matt J
Matt J le 16 Jan 2014
Modifié(e) : Matt J le 16 Jan 2014
It would be more efficient to do
for i = 1:length(t)
P(i) = rho_w*g*sum(Amp.*cos(Ohm*t(i)+Phase))).^2;
end
so that you don't end up doing the same summation computation twice.
By the way, you should be responding to people or following up to their answers in the Comment boxes and keeping those discussions contained in Comment threads. The Answer blocks are meant as a way of initiating completely new answers to your originally posted question.
Sand Man
Sand Man le 16 Jan 2014
Alright, I'll keep that in mind. Thanks.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by