%
%  Math 340 * Victor Matveev * April 20, 2012
%  See solution to homework #10 for details
%
%  dY
%  -- = cos(Y)
%  dx
%

y0 = 0.1;
subplot(122);
hold on;
set(gca,'fontsize',12);

f = @(t,y) cos(y(1));
[T, Y] = ode45(f, [0 3.5], [y0]);       % Find numerical solution

plot(T, Y(:,1), 'r-', 'linewidth', 2);  % Plot numerical solution

x = linspace(0,pi,11); 
y = linspace(-pi,pi,12);
[X, Y] = meshgrid(x,y);
F = ones(size(X));
G = cos(Y);
quiver(X, Y, F, G);   % Plot the direction field using "quiver"
axis tight;

legend('Solution', 'Direction field');
xlabel('x'); ylabel('Y(x)');
title('dY/dx = cos(Y);  Y(0)=0.1');

subplot(121);
hold on;
set(gca,'fontsize',12);
xx = linspace(0,pi,80);
plot(xx, cos(xx), 'k-', 'linewidth', 2);
plot([0 pi], [0 0], 'k--');
plot(y0, 0, 'ok', 'markerfacecolor', [0 0 0], 'markersize', 6);
xlabel('Y'); ylabel('dY/dx');
title('Rate of change of Y vs. Y');
axis tight;


