MATLAB: Creating multiple polar plot graphs for different frequencies

errorfor looploopMATLABplotvariableswhile loop

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

Best Answer

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).