How to convert a cell array to a symbolic array?

Hey guys,
I have a cell array of size x = cell(max(I(:)), numel(I), numel(I), numel(M), max(M(:))) that each element of this cell array is a matrix. I want to convert it to a symbolic array, and then pass this symbolic array to fmincon(). My objective function is a function of x and I want to min objfun w.r.t. x.
If x was a 2*2 matrix by using x = sym('x', [2 2]) I get
[ x1_1, x1_2]
[ x2_1, x2_2]
But how about the cell array? Any idea?
Thanks in advance.

5 Comments

Could anyone please give me a hint? Thanks!
I don't understand well your problem, but maybe you can use:
or C = { x1_1, x1_2; x2_1, x2_2}
or C = cell([2,2]) and then move x into C with a loop
Thanks for your reply.
I am looking for something like you mentioned above, i.e., C = { x1_1, x1_2; x2_1, x2_2} which is a symbolic array of a 2*2 cell in case each cell contains a scalar.
Do you know if I have the size of C for example (3,3,3,10,2) how I can generate the symbolic array of C? In the case that each cell can contain a vector or a matrix, how would the symbolic array be?
Thanks in advance
I have the feeling that you are not really sure about what you need. But either way, you can always move a symbolic matrix to cell like this:
C{3,3,3,10,2} = {};
C_mat_sym = sym('x', [3,3,3,10,2]);
for i = 1:length(C_mat_sym(:))
C{i} = C_mat_sym(i);
end
And if you need the actual value to hace double as result, use subs:
Thanks for your reply. Appreciate that.

Sign in to comment.

 Accepted Answer

  1. fmincon() does not accept symbolic expressions or functions as any parameter. The only way to pass a symbolic expression or function into fmincon is to parameterize a function, passing the value in as an extra parameter http://www.mathworks.com/help/matlab/math/parameterizing-functions.html . The symbolic expression or function can never be used as x0 or A or b or Aeq or beq or lb or ub, and f and nonlcon require function handles not symbolic values.
  2. Your objective function for fmincon must return something scalar and numeric. You can pass in symbolic values as extra parameters by parameterizing, but whatever computation you do must return scalar single() or double()
  3. You indicate that each element of the cell array contains a matrix and that you want to convert the cell to symbolic. However, symbolic arrays cannot contain sub-arrays or vectors or cells: symbolic arrays must have each element be a scalar symbolic value and the array overall must be cuboid. If you knew that each cell entry was the same size, then you could cat() everything along higher dimensions after some reshape() [permute would probably be easier])
  4. See matlabFunction, and pay close attention to the 'vars' option.

7 Comments

