Asked by Susan
on 29 Apr 2019

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.

Answer by Walter Roberson
on 30 Apr 2019

Accepted Answer

- 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.
- 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()
- 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])
- See matlabFunction, and pay close attention to the 'vars' option.

Susan
on 3 May 2019

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

Walter Roberson
on 3 May 2019

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.

Susan
on 3 May 2019

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## Susan (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/459247-how-to-convert-a-cell-array-to-a-symbolic-array#comment_699757

## gonzalo Mier (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/459247-how-to-convert-a-cell-array-to-a-symbolic-array#comment_699803

## Susan (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/459247-how-to-convert-a-cell-array-to-a-symbolic-array#comment_699894

## gonzalo Mier (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/459247-how-to-convert-a-cell-array-to-a-symbolic-array#comment_699957

## Susan (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/459247-how-to-convert-a-cell-array-to-a-symbolic-array#comment_700121

Sign in to comment.