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

3 vues (au cours des 30 derniers jours)
% 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

Réponses (2)

Torsten
Torsten le 3 Avr 2022
Modifié(e) : Torsten le 3 Avr 2022
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 commentaires
Torsten
Torsten le 3 Avr 2022
Didn't you read in the MATLAB documentation that you must first apply meshgrid on the t and x vectors ?
Naveen Krish
Naveen Krish le 3 Avr 2022
How should i apply meshgrid? i'm not quite familiar with that. And the documentation i cant able to relate it to my problem

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 3 Avr 2022

Catégories

En savoir plus sur Encryption / Cryptography dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by