基于Matlab的结点电压法及相关定理验证的人机交互界面!
基于Matlab的结点电压法及相关定理验证的人机交互界面!
前言
本篇主要针于无储能元件的电路分析
由结点电压法求得相关结点的电压
进而验证戴维宁定理、叠加定理等
提示:以下是本篇文章正文内容,下面案例可供参考
一、实验背景
问题:对于大型网络,方程数目庞大,难于求解
方法:结点电压方程的变量数少,适合任意结构的电路,容易编程语言实现
优势:结合计算机专业的优势,学习电路的计算机分析方法
二、实验内容
(1).建立任意电阻网络的节点电压方程
(2).建立含电阻、电流源及电压源的任意电路的结点电压法方程
(3).验证两个电路定理(叠加定理、戴维宁定理)
(4).编写人机交互界面,方便操作与展示
1.原理简介
1).结点电压法(nodal method of analysis):在电路中选择某一结点为参考结点,其他结点与此参考结点之间的电压称为结点电压。以结点电压为电路变量,根据KCL列写电路方程求解电路的方法称为结点电压法。
(2). 戴维宁定理(Thevenin’s theorem):含独立电源的线性电阻单口网络N,就端口特性而言,可以等效为一个电压源和电阻串联的单口网络。电压源的电压等于单口网络在负载开路时的电压Uoc;电阻R0是单口网络内全部独立电源为零值时所得单口网络的等效电阻。
(3). 叠加定理 (Superposition theorem):对于一个线性系统,一个含多个独立源的双边线性电路的任何支路的响应(电压或电流),等于每个独立源单独作用时的响应的代数和,此时所有其他独立源被替换成他们各自的阻抗。
2.编程流程图
代码如下:
求出最多结点个数
function results = maxpointcalculate(app)
maxpoint=0;
if app.RNumber~=0
for i=1:1:app.RNumber
if app.RNP(i,3)>maxpoint
maxpoint=app.RNP(i,3);
end
if app.RNP(i,4)>maxpoint
maxpoint=app.RNP(i,4);
end
end
end
if app.VNumber~=0
for i=1:1:app.VNumber
if app.VNP(i,3)>maxpoint
maxpoint=app.VNP(i,3);
end
if app.VNP(i,4)>maxpoint
maxpoint=app.VNP(i,4);
end
end
end
if app.INumber~=0
for i=1:1:app.INumber
if app.INP(i,3)>maxpoint
maxpoint=app.INP(i,3);
end
if app.INP(i,4)>maxpoint
maxpoint=app.INP(i,4);
end
end
end
app.MaxPointNumber=maxpoint; %求出最多有多少个结点
end
处理电压源
data = pd.read_csv(
'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())NumberofCompaniedVoltSource=0;
for g=1:1:app.VNumber
if app.VNP(g,5)==1
NumberofCompaniedVoltSource=NumberofCompaniedVoltSource+1;%有几个有伴电压源
end
end
NumberofNoCompanyVoltSource=0;
for g=1:1:app.VNumber
if app.VNP(g,5)==0
NumberofNoCompanyVoltSource=NumberofNoCompanyVoltSource+1;%有几个无伴电压源
end
end
NewCurrentsource=zeros(NumberofCompaniedVoltSource,4);%新电流源=U/R 待会儿插入到INP中!!!!!!!!!!!!
k=1;
for g=1:1:app.VNumber
if app.VNP(g,5)==1
NewCurrentsource(k,2)=app.VNP(g,2)/app.VNP(g,6);
NewCurrentsource(k,3)=app.VNP(g,3);
NewCurrentsource(k,4)=app.VNP(g,4);
k=k+1;
end
end
处理有伴电流源
%接下来计算有伴电流源 将有伴电流源加到新电流源矩阵中
if app.INumber~=0
app.INP=[app.INP;NewCurrentsource];
else
app.INP=NewCurrentsource;
end
求出电导矩阵
GMatrix=zeros(app.MaxPointNumber+NumberofNoCompanyVoltSource+1,app.MaxPointNumber+NumberofNoCompanyVoltSource+1);
for j=1:1:app.RNumber
GMatrix(app.RNP(j,3),app.RNP(j,3))=GMatrix(app.RNP(j,3),app.RNP(j,3))+(1/app.RNP(j,2));
GMatrix(app.RNP(j,4),app.RNP(j,4))=GMatrix(app.RNP(j,4),app.RNP(j,4))+(1/app.RNP(j,2));
GMatrix(app.RNP(j,3),app.RNP(j,4))=GMatrix(app.RNP(j,3),app.RNP(j,4))-(1/app.RNP(j,2));
GMatrix(app.RNP(j,4),app.RNP(j,3))=GMatrix(app.RNP(j,4),app.RNP(j,3))-(1/app.RNP(j,2));
end
计算初始电流源矩阵
IsMatrix=zeros(app.MaxPointNumber+NumberofNoCompanyVoltSource+1,1);
for p=1:1:app.INumber+NumberofCompaniedVoltSource
IsMatrix(app.INP(p,4),1)=IsMatrix(app.INP(p,4),1)+app.INP(p,2);%终结结点,加上电流源数值
IsMatrix(app.INP(p,3),1)=IsMatrix(app.INP(p,3),1)-app.INP(p,2);%起始结点,减去电流源数值
end
将无伴电流源增补方程加入到电导矩阵中
i=1;%第i个无伴电流源
if NumberofNoCompanyVoltSource~=0
for j=1:1:app.VNumber
if app.VNP(j,5)==0
GMatrix(app.VNP(j,4),app.MaxPointNumber+i)=-1;%无伴电压源终结结点 结点电压方程需要 减去电流
GMatrix(app.VNP(j,3),app.MaxPointNumber+i)=1;%无伴电压源起始结点 结点方程就要 加上电流
GMatrix(app.MaxPointNumber+i,app.VNP(j,4))=1;
GMatrix(app.MaxPointNumber+i,app.VNP(j,3))=-1;
IsMatrix(app.MaxPointNumber+i,1)=app.VNP(j,2);%无伴电压源终结结点电压-起始结点电压=无伴电压源电压数值
i=i+1;
end
end
end
声明并计算最终结点电压矩阵
UnMatrix=pinv(GMatrix)*IsMatrix;
app.UnMatrix22=UnMatrix;
UnMatrix2=zeros(app.MaxPointNumber-1,1);
for m=1:1:app.MaxPointNumber-1
UnMatrix2(m,1)=UnMatrix(m,1);
end
app.UITable.ColumnName(1,1)={'数值/v'};
for i=1:1:app.MaxPointNumber-1
app.UITable.RowName(i,1)={strcat('Un',num2str(i))};
app.UITable.Data(i,1)=UnMatrix2(i,1);
end
戴维宁定理验证(求两结点开路电压并利用图像斜率求等效电阻)
Voltage1=app.UnMatrix22(app.RNP(str2double(strrep(app.DropDown.Value,'R','')),4),1)-app.UnMatrix22(app.RNP(str2double(strrep(app.DropDown.Value,'R','')),3),1);
app.RNP2=app.RNP;
Resistance1=app.RNP2(str2double(strrep(app.DropDown.Value,'R','')),2);
app.RNP2(str2double(strrep(app.DropDown.Value,'R','')),2)=0.5*app.RNP2(str2double(strrep(app.DropDown.Value,'R','')),2);
Resistance2=0.5*Resistance1;
app.UnMatrix22=CALCULATE(app,app.RNP2,app.VNP,app.INP);
%调用计算函数
Voltage2=app.UnMatrix22(app.RNP(str2double(strrep(app.DropDown.Value,'R','')),4),1)-app.UnMatrix22(app.RNP(str2double(strrep(app.DropDown.Value,'R','')),3),1);
Current1=Voltage1/Resistance1;
Current2=Voltage2/Resistance2;
EqualResistance=abs((Voltage2-Voltage1)/(Current2-Current1));
%app.UITable2.Data(1,1)=EqualVoltage;
app.UITable2.Data(1,3)=EqualResistance;
%再次调用函数求开路电压
app.RNP2(str2double(strrep(app.DropDown.Value,'R','')),2)=99999999999999999999999;
UnMatrix=CALCULATE(app,app.RNP2,app.VNP,app.INP);
app.UITable2.Data(1,1)=abs(UnMatrix(app.RNP(str2double(strrep(app.DropDown.Value,'R','')),4),1)-UnMatrix(app.RNP(str2double(strrep(app.DropDown.Value,'R','')),3),1));
app.UITable2.Data(1,2)=abs((UnMatrix(app.RNP(str2double(strrep(app.DropDown.Value,'R','')),4),1)-UnMatrix(app.RNP(str2double(strrep(app.DropDown.Value,'R','')),3),1))/EqualResistance);
calculateButtonPushed(app);
if app.Type.Value=="电压源"
app.Switch.Enable='on';
if app.Switch.Value=="yes"
app.CompaniedR.Enable='on';
n=1;
while(n<=app.RNumber)
i=n;
m=strcat('R',num2str(i));
app.CompaniedR.Items{n}=m;
n=n+1;
end
end
end
if app.Type.Value=="电阻"
app.Switch.Enable='off';
app.CompaniedR.Enable='off';
end
if app.Type.Value=="电流源"
app.Switch.Enable='off';
app.CompaniedR.Enable='off';
end
......
叠加定理验证(分别计算各个激励在端口单独作用的响应)
%计算电流源单独作用的节点电压
if tempINumber~=0
for i=1:1:tempINumber
for m=1:1:tempINumber
app.INP(m,2)=0;
end
app.INP(i,1:4)=INP2(i,1:4);
UnMatrix=CALCULATE(app,app.RNP,app.VNP,app.INP);
app.UITable4.ColumnName(1,times)={strcat('Is',num2str(times))};
for i=1:1:app.MaxPointNumber-1
app.UITable4.RowName(i,times)={strcat('Un',num2str(i))};
app.UITable4.Data(i,times)=UnMatrix(i,1);
end
times=times+1;
end
end
%计算电压源单独作用的节点电压
if tempVNumber~=0
for i=1:1:tempVNumber
for m=1:1:tempVNumber
app.VNP(m,2)=0;
end
app.VNP(i,1:4)=VNP2(i,1:4);
UnMatrix=CALCULATE(app,app.RNP,app.VNP,app.INP);
app.UITable4.ColumnName(1,times)={strcat('Us',num2str(times))};
for i=1:1:app.MaxPointNumber-1
app.UITable4.RowName(i,times)={strcat('Un',num2str(i))};
app.UITable4.Data(i,times)=UnMatrix(i,1);
end
times=times+1;
end
end
app.VNP=VNP2;%返回修改前的值
app.INP=INP2;
计算函数
function results = CALCULATE(app,rnp,vnp,inp)
NumberofCompaniedVoltSource=0;
for g=1:1:app.VNumber
if vnp(g,5)==1
NumberofCompaniedVoltSource=NumberofCompaniedVoltSource+1;%有几个有伴电压源
end
end
NumberofNoCompanyVoltSource=0;
for g=1:1:app.VNumber
if vnp(g,5)==0
NumberofNoCompanyVoltSource=NumberofNoCompanyVoltSource+1;%有几个无伴电压源
end
end
编者独白
人机交互界面设计方法
(1).在MATLAB的appdesigner上构建交互界面的基本框架
(2).添加回调函数执行相应功能