Debugging a Plot of V vs. z2 for June 7

  • Topic: MATLAB 
  • Thread starter QM3BP
  • Start date
  • Tags Tags
    Debugging Plot
1 reply · 3K views · last reply
QM3BP
Messages
1
Reaction score
0
Code:

mu12=5;
mu123=20/21;
z1=1:15;
z2=-10:0.05:10;

counter=1;
index=1;
count=1;
indexer=1;
m=1;
n=1;

for l=1:1:15;

for p=1:1:401;
x=(sqrt(mu12)/10)*z1(n)+z2/(sqrt(mu123));
y=(-(sqrt(mu12)/10)*z1(n))+(z2/(sqrt(mu123)));

z=1:401;
z(indexer)=z1(n)/sqrt(mu12);
indexer=indexer+1; end;

n=n+1; end

for q=1:1:15;

V13=[];
for i=0:1:398; if abs(x(counter))<1 V13(counter,m)=-1; else V13(counter,m)=0; end; counter=counter+1; end;

V23=[];
for j=0:1:398; if abs(y(index))<1 V23(index,m)=-1; else V23(index,m)=0; end; index=index+1; end;

V12=[];
for k=0:1:398; if abs(z(count))<1 V12(count,m)=-1; else V12(count,m)=0; end; count=count+1; end;

V=[];
V(:,m)=V12(:,m)+V23(:,m)+V13(:,m);

m=m+1; end;

plot(z2,V(:,1))





Error Message:

? Attempted to access x(402); index out of bounds because numel(x)=401.

Error in ==> Vvsz2plot_june7_debug at 28
for i=0:1:398; if abs(x(counter))<1 V13(counter,m)=-1; else V13(counter,m)=0; end; counter=counter+1; end;
 
Learn to indent and format your code! We have a [ code ] tag at this site, but it won't help with your code because your code is formatted flush left.The problem is that you are never resetting the variable counter. That variable exceeds 401 on the second pass through the for q=1:1:15; loop.