0
votes

I am interested in writing some matrix elements as functions which can take value as per my convenience and then the required matrix operations can be applied on top of that. More precisely, I am trying to integrate over x, the trace of a matrix which has matrix elements as functions of x (which are unknown analytically as they come through products of matrices dependent on x) .

When I try to write the matrix elements as functions, then I obviously get the error- Conversion to double from function_handle is not possible. Is there an easy way to write the matrix elements as functions ?

Thanks. Please ask if my question is not clear.

For example, its something like this:-

N_k = 10; 
M_sigma = cell(N_k,1);
M_rho = cell(N_k,1);

for ii = 1:N_k
    M_sigma {ii}(1,2) = @(sigma) sigma; %this kind of thing is not allowed in matlab
    M_sigma {ii}(2,1) = @(sigma) -conj(sigma);
    M_sigma {ii}(1,1) = 0;
    M_sigma {ii}(2,2) = 0;
end

for ii = 1:N_k
    M_rho {ii}(1,2) = @(rho) rho; 
    M_rho {ii}(2,1) = @(rho) -conj(rho);
    M_rho {ii}(1,1) = 0;
    M_rho {ii}(2,2) = 0;
end



M_tau = cell(N_k,1);

for ii = 1:N_k
    M_tau {ii} = exp(M_sigma{ii})*exp(M_rho{ii}); 
end


    % the following statement is wrong but I want to do something like 
    %:-write M_tau as a function of sigma and sum(integrate) the trace of M_tau for all values of sigma

integral(@(sigma) M_tau{1}(sigma), {0,1})
1
Write the code that you have right now, otherwise it becomes difficult to think what exactly you are trying to do?user4085386
Can you show us some code? It's hard to visualize what it is you're asking.rayryeng
You are only repeating the same matrix of function, but even by producing the proper function, it takes 2 inputs (sigma and rho), so the integral cannot be defined for sigma [0 to 1]. Or do you plan to fix rho ?Hoki

1 Answers

0
votes

I don't think that you would be able to accomplish that with function handles. I think the best way you could do it would be to define a function as follows :

function x = myFunction(rowindex,colindex)
     x = x * rowindex + colindex;
end

You would replace this function with your algorithm and then you could iterate over it doing the following:

for a=1:10
   for b=1:10
      x(a,b)=myFunction(a,b);
   end
end