
%% Symbolic Solution and Euler Approximations

%% Symbolic Solution

  sol1 = dsolve ('Dy = cos(x)-y', 'y(0)=-0.4', 'x')
     y= inline(vectorize(sol1),'x');
     x = 0:0.02:6;
     P=    plot(x,y(x));

xlabel 'x'
ylabel 'y'
title ' Symbolic Solution '
print -dpng figuresymbolic

%% Euler method with 4 steps

f= inline('-y+ cos(x)','x','y')
     [x1,y1] = myeuler(f,0,-.4,6,4);
     [x1,y1];
P1 = plot (x1,y1, x1,y1,'+');
axis ([0 6 -1.5 2])
xlabel 'x'
ylabel 'y'
title ' Euler Method with 4 steps '
print -dpng figure1

%% Euler method with 12 steps

f= inline('-y+ cos(x)','x','y')
     [x2,y2] = myeuler(f,0,-.4,6,12);
     [x2,y2];
P2 = plot (x2,y2, x2,y2,'*');

axis ([0 6 -1 1.5])
xlabel 'x'
ylabel 'y'
title ' Euler Method with 12 steps '
print -dpng figure2
%% All 3 solutions
plot (x1,y1, x1,y1,'+', x2,y2, x2,y2,'*', x,y(x));
legend('euler4 ', ' ',  'euler12', ' ',  'symbolic')
xlabel 'x'
ylabel 'y'
title ' Symbolic Solution and two Euler Approximations '
print -dpng figure3

