MATLAB绘制二阶电路响应
作者:互联网
MATLAB绘制二阶电路响应
一、效果展示
主界面
子页
二、代码
下面展示一些 源码
。
syms Us U0 Req C Is I0 L high;
RoadFlag=1;
StateFlag=0;
init()
while RoadFlag==1
RoadType=menu('电路类型','RC','RL','RCL','恢复出厂设置','结束');
switch RoadType
case 1
StateFlag=1;
while StateFlag==1
StateFlag=menu ('选择输入类型','等效电压源','初始电容电压','等效电阻','电容','零输入响应','零状态响应','全响应','退出');
switch StateFlag
case 1;
Us=input('请输入Us的值:');
case 2
U0=input('请输入U0的值:');
case 3
Req=input('请输入Req的值:');
case 4
C=input('请输入C的值:');
case 5 %零输入响应
tao=Req*C;
x1=0;dx=10*tao;%确定起始点和增量
x2=x1+dx;%确定画图时的横坐标终止值x1
y1=0;dy=U0;%确定起始点和增量
y2=y1+dy+dy/5;%确定画图时的横坐标终止值x1
figure('NumberTitle','off','Name','一阶RC电路零输入动态分析');
hold on
axis([x1 x2 y1 y2]);
grid;
title('Uc-t');
for t=x1:0.01:x2;
Uc=U0*(exp(-t/tao));
plot(t,Uc,'k.','markersize',15);
fprintf('时间=%d 电容电压=%d\n',t,Uc);
pause(0.01);
end
fprintf('U0=%dV,Req=%dΩ,C=%d\n',U0,Req,C);
fprintf('时间常数=%ds\n' ,tao);
case 6 %零状态响应
tao=Req*C;
x1=0;dx=10*tao;
x2=x1+dx;
y1=0;dy=Us;
y2=y1+dy+dy/5;
figure('NumberTitle','off','Name','一阶RC电路零状态动态分析');
hold on
axis([x1 x2 y1 y2]);
grid;
title('Uc-t');
for t=x1:0.01:x2;
Uc=Us*(1-exp(-t/tao));
plot(t,Uc,'k.','markersize',15);
fprintf('时间=%d 电容电压=%d\n',t,Uc);
pause(0.01);
end
fprintf('Us=%dV,Req=%dΩ,C=%d\n',Us,Req,C);
fprintf('时间常数=%ds\n' ,tao);
case 7 %全响应
tao=Req*C;
x1=0;dx=10*tao;
x2=x1+dx;
y1=0;dy=Us+U0;
y2=y1+dy+dy/5;
figure('NumberTitle','off','Name','一阶RC电路全响应分析');
hold on
axis([x1 x2 y1 y2]);
grid;
title('Uc-t');
for t=x1:0.01:x2;
Uc=U0*(exp(-t/tao))+Us*(1-exp(-t/tao));
plot(t,Uc,'k.','markersize',15);
fprintf('时间=%d 电容电压=%d\n',t,Uc);
pause(0.01);
end
fprintf('Us=%dV,U0=%dV,Req=%dΩ,C=%d\n',Us,U0,Req,C);
fprintf('时间常数=%ds\n' ,tao);
case 8
StateFlag=0;
RoadFlag=0;
end
end
case 2
StateFlag=2;
while StateFlag==2
StateFlag=menu ('选择输入类型','等效电流源','初始电感电流','等效电阻','电感','零输入响应','零状态响应','全响应','退出');
switch StateFlag
case 1;
Is=input('请输入Is的值:');
case 2
I0=input('请输入I0的值:');
case 3
Req=input('请输入Req的值:');
case 4
L=input('请输入L的值:');
case 5 %零输入响应
tao=Req/L;
x1=0;dx=10*tao;%确定起始点和增量
x2=x1+dx;%确定画图时的横坐标终止值x1
y1=0;dy=I0;%确定起始点和增量
y2=y1+dy+dy/5;%确定画图时的横坐标终止值x1
figure('NumberTitle','off','Name','一阶RL电路零输入动态分析');
hold on
axis([x1 x2 y1 y2]);
grid;
title('Il-t');
for t=x1:0.01:x2;
Il=I0*(exp(-t/tao));
plot(t,Il,'k.','markersize',15);
fprintf('时间=%d 电感电流=%d\n',t,Il);
pause(0.01);
end
fprintf('U0=%dV,Req=%dΩ,C=%d\n',I0,Req,L);
fprintf('时间常数=%ds\n' ,tao);
case 6
tao=Req/L;
x1=0;dx=10*tao;
x2=x1+dx;
y1=0;dy=Is;
y2=y1+dy+dy/5;
figure('NumberTitle','off','Name','一阶RL电路零状态动态分析');
hold on
axis([x1 x2 y1 y2]);
grid;
title('Il-t');
for t=x1:0.01:x2;
Uc=Il*(1-exp(-t/tao));
plot(t,Il,'k.','markersize',15);
fprintf('时间=%d 电容电压=%d\n',t,Il);
pause(0.01);
end
fprintf('I0=%dV,Req=%dΩ,L=%d\n',I0,Req,L);
fprintf('时间常数=%ds\n' ,tao);
case 7 %全响应
tao=Req/L;
x1=0;dx=10*tao;
x2=x1+dx;
y1=0;dy=Is+I0;
y2=y1+dy+dy/5;
figure('NumberTitle','off','Name','一阶RL电路全响应分析');
hold on
axis([x1 x2 y1 y2]);
grid;
title('Il-t');
for t=x1:0.01:x2;
Il=I0*(exp(-t/tao))+Is*(1-exp(-t/tao));
plot(t,Il,'k.','markersize',15);
fprintf('时间=%d 电感电流=%d\n',t,Il);
pause(0.01);
end
fprintf('Is=%dV,I0=%dV,Req=%dΩ,L=%d\n',Is,I0,Req,L);
fprintf('时间常数=%ds\n' ,tao);
case 8
StateFlag=0;
RoadFlag=0;
end
end
case 3
StateFlag=3;
while StateFlag==3
StateFlag=menu ('选择输入类型','等效电源','初始Uc/Il','等效电阻','电容/电感','零输入响应','零状态响应','全响应','退出');
switch StateFlag
case 1;
Us=input('请输入Us的值:');
Is=input('请输入Is的值:');
case 2
U0=input('请输入Uc的初始值:');
I0=input('请输入Il的初始值:');
case 3
Req=input('请输入Req的值:');
case 4
C=input('请输入C的值:');
L=input('请输入L的值:');
case 5 %零输入响应
figure('NumberTitle','off','Name','二阶电路零输入动态分析');
y=dsolve('L*C*D2y+Req*C*Dy+y=0','y(0)=U0','Dy(0)=-I0/C','t');
x=-C*diff(y,1);
z=L*diff(x,1);
Ul=eval(z)+I0/C;
Ic=eval(x);
Il=eval(x);
Uc=eval(y);
t=0:0.01:10;
plot(t,Uc,t,Ic,t,Ul,t,Il);
legend('Uc-t','Ic-t','Ul-t','Il-t');
%测试用
% Req=1;
% % L=1;
% % C=1;
% % I0=1;
% % U0=10;
case 6 %零状态响应
figure('NumberTitle','off','Name','二阶电路零状态动态分析');
high=10;
hold on
axis([0 8 -high,high]);
grid;
y=dsolve('L*C*D2y+1/Req*L*Dy+y=Is','y(0)=0','Dy(0)=-U0/L','t');%求Il;
x=L*diff(y,1);
z=-C*diff(x,1);
Ic=eval(z);
Uc=eval(x);
Ul=eval(x);
Il=eval(y);
t=0:0.01:10;
plot(t,Uc,t,Ic,t,Ul,t,Il);
legend('Uc-t','Ic-t','Ul-t','Il-t');
case 7 %全响应
hold on
high=Us+U0;
axis([0 8 -high,high]);
grid;
y1=dsolve('L*C*D2y+Req*C*Dy+y=0','y(0)=U0','Dy(0)=-I0/C','t');
x1=-C*diff(y,1);
z1=L*diff(x,1);
y2=dsolve('L*C*D2y+1/Req*L*Dy+y=Is','y(0)=0','Dy(0)=-U0/L','t');%求Il;
x2=L*diff(y,1);
z2=-C*diff(x,1);
Ic=eval(x1+z2);
Uc=eval(y1+x2);
Ul=eval(z1+x2);
Il=eval(x1+y2);
t=0:0.01:10;
plot(t,Uc,t,Ic,t,Ul,t,Il);
legend('Uc-t','Ic-t','Ul-t','Il-t');
case 8
StateFlag=0;
RoadFlag=0;
end
end
case 4
init();
case 5
RoadFlag=0;
end
end
标签:case,tao,Req,二阶,MATLAB,x1,绘制,Uc,Il 来源: https://blog.csdn.net/FourierFisher/article/details/110259190