Coding Circuits in Matlab

33 次查看(过去 30 天)
Ibrahim Ben Mrad
Ibrahim Ben Mrad 2022-3-28
回答: pratiksha 2024-9-25
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!

回答(2 个)

Prasanna Konyala
Prasanna Konyala 2022-3-31
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 2024-9-25
% 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);

类别

Help CenterFile Exchange 中查找有关 Simscape Electrical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by