-2
$\begingroup$

So I'm following this algorithm enter image description here 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 i = 1:n-1
   g = @(z) z - y(i) - h*f(t(i+1),z);
   gp = @(z) 1 - h*f_y(t(i+1),z) ;
   y(i+1) = newton(f,f_y,y(i),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.

$\endgroup$
3
  • 3
    $\begingroup$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. $\endgroup$
    – Community Bot
    May 11 at 16:36
  • $\begingroup$ In the Euler method, you might want to pass the just constructed g functions to the Newton solver. $\endgroup$ May 11 at 22:05
  • 3
    $\begingroup$ What have you already tried? Have you tried to run your code with a linear function $f(x,t)$? $\endgroup$ May 12 at 2:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.