An implementation issue with MatlabFunction
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mohammad Shojaei Arani
el 27 de Dic. de 2022
Comentada: Mohammad Shojaei Arani
el 28 de Dic. de 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
0 comentarios
Respuesta aceptada
Star Strider
el 27 de Dic. de 2022
Editada: Star Strider
el 27 de Dic. de 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 = matlabFunction(f, 'Vars',{a,b,[x1,x2,x3]})
.
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 comentarios
Walter Roberson
el 28 de Dic. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Conversion Between Symbolic and Numeric en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!