how to solve Index exceeds the number of array elements (1).

1 view (last 30 days)
Im writing a code base on ADAM BASFORTH 2 order to resolved a heat trasnfer problem with boundaries and I got the error message typed in the title... can someone help me out please.
There is the code...(line 27)
close all;
clc;
clear;
a=0.02;
ta=20;
dt=4;
n=5;
% initial condition
u_analytical(1)=0;
u_numerical(1)=0;
f(1)= -(a*ta) * u_numerical(1)
% for the first step, let's use analytical solution
i=2;
t(i)=(i-1)*dt;
u_analytical(i)= 7.1842 * exp(0.14*(i)) + 2.81578 * exp(0.14*(i));
u_numerical(i)=u_analytical(i);
f(1)= -(a*ta) * u_numerical(1);
% the main loop
for i=3:n
t(i)=(i-1)*dt;
u_analytical(i)= 7.1842 * exp(0.14*(i)) + 2.81578 * exp(0.14*(i));
ff=1.5*f(i-1)-0.5*f(i-2);
u_numerical(i)=u_numerical(i-1) + ff * dt;
f(1)= -(a*ta) * u_numerical(1);
plot(t,u_analytical,'r-',t,u_numerical,'b+')
axis([0 60 0 60 ])
pause(0.05);
end

Answers (2)

Adam Danz
Adam Danz on 19 Sep 2019
Edited: Adam Danz on 23 Sep 2019
'f' has a single value (0). In the for-loop you attempt the get the 2nd value here:
% i = 3
ff=1.5*f(i-1)-0.5*f(i-2);
% ^^^.....(3-2=1)
'f' will never be a vector given your current code and it will always have just 1 value so you can't index it.

Fabio Freschi
Fabio Freschi on 19 Sep 2019
When i = 2 and inside the loop you have
f(1)= -(a*ta) * u_numerical(1);
I guess it should be
f(i)= -(a*ta) * u_numerical(1);
or
f(i)= -(a*ta) * u_numerical(i);

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!