Piecewise linear optimisation using matlab - matlab

I want to solve the following optimisation problem, whose objective function is independent of x:
minimise t
subject to a_i*x+b_i<=t
over x for all i from 1 to n=100
This problem arised from if I rewrite a piecewise linear optimisation problem to a linear optimisation problem.
Question:
How can I implement this in the matlab using linprog. In linprog I am asked to insert the objective function f as a matrix multiplying with x. Is it possible to have a objective function independent of x? If not, how am I going to implement this?
P.S: I don't know why Mathjax is not working here, I have been looking for how to ask questions with Math formula, but I was not successful. Therefore any correction is welcomed.
Edit:Here is the official document from Matlab for linprog. In it it is stated that the minimalising function has the shape of f^Tx. My problem is just that my minimalising function doesn't take such shape and is in the absence of x. How can I implement this in the Matlab?

You need to reformulate your problem into standard LP, i.e. find the nominal f, A and b as required by MATLAB's function linprog.
Here is the reformulation. From your question it seems x and t are two scalars.
Assume the given a_i's and b_i's are stored in column vectors a and b respectively, then your original problem:
min t, s.t. a * x + b <= t * ones(n,1)
is equivalent to:
min [0; 1]' * [x; t], s.t. [a, ones(n,1)] * [x; t] <= -b.
Thus, just use linprog([0; 1], [a, ones(n,1)], -b), and the solution is [x; t].

Related

Matlab Matrix Minimization

I have the following matrix
R=(A-C)*inv(A+B-C-C')*(A-C');
where A and B are n by n matrices. I want to find n*n matrix C such that the determinant of R is minimized, SO:
C=arg min (det(R));
Is there any function in MATLAB that can handle this problem?
It seems like you are trying to find the minimum of an unconstrained multivariable function. This can probably be achieved with fminunc
fun = #(x)x(1)*exp(-(x(1)^2 + x(2)^2)) + (x(1)^2 + x(2)^2)/20;
x0 = [1,2];
[x,fval] = fminunc(fun,x0)
Note that there are no examples in the documentation where a matrix is used, this is probably because horrendous performance could be expected when trying to solve this problem for a matrix of any nontiny size. (This is not because of matlab, but because of the nature of the problem).
It is also good to realize that this method does not (cannot) guarantee an optimum, only a local optimum.

Matlab equivalent to Mathematica's FindInstance

I do just about everything in Matlab but I have yet to work out a good way to replicate Mathematica's FindInstance function in Matlab. As an example, with Mathematica, I can enter:
FindInstance[x + y == 1 && x > 0 && y > 0, {x, y}]
And it will give me:
{{x -> 1/2, y -> 1/2}}
When no solution exists, it will give me an empty Out. I use this often in my work to check whether or not a solution to a system of inequalities exists -- I don't really care about a particular solution.
It seems like there should be a way to replicate this in Matlab with Solve. There are sections in the help file on solving a set of inequalities for a parametrized solution with conditions. There's another section on spitting out just one solution using PrincipalValue, but this seems to just select from a finite solution set, rather than coming up with one that meets the parameters.
Can anybody come up with a way to replicate the FindInstance functionality in Matlab?
Building on what jlandercy said, you can certainly use MATLAB's linprog function, which is MATLAB's linear programming solver. A linear program in the MATLAB universe can be formulated like so:
You seek to find a solution x in R^n which minimizes the objective function f^{T}*x subject to a set of inequality constraints, equality constraints, and each component in x is bounded between a lower and upper bound. Because you want to find the minimum possible value that satisfies the above constraint given, what you're really after is:
Because MATLAB only supports inequalities of less than, you'll need to take the negative of the first two constraints. In addition, MATLAB doesn't support strict inequalities, and so what you'll have to do is enforce a constraint so that you are checking to see if each variable is lesser than a small number, perhaps something like setting a threshold epsilon to 1e-4. Therefore, with the above, your formulation is now:
Note that we don't have any upper or lower bounds as those conditions are already satisfied in the equality and inequality constraints. All you have to do now is plug this problem into linprog. linprog accepts syntax in the following way:
x = linprog(f,A,b,Aeq,beq);
f is a vector of coefficients that work with the objective function, A is a matrix of coefficients that work with the inequality, b is a vector of coefficients that are for the right-hand side of each inequality constraint, and Aeq,beq, are the same as the inequality but for the equality constraints. x would be the solution to the linear programming problem formulated. If we reformulate your problem into matrix form for the above, we now get:
With respect to the linear programming formulation, we can now see what each variable in the MATLAB universe needs to be. Therefore, in MATLAB syntax, each variable becomes:
f = [1; 1];
A = [-1 0; 0 -1];
b = [1e-4; 1e-4];
Aeq = [1 1];
beq = 1;
As such:
x = linprog(f, A, b, Aeq, beq);
We get:
Optimization terminated.
x =
0.5000
0.5000
If linear programming is not what you're looking for, consider looking at MATLAB's MuPAD interface: http://www.mathworks.com/help/symbolic/mupad_ug/solve-algebraic-equations-and-inequalities.html - This more or less mimics what you see in Mathematica if you're more comfortable with that.
Good luck!
Matlab is not a symbolic solver as Mathematica is, so you will not get exact solutions but numeric approximations. Anyway if you are about to solve linear programming (simplex) such as in your example, you should use linprog function.

