% Ylesanded 17, ylesanne 4

f=@(x,y) 2*x*y;

% leiame lahendi v22rtuse, kui x=1

x0=0;
xn=1;
y0=2;
h=0.25;
n=(xn-x0)/h;
x(1)=0;
y(1)=2;

% a) Euleri meetodiga

for i=2:(n+1)
   x(i)=x(i-1)+h;
   y(i)=y(i-1)+h*f(x(i-1),y(i-1));
end
x
y

yeuler=y(n+1)
hold on
plot(x,y,'--')

% a) Heuni meetodiga

for i=2:(n+1)   
   y(i)=y(i-1)+h/2*(f(x(i-1),y(i-1))+f(x(i),y(i-1)+h*f(x(i-1),y(i-1))));
end
x
y
yheun=y(n+1)

hold on
plot(x,y,'*')

% a) keskpunkti meetodiga

y1k=y0+h*f(x0+h/2,y0+h/2*f(x0,y0))

for i=2:(n+1) 
   y(i)=y(i-1)+h*f(x(i-1)+h/2,y(i-1)+h/2*f(x(i-1),y(i-1)));
end
x
y
ykeskp=y(n+1)

hold on
plot(x,y,'diamond')