Index in position 1 is invalid. Array indices must be positive integers or logical values. Error in explicitftcs (line 28) u(Nx+1,k) = 1.; >> u(Nx+1,k) = 1.; Index in

1 次查看(过去 30 天)
% Heat diffusion in one dimensional wire within the explicit FTCS method
clear;
% Parameters to define the heat equation and the range in space and time
D = 1*10^-4; % Heat Conductivity parameter in m2/s
K = 1*10^-4; % Heat Conductivity parameter in s^-1
L = 5.; % Total length in m
T = 43200.; % Final Time
% Parameters needed to solve the equation within the explicit method
Nt = 7200; % Number of time steps
Dt = T/Nt; % Time step
Nx = 0.1; % Number of space steps in m
Dx = L/Nx; % Space step
b = 1-(2*D*Dt/Dx^2)- K*Dt; % beta parameter in the finite- difference implementation
% Remember that the FTCS method is stable for b=<1
disp(b);
% The initial condition: the initial temperature of the pipe
for i = 1:Nx+1
x(i) = (i-1)*Dx; % we also define vector x, due to the space discretization
u(i,1) = sin (pi*x(i));
end
% Boundary conditions: the temperature of the pipe at the boundaries at any time
for k =1:Nt+1
u(1,k) = 1.;
u(Nx+1,k) = 1.;
t(k) = (k-1)*Dt; % we also define vector t, due to the time discretization
end
% Implementation of the explicit FTCS method
for k =1:Nt % Time Loop
for i=2:Nx; % Space Loop
u(i,k+1) =u(i,k) + 0.5*b*(u(i-1,k)-2.*u(i,k)); % Implementation of the FTCS Method
end
end
%Graphical representation of the temperature at different selected times
Figure(1)
plot(x,u(:,1),'-b',x,u(:,round(Nt/100)),'--g',x,u(:,round(Nt/10)),':b',x,u(:,Nt),'-.r')
Title('Temperature of the pipe within the explicit FTCS method')
xlabel('X')
ylabel('T')
figure(2)
mesh(t,x,u)
title('temperature of the pipe within the explicit FTCS method')
xlabel('t')
ylabel('x')
I need to plot the profile C vs x at t=[2,4,6,8,10,12] hours

回答(2 个)

Torsten
Torsten 2022-4-3
编辑:Torsten 2022-4-3
Nx should be the number of grid points in x-direction, not the length of these intervals.
You mixed this up by setting Nx = 0.1 instead of 51.
Further, Dx should be L/(Nx-1), not L/Nx.
This update for u is wrong:
u(i,k+1) =u(i,k) + 0.5*b*(u(i-1,k)-2.*u(i,k)); % Implementation of the FTCS Method
Look up the FTCS method again:
  7 个评论
Naveen Krish
Naveen Krish 2022-4-3
How should i apply meshgrid? i'm not quite familiar with that. And the documentation i cant able to relate it to my problem

请先登录,再进行评论。


Image Analyst
Image Analyst 2022-4-3

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by