%%% NOTE: you have to download dirfield.m from the course web page !!!

%%%%%%%%  Problem 1 %%%%%%%%%%%%%

%%%%%%%% (a) exact equation

sol = dsolve('(2*y+4*t)*Dy + 4*y+16*t = 0', 'y(0) = sqrt(2)', 't')

% Note: Matlab is not clever enough to pick the correct + or - sign.
%       Only the solution with "+" is correct, the other one
%       does not satisfy the initial condition:
subs(sol,'t',sym(0))
s = sym('-2*t+(-4*t^2+2)^(1/2)')      % this is the correct solution

%% plot direction field, initial point, solution:

f = inline('-(4*y + 16*t)./(2*y + 4*t)','t','y');     % need ./  !!!
dirfield(f, -1:.1:1, -2:.2:2);  hold on
plot(0,sqrt(2),'ko')                                  % show initial condition
ezplot(s,[-1 1]);   hold off; 

disp('hit any key'); pause

%%%%%%%% (b) linear equation

sol = dsolve('t*Dy - 2*y - 1/t = 0', 'y(1)=-2', 't')

%% plot direction field, initial point, solution:

f = inline('(2*y + 1./t)./t', 't', 'y');             % need ./  !!!
dirfield(f, 0:.05:2, -10:.3:0);  hold on
plot(1,-2,'ko')                                      % show initial condition
ezplot(sol,[0,2]);               hold off

disp('hit any key'); pause

%%%%%%%% (c) separable equation

sol = dsolve('1/sin(t)*Dy - exp(y) = 0', 'y(0)=0', 't')

f = inline('sin(t).*exp(y)','t','y');                % need .*  !!!
dirfield(f, -2:.2:2, -1:.2:3);  hold on
plot(0,0,'ko')                                       % show initial condition
ezplot(sol,[-2,2]);             hold off

disp('hit any key'); pause

%%%%%%%%  Problem 2 %%%%%%%%%%%%%

sol = dsolve('Dy = 6 - y/50','y(0)=400','t')

f = inline('6 - y/50','t','y');                 % need 't','y' even if f does not depend on t

dirfield(f,-200:20:200,0:50:1000); hold on
plot(0,400,'ko')                                % show initial condition
ezplot(sol,[-200,200]);            hold off
axis([-200 200 0 1000])



