MATLAB: Newton Raphson loop for backward Euler

backward eulernewton raphsonode

Dear all,
I would like to use Newton-Raphson method with backward Euler to meet a specific tolerance. How to change the loop below to get it?
% time step
h = (t_final - t_init)/n; % with n number of time steps
% vectors
t = [tinit zeros(1,n)]; % time
y = [yinit zeros(1,n)]; % solution
% Backward Euler loop
for i = 1:n
t(i+1) = t(i) + h;
y_temp = y(i) + h(f(t(i), y(i)));
y(i+1) = y(i) + hf(t(i+1), y_temp);
end
I would do something like:
for i = 1:n
error = 1;
tolerance = 1e-6;
t(i+1) = t(i) + h;
y_temp = y(i) + h(f(t(i), y(i)));
while error >= tolerance
y(i+1) = y(i) + hf(t(i+1), y_temp);
error = abs(y(i+1) - y_temp) % (local) absolute error
y_temp = y(i+1);
end
end
Is this the correct approach? Could someone give an hint? Thanks in advance.

Best Answer

You use a simple fixed-point iteration to get y(i+1), no Newton-Raphson.
Best wishes
Torsten.