Borrar filtros
Borrar filtros

Matrix multiplication of n function handles

2 visualizaciones (últimos 30 días)
Sascha Wolter
Sascha Wolter el 19 de Feb. de 2016
Comentada: Georgios Koutsakis el 3 de Oct. de 2019
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 comentario
Stephen23
Stephen23 el 19 de Feb. de 2016
Editada: Stephen23 el 19 de Feb. de 2016
Note that you do not have "a function handle", but a cell array of function handles. These are two quite different things!

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 19 de Feb. de 2016
Editada: Stephen23 el 19 de Feb. de 2016
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 comentario
Georgios Koutsakis
Georgios Koutsakis el 3 de Oct. de 2019
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!

Iniciar sesión para comentar.

Categorías

Más información sobre Matrices and Arrays 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!

Translated by