**Info**

This question is closed. Reopen it to edit or answer.

# Nesting parentheses issue: any workaround?

1 view (last 30 days)

Show older comments

Greetings. I need to write a function based on a polynomial expression, but MatLab seems to dislike nested parentheses with depth greater than 32.

Since this function would serve as a parameter to a mixed integer nonlinear solver, i cannot use temporary variables since the solver would not recognize them ( i've already tried this approach ). Is there any way to "force" matlab, so that it wouldn't complain?

For instance eval, str2fun, or even m-files return the same error, that is matlab complaining because of nesting depth limit.

Thanks for your attention.

==========================================

Addendum: The question is nontrivial. I thank for your help but you are off target, sorry to point out that.

Hope this may help: let's say you have a variable x that can be expressed as

x(t+1) = x(t)*(polynomial expression here)

when you develop this function you obtain

x(t+1) = x(t)*(x(t-1)*(x(t-2)*(...)))+( remaining polynomial elements)

at this point however I DO NOT have to solve this function. I have to pass this function as a constraint to a solver. In other words I have to define something like

my_func = @(x)( x(t)*(x(t-1)*(x(t-2)*(...)))+( remaining polynomial elements))

where my_func is an argument of another function, thus I don't have to solve the function itself.

However matlab fails when the evaluating my_func. Also, since the solver actually parse the whole expression, i cannot define a sub function, since the solver would not know how to parse it.

Thanks again...

##### 10 Comments

Daniel Shub
on 29 May 2013

### Answers (8)

Jan
on 28 May 2013

It is not only Matlab, but I dislike 32 nested parenthesis also. It is impossible to debug such monsters efficiently.

I cannot imagine a situation, where this is required. Using temporary array should be sufficient also and it does not waste computing time:

a = ((1 + 2) * 3);

% versus:

b = 1 + 2;

a = b * 3;

##### 0 Comments

Daniel Shub
on 28 May 2013

Based on some of your comments to the original question and answers, it seems the "solver" is a custom function that you (or someone else) wrote and not a standard MATLAB function or one available on the FEX. This solver requires an argument "handle_fun" that is in a very particular form such that it can parse the argument as a polynomial. You are then trying to use this function with an extremely large polynomial that exceeds the limit of MATLAB nesting.

There is no way to increase the maximum MATLAB nesting depth (although one could file an enhancement request). Assuming that when your polynomial is represented in the allowable form that minimizes the nesting depth you still exceed the maximum, you cannot use the solver as is. One possibility would be to modify the solver to take the argument in a different and more efficient form. If the code for the solver is not available, then you will need a different solver.

I would guess that any solver that requires arguments in such a crazy format likely is not well designed for the problem at hand.

##### 0 Comments

Walter Roberson
on 27 May 2013

##### 1 Comment

Walter Roberson
on 28 May 2013

Remember you can define utility functions. For example,

Q = @(x) roots([5*x exp(-tan(x)]);

P = @(x) (17 + 35 * sin(Q(x));

fzero(P, [-1 1])

Kwen
on 27 May 2013

##### 0 Comments

Teja Muppirala
on 28 May 2013

I am in agreement with Jan. I cannot recall a single instance where I needed to use 32 parenthesis for anything. If you really need to do something like that, do it recursively, or with some other method.

In your case, I think it can be done quite simply

%%1. Your current method. (I think you meant STR2FUNC not STR2FUN)

t_previous = num2str(1);

time_horizon = 5;

local_vec = [4 -3 2 9 6]; % Just random numbers

for i =1:time_horizon

t_previous = [ '(' t_previous ')*(1 - x(' num2str(i) ')+' num2str(local_vec(i)) ')'];

end

constr = str2func( [ '@(x)(' t_previous ')' ])

%%2. The exact same thing in one line of readable, scalable code

constr2 = @(x) prod(1 - x + local_vec)

%%3. Confirm they are the same

for n = 1:10

x = randn(1,time_horizon)

constr(x)

constr2(x)

end

##### 6 Comments

Walter Roberson
on 29 May 2013

Okay, then... "there is".

Unfortunately for you, your "only two answers are possible" does not allow for statement of what the workaround is.

Jan
on 28 May 2013

##### 0 Comments

NotOfYourBusiness OrMaybeItIs
on 28 May 2013

##### 4 Comments

per isakson
on 29 May 2013

For The MathWorks' sake ask their tech support for help. Obviously, we cannot help you.

Jan
on 29 May 2013

Edited: Jan
on 29 May 2013

To summarize my comments, which might be better in the answers section:

- Modern Matlab versions cannot handle more than 32 nested parenthesis. (You have found out this fact already)
- There is no workaround like a magic flag.
- A promising workaround is a redesign of your program. The created function can get nicer and simultaneously contain less nested expressions (and from my point of view, this means the same).
- You could try to let a computer algebra program perform the simplifications of the string before you apply STR2FUNC.
- If the solver does not accept function handles, perhaps the solver is not sufficient for what you want to do. But when it accepts anonymous functions, function handles are expected to work also.

##### 0 Comments

### See Also

### Community Treasure Hunt

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

Start Hunting!