Why not simply

The problem in fact is not the function but its argument. i is a range and a range is NOT a vector but rather some sort of implicit loop. So f(i) is neither a range nor a vector - its kind of an invalid object suitable only for display. You can see this if you try to assign f(i) a variable:

But what you did works OK if you turn "i" into a true vector. The simplest way to do so is by using an immediate inline evaluation after the definition:

But that method of turning a range into a vector is undocumented and so is subject to be changed without notice.
So a more "legal" way could be this

Note that I use vectorization when I call f(v).
You will see why when you change the x^2 to x*x in the function definition and don't use vectorization 🙂
BTW, ranges should only be used on three occasions:
1) to index vectors and matrices
2) to provide a range of values for the abscissa in a 2D plot
3) in a program when you use a for-loop
Nothing else!
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.


