Matlab error when using a vector

58 Views Asked by At

I keep getting an error when I run my code that uses a vector in my function. Here is my code:

clear all;
    L=30;
    Nt=600;             % Maximum # of time iterations.
    Nx=100;             % Number of points in the x-direction
    deltat=0.1;
    deltax=L/Nx;
    n=1;                % These two paremeters "m" and "n" serve to identify the mode
    m=1;
    c=1;
    t=0;
  
% Generating the grid in the xy-plane. Two arrays.
    for i=1:Nx+1
        x(i)= (i-1)*deltax;
    end
    
 % Graph of the Initial Condition
    for i=1:Nx+1
        u(i)= sum(9/(n^2*pi^2))*sin((n*pi)/3)*sin(((n*pi)/30)*x)*cos(((2*n*pi)/30)*t);
    end 
    plot(x,u);
    axis([0 1 -1 1]);
    xlabel('x-axis');
    ylabel('y-axis')
    pause
    for nt=1:Nt
        t=(nt-1)*deltat;
        for i=1:Nx+1
            u(i)= sum(9/(n^2*pi^2))*sin((n*pi)/3)*sin(((n*pi)/30)*x)*cos(((2*n*pi)/30)*t);
        end
        plot(x,u);
        axis([0 1 -1 1]);
        xlabel('x-axis');
        ylabel('y-axis')
        pause(0.01)  
    end

The error is occurring on this line u(i)= sum(9/(n^2*pi^2))*sin((n*pi)/3)*sin(((n*pi)/30)*x)*cos(((2*n*pi)/30)*t); It says that the indices on the left hand side is not compatible with the size of the right side. Does anyone know why this is happening?