function [eta_trj,Q] = optimize_Trajectory_M_2_1(A,Qr,P,N,K,M,sigma2,rho,H,w,Smax,dmin)
%UNTITLED10 Summary of this function goes here
%   Detailed explanation goes here



cvx_begin quiet
        cvx_solver sedumi
        variable Q(N,2,M)
        variable eta_trj
        variable S(N,K,M)

%         maximize eta_trj 
        maximize (min(rate_convex_trajectory_M_2_1(Q,A,Qr,P,N,K,M,sigma2,rho,H,w,eta_trj,dmin,S)))
        
        subject to 
            % dmin constraint
            for n = 1:N
                dmin^2 <= norm(Qr(n,:,1)-Qr(n,:,2),2)^2 + 2*(Qr(n,:,1)-Qr(n,:,2))*(Q(n,:,1)-Q(n,:,2))' ; 
            end 
            % S "slack variable constraint"
            
            for n = 1:N 
                for m = 1:M 
                    for k  = 1:K 
                        S(n,k,m) <=  norm(Qr(n,:,m)-w(k,:),2).^2 + 2*(Qr(n,:,m)-w(k,:))*(Q(n,:,m)-w(k,:))' ; 
                        S(n,k,m) >= 0  ;                        
                    end
                end
            end

            
            Q(1,:,1) == Q(N,:,1) ;
            Q(1,:,2) == Q(N,:,2) ; 
            
            for n = 1:N-1
                for m = 1:M
                norm(Q(n+1,:,m)-Q(n,:,m),2) <= (Smax) ;
                end
            end

                    
cvx_end

eta_trj = min(rate_convex_trajectory_M_2_1(Q,A,Qr,P,N,K,M,sigma2,rho,H,w,eta_trj,dmin,S));

end

