Plotting Transmission Loss with for loop
4 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Star Strider
on 3 Apr 2019
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 Comments
Star Strider
on 3 Apr 2019
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!