clear all;clc;
y=3.8;
t0=0;
h=5;
n=1;
R=20;
for C=0.5;
for t=t0:h:30
    f=-y./(R.*C);
    s(n)=y;
    y=y+h*f;
    n=n+1;
end
t=0:h:30;
plot(t,s,'-*');hold on
hold on
y=3.8; n=1;
end
% Vg=sin(2e3*pi*t);
% plot(t,Vg);
grid on
% The same solution with RK4
clear all;clc;
y=3.8;
t0=0;
h=5;
n=1;
R=20;
for C=0.5;
for t=t0:h:30
    
    %f=-y./(R.*C);
    K1=-y./(R.*C);
    K2=-(y+(h/2)*K1)./(R.*C);
    K3=-(y+(h/2)*K2)./(R.*C);
    K4=-(y+(h)*K3)./(R.*C);
    s(n)=y;
    y=y+(h/6)*(K1+2*K2+2*K3+K4);
    
    n=n+1;
end
t=0:h:30;
plot(t,s,'r--o');
xlabel('TIME (sec)')
ylabel ('VOLTAGE (V)')
hold on
y=3.8; n=1;
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Analytical Solution (Exact solution)
y=3.8*exp(-t./(R*C))
plot(t,y, 'g:+')

legend ('Euler','RK4','Analytical Solution')








