Is this correct approach for a single summation in time domain?
1 回表示 (過去 30 日間)
古いコメントを表示
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/155139/image.jpeg)
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 件のコメント
Skye
2014 年 1 月 15 日
編集済み: Skye
2014 年 1 月 15 日
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
採用された回答
Matt J
2014 年 1 月 15 日
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).';
0 件のコメント
その他の回答 (3 件)
Mark
2014 年 1 月 15 日
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.
Sand Man
2014 年 1 月 16 日
2 件のコメント
Matt J
2014 年 1 月 16 日
編集済み: Matt J
2014 年 1 月 16 日
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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!