Creating multiple polar plot graphs for different frequencies

조회 수: 6 (최근 30일)
Cameron Latta
Cameron Latta 2019년 10월 19일
답변: Daniel M 2019년 10월 19일
Essentially trying to use a loop to generate multiple plots for different values of f.
I get this error message:
Index exceeds the number of array elements (1).
Error in PistonDirectivity_2_11 (line 21)
f = f(n);
f = [125 250 500 1000 2000 4000 8000];
%f = 3.3820e+03; %frequency
c = 340; %speed of sound at 20 degrees
rho0 = 1.2; %Acoustic impedance at 20 degrees
r = 100; %radius
a = 0.16; %piston radius
u0 = 1; %particle velocity
theta = linspace(-pi/2+1.e-16,pi/2-1.e-16,181)'; %range of theta values from 90 to -90 degrees
%p = (1j*rho0*c*k*a^2*u0/(2*r) .* 2.* besselj(1,kasintheta)./kasintheta) * exp(-1j*k*r);
n = 1;
%% loop to make multiple plots
%while n < length(f)
for n = 1 : length(f)
f = f(n);
k = 2*pi*f./c; %wavenumber
p = besselj(1, k*a.*sin(theta))./(k*a.*sin(theta));
db = 20*log10(abs(p)); %calculate dB and plot polar response
mx = max(db);
dynamic_range = 40;
db = db + dynamic_range - mx;
db = max(db,zeros(size(db)));
polar(theta,db); %plot polar response
figure %plot flat response
plot(theta,db)
xlabel('Theta (rads)')
ylabel('Level (dB)')
hold on
n = n+1;
end
  댓글 수: 2
Daniel M
Daniel M 2019년 10월 19일
You're overwriting f in your loop. Don't do that.
Cameron Latta
Cameron Latta 2019년 10월 19일
Ahh, thank you! That seems to have fixed it! Cheers

댓글을 달려면 로그인하십시오.

채택된 답변

Daniel M
Daniel M 2019년 10월 19일
I'm writing the same thing here so this question can be answered.
The issue is you are overwriting your variable f when you write f = f(n). I suggest changing it inside your loop, to fn = f(n).

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by