Index in position 2
Afficher commentaires plus anciens
I am trying to create a Dufort Frankel Finite Difference Scheme. I keep getting an error that says "Index in position 2 is invalid. Array indices must be positive integers or logical values." I think I understand that Matlab must start at 1 and that my tdx-1 messes this up. I have tried several things to change this but I am having trouble getting it to work. Could someone inform me of how to correct this issue?
%%%%% dT/dt = d^2T/dx^2 - partial derivatives
%%%% T = temperature = f(x,t)
%%%%% time derivative = second spatial derivative
%%%%(T(x,t+dt) - T(x,t))/dt = (T(x+dx,t) - 2*T(x,t) + T(x-1,t))/ dx^2
%%% Propagate this system in time
%%%% Solve for T(x,t+dt) =T(x,t) + k*dt/dx^2 * (T(x+dx,t) - 2*T(x,t) +
%%%% T(x-1,t))
%%%% length of the pipe
k = 2;
L = 10;
N = 10;
x_vec = linspace(0,L,N);
dx = x_vec(2)-x_vec(1);
dt = 0.5*(dx^2)/(2*k);
t_vec = 0:dt:10;
T_mat = zeros(length(x_vec),length(t_vec));
T_mat(1,:) = 200;
T_mat(end,:) = 150;
for tdx = 1;length(t_vec)-1;
for idx = 2:length(x_vec)-1
T_mat(idx,tdx+1) = T_mat(idx,tdx-1) + 2*k*dt/dx^2*( T_mat(idx+1,tdx) - (T_mat(idx,tdx+1)+T_mat(idx,tdx-1)) + (T_mat(idx-1,tdx)));
end
end
[tt,xx] = meshgrid(t_vec,x_vec);
mesh(xx,tt,T_mat)
xlabel('X coordinate (m)')
ylabel('Time (sec)')
zlabel('Temperature (F)')
Réponse acceptée
Plus de réponses (1)
madhan ravi
le 11 Mar 2019
for tdx = 2:length(t_vec)-1
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
