Suppose we want to solve following optimization problem (it is a PCA problem in this post)
$$ \underset{\mathbf w}{\text{maximize}}~~ \mathbf w^\top \mathbf{Cw} \\ \text{s.t.}~~~~~~ \mathbf w^\top \mathbf w=1 $$
As mentioned the the post, using the Lagrange multiplier, we can change the problem into
$$ {\text{minimize}} ~~ \mathbf w^\top \mathbf{Cw}-\lambda(\mathbf w^\top \mathbf w-1) $$ Differentiating, we obtain $\mathbf{Cw}-\lambda\mathbf w=0$, which is the eigenvector equation. Problem solved and $\lambda$ is the largest eigenvalue.
I am trying to do a numerical example here to understand more about how Lagrange multiplier changed the problem, but getting inconsistent results (the following figure shows geometric solution to the problem, but in order to get the same results, I need to change the sign on $\lambda$ in my code.).
X=iris[,c(1,3)]
X$Sepal.Length=X$Sepal.Length-mean(X$Sepal.Length)
X$Petal.Length=X$Petal.Length-mean(X$Petal.Length)
C=cov(X)
r=eigen(C)
obj_fun<-function(x){
w=as.matrix(c(x[1],x[2]),ncol=1)
lambda=r$values[1]
v=t(w) %% C %% w + lambda (t(w) %% w -1)
return(as.numeric(v))
}
gr<-function(w) {
lambda=r$values[1]
v=2* C %% w + 2lambda* w
return(v)
}
library(optimx)
res=optimx(c(1,2), obj_fun,gr, method="BFGS")`
Could anyone tell me if I am correct in code? and the sign before $\lambda$ should be a positive or negative.
Also sorry about R code in math community, I was trying to ask it in statistics community but got no answer. All the code is about to write down a minimization problem and replace $\lambda$ with the largest eigenvalue. Then solve it using library.

