Must be a positive integer or logical
Show older comments
Im having trouble with this code I need to the matrix to go up in steps of either 0.0052 for the time or to complete the loop t<t_2 in steps of 0.0052 or 1:(t_2/del_t).
%Aluminium Material Properties%
clear all
close all
clc
rho = 2700 ; %kg/m^3 Density
k = 170 ; %
cp = 910 ; %
alpha = k/(rho*cp); %
L_d = 0.0047; %m Diameter
L_x = 0.047; %m Length in x direction
L_r = L_d/2; %m Radius
n_r = 2; % Nodes in r direction
n_x = 40; % Nodes in x direction
del_x = L_x/(n_x-1); %m Spacing of Nodes
T_h = 27; %C Temperature of Hot Side
T_i = 19; %C Initial Temperature
t_2 = 180; %s Simulation Time
t(1) = 0; %s Initial Time
Fo_1 = 0.25; % Fourier Number Stability 0.25 For 3D
del_t = (Fo_1*del_x^2)/alpha; % Calculate time step using Fourier Number for Stability
h = 5;
%Set Inital Matrices Temp
T(1:n_r+2,1:n_x+2,1) = T_i; %Sets matrix dimensions and sets it to 19 C
Fo = alpha*del_t/(del_x.^2) ; % Fourier Number
Bi = h*del_x/k ; % Biot Number
if Fo > 0.25
fprintf('Fo Number to big, code unstable \n \n') %Check if Fo is acceptable
elseif Fo*(1-Bi) > 0.25
fprintf('Bi Number to big, code unstable') %Check if Bi is acceptable
end
k = 1;
while t<t_2 % Time Loop
for r=1:n_r+2 % Space Loop r direction
for x=1:n_x+2 % Space Loop x direction
if x==1 || x==42 || r==1 || r==4 % Sets Boundary Conditions for Room Temp
T(r,x,k+1) = 19;
elseif x==2 % Boundary Condition First Node = 27 Celius % Boundary Condition First Node = 27 Celius
T(r,x,k+1) = 27;
elseif x>2 || x==n_x % Middle to End of Material
T(r,x,k+1) = alpha*del_t*(((T(r,x+1,k)-(2*T(r,x,k))+T(r,x-1,k))/L_x^2)+((T(r+1,x,k)-(2*T(r,x,k))+T(r-1,x,k))/L_r^2))+T(r,x,k); %Using finite difference method
end
end
end
t(k) = k+del_t; %Sets time
k = k+del_t;
end
Temp(1:n_r,1:n_x) = T(1:n_r,1:n_x,k);
This is the error that I am receiving
??? Attempted to access T(1,1,2.00525); index must be a positive integer or logical.
Error in ==> Transient2D at 37
T(r,x,k+1) = 19;
3 Comments
per isakson
on 4 Apr 2014
Please do not dumb such a lot of poorly formatted and irrelevant code here.
Luke Cartwright
on 4 Apr 2014
per isakson
on 4 Apr 2014
Edited: per isakson
on 4 Apr 2014
everything before k = 1; for a start.
If you want us to run the code say so and provide the code as an attachment.
Answers (1)
per isakson
on 4 Apr 2014
Edited: per isakson
on 4 Apr 2014
0 votes
Use the debug support. It's good. Here are some links on debugging in Matlab
Find out why k is not a whole number.
k = k+del_t; is the prime suspect.
2 Comments
Luke Cartwright
on 4 Apr 2014
per isakson
on 4 Apr 2014
Edited: per isakson
on 4 Apr 2014
Maybe it is not appropriate to use k as an index. You must make sure k is a whole number.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!