title: Optimize and plot a function in R
body: <p>I created a model in a Dataset to extimate a price sensitivity, now for each individual I want to extimate an "optimal price" to obtain "maximum income"</p>
  
  <p>The price is >0 and &lt;14. The income is = <code>data$money[i]*sow</code></p>
  
  <p>I want to use the optimize function to find the maximum profit changing the price for each "i". I also would like to have a plot of my function.</p>
  
  <p>My function:</p>
  
  <pre><code>MYF&lt;-function(x,i) {
  y&lt;-exp(predict(ols, cbind(data[i,!names(data) %in% c("spread") ], spread=(x-data$price_of_system[i]))))
  sow&lt;-y/(1+y)
  return(data$money[i]*sow)
  }
  </code></pre>
  
  <p>So I want to look for the <code>x</code>(my_price) that maximize <code>(data$money[i]*sow)</code>  for <code>i=5</code>
  I use</p>
  
  <pre><code>max &lt;- optimize(MYF, tol = 0.0001,lower=0,upper=14, maximum = TRUE, i=5)
  </code></pre>
  
  <p>But R returns: <code>invalid function value in 'optimize'</code>. I'd like to find also the way how to plot that. Thank you</p>