Problem: solve stiff different equation
Method: implicit Euler
Plan: I calculate next 'y' by solvin non-linear equation use secant mehod. My function is dy/dx = sin(x+y)
There is right solution . I used newton method
main.m
h=0.01;
x(1)=0;
y_expl(1)=0;
y_impl(1)=0+h;
dy(1)=0;
eps=1.0e-6;
for i=1:1000
x(i+1)=x(i)+h;
y_impl(i+1)=newton(x(i),y_impl(i),y_impl(i));
y_expl(i+1)=y_expl(i)+h*f(x(i),y_expl(i));
end
plot(x,y_impl,'r',x,y_expl,'b')
legend('Implicit Euler','Explicit Euler');
newton.m
function [ yn ] = newton( x,y,yi )
eps=1.0e-6;
err=1;
step=0;
step_max=100;
h=0.01;
xn=x+h;
while (err > eps) && (step < step_max)
step=step+1;
yn=y-(F(xn,y,yi,h))/(J(xn,y,h));
err=abs(y-yn)/(abs(yn)+1.0e-10);
y=yn;
end
end
f.m
function [ res ] = f( x,y )
res = sin(x+y);
end
G.m
function [ res ] = J( xn,y,h )
res = h*f(xn,y)-1;
end
F.m
function [ res ] = F( a,y,yn,h )
res = h*f(a,y)-y+yn;
end
Thank for attention