-
Notifications
You must be signed in to change notification settings - Fork 3
/
lfnrsm1.m
113 lines (101 loc) · 3.17 KB
/
lfnrsm1.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
% The M-file name is lnnrsm1.m
% NRS Newton-Raphson-Seydel method for load flow analysis
% NRS provides convergent load flow results.
%=================================================================
lfcntrup; %updateloadflow control parameters (error tolerance, max. iteration etc...)
n=length(x); %size of the states
v=zeros(n,1); %zero eigenvector
OldFigNumber=watchon;
% Reorder the parameters
k_temp=no_gen+no_pv-1;
for i=1:k_temp
paramx(i)=param(i);
end
for i=1:no_pq
ii=k_temp+i;
jj=k_temp+1+2*(i-1);
paramx(ii)=param(jj);
paramx(ii+no_pq)=param(jj+1);
end
param=paramx';
param;
% INITIALIZE NRS
% Starting Values for lambda0 and v0
% inverse iteration to obtain estimates of lambda0 near 0 and v0
[f,J]=eval([CurrentSystem,'(data,x,[0;param],v)']); % mismatch and jacobian matrix
A=J(2:n+1,1:n);
rand('state',100)
v=rand(n,1);
v=v/norm(v); %normalized random right eigenvector
lambda=0; %initial estimate of eigenvalue
OPTIONS.disp=0; %do not show the intermediate steps
sigma=eps;
[x1, x2,flag]=eigs(A,1,sigma,OPTIONS); %the smallest eigenvalue and corresponding eigenvector
%Inverse iteration method
for j=1:6
y=(A-lambda*eye(size(A)))\v;
lambda=lambda+norm(v)^2/(v'*y);
v=y/norm(y);
end
lambda_check=lambda;
v_check=v;
v=x1;
lambda=x2;
ConvergenceFlag=0;
for j=1:round(MaxIterations/ReportCycle)
OldFigNumber=watchon;
t0=clock;
for i=1:ReportCycle
x0=x;
v0=v;
lambda0=lambda;
[f,J]=eval([CurrentSystem,'(data,x,[0;param],v)']);
JJ_nrs=[J(2:n+1,1:n) zeros(n,n) zeros(n,1)
J(2:n+1,n+1:2*n) J(2:n+1,1:n)-lambda*eye(n) -v
zeros(1,n) v'/norm(v) 0
];
ff_nrs=[f(2:n+1)
(J(2:n+1,1:n)-lambda*eye(n))*v
norm(v)-1];
delta=-sparse(JJ_nrs)\ff_nrs;
x=x0+delta(1:n);
v=v0+delta(n+1:2*n);
lambda=lambda0+delta(2*n+1)
end
AbsError=max([abs(x-x0);abs(v-v0);abs(lambda-lambda0)]);
if ((x0==0) & (v0==0)& (lambda0==0))
RelError='NA';
else
RelError=AbsError/max([abs(x0);abs(v0);abs(lambda0)]);
end
lfsetsta;
% set LF control control errors
set(AbsErrorDisp,'String',num2str(AbsError));
if isstr(RelError)
set(RelErrorDisp,'String',RelError);
else
set(RelErrorDisp,'String',num2str(RelError));
end
set(NumIterations,'String',num2str(j*ReportCycle));
set(IterationTime,'String',num2str(etime(clock,t0)/ReportCycle));
if (AbsError<=LFAbsTol)&((~isstr(RelError))&(RelError<=LFRelTol)|isstr(RelError))
ConvergenceFlag=1;
break;
end
end
if ConvergenceFlag==0
'NRS Failed to Converge'
break;
end
for i=1:k_temp
paramx(i)=param(i);
end
for i=1:no_pq
ii=k_temp+i;
jj=k_temp+1+2*(i-1);
paramx(jj)=param(ii);
paramx(jj+1)=param(ii+no_pq);
end
param=paramx;
param;
watchoff;