Using Implicit Euler Method with Newton-Raphson method
古いコメントを表示
So I'm following this algorithm to write a code on implicit euler method

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 k = 1:n-1
g = @(z) z - y(k) - h*f(t(k+1),z);
gp = @(z) 1 - h*f_y(t(k+1),z) ;
y(k+1) = newton(f,f_y,y(k),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.
7 件のコメント
Torsten
2022 年 5 月 11 日
You forgot to include the call to imp_euler.
Eugene Miller
2022 年 5 月 11 日
Torsten
2022 年 5 月 11 日
I mean: The code you provided doesn't work without the knowledge of f, fy, t0,T,y0,h,tol and N (I think n).
Eugene Miller
2022 年 5 月 11 日
Torsten
2022 年 5 月 11 日
How did you come the conclusion that the code does not work if you didn't test it ? Because for testing, inputs of the mentionned variables had to be provided.
Eugene Miller
2022 年 5 月 11 日
Eugene Miller
2022 年 5 月 11 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!