Index exceeds matrix dimensions
1 view (last 30 days)
Show older comments
Hi everyone,
I am trying to model a problem for FTCS and I am using this code:
% --- Define constants and initial condition
L =1; % length of domain in x direction
tmax= 0.8; % end time
nx =10; % number of nodes in x direction
nt =10; % number of time steps
dx = L/(nx-1);
dt = tmax/(nt-1);
r = 0.5; r2 = 1 - 2*r;
% --- Loop over time steps
t = 0;
u =100; % initial condition
for m=1:nt
uold = u; % prepare for next step
t = t + dt;
for i=2:nx-1
u(i) = r*uold(i-1) + r2*uold(i) + r*uold(i+1);
end
end
But I couldn't figure out whats the mistake here, I am new to this so anyhelp is much appreciated.
0 Comments
Answers (3)
Reshma Nerella
on 10 Nov 2020
Hi,
In the code, you are assigning a value to variable 'u'
u =100 % 1x1 Double
Implies you can only index till 1(since we have only one element).
for i=2:nx-1
u(i) = r*uold(i-1) + r2*uold(i) + r*uold(i+1); %You are indexing 'u' from 2, beyond dimensions.
end
If you want to find 'u' matrix, consider preallocating it for speed and assign required values to it.
u = zeros(1,nx-1); %creates a matrix of size 1 x (nx-1) with zeros
For the initial condition, if you want first element of 'u' to be 100, You can assign it this way.
u(1) = 100;
You may get the issue with 'uold'
uold = u; % assigning a single value
for i=2:nx-1
u(i) = r*uold(i-1) + r2*uold(i) + r*uold(i+1); %Indexing it beyond dimensions
end
Hope this helps!
0 Comments
VBBV
on 9 Dec 2021
Edited: VBBV
on 9 Dec 2021
n = 10;
Lx = 1;
dx = Lx/(n-1);
x = 0:dx:Lx;
% 2. Parameters for the t vector
m = 80;
tf = 0.8;
dt = tf/(m-1);
t = 0:dt:tf;
% 3. Other parameters needed for the solution
% The value of alpha
Fo = .5; % a mutiplicative constant that should be < = 1/2
% to insure stability
% Initial and boundary conditions
f = 100;
g1 = 0;
g2 = 0;
u = zeros(n,m);
% u(2:n-1,1) = f; % Put in the initial condition starting from
% 2 to n-1 since f(0) = 0 and f(N) = 1
u(1,:) = g1; % The boundary conditions, g1 and g2 at
u(n,:) = g2; % x = 0 and x = 1
u(2:n-1,:) = f;
% Implementation of the explicit method
for k = 2:m-1 % Time Loop
for i= 2:n-1 % Space Loop
% u(i,k+1) = Fo*(u(i-1,k)+u(i+1,k))+(1-2*Fo)*u(i,k);
u(i,k+1) = (Fo*(u(i-1,k)+u(i+1,k))+2*u(i,k))/4;
end
end
figure
hold all
for i=1:2:numel(t)
plot(x,u(:,i),'linewidth',1.5,'DisplayName',sprintf('t = %1.2f',t(i)));
hold on
%fprintf(x,u(:,i),'linewidth',1.5,'DisplayName',sprintf('t = %1.2f',t(i)));
end
a = ylabel('Heat');
set(a,'Fontsize',10);
a = xlabel('x');
set(a,'Fontsize',10);
a=title(['Using The Explicit Method - Fo =' num2str(Fo)]);
set(a,'Fontsize',10);
legend('-DynamicLegend','location','bestoutside');
grid;
figure
[X, T] = meshgrid(x,t);
s2 = contourf(x,t,u.',0:5:100);
colorbar
shading interp
axis([0 1 0 0.12])
title(['3-D plot of the 1D Heat Equation using the Explicit Method - Fo =' num2str(Fo)])
xlabel('Bar length')
ylabel('Time step')
You need to change the boundary conditions to get the right solution.
Also your finite difference scheme application seems to be incorrect
0 Comments
See Also
Categories
Find more on Fourier Analysis and Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!