Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab Error

  1. Jun 7, 2012 #1
    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;
     
  2. jcsd
  3. Jun 7, 2012 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab Error
  1. MATLAB ezplot error (Replies: 1)

  2. Error code in Matlab (Replies: 3)

Loading...