How to Simulate Electrical Circuits Using MATLAB and Simulink

Learn how to simulate an RC circuit in MATLAB and Simulink to analyze capacitor charging behavior.


Simulating electrical circuits is crucial for analyzing their behavior before building physical systems. MATLAB and Simulink are powerful tools that allow engineers to simulate, test, and visualize complex electrical circuits with ease. In this article, we’ll explore how to simulate an RC (Resistor-Capacitor) circuit using MATLAB and Simulink, specifically focusing on the charging process of a capacitor.

Understanding an RC Circuit

An RC circuit consists of a resistor and a capacitor connected in series to a voltage source. When voltage is applied, the capacitor charges over time, and the voltage across it increases following an exponential curve. The time it takes for the capacitor to charge depends on the resistor value and the capacitor's capacitance. The key equation for the voltage Vc(t)V_c(t) across the capacitor is:

Vc(t)=Vin(1etRC)V_c(t) = V_{in} \left(1 - e^{-\frac{t}{RC}}\right)

Where:

  • VinV_{in} is the input voltage.
  • RR is the resistance.
  • CC is the capacitance.
  • tt is time.
  • ee is the exponential function.

The time constant τ\tau (tau) for an RC circuit is defined as τ=RC\tau = RC, and it represents the time it takes for the voltage across the capacitor to reach approximately 63% of its final value.

Steps to Simulate the Circuit in MATLAB and Simulink

Step 1: Setting Up the Circuit

To simulate an RC circuit in MATLAB and Simulink, follow these steps:

  1. Open Simulink: Launch MATLAB and open Simulink by typing simulink in the command window.
  2. Create a New Model: In Simulink, create a new model by clicking on the "New" button and selecting "Model."
  3. Add Components:
    • Drag and drop the following components from the Simulink library:
      • Resistor (from the Simscape → Foundation Library → Electrical → Electrical Elements)
      • Capacitor (from the same path as the resistor)
      • Voltage Source (Simscape → Foundation Library → Electrical → Electrical Sources)
      • Ground (Simscape → Foundation Library → Electrical → Electrical Elements)
      • Scope (Simulink → Sinks)
      • PS-Simulink Converter and Simulink-PS Converter (Simscape → Utilities)
Step 2: Connecting the Components

Now, connect the components as follows:

  1. Connect the positive terminal of the voltage source to one end of the resistor.
  2. Connect the other end of the resistor to the positive terminal of the capacitor.
  3. Connect the negative terminal of the voltage source and the negative terminal of the capacitor to the ground.
  4. Connect the voltage measurement block across the capacitor to observe its voltage.
  5. Use the PS-Simulink Converter to convert the physical signal to a Simulink signal and connect it to a Scope to display the results.
Step 3: Parameter Setup

Next, assign the following values to the components:

  • Resistor (R): 10 kΩ
  • Capacitor (C): 100 μF
  • Input Voltage (Vin): 5 V
  • Simulation Time: 10 seconds
Step 4: Running the Simulation

After setting up the circuit, run the simulation by clicking the "Run" button. The voltage across the capacitor will be displayed on the Scope.

MATLAB Code for Simulating an RC Circuit

Here is an example of MATLAB code that you can use to simulate the charging of a capacitor in an RC circuit:

% MATLAB code to simulate RC circuit
R = 10000; % Resistance in Ohms
C = 100e-6; % Capacitance in Farads
Vin = 5; % Input Voltage in Volts
t = 0:0.01:10; % Time vector from 0 to 10 seconds with 0.01 second intervals

% Equation for voltage across the capacitor
Vc = Vin * (1 - exp(-t / (R * C)));

% Plot the results
figure;
plot(t, Vc, 'LineWidth', 2);
xlabel('Time (s)');
ylabel('Voltage (V)');
title('Voltage Across Capacitor in RC Circuit');
grid on;

This code calculates the voltage across the capacitor over time and plots the result. You can change the values of RR, CC, and VinV_{in} to simulate different scenarios.

The Waveform for the RC Circuit

Here is the waveform for the voltage across the capacitor in an RC circuit, showing how it charges over time. The voltage increases as the capacitor charges, following an exponential curve, approaching the input voltage VinV_{in} over time.


The graph above represents the charging process of the capacitor in an RC circuit. As the simulation runs, you'll notice that the voltage across the capacitor increases rapidly at first and then slowly approaches the final value of VinV_{in}. This is due to the exponential nature of the charging process.

Analyzing the Results

From the simulation results and the waveform, it is clear that:

  1. Initial Charging: At t=0t = 0, the voltage across the capacitor is 0. This is because the capacitor is uncharged.
  2. Exponential Rise: As time progresses, the voltage across the capacitor increases exponentially, approaching the input voltage VinV_{in}.
  3. Time Constant (τ): After a time equal to τ=RC\tau = RC, the voltage across the capacitor reaches approximately 63% of its final value.
  4. Steady-State: After 5 time constants (5×τ5 \times \tau), the capacitor is considered fully charged, and the voltage across it equals VinV_{in}.

Conclusion

Simulating electrical circuits using MATLAB and Simulink provides valuable insights into their behavior before physically building them. In this article, we simulated an RC circuit and demonstrated how to observe the charging process of a capacitor. The combination of MATLAB’s scripting capabilities and Simulink’s graphical interface makes it easy to simulate and analyze complex circuits.

Prasun Barua is an Engineer (Electrical & Electronic) and Member of the European Energy Centre (EEC). His first published book Green Planet is all about green technologies and science. His other …

Post a Comment

© Prasun Barua . All rights reserved. Developed by Jago Desain