% y'' - y = eps.ty  ..... convert to 1st-order system y1'=y2, y2'=y1(1+eps.t) 

eps = 0.04;
f = @(t,y) [y(2); y(1).*(1+eps*t)];     % construct a vector func to rep ODE
y0 = [1; -1];
tmax = 5;
[t,y] = ode45(f, [0, tmax], y0);
figure; plot(t, y(:,1), 'k-'); xlabel t; ylabel y(t);  % check your solution

y0 = exp(-t);                                        % zeroth-order
y1 = -(1+t).*t.*exp(-t) / 4  + (exp(t)-exp(-t))/8;   % first-order
ya = y0 + eps*y1;                                    % 2-term approximation
E = ya - y(:,1);                                     % error vs time

figure; subplot(2,1,1); % allows you to put multiple plots on one figure
plot(t, [y(:,1) y0 eps*y1 ya], '-');  % plot the 4 curves asked for
xlabel t; ylabel y(t); legend('y', 'y_0', 'y_1', 'y_a');
subplot(2,1,2); plot(t, E, '-'); xlabel t; ylabel('error E(t)');
T=3; v = max(abs(E(find(t<T)))); axis([0 T -v v]);   % trick: zoom in correctly
print -depsc2 p100_ex2.eps                           % make a printable file

