필터 지우기
필터 지우기

Matrix multiplication of n function handles

조회 수: 2 (최근 30일)
Sascha Wolter
Sascha Wolter 2016년 2월 19일
댓글: Georgios Koutsakis 2019년 10월 3일
Hello everybody,
i try to fit data to a function by using fminsearch and a function handle. Therefor i need the following code:
clear;
format compact;
Lambda=@(x,k) x(1)^x(2)*x(3)*exp(-0.2*k);
PartTransferMatrix{1,1}=@(x,k) Lambda(x,k)*exp(1/Lambda(x,k+1)-1/Lambda(x,k));
PartTransferMatrix{1,2}=@(x,k) Lambda(x,k)*exp(-(1/Lambda(x,k+1)+1/Lambda(x,k)));
PartTransferMatrix{2,1}=@(x,k) Lambda(x,k)*exp(1/Lambda(x,k+1)-1/Lambda(x,k));
PartTransferMatrix{2,2}=@(x,k) Lambda(x,k)*exp(-(1/Lambda(x,k+1)+1/Lambda(x,k)));
TransferMatrix=@(x)prod(PartTransferMatrix(x,1:100));%Not working here
x are the parameters later on for the fit and k is a position. The position dependend Funktion Lambda is used to calculate the also position dependen Partial Transfer Matrix "PartTransferMatrix" in dependence of the fit parameters and the position.
The problem arises in the next step: I need to calculate the product for k=1:N of PartTransferMatrix_k, which is again a 2x2 matrix. The problem is, that first i dont know how to calculate the produkt of N matrices and how to keep the TransferMatrix a function handle. As far as i know a function handle is not expanded until it is called later. So the function handle "TransferMatrix" includes a variable "k", which later on is not known.
Short summary: I want to do a matrix multiplication of a 2x2 function handle matrix for k=1:N, e.g.N=100.
I hope you can help me.
Thank you and kind regards,
Sascha Wolter
  댓글 수: 1
Stephen23
Stephen23 2016년 2월 19일
편집: Stephen23 2016년 2월 19일
Note that you do not have "a function handle", but a cell array of function handles. These are two quite different things!

댓글을 달려면 로그인하십시오.

답변 (1개)

Stephen23
Stephen23 2016년 2월 19일
편집: Stephen23 2016년 2월 19일
Solution
You can define an anonymous function inside of another anonymous function:
@(f)@(x)f(x,1:100)
which we can apply to your code inside a cellfun call:
Lambda = @(x,k) x(1)^x(2)*x(3)*exp(-0.2*k);
PartTransferMatrix{1,1} = @(x,k) Lambda(x,k).*exp(+(1./Lambda(x,k+1)-1./Lambda(x,k)));
PartTransferMatrix{1,2} = @(x,k) Lambda(x,k).*exp(-(1./Lambda(x,k+1)+1./Lambda(x,k)));
PartTransferMatrix{2,1} = @(x,k) Lambda(x,k).*exp(+(1./Lambda(x,k+1)-1./Lambda(x,k)));
PartTransferMatrix{2,2} = @(x,k) Lambda(x,k).*exp(-(1./Lambda(x,k+1)+1./Lambda(x,k)));
TransferMatrix = cellfun(@(f)@(x)f(x,1:100),PartTransferMatrix,'UniformOutput',false)
where TransferMatrix is a cell array of function handles, each one is the function of each cell of PartTransferMatrix calculated for the values x and 1:100. The functions can be called simply:
TransferMatrix{1,1}(1:3)
Note that I also had to convert the division and multiplication operations in PartTransferMatrix to be element-wise ones, otherwise it throws an error. You might also want to check the + and - signs and your bracketing in the PartTransferMatrix functions.
How do you intend to use fminsearch with four function handles in a cell array?
Alternative without a Cell Array
The use of cell array and multiple function handles might make things more complicated and slower. Here is an alternative method that does not use a cell array and calculates one 3D array with all of the values:
PartFun = @(x,k) bsxfun(@times, Lambda(x,k), exp(...
bsxfun(@rdivide, [+1,-1;-1,+1], Lambda(x,k+1)) + ...
bsxfun(@rdivide, [-1,-1;-1,-1], Lambda(x,k))));
TranFun = @(x) PartFun(x,reshape(1:100,1,1,[]));
Again you need to check the bracketing yourself and coefficients yourself, as I suspect you really meant the second set to be [-1,+1;+1,-1].
  댓글 수: 1
Georgios Koutsakis
Georgios Koutsakis 2019년 10월 3일
Hello,
Thanks a lot for proposed solution.
Looks like the answer of the TransferMatrix{1,1}(1:N) a 1xN array which each value is the result of each individual PartTransferMatrix{1,1}.
How could one do a matrix multiplication with the proposed function handles and solve for the overall TransferMatrix?
Thanks!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by