Here are some MATLAB codes for simulating different Op-Amp circuits.
1. Inverting Op-Amp Simulation
This MATLAB script simulates an inverting amplifier and plots the input vs. output waveform.
Circuit Details:
• R1 (Input Resistor) = 10kΩ
• Rf (Feedback Resistor) = 100kΩ
• Gain: Vout=−(RfR1)VinV_{out} = -\left(\frac{R_f}{R_1}\right) V_{in}
• Expected Gain = -10 (Inverting).
MATLAB Code:
clc; clear; close all;
% Circuit Parameters
R1 = 10e3; % 10kΩ
Rf = 100e3; % 100kΩ
A = -Rf / R1; % Gain
% Time and Input Signal
t = 0:0.0001:0.02; % Time vector (20ms)
Vin = 1 * sin(2 * pi * 50 * t); % 50Hz sine wave input
% Output Signal
Vout = A * Vin;
% Plot Results
figure;
subplot(2,1,1);
plot(t, Vin, 'b', 'LineWidth', 1.5);
title('Input Signal (Vin)');
xlabel('Time (s)');
ylabel('Voltage (V)');
grid on;
subplot(2,1,2);
plot(t, Vout, 'r', 'LineWidth', 1.5);
title('Output Signal (Vout)');
xlabel('Time (s)');
ylabel('Voltage (V)');
grid on;
Expected Output:
• The output waveform is inverted and amplified by a factor of 10.
2. Non-Inverting Op-Amp Simulation
This MATLAB script simulates a non-inverting amplifier.
Circuit Details:
• R1 = 10kΩ
• Rf = 100kΩ
• Gain: Vout=(1+RfR1)VinV_{out} = \left(1 + \frac{R_f}{R_1}\right) V_{in}
• Expected Gain = 11 (Non-inverting).
MATLAB Code:
clc; clear; close all;
% Circuit Parameters
R1 = 10e3; % 10kΩ
Rf = 100e3; % 100kΩ
A = 1 + (Rf / R1); % Gain
% Time and Input Signal
t = 0:0.0001:0.02; % Time vector
Vin = 1 * sin(2 * pi * 50 * t); % 50Hz sine wave
% Output Signal
Vout = A * Vin;
% Plot Results
figure;
subplot(2,1,1);
plot(t, Vin, 'b', 'LineWidth', 1.5);
title('Input Signal (Vin)');
xlabel('Time (s)');
ylabel('Voltage (V)');
grid on;
subplot(2,1,2);
plot(t, Vout, 'r', 'LineWidth', 1.5);
title('Output Signal (Vout)');
xlabel('Time (s)');
ylabel('Voltage (V)');
grid on;
Expected Output:
• The output is in phase with the input and amplified by 11 times.
3. Op-Amp Integrator Simulation
This MATLAB script simulates an Op-Amp Integrator.
Circuit Details:
• R = 10kΩ, C = 0.1µF
• Integrator Equation: Vout=−1RC∫VindtV_{out} = -\frac{1}{RC} \int V_{in} dt
• A square wave input is used, and the output should be a triangular wave.
MATLAB Code:
clc; clear; close all;
% Circuit Parameters
R = 10e3; % 10kΩ
C = 0.1e-6; % 0.1µF
tau = R * C; % Time constant
% Time and Input Signal (Square Wave)
t = 0:0.0001:0.02; % Time vector
Vin = square(2 * pi * 50 * t); % 50Hz square wave input
% Output Signal (Integration of Vin)
Vout = -cumtrapz(t, Vin) / tau; % Numerical integration
% Plot Results
figure;
subplot(2,1,1);
plot(t, Vin, 'b', 'LineWidth', 1.5);
title('Input Signal (Square Wave)');
xlabel('Time (s)');
ylabel('Voltage (V)');
grid on;
subplot(2,1,2);
plot(t, Vout, 'r', 'LineWidth', 1.5);
title('Output Signal (Triangular Wave)');
xlabel('Time (s)');
ylabel('Voltage (V)');
grid on;
Expected Output:
• Input: Square Wave.
• Output: Triangular Wave.
4. Op-Amp Differentiator Simulation
This MATLAB script simulates an Op-Amp Differentiator.
Circuit Details:
• R = 10kΩ, C = 0.1µF
• Differentiator Equation: Vout=−RCdVindtV_{out} = -RC \frac{dV_{in}}{dt}
• A triangular wave input is used, and the output should be a square wave.
MATLAB Code:
clc; clear; close all;
% Circuit Parameters
R = 10e3; % 10kΩ
C = 0.1e-6; % 0.1µF
tau = R * C; % Time constant
% Time and Input Signal (Triangular Wave)
t = 0:0.0001:0.02; % Time vector
Vin = sawtooth(2 * pi * 50 * t, 0.5); % 50Hz triangular wave input
% Output Signal (Differentiation of Vin)
Vout = -tau * diff(Vin) ./ diff(t);
t = t(1:end-1); % Adjust time vector for differentiation
% Plot Results
figure;
subplot(2,1,1);
plot(t, Vin(1:end-1), 'b', 'LineWidth', 1.5);
title('Input Signal (Triangular Wave)');
xlabel('Time (s)');
ylabel('Voltage (V)');
grid on;
subplot(2,1,2);
plot(t, Vout, 'r', 'LineWidth', 1.5);
title('Output Signal (Square Wave)');
xlabel('Time (s)');
ylabel('Voltage (V)');
grid on;
Expected Output:
• Input: Triangular Wave.
• Output: Square Wave.
Summary of Simulations
Op-Amp Circuit Input Signal Expected Output
Inverting Amplifier Sine Wave Amplified & Inverted
Non-Inverting Amplifier Sine Wave Amplified (Same Phase)
Integrator Square Wave Triangular Wave
Differentiator Triangular Wave Square Wave