ERROR: Index exceeds matrix dimensions
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Quite new to matlab, I was doing some practice questions but when I came across this particular one I hit a brick wall.
%---------------------------------------------------------------------------------------------------------------------------
% Basic rectangular domain with uniform separation h between nodes
clc;clear all;close all;
hx = 0.01;
hy = 0.005;
Nx = 1/hx+1 ;
Ny = 4/hy+1 ;
x = linspace(0,1,Nx) ;
y = linspace(0,4,Ny) ;
x = x' ;
y = y' ;
%Create a mapping from the 2-D lattice indices to a single vector index by
%labelling all the points in the domain
VecToCoord = zeros(0,2) ; %First index is the vector inex, first column gives the xindex and the second gives the yindex
for i = 1:Nx
VecToCoord = [VecToCoord;[[1:Nx]',i*ones(Nx,1)]];
end
CoordToVec = zeros(Nx,Ny); %first element is the xindex and second is the yindex
count = 1 ;
for j = 1:Ny
for i = 1:Nx
CoordToVec(i,j) = count ;
count = count + 1 ;
end
end
N= Nx*Ny; %Total number of nodes in whole domain
%Store Laplacian stencil
stencil = [1,1,-4,1,1] ;
%Find matrix for all nodes
A = sparse(Nx,Ny) ;
%RHS vector
b = zeros(Nx,1) ;
f = @(x,y) 50*sin(4*pi*x*(1-x))-50*cos(4*pi*y);
for i =1:N
xCoord = VecToCoord(i,1) ; %<----------------------ERROR:INDEX EXCEEDS MATRIX DIMENSIONS
yCoord = VecToCoord(i,2) ;
xval = x(VecToCoord(i,1)) ;
yval = y(VecToCoord(i,2)) ;
%-----------------------------------------------------------------------------------------------------------------------
if x(xCoord) == 0 || x(xCoord) == 1 || y(yCoord) == 0 || y(yCoord) ==4 %BOUNDARIES
%Boundary node
A(i,i) = 1 ; %implement dirichlet condition
b(i) = yval-xval ;
else %Internal nodes
upInd = i + Nx;
downInd = i - Nx;
leftInd = i - 1 ;
rightInd = i + 1 ;
%elements of update matrix in row i
A(i,upInd) = 1/(hy^2) ;
A(i,downInd) = 1/(hy^2) ;
A(i,i) = -2/hx^2 -2/hy^2 ;
A(i,leftInd) = 1/(hx^2) ;
A(i,rightInd) = 1/(hx^2) ;
b(i) = f(xval,yval) ;
end
end
sol = A\b ;
%Convert to a matrix to plot as surface
sol_Matr = zeros(Nx,Ny) ;
for i = 1 : Nx
for j = 1 :Ny
sol_Matr(i,j) = sol(CoordToVec(i,j)) ;
end
end
surf(x,y,sol_Matr')
hold on
shading interp
xindex = find(x==0.5) ;
yindex = find(y==0.5) ;
sol_Matr(xindex,yindex)
I'm pretty sure the cause of the error lies between the two long dashed lines(--------) that I put in the codes. I've also pointed out which line is the error in the codes. I am aware that the error is occuring due to me trying to access an element past the actual length of the array and thus it wouldn't exist, but as most of it is a skeleton code where I just had to tweak about here and there, I am not sure whats causing the problem. I tried changing some stuff about to no avail. I would highly appreciate it if someone could point out where the problem is and advice me on how to change those lines. Thank you very much :)
0 Commenti
Risposte (1)
Image Analyst
il 31 Ott 2020
It's because the matrix has 10201 rows and you're trying to go up to N which is 80901. So you can only go as many rows as you have, which is size(VecToCoord, 1):
for i =1: size(VecToCoord, 1)
xCoord = VecToCoord(i,1) ; %<----------------------ERROR:INDEX EXCEEDS MATRIX DIMENSIONS
3 Commenti
Image Analyst
il 1 Nov 2020
Well just hover over the variables to see what their values and sizes are.
Like what is the size of CoordToVec, i, and j? Obviously either i or j is bigger than the corresponding dimension of CoordToVec.
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!