Thank you so much for your reply. I found it very helpful.
1- Yes, I know that the fmincon() does not accept symbolic expression. What I want to do is define the variable as a symbolic expression and find out the objective function accordingly. Then, as you taught me in another post, I will use the matlabFunction() to convert the symbolic function f to a function handle and then pass it on the fmincon().
2- I agree. Here, for another project my objective function f is a cell of size (I, J, J, K, M). I saw in another post that you suggested using gamultiobj() for the case objective function is not scalar. However, I couldn't figure out how to implement that.
I am minimizing an objective function with respect to variable X. The variable has the following format
X = cell(I, J, J, K, M) and inside each cell I have a Nt*Nr matrix. The goal is to find the optimal values of each matrix.
I defined the symbolic array of X (I am not sure if I am doing it correctly, though) and pass it to my objective function to calculate the objective function as follows:
X = cell2sym(cell(max(I(:)), numel(I), numel(I), numel(M), max(M(:))));
objfun = f(X)
but, I get the following error: Brace indexing is not supported for variables of this type.
while I am able to get the value of f(X0) without any issue (X0 is an initial value of X). Could you please kindly explain what the problem is? Any suggestion?
Previously, when my optimization variables where scalar I use the fmincon() as follows
ptilda = sym('ptilda', [I M J K]);
snr = Calculate_snr(ptilda, .... some other parameters...);
rate = CalculateLAA_rate(snr, ... some other parameters...);
objfun_symbExpression_ptilda = -Calculate_sumrate(rate, ...some other parameters,...);
objfun_AnonymousFunction_ptilda = matlabFunction(objfun_symbExpression_ptilda, 'Vars', {ptilda});
objfun_ptilda = @(ptilda) objfun_AnonymousFunction_ptilda(ptilda);
now, the variable is a cell and inside each cell I have a matrix with diferent sizes. I can assume that the size of matrices are the same. Any suggestion how I can implement that? You mentioned about the cat(), but I am not sure if I quite you. Thanks!
cell() creates a cell array in which the contents are all empty. cell2sym() of that creates an empty sym array.
Thanks for your reply. So, what is your suggestion? What should I do to solve this issue? Thanks!
This is the algorithm I am trying to implement. Any idea how can I make it work?
%%
%%
c0 = zeros(IL, JL, K, M); %initial value for variable c
p0 = ones(JW , K); %initial value for variable p
ptilda0 = zeros(IL, M, JL, K);%initial value for variable ptilda
v0 = cell(IL, JL, JL, K, M); %initial value for variable v. Each cell contains a matrix of size Nt*Nr
iteration = 1;
epsilon = 1e-2;
while 1
%%%%%% First Step
c = sym('c', [IL, JL, K, M)];
objfun_c_symbExpression = f(c, p0, ptilda0, v0);
objfun_c_AnonymousFunction = matlabFunction(objfun_c_symbExpression, 'Vars', {c});
objfun_c = @(c) objfun_c_AnonymousFunction(c);
opts = optimset('Display','iter','Algorithm','sqp','MaxFunEval',inf,'MaxIter',Inf);
[c_opt,fval_c,flag_c] = fmincon(objfun_c,c0,A_c,b_c,[],[],lb_c,ub_c,[],opts); % I have defined all A_c, b_c, lb_c, ub_c
%%%% Second Step
p = sym('p', [JW , K]);
objfun_p_symbExpression = f(c_opt, p, ptilda0, v0);
objfun_p_AnonymousFunction = matlabFunction(objfun_p_symbExpression, 'Vars', {p});
objfun_p = @(p) objfun_p_AnonymousFunction(p);
opts = optimset('Display','iter','Algorithm','sqp','MaxFunEval',inf,'MaxIter',Inf);
[p_opt,fval_p,flag_p] = fmincon(objfun_p,p0,[],[],[],[],lb_p,ub_p,nonlcon_p,opts);
%%%% Third Step
ptilda = sym('ptilda', [IL, M, JL, K]);
objfun_ptilda_symbExpression = f(c_opt, p_opt, ptilda, v0);
objfun_ptilda_AnonymousFunction = matlabFunction(objfun_ptilda_symbExpression, 'Vars', {ptilda});
objfun_ptilda = @(ptilda) objfun_ptilda_AnonymousFunction(ptilda);
opts = optimset('Display','iter','Algorithm','sqp','MaxFunEval',inf,'MaxIter',Inf);
[ptilda_opt,fval_ptilda,flag_ptilda] = fmincon(objfun_ptilda,ptilda0,A_ptilda,b_ptilda,[],[],lb_ptilda,ub_ptilda,nonlcon_ptilda,opts);
%%%% Fourth Step (I have problem in implementing this part)
v = sym('v', ....);
objfun_v_symbExpression = f(c_opt, p_opt, ptilda_opt, v);
objfun_v_AnonymousFunction = matlabFunction(objfun_v_symbExpression, 'Vars', {v});
objfun_v = @(v) objfun_v_AnonymousFunction(v);
opts = optimset('Display','iter','Algorithm','sqp','MaxFunEval',inf,'MaxIter',Inf);
[v_opt,fval_v,flag_v] = fmincon(objfun_v,v0,A_v,b_v,[],[],lb_v,ub_v,nonlcon_v,opts);
%% Calculate the f with the computed varaibles
f_evolution = [f_evolution, f(c_opt, p_opt, ptilda_opt, v_opt)];
%% termination criterion
if size(f_evolution, 2)>2
err = abs(f_evolution(end) - f_evolution(end - 1) );
if err < epsilon
break,
end
end
iteration=iteration+1;
end
You never write into v0 so it is not clear why you have it.
You pass it to f, but I do not see any code for f.
Eventually in step 4, you pass v0 to fmincon in the initial value location. fmincon cannot accept a cell array in the initial value location.
Thanks Walter for your reply.
f is a complex function that calls several functions that's why I didn't provide any code for that.
f is a function of (v, p, ptilda, c), so v0 shows up in all steps. In Step 4, I would like to figure out what is optimal v while its initial value is v0.
Since v is a cell array, I don't know how to get its symbolic array to generate the objective function.
You mentioned that fmincon cannot accept a cell array in the initial value location. In this case, the the variable is a cell array, is there any other function that can be used instead of fmincin()?
Thanks in advance
No. In every optimizer that MATLAB provides, the variables being optimized and the initial value must be numeric. Details vary about whether the initial value must be a row vector or column vector or can be a 2D array, but numeric is the only possibility.
Ah, wait... there just might be some optimizer hidden in MuPAD if you knew exactly what to ask for using feval(symengine) or evalin(symengine), and in that case the initial conditions would be symbolic numbers rather than pure numeric. But not cell.
If your v0 is an N x M cell, each member of which is a P x Q matrix, then cell2mat(v0) and pass that or that reshaped to a vector. But this requires that some content having been written into v0.
Thank you so much for your reply. It was very helpful. Let me see if I can solve the issue with what you suggested. Thanks again

Sign in to comment.

More Answers (0)

Asked:

on 29 Apr 2019

Commented:

on 3 May 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!