以下是程序:
clear;
clc;
E=20
L=3.3e-3
C=1e-3
R=19;
T=4e-4;
a = 1/(2*R*C);
w = sqrt(1/L/C-1/4/R^2/C^2);
A2 = [0,-1/L;1/C,-1/R*C];
A1 = A2;
A3=[0,0;0,-1/C*R];
B1=[1/L;0];
B2=[0;0];
B3=B2;
I=[1,0;0,1];
for m=1:5001
Iref=0.1+0.01*(m-1);
i(1)=0;
u(1)=0;
for n=1:5000
X = [ i( n ); u( n ) ];
p1=(exp(-a*T)/w*((a*sin(w*T))+(w*cos(w*T))));
q1=exp(-a*T)/w*sin(w*T);
Ib1=(Iref/p1)+(q1*u(n)/L/p1)-(((1-p1)/R)+(q1/L))*E/q1;
if i(n)<=Ib1
X(:,n+1)=exp(A1*T)*X+(exp(A1*T)-I)*A1^-1*B1*E;
else
syms t1;
p3=exp(A1*t1)*X+(exp(A1*t1)-I)*A1^-1*B1*E;
q3=p3(1,:);
t1=solve(Iref==q3,t1);
t1=double(t1);
p2=(exp(-a*t1)/w*(a*sin(w*t1)+w*cos(w*t1)));
q2=exp(-a*t1)/w*sin(w*t1);
Ib2 = Iref/p2+q2*u(n)/L/p2-((1-p2)/R+q2/L)*E/p2;
if i(n) < Ib2
t2=T-t1;
X(:,n+1)=exp(A2*t2)*exp(A1*t1)*X+exp(A2*t2).*(exp(A1*t1)-I)*(A1^-1)*B1*E;
else
syms t2 ut1;
S=[Iref;ut1];
p4=exp(A2*t2)*S;
q4=p4(1,:);
t2=solve(0==q4,t2);
t2=double(t2);
t3=T-t1-t2;
X(:,n+1)=exp(A3*t3)*exp(A2*t2)*exp(A1*t1)*X+exp(A3*t3)*exp(A2*t2)*(exp(A1*t1)-I)*A1^-1*B1*E;
end
end
end
i1(m,:) = i(end-150:end);
% uo(m,:) = u(end-80:end);
H= plot(Iref*ones(1,length(i1(m,:))),i1(m,:));
% set(H,'linestyle','none','marker','.','markersize',3);
hold on
drawnow
end
程序是从X(:,n+1)=exp(A3*t3)*exp(A2*t2)*exp(A1*t1)*X+exp(A3*t3)*exp(A2*t2)*(exp(A1*t1)-I)*A1^-1*B1*E;循环一次后开始第二次循环时在 X = [ i( n ); u( n ) ]; 发生错误,我认为是初始化不能放在n循环里,但是X又跟n循环有关,又不能直接放在外面,恳请各位大佬们教教新手,如何把初始化那句 X = [ i( n ); u( n ) ]; 放在n循环外面?或者让这句话只在n循环中执行一次?谢谢各位!!!!!!