#!/usr/bin/octave
#Diffegyenlet megoldasa Euler-modszerrel


##Masodrendu diffegyenlet visszavezetese elsorendure: x'=v,v'=-x
function osc=osc(xx,tt)
    osc=[xx(2),-xx(1)]';
end

function euler=euler(t1,t2,x0,n,f)
    dt=(t2-t1)/n;
    t=t1+dt*[0:n];
    [neq,qq]=size(x0);
    x=zeros(neq,n+1);
    x(:,1)=x0;
    for i=1:n
	x(:,i+1)=x(:,i)+f(x(:,i),t(i))*dt;
    end
    euler=[t',x']';
end



y=euler(0,2*pi,[0,1]',500,@osc);
t=y(1,:);
x=y(2,:);
v=y(3,:);

xlabel("t")
hold on
plot(t,x,"1;x;")
hold on
plot(t,v,"2;v;")
print("osc.jpg","-djpg")


