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.

 채택된 답변

Torsten
Torsten 2015년 3월 20일

0 개 추천

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

댓글 수: 3

Clint_E
Clint_E 2015년 3월 20일
편집: Clint_E 2015년 3월 20일
Thanks a lot, so please how would you change it to get Newton-Raphson? Best wishes
yold = y(i)+h*f(t(i),y(i));
while error >= tolerance
ynew = yold-(yold-(y(i)+h*f(t(i+1),yold)))/(1-h*df(t(i+1),yold));
error = abs(ynew-yold);
yold=ynew;
end
y(i+1) = ynew;
df is the derivative of f with respect to y.
Best wishes
Torsten.
Clint_E
Clint_E 2015년 3월 20일
Thanks a lot! Best wishes

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

질문:

2015년 3월 20일

댓글:

2015년 3월 20일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by