Why doesn't my code for extracting the first principal component work? Following the Wikipedia article on PCA, I extract the weights of the first principal component by calculating
$$\textbf{w}_1 = \mathrm{\arg\ max} \left\{ \frac{\textbf{w}^\intercal \textbf{X}^\intercal \textbf{X} \textbf{w}}{\textbf{w}^\intercal \textbf{w}} \right\}.$$
However, my R code for doing so with doesn't seem to correspond with the output of R's prcomp() function.
# Set seed
set.seed(1)

# Initialize and scale data
X = diag(10)
X = scale(X)

# Define objective function
objec = function(w){ (t(w)%*%t(X)%*%X%*%w)/(t(w)%*%w) }

# Run optimization with Nelder--Mead method
o = optim(c(0, 0, 1, 0, 0, 0, 0, 0, 0, 0), objec, 
          control=list(maxit=10000, fnscale=-1), method="Nelder-Mead")

# Check output
print(o)

# Print out output of optimization
o$par

# Print out output of prcomp()'s first principal component
prcomp(X)$rotation[, 1]

What's going on?
 A: What you're using is an absolutely horrid and unreliable method for computing the eigenvector corresponding to the largest eigenvalue of $X^TX$. Both in formulation and in choice of optimization algorithms (Nelder-Mead? Seriously? This is the 21st century.)
Your optimization may not have converged, and if it did, it, it may have converged to the wrong answer.
If it did converge to the solution of the problem as input to the optimizer, it is only unique to within a scale factor applied to the solution w.  This lack of uniqueness is one of only several things complicating your optimization.  If you divide each component of your "optimal" w by $\sqrt{(w^Tw)}$, then it will have unit norm, as does the output of prcomp(X)$rotation[, 1], and is unique to within a scale factor of +/-1, and it is "arbitrary" which of these (+/-1) is chosen.
So in short, if your solution w, divided by $\sqrt{(w^Tw)}$ matches the output of prcomp(X)$rotation[, 1], or the negative of this output, then you have obtained agreement.  If it doesn't, then your solution is wrong.
If you provide the (+ or -) unit norm eigenvector corresponding to the largest eigenvalue of $X^TX$ as a starting value to your optimizer, hopefully it will "converge" to the correct solution, i.e., stay roughly where it started.  As your starting point moves further from the solution, the optimizer will have more difficulty converging to the correct solution.  I don't know what starting point your optimization is using, but note that w equal to the vector of zeros, which is the most common default value of starting point, could be problematic given your objective function.
Did you run optim with highest (most detailed) level of information output?  If not, you should. What does it say? Not that I would trust much coming from Nelder-Mead regarding convergence.
The point of your whole exercise should be only pedagogical, i.e., to illustrate why this is not a good approach, and you should use ready made, purpose-built algorithms (at least at the eigenvector or preferably singular vector level, if not principal component) for actual computation in which you want to reliably get the answer.
