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

1 vue (au cours des 30 derniers jours)
Andres Pena
Andres Pena le 19 Sep 2019
Modifié(e) : Adam Danz le 23 Sep 2019
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

Réponses (2)

Adam Danz
Adam Danz le 19 Sep 2019
Modifié(e) : Adam Danz le 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 le 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);

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by