Debugging a Plot of V vs. z2 for June 7

  • Context: MATLAB 
  • Thread starter Thread starter QM3BP
  • Start date Start date
  • Tags Tags
    Debugging Plot
Click For Summary
SUMMARY

The discussion focuses on debugging a MATLAB script that generates a plot of V versus z2, specifically addressing an "index out of bounds" error. The error occurs because the variable 'counter' is not reset, causing it to exceed the length of the array 'x' during the second iteration of the outer loop. The code snippet provided includes the definitions of variables mu12 and mu123, as well as the nested loops that compute the values for V. The solution involves resetting the 'counter' variable before each iteration of the outer loop to prevent exceeding the array bounds.

PREREQUISITES
  • Familiarity with MATLAB programming and syntax
  • Understanding of array indexing and bounds in MATLAB
  • Knowledge of plotting functions in MATLAB
  • Basic concepts of control flow using loops in programming
NEXT STEPS
  • Review MATLAB array indexing rules to avoid out-of-bounds errors
  • Learn about MATLAB debugging techniques to identify and fix runtime errors
  • Explore MATLAB plotting functions to enhance data visualization
  • Investigate best practices for code formatting and indentation in MATLAB
USEFUL FOR

MATLAB programmers, data analysts, and anyone involved in scientific computing who needs to debug and optimize plotting scripts.

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;
 
Physics news on Phys.org
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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
5K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K