title: Error messages when attempting to plot a complicated function in R
body: <p>I have written some code to demonstrate the benefits of diversifying across a range of assets when planning a portfolio.</p>
  
  <pre><code>#Portfolio variance
  y &lt;- function(x) {
    vars &lt;- rnorm(n = x, mean = 0.7, sd = 0.07)
      # rho generating function:
      ranrho &lt;- function(z) {
      c(rep(0, times = z), runif(n = x-z, min = -1, max = 0.9)) + 0.1
      }
      #you get the lower diagonal of the covar matrix:
      rhold &lt;- sapply(1:x, ranrho)
      rhold
      #and the full corr matrix
    corr &lt;- rhold + t(rhold)
    corr &lt;- corr * sqrt(vars)
    corr &lt;- t(t(corr) * sqrt(vars))
    #And the variance-covariance matrix:
      vcov &lt;- corr + if(x==1) {vars
      } else {diag(vars)}
    #And recover the portfolio variance through the following equation
      portvar &lt;- (1/x^2)*sum(vars) + if(x==1) { as.numeric(0)
    } else {(1/x^2)*(sum(corr)/2)}
    return(portvar)
  }
  
  y(1)
  y(5)
  y(10)
  y(20)
  y(50)
  y(100)
  
  plot(1:100, y(1:100))
  
  </code></pre>
  
  <p>y returns a single value for each value of x that is inputted. x is an integer representing number of assets and y is the consequent variance of the portfolio. I have examples for different values of x and each return a sensible value for y with no issues.</p>
  
  <p>My problem is that I'm getting the following error when trying to plot my function:</p>
  
  <p>Error in rhold + t(rhold) : non-conformable arrays
  In addition: Warning message:
  In 1:x : numerical expression has 100 elements: only the first used</p>
  
  <p>Firstly, since the code works when I write y([whatever]), rhold and t(rhold) are clearly conformable. I'm not sure what the second error means. Am I using the plot function wrong?</p>
  
  <p>Another issue - I have looked but can't seem to find how you might plot a function which only accepts integer values of x in its domain, but returns values of y on the continuous real line.</p>
  
  <p>Grateful for any help.</p>