Index exceeds the number of array elements. Index must not exceed 1
2 views (last 30 days)
Show older comments
x_exact = @(t) 1./3.*2.*exp(-3.*t).*(2.*exp(6.*t) + 1) + 1./3.*2.*exp(-3.*t).*(exp(6.*t) - 1) % x exact solution
y_exact = @(t) 2./3.*2.*exp(-3.*t).*(exp(6.*t) - 1) + 1./3.*2.*exp(-3.*t).*(exp(6.*t) + 2) % y exact solution
h = 0.1 % step
t0 = 0 % first time value
ts = 4 % last time value
t=t0:h:ts
for i = 1:length(t)
if (t(i) == t0)
x_ex(i) = x_exact(t(i))
x_iEm(i) = x_exact(t(i))
else
x_ex(i) = x_exact(t(i))
x_past = x_iEm(i-1)
y = y_exact(t(i))
x_iEm(i) - h*x_iEm(i) == x_past + h*2*y % error occur in this line code
end
end
hi guy , so i'm trying to solve the ODE systems , and calculate x_ex ( exactly solution ) and x_iEm ( implicit Euler method) , and i get the error " Index exceeds the number of array elements. Index must not exceed 1 " , does anyone know the way to fix it ???
it seem like the error occurs when i = 2 -> t(i) = 0.1 , in the line code i comment
0 Comments
Accepted Answer
Alan Stevens
on 29 Nov 2022
What are yoy trying to do with
x_iEm(i) - h*x_iEm(i) == x_past + h*2*y;
when i = 2, there is, as yet, no value assigned to x_iEm(i), so you sem to be testing for logical equality with values on the left that don't exist.
2 Comments
Alan Stevens
on 29 Nov 2022
The == test for equality. To assign something to a variable just use =.
If you intended to obtain x_iEm(i) from x_iEm(i) - h*x_iEm(i) == x_past + h*2*y;then you need
x_iEm(i) = (x_past + h*2*y)/(1-h);
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!