nonlinear matrix equation solving in matlab

is it possible to solve below equation in matlab?
A*X+B*exp(X)=C
A, B are square and constant matrices. C is a constant and column matrix.
X is a column matrix which should be found.( exp() acts element by element on X).
If you are looking for a numeric method, you might want to try fsolve
X = fsolve( #(x) A*x + B*exp(x) - C, x0 );
Due to the non-linear nature of the problem you need to provide an initial guess x0 - the quality of which can affect the performance of the solver.

Matlab Second Order Cone Solver That Allows Function Handles

I need to solve the following SOCP in Matlab:
argmin_x ||R*x||_2 s.t. s^H * x = 1 and ||x||_2 < d,
where x is an Nx1 vector and R is an MxN matrix.
CVX can solve this type of problem. However, CVX requires me to give R and does not allow me to instead give a function handle that will return R*x. This is a problem for me since once R becomes large, computing R*x directly takes too long. There exists an efficient algorithm for computing R*x that I would like to take advantage of, so I am hoping that there is another SOCP solver that I could use.

Matlab: Error using ==> mpower

I'm trying to use fsolve in matlab to solve a system of nonlinear equations numerically. Here is a test sample of my program, k1 and R are parameters and x0 is the start point.
function y=f(k1, R, x0)
pair=fsolve(#system,x0);
y=pair(1);
function r=system(v)
int1=#(x) exp(k1*x);
int2=#(x) exp(k1*x^2)/(x^4);
r(1)=exp(v(1))*quadl(int1,0,v(1));
r(2)=exp(k1*v(2))*quadl(int2,v(1),20)*k1*R;
end
end
The strange thing is when I run this program, matlab keeps telling me that I should use .^ instead of ^ in int2=#(x) exp(k1*x^2)/(x^4). I am confused because the x in that function handle is supposed to be a scalar when it is used by quadl. Why should I have to use .^ in this case?
Also I see that a lot of the examples provided in online documentations also use .^ even though they are clearly taking power of a scalar, as in here. Can anybody help explain why?
Thanks in advance.
in the function int2 you have used matrix power (^) where you should use element-wise power (.^). Also, you have used matrix right division (/) where you should use element-wise division (./). This is needed, since quadl (and friends) will evaluate the integrand int2 for a whole array of x's at a time for reasons of efficiency.
So, use this:
function y = f(k1, R, x0)
pair = fsolve(#system,x0);
y = pair(1);
function r = system(v)
int1 = #(x) exp(k1*x);
int2 = #(x) exp(k1*x.^2)./(x.^4);
r(1) = exp( v(1)) * quadl(int1,0,v(1));
r(2) = exp(k1*v(2)) * k1*R*quadl(int2,v(1),20);
end
end
Also, have a look at quadgk or integral (if you're on newer Matlab).
By the way, I assume your real functions int1 and int2 are different functions? Because these functions are of course trivial to solve analytically...
Internally MATLAB will evaluate the function fun for necessary values of x, which is more than one and x is a vector. a and b are only used to describe the limits of integration. From the documentation
fun is a function handle. It accepts a vector x and returns a vector y, the function fun evaluated at each element of x. Limits a and b must be finite.
Hence, you must use .^ to operate on individual elements of vector x.