MATLAB: Dealing with symbolic vectors and function files

#symbolicvectors #functionarguments

I have code to produce a function file of two input variables (currently "x1" and "x2") based on a large summation of terms which are created in part by data from two equal length vectors, "t" and "y" (all terms in "t" and "y" are real positive numbers). The purpose of the function file is to be able to put the final g function into a minimizer.
g=0;
x=sym('x', [1 2]);
for i=1:n
g=g+x(1)*exp(-t(i)/x(2))-y(i)*log(x(1))+y(i)*t(i)/x(2);
end
f=matlabFunction(g,'File','ll1');
Note that in the function for g, x(1)=x1 and x(2)=x2 as a default output of the second line in the first set of code, where both x1 and x2 are symbolic variables
The resulting function file for a particular set of data is:
function g = ll1(x1,x2)
%LL1

% G = LL1(X1,X2)

% This function was generated by the Symbolic Math Toolbox version 7.0.

% 19-Apr-2016 17:44:32

t2 = 1.0./x2;
g = t2.*4.323833333333333e3+x1-log(x1).*2.024e3+x1.*exp(-t2)+x1.*exp(t2.*..................
There are many more terms in g that were not included. I would much prefer to have x components of the function file in the way that I have g defined in the for loop (with x(1) and x(2) not resolved into their individual components, x1 and x2). To fix this, I then manually modify this function so I can minimize it making it a function of only x:
function g = ll1(x)
%LL1
% G = LL1(X1,X2)
% This function was generated by the Symbolic Math Toolbox version 7.0.
% 19-Apr-2016 17:44:32
x1=x(1);
t2 = 1.0./x(2);
g = t2.*4.323833333333333e3+x1-log(x1).*2.024e3+x1.*exp(-t2)+x1.*exp(t2.*...........
Manually modifying the function file each file each time is a bit of a pain and I was wondering if there is a way to eliminate the need for manually modifying the function to get it to agree with the structure that agrees with the minimizer (where f is a function of only one input, "x", in this case with this input being a vector x=[x1,x2]). I'm currently using the minimizing function "fminunc".
A few ideas I had (I dont know enough about matlab to know if there is a way to do any of these things):
1. any way that I could create the initial function for g without ever having to deal with x1 and x2
2. a minimizer that doesnt require the multivariable input in the form of a vector
3. anything that would allow me to modify the function file from a the script file
4. anything that would allow me to convert to using "x=[x1,x2]" before putting the arguments into the function
I'm open to all suggestions that allow me to not modify the function manually. Thanks

Best Answer

I’m not certain of everything you want to do, but optimisation functions in MATLAB all require a vector of parameters as an argument.
You don’t have to re-weite your entire function, just ‘nest’ it inside an anonymous function:
function g = ll1(x1,x2) % Function File
. . .
end
------------------------------------------------------
ll1_fcn = @(x) ll1(x(1),x(2));
X0 = [1; 2];
X_est = fminsearch(ll1_fcn, X0); % Example Using ‘fminsearch’
This is an illustration. I don’t know what optimisation function you want to use.