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

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?

4 件のコメント

David Hill
David Hill 2021 年 4 月 8 日
What is the X matrix or array? X(i+N+1) appears to exceed the array and X(i+2N+2) is much bigger than the array. Therefore without understanding what X and how you are indexing into it with i and j, I cannot help.
Anthony Gurunian
Anthony Gurunian 2021 年 4 月 8 日
That's my mistake, there is 3*(N+1) variables
Cris LaPierre
Cris LaPierre 2021 年 4 月 8 日
Perhaps I'm getting hung up on symantics here, but it appears you have a single variable, X. Is it correct to say that your array X has 3*(N+1) columns?
Anthony Gurunian
Anthony Gurunian 2021 年 4 月 8 日
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
David Hill 2021 年 4 月 8 日

0 投票

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');

7 件のコメント

David Hill
David Hill 2021 年 4 月 8 日
Even simplier
x=reshape(x,[],3);
f=prod(exp(-beta*(sum(diff(x,1).^2),2)))*sum((x(:,1)-x(:,1)').^2+(x(:,2)-x(:,2)').^2+(x(:,3)-x(:,3)').^2,'all');
Anthony Gurunian
Anthony Gurunian 2021 年 4 月 8 日
Thanks for your response. Sorry for not being clear, but the 'variables' are the columns of X. So you can't say X_i - X_j is X(1:n+1)-X(1:n+1)' for example. I am trying to use the syntax required in 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
David Hill 2021 年 4 月 8 日
It should be very close to the solution. Is X a 1x(3n+3) array? If so, it should work.
Anthony Gurunian
Anthony Gurunian 2021 年 4 月 8 日
編集済み: Anthony Gurunian 2021 年 4 月 9 日
You don't know the number of rows in X when defining this function. Each variable is a collumn vector. The number of rows is defined later in integralN_mc(). It depends on the number of times the function f is called per itteration, but this number is also adapted within integralN_mc() for efficiency.
Rik
Rik 2021 年 4 月 9 日
I don't see an indication that his code depends on a specific number of rows. The number of columns is a different matter, but that is also possible.
David Hill
David Hill 2021 年 4 月 9 日
編集済み: David Hill 2021 年 4 月 9 日
Is there a different f for each row? If not, I don't understand your equation. If so, then just do a loop with my equation. How can you not know the number of rows? X must be defined before this equation is processed.
for k=1:size(x,1)
f(k)=prod(exp(-beta*(diff(x(k,1:n+1)).^2+diff(x(k,n+2:2*n+2)).^2+diff(k,x(2*n+3:3*n+3)).^2)))*...
sum((x(k,1:n+1)-x(k,1:n+1)').^2+(x(k,n+2:2*n+2)-x(k,n+2:2*n+2)').^2+...
(x(k,2*n+3:3*n+3)-x(k,2*n+3:3*n+3)).^2,'all');
end
Or,
for k=1:size(x,1)
X=reshape(x(k,:),[],3);
f(k)=prod(exp(-beta*(sum(diff(X,1).^2),2)))*sum((X(:,1)-X(:,1)').^2+...
(X(:,2)-X(:,2)').^2+(X(:,3)-X(:,3)').^2,'all');
end
Anthony Gurunian
Anthony Gurunian 2021 年 4 月 10 日
I ended up writing a custom function f = Rg(X) and called it using integralN_mc(@(X) Rg(X), ...)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by