Plotting Transmission Loss with for loop
2 次查看(过去 30 天)
显示 更早的评论
Hi,
So I am trying to plot the following script but all I am getting is an empty plot with an x-axis going from 2999 to 3000.1.. It should be a sinusoidal wave due to the sine squared at the end of the equation. At least I think so..
Any advice?
Thanks for the help.
0 个评论
采纳的回答
Star Strider
2019-4-3
The ‘f’ variable will be the scalar last value at the end of the loop. What you likely intended was:
s1 = 0.05;
s2 = 0.15;
L = 0.18;
c = 343;
n = 3000;
fv = 1:n;
for f = 1:numel(fv)
TL(f) = 10*log10(1+.25*(((s2/s1)-(s1/s2))^2)*sin((2*pi*f*L)/c)*sin((2*pi*f*L)/c));
end
plot(fv,TL)
This will produce the plot you anticipated, with ‘fv’ and ‘TL’ now both being vectors. .
2 个评论
Star Strider
2019-4-3
My pleasure!
‘TL’ is a vector, however ‘f’ is a scalar in every loop iteration, and plotting a vector against a scalar plots one point only. The plot function plots lines between points, not points themselves, unless you use a marker, so the plot appeared blank.
The key was defining ‘fv’ as a vector, and then using ‘f’ as elements of the vector in the loop. The numel call returned the number of elements in ‘fv’ (similar functions are length and size), so ‘f’ as an index counter also worked as a variable here. A more rigorous approach would be:
for f = 1:numel(fv)
TL(f) = 10*log10(1+.25*(((s2/s1)-(s1/s2))^2)*sin((2*pi*fv(f)*L)/c)*sin((2*pi*fv(f)*L)/c));
end
that would allow ‘fv’ to be any vector of values, not only integers.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Pulsed Waveforms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!