# RC Circuit Transient Analysis using GUI | MATLAB Tutorial

Considering the RC Circuit (also called RC network) shown in this figure.

We can use the Kirchhoff’s current law (KCL) to write the following equation,

We can rearrange into the equation,

The solution to the equation above is,

Where,
Vm is the initial voltage across the capacitor,
RC is the time constant,
This solution represents the voltage across a discharging capacitor.

Now, to obtain the voltage across a charging capacitor, let us consider this figure that includes a voltage source,

Again, using KCL, the equation describing the charging RC circuit is,

If the capacitor is not charged initially, that is V0(t) = 0 when t = 0, then the solution to the equation above is given by,

The following examples illustrate the use of MATLAB for solving problems related to RC circuits.

MATLAB Code

```
function varargout = RC(varargin)
% RC MATLAB code for RC.fig
% RC, by itself, creates a new RC or raises the existing
% singleton*.
%
% H = RC returns the handle to a new RC or the handle to
% the existing singleton*.
%
% RC('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in RC.M with the given input arguments.
%
% RC('Property','Value',...) creates a new RC or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before RC_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to RC_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%

% Edit the above text to modify the response to help RC

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @RC_OpeningFcn, ...
'gui_OutputFcn', @RC_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before RC is made visible.
function RC_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to RC (see VARARGIN)

% Choose default command line output for RC
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes RC wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = RC_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double

% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double

% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

h1 = get(handles.edit1,'string');
R = str2num(h1);

h2 = get(handles.edit2,'string');
C = str2num(h2);

h3 = get(handles.edit3,'string');
Vs = str2num(h3);

h4 = R*C;
T = h4;
t = 0:h4/1000:5*h4;
Vout = Vs*(1-exp(-t/h4));
plot(t, Vout);
grid on
title('Transient Analysis - RC circuit');
xlabel('Time (s)');
ylabel('Voltage across capacitor (V)');
legend([' Time Constant = ' num2str(T)]);

str = ['Time Constant is: ', num2str(T)]
set(handles.T, 'String', str);
```

RESULT:

1. Resistance = 10000,

∴ R = 10KΩ,

1. Capacitance = 0.000006,

∴ C = 6fF,

1. Supply Voltage = 12,

∴ Vs = 12V.

1. Resistance = 5000,

∴ R = 5KΩ,

1. Capacitance = 0.00001,

∴ C = 10fF,

1. Supply Voltage = 25,

∴ Vs = 25V.

### Recommended Posts

##### Test Post

17 Feb 2018 - Tutorial

##### Counters using conditionally executed subsystems | Simulink Tutorial

16 Feb 2018 - Simulink, Tutorial

##### Regulating drum boiler pressure

16 Feb 2018 - Simulink, Tutorial