An implementation issue with MatlabFunction

3 Ansichten (letzte 30 Tage)
Mohammad Shojaei Arani
Mohammad Shojaei Arani am 27 Dez. 2022
Hello,
I have a question which I explain by an example.
function ll = LL(X1,X2,X01,X02,par1,par2,par3,par4,par5,par6)
THE BODY WILL BE EXPLAINED LATER
end
PLEASE NOTE THAT MY ACTUAL PROBLEM IS MORE COMPLICATED THAN THIS. SO, PLEASE GIVE ME GENERAL ADVICES RATHER THAN A SPECIFIC ONES WHICH PROBABLY WORK FOR THIS PROBLEM ONLY. THANKS!
This function is created by the followig commands
syms x y
syms X X0 [1 2]
syms par [1 6]
mu1(x,y)=[par(1)*x^2*y+par(2)*y^3+x^3;par(3)*x^5*y^7+par(4)*y];sigma1(x,y)=[par(5)+x*y -x;y^2*x x*y*par(6)];
V=argnames(mu1);
p=sym2cell(X0);
mu_X0=subs(mu1,V,X0).';mu_X0=mu_X0(p{:});
sigma_X0=subs(sigma1,V,X0);sigma_X0=sigma_X0(p{:});
D=sigma_X0*sigma_X0.'; det_D=simplify(det(D)); inv_D=simplify(inv(D));
dt=1;S=1/dt*(X-X0-mu_X0*dt)*inv_D*(X-X0-mu_X0*dt).';
d=2;LL=-1/2*(d*log(2*pi*dt)+log(det_D)+S);
LL=matlabFunction(LL,'File','LL','vars',[X X0 par],'Outputs',{'ll'}); %Log-Likelihood
My question: I wouldlike to make a slight change to the way this matlabfunction is created so that at the end it would look like the following
function ll = LL(X,X0,par1,par2,par3,par4,par5,par6)
BODY: here, instead of stuff like X1,X2,X01,X02 I would like to have stuff like X(1),X(2),X0(1),X0(2)
end
I find it difficult to implement this (and this is very annoying).
I look forward to your kind help!
Thanks,
Babak

Akzeptierte Antwort

Star Strider
Star Strider am 27 Dez. 2022
Bearbeitet: Star Strider am 27 Dez. 2022
If you want matlabFunction to produce a vector for several arguments, in the 'Vars' argument, enclose them in square brackets to concatenate them.
Example —
syms a b x1 x2 x3
f = a*x1 + b*x2 + x3
f = 
F = matlabFunction(f, 'Vars',{a,b,[x1,x2,x3]})
F = function_handle with value:
@(a,b,in3)in3(:,3)+a.*in3(:,1)+b.*in3(:,2)
.
The ‘x’ arguments will appear as a row vector of ‘in3(1)’, ‘in3(2)’ and ‘in3(3)’ however when you call the function, you can of course name the arguments anything you want. Also, the arguments in the square brackets can be any arguments you want, including all of them. They do not have to be any specific subset.
This is a general illustration, however it will work for any function you choose as a function argument to matlabFunction.
.
EDIT — Corrected typographical errors.
.
  4 Kommentare
Walter Roberson
Walter Roberson am 28 Dez. 2022
This is the advice I gave you in https://www.mathworks.com/matlabcentral/answers/1884182-a-symbolic-implementation-issue#answer_1136137 along with an explanation for why you were having difficulty.
Mohammad Shojaei Arani
Mohammad Shojaei Arani am 28 Dez. 2022
Hi Walter,
I looked at it again. Indeed, a nice and comprehensive answer. You are right. Sorry (I do not remember, but probably I did not understand it then). I admit that I understand math very well but when it comes to programming ...sometimes I really want to punch the screen :-)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by