# Index in position 2 exceeds array bounds (must not exceed 1)

26 views (last 30 days)
Aditya Sonwane on 30 Sep 2019
Commented: Aditya Sonwane on 1 Oct 2019
I am solving a I D transient heat conduction equation (given below) and I keep getting this error, I am unable to resolve it.
ANY HELP IS APPRECIATED
u_c = zeros(n+1,1);
u_p = zeros(n+1,1);
for i = 1:n+1
u_c(i+1,1) = U_i;
end
for m = 2:T+1 % Time Loop
for i = 2:n % Space Loop
u_c(m,i+1) = u_p(m,i+1) + 0.5*(u_p(m+1,i+1)-(2*u_p(m,i+1))+u_p(m-1,i+1)) ;
end
u_p = u_c;
u_c(1,i) = ((k*u_p(1,i)) + (dx*h*U_f))/(k+(dx*h));
u_c(n,i) = ((k-(dx*h))*u_p(n-1,i))+(U_f*dx)/k;
end

meghannmarie on 30 Sep 2019
Edited: meghannmarie on 30 Sep 2019
The variables u_c and u_p are vectors or the size of the second dimension is 1. Then you try to set these variables with i in the second dimension which is more than 1. Maybe first 2 lines should be this:
u_c = zeros(n+1,T+2);
u_p = zeros(n+1,T+2);
Hard to tell without the data.

#### 1 Comment

Aditya Sonwane on 1 Oct 2019
I understand, I have made some changes in the matrix formation of u_c and u_p and am providing ,my overall code please take a look and let me know.
%% MAIN FUNCTION %%
clc
clear all
%thickness of strip = L = 120mm = 0.12m
%u_p = previous temperature
%u_c = current temperature
%total time = T in sec
%timesteps = ts
U_i = 673; %initial medium temperature
U_f = 298; %quenching medium temperature
h = 3000; %in W/m^2-K
rho = 2.7889*10^3; %in kg/m^3
Cp = 0.7639*10^3; %in J/kg-K
k = 0.2473*10^3; %in W/m-K
lamda = k/(rho*Cp); %= (0.2473*10^3)/(2.7889*10^3*0.7639*10^3) = 0.1161*10^-3
T = 200;
L = 0.12;
n = 50;
dx = L/n;
%dt = (0.5/lamda)*dx^2;
ts = 500;
dt = T/ts;
%%%%%%%%%% END OF INPUT %%%%%%%%%%
u_p = zeros(1,n+1);%n+1 is for space and ts is for space
u_c = zeros(1,n+1);
w = solver(i, n, T, dx, h, k, U_f, U_i);
%plot(ts, w')
clc
clear all
%thickness of strip = L = 120mm = 0.12m
%u_p = previous temperature
%u_c = current temperature
%total time = T in sec
%timesteps = ts
U_i = 673; %initial medium temperature
U_f = 298; %quenching medium temperature
h = 3000; %in W/m^2-K
rho = 2.7889*10^3; %in kg/m^3
Cp = 0.7639*10^3; %in J/kg-K
k = 0.2473*10^3; %in W/m-K
lamda = k/(rho*Cp); %= (0.2473*10^3)/(2.7889*10^3*0.7639*10^3) = 0.1161*10^-3
T = 200;
L = 0.12;
n = 50;
dx = L/n;
%dt = (0.5/lamda)*dx^2;
ts = 500;
dt = T/ts;
%%%%%%%%%% END OF INPUT %%%%%%%%%%
u_p = zeros(1,n+1);%n+1 is for space and ts is for space
u_c = zeros(1,n+1);
w = solver(i, n, T, dx, h, k, U_f, U_i);
%plot(ts, w')
%% SOLVER %%
function u_c = solver(i, n, T, dx, h, k, U_f, U_i)
u_c = zeros(1,n+1);
u_p = zeros(1,n+1);
for i = 1:n+1
u_c(i+1,1) = U_i;
end
for ts = 2:T+1 % Time Loop
for i = 2:n % Space Loop
u_c(i+1) = u_p(i+1) + 0.5*(u_p(i+1)-(2*u_p(i+1))+u_p(i+1)) ;
end
u_p = u_c;
u_c(i) = ((k*u_p(i)) + (dx*h*U_f))/(k+(dx*h));
u_c(i) = ((k-(dx*h))*u_p(i))+(U_f*dx)/k;
end
end
%% PLOTTEMP %%
function plottemp(ts, u_c)
plot(ts,u_c)
title('Temperature Distribution in the aluminum strip')
xlabel('length of strip')
ylabel('temp')
end
END