> stepx=50; % Number of spatial steps > T=5; % Total time > k=0.01; % Duration of time step > stept=T/k; % Number of time steps > h=.01; % Width of each spatial > c=0.001; % Constant (Supposing the > value as 0.001) > new_c = c*k/(2*h); % Courant Number > x=-2*pi:h:2*pi; > u=zeros(stept,stepx);%Initialize velocity data array > to zero. > % Giving Initial Condition > u=sin(4*pi*x);
u=zeros(stept,stepx) gives a matrix, u=sin(4*pi*x) gives a vector. So in the next step, you want to write a matrix element (u(2,2)) into a vector which does not work. Furthermore, you generate an x vector (x=-2*pi:h:2*pi) the dimension of which does not equal stepx.
> > %The below for loop calculates subsequent velocities > for n=2:k:4 > for j=2:h:stepx-1 > > u(n+1,j) = u(n,j) + new_c*(u(n,j+1) + > n,j+1) + u(n,j-1)); > Error at above line syats u(2,2) is out of bounds > Please help to solve the above problem guys