T = 4;
A = 5;
R = 1000;
C = 0.0005;

%

tau = R * C

%

t = -T:0.05:2*T
n = (1:60)'

%

wn = 2 * pi * n / T

%

a0 = A
bn = A * (1 - cos(n * pi)) ./ (n * pi)

%

Vin = a0 / 2 + sum(bn .* sin(wn * t), 1)

%

plot(t, Vin)

%

magHjw = 1 ./ sqrt(1 + (R * C * wn).^2)

%

magHj0 = 1 / sqrt(1 + (R * C * 0)^2)

%

angleHjw = atan(-R * C * wn)

%

Vc = a0 * magHj0 / 2 + sum(bn .* magHjw .* sin(wn * t + angleHjw), 1)

%

plot(Vin)
hold on
plot(Vc)
hold off