Index in position 2 exceeds array bounds (must not exceed 25).

function[spl] = tryal2(f,P,t)
if f == 40
w = 2*pi*f;
A = 10;
end
c = 343;
x = [0:.5:11];
y = [0:.5:15];
k = w/c;
[xx,yy]= meshgrid(x,y);
H = sqrt((xx-0).^2)+(yy-9).^2;
Lx = length(x);
Ly = length(y);
phasor = zeros(Lx,Ly);
Meanpressure = zeros(Lx,Ly);
spl = zeros(Lx,Ly);
for a=1:Lx
for b=1:Ly
phasor(a,b) = ((A+P)./H(a,b))*exp(1j*((w*t)-((w/c)*H(a,b))));
Meanpressure(a,b) = sqrt(0.5*phasor(a,b)*conj(phasor(a,b)));
spl(a,b)= 20*log10(Meanpressure(a,b)/(20*10^(-6)));
end
end
figure
surf(xx,yy,spl)
The error occurs in phasor(a,b) = ((A+P)./H(a,b))*exp(1j*((w*t)-((w/c)*H(a,b)))); i am confused what i am doing wrong I need a grid by 11 by 14 but i only get answers if x and y martixes are the same.

Answers (3)

benjamin - isn't H of length Lx (or Ly since identical) and is an array? Treating it like a matrix as H(a,b) would then lead to this error. Note how
H = sqrt((xx-0).^2)+(yy-9).^2;
and so is not a matrix since xx and yy are arrays.

3 Comments

So what do I need to do to fix it
well either make H a matrix or just grab H(b) on each iteration. to be honest i don't know enough about your code (i.e. what it does) to tell you what to do.
Benjamin - please provide the inputs to your function. What is f? What are P and t?
Also, please further the conversation by adding comments and not creating answers.

Sign in to comment.

if i run the code with
x = (1:h:15)
y = (1:h:15)
i get 123124124.PNG
if i run the code with
x =[1:h:12]
y =[1:h:15]
then i get the error
Error in tryal1 (line 39)
phasor(a,b) = ((A+P)./H(a,b))*exp(1j*((w*t)-((w/c)*H(a,b))));
my code is to plot the Sound pressure leve in DB on a meshgrid but my dimensions of the grid are 11 by 14 but the formula on line 39 wont work

Tags

Asked:

on 26 Apr 2019

Commented:

on 27 Apr 2019

Community Treasure Hunt

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

Start Hunting!