# Defining an anonymous function with 3*(N+1) variables

59 views (last 30 days)
Anthony Gurunian on 8 Apr 2021 at 18:44
Commented: Anthony Gurunian on 10 Apr 2021 at 15:18
I need to define a function
f = @(X)...
where X has 3*(N+1) variables in the form: X(:,1), X(:,2), ... X(:,3*(N+1)) . The problem is that the expression for the function is very long. However, there is a pattern which I want to take advantage of.
The function is Notice that the function can be expressed in terms of summations and products. I want to take advantage of this fact so that I don't have to manually type out the products and sums. Is there any way of doing this using for loops?
Anthony Gurunian on 8 Apr 2021 at 21:44
Yes that is correct. I'm trying to use the syntax required here: https://www.mathworks.com/matlabcentral/fileexchange/53477-monte-carlo-integration
" f is a vectorized function a la f=@(X)sin(X(:,1))+cos(X(:,2))"

David Hill on 8 Apr 2021 at 19:29
f=prod(exp(-beta*(diff(x(1:n+1)).^2+diff(x(n+2:2*n+2)).^2+diff(x(2*n+3:3*n+3)).^2)))*sum((x(1:n+1)-x(1:n+1)').^2+(x(n+2:2*n+2)-x(n+2:2*n+2)').^2+(x(2*n+3:3*n+3)-x(2*n+3:3*n+3)).^2,'all');
Anthony Gurunian on 10 Apr 2021 at 15:18
I ended up writing a custom function f = Rg(X) and called it using integralN_mc(@(X) Rg(X), ...)