Create a general function or script, where the user can specify the number of variables, which is both symbolic and possible to evaluate with fminunc?

2 Ansichten (letzte 30 Tage)
Is it possible to create a general function where the user can specify the number of variables between each execution? The variables would have the same relation between themselves and their neighbours e.g. x2 would have the same relation with x1 and x3 as x3 would with x2 and x4 and so forth. The aim is to refine the partition without having to hard code the function.
It would help a lot if the function was symbolic since I need to differentiate it, I need the Hessian, but the function also has to be able to be evaluated with fminunc, I need to find the values of the variables,x, which give the function its maximum.
I have included a sample of the code to give you an idea, I would like to be able to extend it to an arbitrary high xN.
function q=defOrnUhl(x0,xT,t0,T,theta)
m=10; % number of intervals
deltaT=(T-t0)/m;
theta=theta;
x0=x0;
xT=xT;
vpaPi=vpa(pi);
syms x1 x2 x3 x4 x5 x6 x7 x8 x9 % the number of variables are m-1
q=symfun((-1/2*log(2*vpaPi*deltaT)-1/2*(((x1-(1-theta*deltaT)*x0))^2)/deltaT...
-1/2*log(2*vpaPi*deltaT)-1/2*(((x2-(1-theta*deltaT)*x1))^2)/deltaT...
-1/2*log(2*vpaPi*deltaT)-1/2*(((x3-(1-theta*deltaT)*x2))^2)/deltaT...
-1/2*log(2*vpaPi*deltaT)-1/2*(((x4-(1-theta*deltaT)*x3))^2)/deltaT...
-1/2*log(2*vpaPi*deltaT)-1/2*(((x5-(1-theta*deltaT)*x4))^2)/deltaT...
-1/2*log(2*vpaPi*deltaT)-1/2*(((x6-(1-theta*deltaT)*x5))^2)/deltaT...
-1/2*log(2*vpaPi*deltaT)-1/2*(((x7-(1-theta*deltaT)*x6))^2)/deltaT...
-1/2*log(2*vpaPi*deltaT)-1/2*(((x8-(1-theta*deltaT)*x7))^2)/deltaT...
-1/2*log(2*vpaPi*deltaT)-1/2*(((x9-(1-theta*deltaT)*x8))^2)/deltaT...
-1/2*log(2*vpaPi*deltaT)-1/2*(((xT-(1-theta*deltaT)*x9))^2)/deltaT), [x1 x2 x3 x4 x5 x6 x7 x8 x9]);
end

Antworten (1)

Karan Gill
Karan Gill am 29 Jun. 2017
Bearbeitet: Karan Gill am 17 Okt. 2017
  • To use fminunc, first convert the Symbolic function to a MATLAB function using https://www.mathworks.com/help/symbolic/matlabfunction.html
  • EDIT: For a variable number of input arguments, as Walter says below, symfuns can't do that. However, it seems like you know the number of variables in "m". Perhaps, you could declare an array of symbolic variables as below:you could pass in the maximum number of variables you think you'd need, and then differentiate w.r.t. only the required variables. Something like:
>> m = 10; % some number
>> vars = sym('v%d',[1 m])
vars =
[ v1, v2, v3, v4, v5, v6, v7, v8, v9, v10]
>> f = symfun(prod(vars),vars)
f(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) =
v1*v2*v3*v4*v5*v6*v7*v8*v9*v10
>> diff(f,vars(10))
ans =
v1*v2*v3*v4*v5*v6*v7*v8*v9
  2 Kommentare
Walter Roberson
Walter Roberson am 29 Jun. 2017
It is effectively not possible for symbolic functions to have variable numbers of arguments.
Karan Gill
Karan Gill am 5 Jul. 2017
Sorry, I misunderstood the question. Thanks for pointing that out. I'll edit my answer to something that's more hopefully more helpful.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by