# Using Implicit Euler Method with Newton-Raphson method

So I'm following this algorithm and here is my attempt:

function y = imp_euler(f,f_y,t0,T,y0,h,tol,N)
t = t0:h:T;
n = length(t);
y = zeros(n,1);
y(1) = y0;
for i = 1:n-1
g = @(z) z - y(i) - h*f(t(i+1),z);
gp = @(z) 1 - h*f_y(t(i+1),z) ;
y(i+1) = newton(f,f_y,y(i),tol,N);
end
end


where

function sol=newton(f,fp,x0,tol,N)
i=0;
sol = zeros(N,2);
fc=abs(f(x0));
while (fc>tol)
xc = x0 - (f(x0)/fp(x0));
fc=abs(f(xc));
x0 = xc;
i=i+1;
sol(i,:) = [i; x0];
if (i>N)
fprintf('Method failed after %d iterations. \n',N);
break
end
end
sol = sol(any(sol,2),:);
end


Unfortunately, my code does not work for some reason. Could anybody guide me on how to code this? Comments are appreciated.

• Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
– Community Bot
May 11 at 16:36
• In the Euler method, you might want to pass the just constructed g functions to the Newton solver. May 11 at 22:05
• What have you already tried? Have you tried to run your code with a linear function $f(x,t)$? May 12 at 2:32