The voltage waveform shown in Figure 1 is applied to the RC
Solution
function [v, t] = rectangular_RC(vs, r, c)
 tau = r * c;
% Use the correct formula
 t1 = linspace(20,500);
 v1 = vs * (1 - exp(-t1/tau));
% Second half of the pulse: 0.51 to 1 seconds.
 % Take into account the max voltage reached.
 % Use the appropriate formula.
 Vm = v1(end);
 t2 = linspace(0.51, 1, 50);
 v2 = Vm * exp(-t1/tau);
% Assemble the final vectors to return
 t = [t1 t2];
 v = [v1 v2];
Now, we are going to call the function from our main code, like this
% Given constants
 vs = 10;
 c = 10e-6;
% Case 1. R = 500kohms
 r1 = 5e3;
 [v1, t1] = rectangular_RC(vs, r1, c);
% Case 2. R = 500kohms
 r2 = 20e3;
 [v2, t2] = rectangular_RC(vs, r2, c);
% Plot the responses
 plot(t1, v1, \'bo\', t2, v2, \'r+\')
 grid on
% Add labels
 title(\'RC circuit - rectangular input\')
 xlabel(\'Time (s)\')
 ylabel(\'Voltage (V)\')
 legend([\'R_1 = \' num2str(r1)], ...
        [\'R_2 = \' num2str(r2)])

