Problems with nested MATLAB statements and halving
New to the site, but I'm trying to hone some MATLAB skills for work and study and was looking for some help with the following:
I want to write my own algorithm for finding the Hinf norm of a system using halving, as the MATLAB function 'hinfsyn' does.
I have included the code that I have so far:
function [ hnorm ] = matmath(A,B,C,D,glow,ghigh,tol)
if 2*(ghigh-glow) < tol
gam = (ghigh+glow)/2;
hnorm = gam;
else
Dgam = ((gam^2)*eye(size(D)))-(D'*D);
A_clp = [ A + (B/Dgam*D'*C) -B/Dgam*B'
(C'*C) + C'*D/Dgam*D'*C -A'-(C'*D/Dgam*B')];
eigcl = eig(A_clp);
for i = 1:length(eigcl)
if real(eig(i)) == 0
glow = gam;
else
ghigh = gam;
end
end
end
I rationalized the problem into a few steps:
- Using the gamma bounds as input, compute the first iteration: gam = (ghigh-glow) / 2. If 2 * (ghigh-glow) <tol then the program stops with hinf = gam.
- Compute the eignvalues โโof the closed loop matrix A.
- Check purely imaginary eigenvalues. If there is a purely imaginary eigenvalue, then the new glow = gam. Otherwise set ghigh = gam.
- Keep repeating until you are satisfied with the gamma accuracy.
I believe my calculations in the matrix are correct, but I have a hard time working with if / for statements. My code only completes the first iteration when I run it in MATLAB. Any advice would be greatly appreciated! Thank!
Update: Here is the code I have simplified that completes one iteration successfully and has been tested for different values.
function [ hnorm ] = matmath(A,B,C,D,glow,ghigh,tol)
gam = (ghigh+glow)/2;
if 2*(ghigh-glow) < tol
hnorm = gam
else
Dgam = ((gam^2)*eye(size(D)))-(D'*D);
A_clp = [ A + (B/Dgam*D'*C) -B/Dgam*B'
(C'*C) + C'*D/Dgam*D'*C -A'-(C'*D/Dgam*B')];
eig_clp = eig(A_clp)
for z = 1:length(eig_clp)
if abs(real(eig_clp(z)))<1e-10
glow = gam
break
end
end
ghigh = gam
end
source to share
If you want your code to 2*(gmax - gmin) < tol
run before reaching , use a loop while
:
function [ hnorm ] = matmath(A,B,C,D,glow,ghigh,tol)
while 2*(ghigh-glow) >= tol
Dgam = ((gam^2)*eye(size(D)))-(D'*D);
A_clp = [ A + (B/Dgam*D'*C) -B/Dgam*B'
(C'*C) + C'*D/Dgam*D'*C -A'-(C'*D/Dgam*B')];
eigcl = eig(A_clp);
for i = 1:length(eigcl)
if real(eig(i)) == 0
glow = gam;
else
ghigh = gam;
end
end
end
gam = (ghigh+glow)/2;
hnorm = gam;
source to share