% calculate loss ram
function [Laverage,H] = Losscalc(GY,GYd,Wn,Wnd,F1,Juu,sset)
sset = [43 60 64];
load GY
load GYd
load Wn
load Wnd
load Juu
load Jud
load F1


%ny = size(sset,2);
ny = size(GY,2);
nd = size(GYd,2);

GY = GY(sset,:);
GYd = GYd(sset,:);
Wn = Wn(sset,sset);
%F1= [F1(43,:);F1(60,:);F1(64,:)];
F1 = -(GY*inv(Juu)*Jud-GYd);
Ftilde = [F1*Wnd Wn];

H = inv(Ftilde*Ftilde')*GY*inv(GY'*inv(Ftilde*Ftilde')*GY)*sqrtm(Juu);
H = H';

G = H*GY;
Gd = H*GYd;

Md = sqrtm(Juu)*(inv(Juu)*Jud-inv(G)*Gd)*Wnd;
Mn = sqrtm(Juu)*inv(G)*H*Wn; %Typo in IECR paper (H missing)
Laverage = (norm([Md Mn],'fro')^2)/6/(ny+nd);
%Laverage = (norm([Md Mn],'fro')^2)/2;