Coding Circuits in Matlab

30 views (last 30 days)
Ibrahim Ben Mrad
Ibrahim Ben Mrad on 28 Mar 2022
Answered: pratiksha on 25 Sep 2024
Hello everybody,
Im just getting started with Matlab. Can someone tell me how to write a code of a Resistor for example or a small electrical circuit consisting of two in series Resistors so the plot can be done in Simulink
thx in advance!

Answers (2)

Prasanna Konyala
Prasanna Konyala on 31 Mar 2022
Hi Ibrahim
From my understanding, you want to create a circuit with resistors in series.
To find the equivalent resistance you can use sum() (as the sum of resisters is the equivalent resistance in series connection).
If you want to design and implement and simulate the circuits, I suggest you try Simscape Electrical toolbox. It makes the process simpler than writing whole code in Matlab.
You can leverage plot in matlab or scope in Simscape for plotting

pratiksha
pratiksha on 25 Sep 2024
% Define the symbolic variables
syms v1 v2 v3
% Given values
R1 = 1/3; % Resistance in Ohms
R2 = 1/2; % Resistance in Ohms
R3 = 1/6; % Resistance in Ohms
I1 = 4; % Current in Amps
I2 = 9; % Current in Amps
V_source = 5; % Voltage source value in Volts
% KCL equations using the supernode method
% Equation at node v1:
eq1 = (v1 - v2) / R1 == I1;
% Equation at node v3:
eq2 = (v3 - v2) / R3 == -I2;
% Supernode: Combine v2 and v3 with voltage source constraint
% Voltage constraint: v3 = v2 + V_source
eq3 = v3 == v2 + V_source;
% KCL at the supernode (combining v2 and v3):
% (v2 - v1) / R1 + v2 / R2 + (v2 - v3) / R3 = 0
eq4 = (v2 - v1) / R1 + v2 / R2 + (v2 - v3) / R3 == 0;
% Solve the equations
sol = solve([eq1, eq2, eq3, eq4], [v1, v2, v3]);
% Display the results
v1_sol = double(sol.v1);
v2_sol = double(sol.v2);
v3_sol = double(sol.v3);
fprintf('The solution for v1 is: %.2f V\n', v1_sol);
fprintf('The solution for v2 is: %.2f V\n', v2_sol);
fprintf('The solution for v3 is: %.2f V\n', v3_sol);

Categories

Find more on Simscape Electrical in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!