How to use integral function in cvx instead of fmincon in matlab

Hi all,

I wanna use integral function in cvx instead of fmincon in matlab.

here is a simple example.
3 case

Case.1 : fmincon with integral ( work )
Case.2 : cvx without integral ( work ), handed calculation
Case.3 : cvx with integral ( doesn’t work )

how can I change it?

% Case.1 : fmincon with integral ( work )

x0 = 0;
A = 1; b = 40; %constraint Ax <= b
fun = @(x)func(x);
[x optval] = fmincon(fun,x0,A,b)

function result = func(x)
func_temp =@(y) -x+4+y;
result=integral(func_temp,-5,5);
end

%optval = -360
%x = 40

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Case.2 : cvx without integral ( work )

cvx_begin quiet
variable x
minimize(func(x))
x <= 40 %constraint Ax <= b
cvx_end %result => x and cvx_optval

opt_val = cvx_optval
x’

function result = func(x)
result = -10*x+40; % handed calculation
end

%optval = -360
%x = 40

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Case.3 : cvx with integral ( doesn’t work )

cvx_begin quiet
variable x
minimize(func(x))
x <= 40 %constraint Ax <= b
cvx_end %result => x and cvx_optval

opt_val = cvx_optval
x’

function result = func(x)
func_temp =@(y) -x+4+y;
result=integral(func_temp,-5,5);
end

Perform the integral symbolically (by hand or with assistance of symbolic integrator), and enter the result into CVX, presuming it is in compliance with CVX’s rules. If you can’t do this, CVX is not the right tool for your problem.

Thank you for your reply,

As your advise, I am trying to change my function.

But it has another problem, I can’t find information about it.

there are two problems,

First : covert ‘syms’ to ‘cvx’ (object function needs ‘cvx’ return)
Second : How can I connect ‘syms x’ to ‘variable x’?

I really appreciate your feedback.

%%% Before change.
function result = func(x)
func_temp =@(y) -x+4+y;
result=integral(func_temp,-5,5);
end

%%%After change
function result = func(x)
syms x y
func_temp = -x+4+y;
result = int(func_temp,y, -5, 5);
end

You will need to get whatever result you get from symbolic calculations into a form which complies with CVX’s syntax and rules. So if your inttegral is a function of x, declare x to be a CVX variable, then write out the function (call the function some time after declaring x a CVX variable) or expression using the character x, so that it will look “normal” to CVX.

It is a non-CVX programming matter how you accomplish that.

yes, I think so, it is a non-CVX programming.

it is just how change to another expression.

my integral is function of y…

um… I really don’t know how I figure out.

Is there some reference guide or example, website?

clc;
clear all;

%%cvx
cvx_begin quiet
variable x
minimize(func(x))
x <= 40 %constraint Ax <= b
cvx_end %result => x and cvx_optval

opt_val = round(cvx_optval,10)
x’

function result = func(x)
syms y
func_temp = -x+4+y;
result = int(func_temp,y, -5, 5);
end

The Symbolic Math Toolbox is out of scope of this forum, so you will need to get help elsewhere on how to process solutions from it. But you can first try to manually create the CVX input. If your function does not obey CVX rules, you will be out of luck with CVX. That will certainly be the case if it is not convex.

I thought that the cvx expression is used in only cvx programming. So, I thought I could get information here.

OK, I will try to find information in Symbolic Math Toolbox

Thank you!!

The point is that you can’t just minimize an arbitrary user-defined function with CVX. You have to be able to write it in CVX form yourself. You are integrating a linear (?) function so it should not be hard. It has to end up convex, too.