Your first problem is the use of numbered variables. That is the worst mistake that new coders make. INSTEAD, USE VECTORS. USE MATRICES.
So you might have a for loop, with ind as a variable, where we might see lines like this:
for ind = 1:numfarms
xR(ind) = X(84+ind)*X(104 + ind);
...
end
There still is absolutely no need for symbolic here. Just a need to learn to use vectors, and to learn how to use an objective function for fmincon.
Even better than using a loop, you need to start learning how to use MATLAB as it is designed to be used. So in one line, we can do this:
xR = X(85:104).*X(105:124);
Everything I said in my comment is still true. When you know only one way to write code, you use it, for everything. Every problem looks like a nail when you have only a hammer in your toolbox. So you know how to use syms. And since you think you don't know what those variables are, then you think you need to use symbolic variables. Then you learned how to created numbered symbolic variables, like X86, X87, etc.
This path will lead you into programming hell. It is a place you don't want to live in (it gets really hot in programming hell, I think they have poor air conditioning), but you don't need to go there. What you will find is all those numbered variables will create bugs in your code. All you need to do is mistype one line and you will never find the bug. And as you have found, wanting to type all those lines for every variable you need to create is programmatically abhorrent.
So instead, work with vectors. An optimization tool looks at your objective as a black box. It passes in a guess at the unknowns to your objective. Then you can unpack that vector of unknowns, computing the various parts of your objective.
xR = X(85:104).*X(105:124);
avg_xR = [sum(xR(1:19)), sum(xR(2:20))]/10;
...
Still, no need for anything symbolic. And certainly no need for a long list of numbered variables. Learn to use MATLAB as a language of matrices, vectors, etc. Even the name MATLAB invokes the phrase "MATrix LABoratory", as it should.
Best Answer