I define a function handle as
P = @(t) mtimes(expm(t.*M),P_initial);
Where P_initial is a 4x1 array and M a 4x4 matrix. This works fine, but now I have a function with for every t a 4x1 answer, for example at t = 0 I get:
P(0)
ans =
0.0000
0.0000
0.0011
0.9988
What I want is to get this first element as a separate function of time, so I want a P_1(t) as a function handle, but I do not know how to do so.
Thanks already! :)

 채택된 답변

Torsten
Torsten 2022년 12월 2일

0 개 추천

M = rand(4,4);
P_initial = rand(4,1);
P = @(t) mtimes(expm(t.*M),P_initial);
P(0)
g = @(a,x)a(x);
g(P(0),1)

댓글 수: 2

Wout Laeremans
Wout Laeremans 2022년 12월 2일
Hi Torsten, this comes close to what I want thank you! Do you also know how I can plot g(P(t),1) for a general t?
Torsten
Torsten 2022년 12월 2일
편집: Torsten 2022년 12월 2일
M = rand(4,4);
P_initial = rand(4,1);
P = @(t) mtimes(expm(t.*M),P_initial);
g = @(a,x)a(x);
t = 0:0.01:1;
plot(t,arrayfun(@(t)g(P(t),1),t))

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Davide Masiello
Davide Masiello 2022년 12월 2일
편집: Davide Masiello 2022년 12월 2일

0 개 추천

That's because mtimes(a,b) = a*b, and if a is 4x4 and b is 4x1 than the operation yields a 4x1 array.
I think what you might want to do is
P = @(t) expm(t.*M).*P_initial;
Which will yield a 4x4 matrix where each column is .
But I am not completely sure I interpreted this correctly.

댓글 수: 3

Wout Laeremans
Wout Laeremans 2022년 12월 2일
No no, I will give more context: P gives a probability to be in a certain state. At every moment in time t, the first entry gives the probability to be in state 1, the second to be in state 2 and so on. However, I also need the first entry as a function of time, it est the probability to be in state 1 as a function handle of variable t.
you can define a cell array of functions
M = rand(4,4);
P_initial = rand(1,4);
for i = 1:length(M)
P{i} = @(t)mtimes(expm(t.*M),P_initial);
end
P
P = 1×4 cell array
{@(t)mtimes(expm(t.*M),P_initial)} {@(t)mtimes(expm(t.*M),P_initial)} {@(t)mtimes(expm(t.*M),P_initial)} {@(t)mtimes(expm(t.*M),P_initial)}
With your formulation, however, they are all the same.
Are you sure you must use expm and mtimes?
I think the problem might be ill-posed in that aspect.
Wout Laeremans
Wout Laeremans 2022년 12월 2일
Yes, I want the solution of:
The general solution is given by:
Where I call P = [P_O; P_3; P_2; P_1] and M the transition matrix. I know I can also solve this using an ode solver or just define a time array with a small time increment. However I really want P_O(t) as a function handle.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2022년 12월 2일

편집:

2022년 12월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by