I am translating my MATLAB script into Julia but came across a BoundsError.
I do not know how to fix it, please give me a hand.
Here is the original code
t0 = 0; % Initial time
tend = 10; % End time
h = .05; % Time step size
t = t0:h:tend;
n_steps = length(t) - 1;
y0 = 1;
f = zeros(size(t));
y1 = [y0, f(1, end - 1)];
for ii = 1:n_steps
f(ii) = 3 + exp(-t(ii)) - 2 * y1(ii);
y1(ii + 1) = y1(ii) + f(ii) * h;
end
plot(t, y1, 'b-', 'linewidth', 1.5)
grid on
xlabel('\it t')
ylabel('\it y(t)')
The following is what I wrote
using Plots
t0 = 0;
tend = 10;
h = .05;
t = t0:h:tend;
n_steps = length(t) - 1;
y0 = 1;
f = zeros(Float64, size(t));
y1 = [y0, f[1, end - 1]]; # here is where BoundsError occurs
for ii = 1:n_steps
f[ii] = 3 + exp(-t[ii]) - 2 * y1[ii]
y1[ii + 1] = y1[ii] + f[ii] * h
end
plot(t, y1, line = (:path, 3), lw = 1.5)