function oppgave_6_1_5 k=0:5; % Exact solution: % (a) y_exact = @(t) -t-1+exp(t); % (b) %y_exact = @(t) t-1+exp(-t); H = []; E = []; for k=0:5, h = 0.1*2^(-k); n = round(1/h); % Euler's method t = 0; y=0; for i=1:n, y = eulerstep(t,y,h); t = t+h; end H = [H,h]; E = [E,abs(y-y_exact(1))]; end % plot the error vs h figure(1); clf; fs = 24; %font size loglog(H,E,'x-','LineWidth',3,'MarkerSize',10); xlabel('h','FontSize',fs); ylabel('Error','FontSize',fs); grid on; function y=eulerstep(t,y,h) %one step of Euler's Method %Input: current time t, current value y, stepsize h %Output: approximate solution value at time t+h y=y+h*ydot(t,y); function z=ydot(t,y) %right-hand side of differential equation z = t+y; % oppgave (a) %z = t-y; % oppgave (b) %z = 4*t-2*y; % oppgave (c)