Question: I'm looking to plot the Alpine1 function using Python. The function looks like this:

Alp(x\_1,…,x\_n )= ∑\_(i=1)^n〖|x\_i \* sin(x\_i )+1.01x\_i |〗

[![ALpine1](https://i.stack.imgur.com/fnKr2.png)](https://i.stack.imgur.com/fnKr2.png)

So the function sums (`from i=1 to n`) the absulute of `xi*sin(xi) +1.01xi`. Without the summation the function in Python looks like this: 

```
def f(x):
   return (abs(x * np.sin(x) + 1.01 * x))

```

However, here I used x instead of xi and the summation is left out. How do I correctly implement the summation `from i=1 to n`? Simply using sum() gives just one value, while I require an array. 

I think I need to look in the direction of a `for i in range(1,...)` type of thing but I haven't been able to figure it out. 

Thanks in advance for the one that can help me!

Answer: 
Summation in Python and other languages in general is usually implemented using a `for` loop.
In the formula above, you are looping over all the elements x1, x2, x3 ... xn. 

Now to store such values we use an array or specifically in python a list which is basically a collection of data (where the order matters).

```
x = [x_1, x_2, x_3 ... x_n]

```

that will be the input to your function `f(x)`. To loop over all the values in x we use the syntax.

```
for x_i in x:
    print(x_i)

```

this will print all the values i.e. x\_1, x\_2, x\_3 ... x\_n.

now we'll declare a variable `summation = 0` and store the value of the calculation you need as you mentioned in your question in it and return the value of summation after it has looped through all values of x.

```
summation = 0
for x_i in x:
    summation = summation + (abs(x_i * np.sin(x_i) + 1.01 * x_i))
#Note we returned the summation without the indentation
#so that it is out of the for loop.
return summation

```
Thanks I think I understand a lot better already!

I was wondering another thing actually. Alpine1 is a n-dimensional function, so suppose I want to take the "2D version" the function would directly result in:

```
def Alpine(x1, x2): 
return (np.abs(x1 * np.sin(x1) + 1.01 * x1)+ np.abs(x2 * np.sin(x2) + 1.01 * x2))

```

How do I plot it now? Still two variables (arrays actually) are required (x1 and x2) but `np.apply_along_axis` only works for 1d I think. How should I handle this?

*Maybe using x for x1 and y for x2 makes it easier to understand*