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 Center および File ExchangeSimscape Electrical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by