1

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^2pi^2))sin((npi)/3)sin(((npi)/30)x)cos(((2npi)/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^2pi^2))sin((npi)/3)sin(((npi)/30)x)cos(((2npi)/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?

  • I don't see an i index in the expression for u(i), it only depends on n which is 1. I don't see a definition of x either. How does the sum() command work, does it need an index? – Paul Jan 19 '23 at 18:33
  • 1
    It looks like you closed the "sum" parentheses too soon? – Brian Moehring Jan 19 '23 at 18:36
  • 1
    By the way, this question is currently off-topic for math.se. Consider the [matlab] tag info: "For mathematical questions about MATLAB; questions purely about the language, syntax, or runtime errors would likely be better received on Stack Overflow." – Brian Moehring Jan 19 '23 at 19:03

1 Answers1

1

The main error is the "x" in the expression of "u(i)". "x" is a vector, so it should have an index such as "x(i)".

It's better to specify the sizes of the vectors "x" and "u" before their values are given, such as adding 'x=zeros(1,Nx+1);' and 'u=x;' following the line of 't=0'.