MATLAB Answers

Element by element multiplication of a matrix

3 views (last 30 days)
Divij Gupta
Divij Gupta on 4 Jun 2021
Answered: Star Strider on 4 Jun 2021
I have a function f = e^(tH), where H is a square matrix. I want to compute this function for each value of t = 0:10:100 so I can plot f against t. Using .* is giving an error (sizes not compatible). What can I do?

Answers (2)

Paul
Paul on 4 Jun 2021
For the matrix exponential:
% example data
H = [1 2;3 4];
t = 0:5;
f = cell2mat(cellfun(@expm,mat2cell(H.*reshape(t,1,1,[]),2,2,ones(1,6)),'UniformOutput',false));
% compare a few cases by hand
f(:,:,2) - expm(H*t(2))
ans = 2×2
0 0 0 0
f(:,:,end) - expm(H*t(end))
ans = 2×2
0 0 0 0
If f is supposed to be element-by-element exponential:
f = exp(H.*reshape(t,1,1,[]))
f =
f(:,:,1) = 1 1 1 1 f(:,:,2) = 2.7183 7.3891 20.0855 54.5982 f(:,:,3) = 1.0e+03 * 0.0074 0.0546 0.4034 2.9810 f(:,:,4) = 1.0e+05 * 0.0002 0.0040 0.0810 1.6275 f(:,:,5) = 1.0e+06 * 0.0001 0.0030 0.1628 8.8861 f(:,:,6) = 1.0e+08 * 0.0000 0.0002 0.0327 4.8517

Star Strider
Star Strider on 4 Jun 2021
I have absolutely no idea what result you want.
Try this —
H = [1 2; 3 4]*0.01;
t = 0:10:100;
for k = 1:numel(t)
f{k} = exp(t(k)*H);
end
figure
hold on
for k = 1:numel(t)
plot(t(k)*[1 1; 1 1], f{k}, '.-')
end
hold off
grid
Also, consider the expm function, since ‘element-by-element’ is not precisely clear in this context.
.

Community Treasure Hunt

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

Start Hunting!