title: Solving under/overdetermined systems in R
body: <p>What is the general procedure in solving systems of equations using R (as opposed to manual Gauss-Jordan/Gaussian elimination)?</p>
  
  <p>Must I first determine if the system is determined/under/overdetermined?</p>
  
  <p>If a system is determined, I just use </p>
  
  <pre><code>solve(t(a)%*%a)%*%t(a)%*%b
  </code></pre>
  
  <p>to get <code>$x$</code> in <code>$Ax = b$</code></p>
  
  <p>If it overdetermined or underdetermined, I am not quite sure what to do. I think the above sometimes gives an answer depending on the rank, but the solution is not always unique. How can I get all the solutions? I think if there's no solution, will R just give an error?</p>
  
  <p>Context: I am planning to recommend to my Stochastic Calculus professor that we use R in our upcoming exam (as opposed to tedious calculators/by-hand computation) so I have a feeling only simple functions will do (e.g. solve) for over/underdetermined systems rather than <a href="https://stat.ethz.ch/pipermail/r-help/2007-October/143408.html" rel="nofollow">lengthy programs/functions</a>.</p>
  
  <p>Edit: I tried using <code>solve(a,b)</code>, but I think that still doesn't give me all the solutions.</p>
  
  <p>Here is an underdetermined example (R cannot give an answer since a is not square):</p>
  
  <pre><code>a=matrix(c(1,1,1,3,2,1),byrow=T,nrow=2)
  a
  b=matrix(c(1,2),byrow=T,nrow=2)
  b
  solve(a,b)
  </code></pre>