0

Let's take sample 'y' of length 100 from N(0,1) distribution and function g(x)=x^2+7.

I want to make a graph of function $h(x)=\sum_{i=1}^{100}g(x-y[i])$

So

 y=rnorm(100,0,1)

 g=function(x){x^2+7}

h=function(x){sum(g(x-y))}

And now expressions like

curve(h)

plot(h)

lines(h)

doesnt work, can You please help me how can i draw graph of function h?

Louis
  • 113
  • 3
  • 1
    `h` is a function. You need to generate actual values to plot. Pick a range of values `x`, compute `hx=h(x)`, then `plot(x,hx)` – Aziz Nov 03 '19 at 14:35
  • 3
    Possible duplicate of [How to plot a function curve in R](https://stackoverflow.com/questions/26091323/how-to-plot-a-function-curve-in-r) –  Nov 03 '19 at 14:46
  • Disregarding other issues, given the way you've defined your function `h`, it cannot possibly be used to plot anything, since it returns a single scalar value (namely, the `sum` of whatever is passed to `g`). – Dunois Nov 03 '19 at 15:09
  • This works: `curve(Vectorize(h)(x))` – G. Grothendieck Nov 03 '19 at 15:29

0 Answers0