# How to write sum(exp(-x))

Hi, in my code, I want to calculate the sum of a sequential exp(-x_i), and minimize the it in the objective function. Code is similar as what shown below:

``````cvx_begin
y = [];
for i = 1: n
y = [y c(i) * exp(-x(i))];
f = sum(y) + b
minimize(b);
cvx_end
``````

However, an error appeared when summing up y, shown as
Error using cvx/sum (line 56)
Disciplined convex programming error:
Illegal addition encountered (e.g., {convex} + {concave}).

Moreover, when I added up y(1) and y(2), i.e., y(1)+y(2), in the command line, an error saying that
Error using + (line 83)
Disciplined convex programming error:
Illegal operation: {log-concave} + {log-concave}

I am sure that sum(exp(-x_i)) is a convex function, then, why this error appeared and how I can solve it?

Maybe some element of `c` is negative.

It seems that the problem happens because of exp(-x(1))+exp(-x(2)). Then, how can I add up a sequence of exponential functions in CVX?

CVX 2.1 accepts that without error message. Did you read my previous answer?

If you are using CVX 3.0beta, please switch to CVX 2.1.

I checked that the current version installed in my laptop is CVX2.1.

Execute
`cvx_version`

Thank you, I just used that and checked the version is CVX2.1

This executes and solves without error for me:
`cvx_begin;variable x(2);minimize(exp(-x(1))+exp(-x(2)));x>=0;cvx_end`

I found the difference. Here in my code, x is a convex expression,

`x(i) = sum(sum(square(y(i)-y0)));`

where `y(i)` is a matrix variable and `y0` is some standard matrix. Then, `exp(-x(i))` turns to be a log-concave expression. How should I write if I want to achieve this?

Minimizing a log-concave function is non-convex, and not convertible to convex.

Minimizing a log-convex function is accepted by CVX, because it can be and is converted by CVX into a convex problem.

CVX accepts this:
`cvx_begin;variable x(2);minimize(exp(x(1)^2)+exp(x(2)^2));cvx_end`

It rejects this
`cvx_begin;variable x(2);minimize(exp(-x(1)^2)+exp(-x(2)^2));cvx_end`
Error using + (line 83)
Disciplined convex programming error:
Illegal operation: {log-concave} + {log-concave}

Thank you, yes you are right. Then, how to write `exp(-abs(x1-x0))+exp(-abs(x1-x0))` in cvx, where `x1` and `x2` are variables and `x0` is a constant. I think this expression is convex.

That is adding two- log-concave expressions, which is non-convex and is not allowed in CVX.

Before proceeding further with CVX, please carefully read Why isn't CVX accepting my model? READ THIS FIRST!