Quad in a cell of handle functions - matlab

I have two lists of functions, for instance: log(n*x), n=1:2017 and cos(m*x), m=1:6. I want/need to construct the matrix product of these vectors and then integrating each element of the matrix between 10 and 20.
I have read this post:
Matrix of symbolic functions
but I think that it is not useful for this problem.
I'm trying to do this by using a loop but I can not get it.
Thanks in advance for reading it.

You can solve this problem by assigning the appropriate vectors to n and m as follows:
n = (1:2017)'; % column vector
m = 1:6; % row vector
syms x;
l = log(n*x); % column vector of logs
c = cos(m*x); % row vector of cos
product = l*c; % matrix product
i = int(product, x, 10, 20); % integral from 10 to 20
iDouble = double(i); % convert the result to double

Related

Multiplying a 4D matrix by a vector, and collapsing 1 dimension

I have a question regarding the multiplication of a 4-dimensional object by a 1 dimensional object.
Effectively, I have a 4D object of sizes (15,15,3,5).
I want to multiply out the 4th dimension by using a 5x1 vector, collapsing the last dimension to 1. Then I want to use squeeze to get a (15,15,3) sized object, again multiplying it by a 3x1 vector, leaving me with a 15x15 matrix.
I can do this in a loop, but that is quite costly. Can anyone give me suggestions how to do this without a loop?
For now the loop:
expectationCalc = reshape(mValueFunction(age+1, :, :, :, :), nGridAssets, nGridHumanCapital, nNetInterestRate, nShockstoHumanCapital);
for i = 1:nGridAssets
for j = 1:nGridHumanCapital
expectation(i,j) = mTransitionNetInterestRate(nNetIntRate, :)*(squeeze(expectationCalc(i,j,:,:))*mTransitionShockHumanCapital(ShockHcapital, :)');
end
end
If you reshape your 4D matrix to a 2D matrix, where the 2nd dimension is the one you want to reduce by dot product, and the 1st dimension contains all other dimensions, then you can apply a regular matrix multiplication. The result can then be reshaped to the original size (minus one dimension):
% Input data
M = randn(15,15,3,5);
v1 = randn(5,1);
v2 = randn(3,1);
% 1st multiplication
sz = size(M);
M = reshape(M,[],sz(end));
M = M * v1;
sz(end) = []; % We no longer have that last dimension
M = reshape(M,sz);
% 2nd multiplication
M = reshape(M,[],sz(end));
M = M * v2;
sz(end) = []; % We no longer have that last dimension
M = reshape(M,sz);

What is the fastest way to calculate most dominant eigenvalue/singular value?

I only know of the following power iteration. But it needs to create a huge matrix A'*A when both of rows and columns are pretty large. And A is a dense matrix as well. Is there any alternative to power iteration method below? I have heard of krylov subspace method, but I am not familiar with it. In anycase I am looking for any faster method than the one mentioned below:
B = A'*A; % or B = A*A' if it is smaller
x = B(:,1); % example of starting point, x will have the largest eigenvector
x = x/norm(x);
for i = 1:200
y = B*x;
y = y/norm(y);
% norm(x - y); % <- residual, you can try to use it to stop iteration
x = y;
end;
n3 = sqrt(mean(B*x./x)) % translate eigenvalue of B to singular value of A
I checked 'svd' command of matlab with a 100*100 randomly generated matrix. It is almost 5 times faster than your code.
s = svd(A);
n3 = s(1);

Multiplication of vectors in two loops

I want to multiply two vectors to produce a matrix.
I have a vector 1*m and another 1*n which are in my case V (1*71) and I (1*315). The other vectors have same length as I.
I want to multiply every value of I with all values of V and have the answer in a matrix where every row or column of new matrix is I(t).*V
Ir and Temp are vectors with the size of 1*315 and all the variables have the same length and T is 315.
The other parameters that you see in the code are constant values.
This is the code :
function [I] = solar2diodedyn( Ir,time,Temp )
V = 0:0.01:0.7; %open circuit voltage of one cell in V.
for t=1:time;
T(t)= Temp(t)+273;
Vt(t)=(k*T(t))/q;
Iph(t) = Isc_cell*(Ir(t)/1000)*(1+(T_co*(Temp(t)-25)));
I0(t)=Is1*((T(t)/Tmeas)^(3/n1))*exp(Eg*((T(t)/Tmeas)-1)/(n1*Vt(t)));
I02(t)=Is2*((T(t)/Tmeas)^(3/n2))*exp(Eg*((T(t)/Tmeas)-1)/(n2*Vt(t)));
I(t) = zeros(size(t));
i=length(V);
for x=1:i
I(t) = Iph(t) - I0(t)*(exp((V(x)+I(t)*Rs)/(n1*Vt(t)))-1)-I02(t)*(exp((V(x)+I(t)*Rs)/(n2*Vt(t)))-1)-((V(x)+I(t)*Rs)/Rsh);
end
end
Thanks in advance
If you have two vectors x (of size 1-by-n) and y (of size 1-by-m) and you want a matrix M of size n-by-m such that M(i,j) = x(i) * y(j) then you are trying to compute the outer product of x and y.
This can be done easily with matlab
>> M = x.' * y;

Matlab: work with 2NxN matrix

I have a matrix 2NxN.
And I want get some parametrs by this matrix. For example it:
How, I can do it?
You may want to break your 12x6 matrix, into two 6x6 matrix; let's say: Z and Zb (last one for z bar). Odd rows are Z and evens are Zb.
Considering M to be the combined matrices:
Z = M(1:2:end,:)
Zb = M(2:2:end,:)
read about the colon(:) operator and end to see what 1:2:end means.
Hope it helps.
From what I understand here are the first three:
% Random Matrix
% Needs to be defined before the functions since the functions look for
% the m variable
m = rand(12,6);
% Function 1
p = #(i,j) sign(m(i,j)+m(i+1,j)) * max(abs(m(i,j)),abs(m(i+1,j)));
p(2,2)
% Function 2 - Avg of row
pavg = #(i) mean(m(i,:));
pavg(2)
% Function 3
c = #(i,j) abs(m(i,j)+m(i+1,j)) / (abs(m(i,j)) + abs(m(i+1,j)));
c(2,2)

splitting a matrix row-wise and and finding a linear regression coeff

A= [1 1
2 2
3 3
. .
. .
. .
N N]
I have an [N,2] matrix and I need to split it row-wise into some number of [N/4,2] submatrices. Then for each submatrix I need to find linear regression where the first column of each submatrix is my x data and the second column is my y data. The output should be a struct with fields a,b,c,d.... and values of linear regression for each submatrix
First I tried splitting the matrix with mat2cell where k = length(N)/4 and mat = mat2cell(A, [k k k k], [1 1]).
Next I tried converting mat into struct with out = cell2struct(mat,fields,1) where fields = {'col1','col2'} and use
new = structfun(#(x)polyfit(x.col1, x.col2,1), out,'UniformOutput', false)
But I get the error:
Inputs to STRUCTFUN must be scalar structures.
Does anyone know how to do it? Many thanks
The most straightforawrd way (and probably the fastest) to do this is with a good old for loop:
A = [1:64;1:64]'; % Demo data
m = 4;
N = size(A,1);
k = N/m; % Assumes that length is evenly divisible by 4
c = zeros(m,2); % Coefficients
for i = 1:m
c(i,:) = polyfit(A((i-1)*k+1:i*k,1),A(i-1)*k+1:i*k,2),1);
end
Or rather than using cell2struct and structfun you can use cellfun:
A = [1:64;1:64]'; % Demo data
m = 4;
N = size(A,1);
k = N/m; % Assumes that length is evenly divisible by 4
c = cellfun(#(x)polyfit(x(:,1),x(:,2),1).',mat2cell(A,k+zeros(1,m),2),'UniformOutput',false)
or alternatively
Ac = mat2cell(A,k+zeros(1,m),[1 1])
c = cellfun(#(x1,x2)polyfit(x1,x2,1).',Ac(:,1),Ac(:,2),'UniformOutput',false)
You can convert the output of cellfun to a matrix with:
c = [c{:}].'
As for why you're getting the error, your variable out is a 4-by-1 struct array (array of structures) rather than a simple (scalar) structure of arrays. The documentation for structfun points out this requirement in the description of the short function: "Apply function to each field of scalar structure." This video from The MathWorks tries to explain the difference.