Method Error? objects of type Vector{Float64} are not callable Use square brackets [] for indexing an Array

I am writing a function for the Runge-Kutta method (Rk4) and when I try to put values into the equation I get a method error.

function explicit_rk4(f::Function, yo::Vector, t_final::Real, N::Int)
t=range(0, t_final, N+1)
y=zeros(length(yo),N+1)
h=t[2]-t[1]
y[:,1]=yo
for k = 1:N

k₁=f(y[:,k],t[k])

k₂=f(y[:,k] +k₁*(h/2), t[k] + h/2)

k₃=f(y[:,k] + k₂(h/2),t[k] + h/2)

k₄=f(y[:,k]+k₃*h,t[k]+h)

y[:,k+1]= y[:,k]+(k₁ + 2*k₂ + 2*k₃ + k₄)*(h/6)
end 

return t,y

end

for k=1:i
exp_rk4_t, exp_rk4_y =explicit_rk4( (y,t)->[y[2];-y[1]],[1;0],4*pi,i)
end

MethodError: objects of type Vector{Float64} are not callable
Use square brackets for indexing an Array.

If anyone knows whats up please let me know?

Have you considered changing k₂(h/2) to k₂*(h/2)?

1 Like