MATLAB: How to use matlab to find the optimized matrix with regarding to minimization of Frobenius norm

matrix manipulationnormoptimization

I'm trying to solve a minimization problem whose purpose is to optimize a matrix whose square is close to another given matrix. but I have some problems to execute it by using matlab. The problem is illustrated as follows:
First question is how should I define the such an undetermined matrix when using optimization function in matlab;
Second question is if there is a specified toolbox to solve this problem?

Best Answer

function main
G=zeros(4,4);
G(1,1)=0.48;
G(1,2)=0.24;
G(1,3)=0.16;
G(1,4)=0.12;
G(2,:)=G(1,:);
G(3,:)=G(1,:);
G(4,:)=G(1,:);
Aeq=zeros(4,12);
Aeq(1,1)=1.0;
Aeq(1,2)=1.0;
Aeq(1,3)=1.0;
Aeq(2,4)=1.0;
Aeq(2,5)=1.0;
Aeq(2,6)=1.0;
Aeq(3,7)=1.0;
Aeq(3,8)=1.0;
Aeq(3,9)=1.0;
Aeq(4,10)=1.0;
Aeq(4,11)=1.0;
Aeq(4,12)=1.0;
beq=ones(4,1);
lb=zeros(1,12);
ub=ones(1,12);
x0=ones(1,12)/3;
x = fmincon(@(x)obj(x,G),x0,[],[],Aeq,beq,lb,ub);
function val=obj(x,G)
Q(1,1)=x(1);
Q(1,2)=x(2);
Q(1,3)=0.0;
Q(1,4)=x(3);
Q(2,1)=x(4);
Q(2,2)=x(5);
Q(2,3)=x(6);
Q(2,4)=0.0;
Q(3,1)=0.0;
Q(3,2)=x(7);
Q(3,3)=x(8);
Q(3,4)=x(9);
Q(4,1)=x(10);
Q(4,2)=0.0;
Q(4,3)=x(11);
Q(4,4)=x(12);
val=sum(reshape((Q^2-G).^2,16,1));
end
Best wishes
Torsten.