%Gerardo

function SlopeField

%dv/dt = g-(gamma/m)*v
g=9.81;
m=1;
gamma=2;

Nt=50+1; Nv=50+1;
T_end=10; v_end=10;
dt=T_end/(Nt-1); dv=v_end/(Nv-1);

f=zeros(Nt,Nv,2);
t_axis=zeros(Nt,1);
v_axis=zeros(Nv,1);
for it=1:Nt
    t=dt*(it-1);
    t_axis(it,1)=t;
    for iv=1:Nv
        v=dv*(iv-1);
        v_axis(iv,1)=v;
        f(it,iv,1) = 1;
        f(it,iv,2) = g-(gamma/m)*v;
    end
end

clf
figure(1)
hold on
quiver(t_axis,v_axis,transpose(f(:,:,1)),transpose(f(:,:,2)),3)
axis([0 T_end 0 v_end])

%solution 1:
v0 = 20;
vsol=zeros(Nt,1);
for it=1:Nt
    vsol(it,1) = m*g/gamma-(m*g/gamma-v0)*exp(-gamma/m*t_axis(it,1));
end
plot(t_axis,vsol,'r -')

%solution 2:
v0 = -20;
vsol=zeros(Nt,1);
for it=1:Nt
    vsol(it,1) = m*g/gamma-(m*g/gamma-v0)*exp(-gamma/m*t_axis(it,1));
end
plot(t_axis,vsol,'r -')

