Index out of Bounds and More...

2 views (last 30 days)
Kelsey
Kelsey on 20 Mar 2014
Commented: Walter Roberson on 20 Mar 2014
So I keep being told that my local function is indexing out of bounds at [12,1] and I'm lost as to why/when it's doing that.
I'm also having problems assigning the output variable of the local function to a variable in the main function, but that's probably because I'm not doing it properly. (Specifically N1, N2, and N3)
I'm including the code below; any and all help is appreciated (even if has to do with flaws other than what I'm asking about). Also, for some context, this is supposed to use the Gauss Seidel method to approximate the temperature of different points along a flat plate with a hole cut out of the center.
function P2
close all; clear all; clc;
%lambda = 1
lambda1 = 1;
N1 = P2subfxn(lambda1);
%lambda = 0.5
lambda2 = 0.5;
N2 = P2subfxn(lambda2);
%lambda = 1.5
lambda3 = 1.5;
N3 = P2subfxn(lambda3);
Displaying # of iterations
Iter = ['Lambda = 1 has ' N1 ' iterations' ;...
'Lambda = 0.5 has ' N2 ' iterations' ; 'Lambda = 1.5 has '...
N3 ' iterations'];
disp(Iter)
end
function [n] = P2subfxn(lambda)
Tx = 16; % max x value in Q1
Ty = 11; % max y value in Q1
deltT = zeros(Ty, Tx); % T diff between iterations
T = zeros(Ty, Tx); % T matrix
n = 0; %clearing previous n values
deltaT = 1;
while deltaT >= 0.001
for y = 1:Ty
for x = 1:Tx
T1 = T(y,x); % current Temp at x,y
alpha = 1;
r = ((16 - x)^2 + (11 - y)^2)^0.5;% in/on hole
ry = ((16 - x)^2 + (11 - (y+1))^2)^0.5;% boundary in/on hole
rx = ((16 - (x+1))^2 + (11 - y)^2)^0.5;% boundary in/on hole
% assigning new Temp
if y == 1 % along top boundary
T(y,x) = 40;
elseif r <= 5 % in/on hole
T(y,x) = 0;
elseif ry < 5 % boundary along hole in y direction
alpha = 11 - (25 - (16 - x)^2)^.5 - y; % find alpha
T(y,x) = (1/((2/alpha)+2))*(((2*T(y+1,x))/(alpha*...
(1+alpha)))+((2*T(y-1,x))/(1+alpha))+T(y,x+1)...
+T(y,x-1));
elseif rx < 5 % boundary along hole in x direction
alpha = 16 - (25 - (11 - y)^2)^.5 - x; % find alpha
T(y,x) = (1/((2/alpha)+2))*(((2*T(y,x+1))/(alpha*...
(1+alpha)))+((2*T(y,x-1))/(1+alpha))+T(y+1,x)...
+T(y-1,x));
elseif x == 1 % along left boundary, ghost node
T(y,x) = (1/4)*(T((y+1),x)+T((y-1),x)+2*T(y,(x+1)));
elseif x == Tx % right edge boundary, ghost node
T(y,x) = (1/4)*(T((y+1),x)+T((y-1),x)+2*T(y,(x-1)));
elseif y == Ty % bottom edge boundary, ghost node
T(y,x) = (1/4)*(2*T((y-1),x)+T(y,(x+1))+T(y,(x-1)));
else % general interior node
T(y,x) = (1/4)*(T((y+1),x)+T((y-1),x)+T(y,(x+1))+...
T(y,(x-1)));
end %ends if statement
%relaxation factoring
T(y,x) = lambda * (T(y,x)) + (1 - lambda)*T1;
% saving deltT value
deltT(y,x) = abs(T(x,y) - T1);
end %ends row
end
% checking deltT values
deltaT = max(max(T));
% tracking iteration
n = n + 1;
end
end
  1 Comment
Walter Roberson
Walter Roberson on 20 Mar 2014
Which line is triggering the error? What are is the size of the array being indexed? What are the values of all of the index variables in the enclosing loops?

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!