Can't get Newton Rapshon implicit Euler method to work

I think it's because of the jacobian (jg) but I'm not sure how to fix it.
Here's the code:
function [t,y] = mieulernewtonexternoooo(f,jf,intv,y0,N,TOL,nmax)
h=(intv(2)-intv(1))/N;
t=intv(1);
t0=intv(1);
y=y0;
z=y0;
z1=y0;
for i=1:N
g=@(z)(z1+h*f(t0,z));
jg=h*jf;
z=minewton(g,jg,z,t0,TOL,nmax);
z1=z;
t0=t0+h;
y=[y,z];
t=[t,t0];
end
end
function [z] = minewton(f,jg,y1,t0,TOL,nmax)
diff=TOL*2;
i=0;
y=y1;
while (i<nmax) && (diff>TOL)
i=i+1;
z = y - jg \ f(y);
diff=norm(z-y);
y=z;
end
end
And example inputs:
intv=[0,10];
y0=[2;3];
f=@(t,y)[y(2)-2*y(1)+2*sin(t);y(1)-2*y(2)+2*(cos(t)-sin(t))];
sol=@(t)[2*exp(-t)+sin(t);2*exp(-t)+cos(t)];
N=1000;
nmax=100;
TOL=10^-8;
jf=[-2,1;1,-2];

 Akzeptierte Antwort

Torsten
Torsten am 9 Apr. 2022
g should be
g =@(z) z-z1-h*f(t0+h,z)
with Jacobian
jg = eye(2)-h*jf

Weitere Antworten (0)

Produkte

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by