I would like to implement Runge Kutta Gauss method. This method is implicit and need a nonlinear solver to do so. For that I define a fixed point solver FixIter
, then the function Gauss(f::Function,u₀,h,N)
using LinearAlgebra
function FixIter(fun,u,tol)
while true
uold = u
u = fun(u)
norm(uold - u) > tol || break
end
return u
end
function Gauss(f::Function,u₀,h,N)
A = [0.25 0.25-√3/6 ; 0.25+√3/6 0.25]
b = [0.5, 0.5] ;
u = zeros(length(u₀),N+1)
u[:,1] = u₀;
for i in 1:size(u,2)-1;
g(K) = [f(u[:,i]+ h * (A[1,1] * K[1] + A[1,2] * K[2])),f(u[:,i]+ h * (A[2,1] * K[1] + A[2,2] * K[2]))]
K = FixIter(g,f(u[:,i]),1e-12);
u[:,i+1] = u[:,i] + h * b[1] * K[1] + h * b[2] * K[2];
end
return u;
end
My example is a pendulum problem with the vector field X = (y, \sin(x))
f(u) = [u[2], -sin(u[1])];
h=0.05; N=100;
x0=[1.,0.]
u = Gauss(f,x0,h,N)
However I get the following error
I appreciate any help you can provide.