function handle as array
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Wout Laeremans
am 2 Dez. 2022
Bearbeitet: Torsten
am 2 Dez. 2022
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! :)
0 Kommentare
Akzeptierte Antwort
Torsten
am 2 Dez. 2022
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)
Weitere Antworten (1)
Davide Masiello
am 2 Dez. 2022
Bearbeitet: Davide Masiello
am 2 Dez. 2022
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 Kommentare
Davide Masiello
am 2 Dez. 2022
Bearbeitet: Davide Masiello
am 2 Dez. 2022
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
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.
Siehe auch
Kategorien
Mehr zu Logical 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!