multiplying matrix by another matrix element and using it in a command

2 Ansichten (letzte 30 Tage)
Q=[-0.0905 0.0604 0.0301;0.142 -0.239 0.097;0 0 0];
T =[ 1, 3, 5, 7, 10];
e=expm(Q*T); %%% the problem is here
I have a problem with calculating e, i want to multiply Q with each value of T and still use the expm command

Akzeptierte Antwort

Matt J
Matt J am 14 Nov. 2020
clear e
for i=numel(T):-1:1
e(:,:,i)=expm(Q*T(i));
end

Weitere Antworten (3)

James Tursa
James Tursa am 14 Nov. 2020
Bearbeitet: James Tursa am 15 Nov. 2020
You can use a loop
QT = Q .* reshape(T,1,1,[]);
e = zeros(size(QT));
for k=1:size(QT,3)
e(:,:,k) = expm(QT(:,:,k)); % fixed
end
  1 Kommentar
Georgios Kirkinezos
Georgios Kirkinezos am 15 Nov. 2020
ALSO WORKS!!!
Giving same result like the other accepted answer!
(note:it is missing a clossing parenthesis in the end )

Melden Sie sich an, um zu kommentieren.


Setsuna Yuuki.
Setsuna Yuuki. am 14 Nov. 2020
You need matrix T to have the same number of rows or columns as matrix Q
Q=[-0.0905 0.0604 0.0301;0.142 -0.239 0.097;0 0 0];
%T =[ 1, 3, 5, 7, 10];
T =[1,3,5]
e=expm(Q*T); %change * --> .*

Bruno Luong
Bruno Luong am 14 Nov. 2020
Bearbeitet: Bruno Luong am 14 Nov. 2020
This must be the fastest if your T is a large vector
[P,D] = eig(Q);
e = exp(diag(D)*T(:).');
e = reshape(e,1,size(Q,1),length(T));
e = pagemtimes(P.*e,inv(P))
  3 Kommentare
Bruno Luong
Bruno Luong am 14 Nov. 2020
Bearbeitet: Bruno Luong am 14 Nov. 2020
You mean the Jordan form? It does not exist in numerical world. ;-)
I'm kidding. Indeed Q supposes to be diagonalizable.
Q = P*D/P
D diagonal.
Bruno Luong
Bruno Luong am 15 Nov. 2020
Bearbeitet: Bruno Luong am 15 Nov. 2020
Speed comparison between expm for loop and eigen-value methods. It speed up about 100 fold.
Q = [-0.0905 0.0604 0.0301;
0.142 -0.239 0.097;
0 0 0];
T = linspace(0,10,10000);
tic
clear E1
for i=numel(T):-1:1
E1(:,:,i)=expm(Q*T(i));
end
toc % Elapsed time is 0.227108 seconds.
tic
[P,D] = eig(Q);
E2 = exp(diag(D)*T(:).');
E2 = reshape(E2,[1,size(E2)]);
E2 = pagemtimes(P.*E2,inv(P));
toc % Elapsed time is 0.001398 seconds.
norm(E1(:)-E2(:),Inf) % 4.7531e-16

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by