MATLAB: Error – index must be a positive integer or logical.

indexintegerlogical?

I'm working on numerically solving a second order differential equation in Matlab and I have an issue with my main loop.
clear all; close all; clc;
%% Exact Solution of Linearized Dimensionless Temperature
% Let k(T) = kref
% d2T/dn2 – m^2 T = 0
m = [0, sqrt(.1), sqrt(1), sqrt(10), sqrt(100)];
n = linspace(0,1,1000);
for i = 1:5 % Evaluate the exact temperature soln for each m value
T(:,i) = (exp(m(i).*n) + exp((2 - n).*m(i)))./(exp(2.*m(i)) + 1);
i = i + 1; % Each column represents a different m value
end
subplot(2,1,1)
plot(n,T)
xlabel('Dimensionless Position Along Fin')
ylabel('Dimensionless Temperature')
title('Dimensionless Steady State Temperature Distribution Along Fin') legend('m2 = 0','m2 = .1','m2 = 1','m2 = 10','m2 = 100')
hold on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Numerical Solution of Linearized System, Central Difference Method % Soln for dimensionless temperature
N = 10;
m2 = [0, .1, 1, 10, 100]; % Various values of m2
for i = 1:5 % Index through each m2 value
for j = 1:N
ET(i,j) = 1;
dn = 1/N; % Find dn
n = ((j.*dn).*1000);
R = 2 + m2(i).*dn^2; % Define the constant R
B = [-1; zeros(N-1,1)]; % Create the appropriate vector B
A = zeros(N,N); % Create the appropriate sized system of
Eqs
k = 1;
while k < N % Index through each row and column of matrix A
A(k,k) = -R; % Build the correct systems of equations
A(k+1,k) = 1;
A(k,k+1) = 1;
A(N,N-1) = 2;
k = k + 1;
end
A(N,N) = -R
T1(:,i) = A\B % Solve for temp distribution
ET(i,j) = ((T(n,i) - T1(j,i))./T(n,i))
end
if ET(i,N) > .001
N = N.*2;
% save T1 as new Ti based on final N
end
end
When I run this code (which isn't complete, I need to solve this problem before finishing that final if statement) I get the error: ??? Attempted to access T(300,1); index must be a positive integer or logical.
Error in ==> HTProject at 59 ET(i,j) = ((T(n,i) – T1(j,i))./T(n,i))
The matrix T has 1000 rows and 5 columns so T(300,1) obviously exists. I've even called T(300,1) in the command prompt after getting this error and it returns the correct value. I'm lost as to why this is happening so any help is greatly appreciated. Thanks.

Best Answer

try
n=fix(n);
ET(i,j) = ((T(n,i) - T1(j,i))./T(n,i))