Plotting Transmission Loss with for loop

조회 수: 2 (최근 30일)
Articat
Articat 2019년 4월 3일
댓글: Star Strider 2019년 4월 3일
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.

채택된 답변

Star Strider
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
Articat
Articat 2019년 4월 3일
I see, thank-you!
So previsouly I had it only as TL being a vector? Or was the key here the "numel" function?
Star Strider
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개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by