On 19/12/2013 08:59, Dang, Christophe wrote:
Dear all,
I often have to work with 2-variable functions,
e.g. from equations of state involving pressure and temperature.
I have a range of P and T and build line vectors,
with linspace() or v1:p:v2.
The Kronecker product allows an easy build of P*T
P'.*.T
->
[P(1)*T(1) P(1)*T(2)
P(2)*T(1) P(2)*T(2)]
Now, I'd like to have the same thing with the sum,
without using a loop
(of course it's not the sum only,
there are adequate coefficients
and it's possibly enclosed in a exp() function or so).
A colleague of mine suggested the following way
which works well:
sP = size(P, "c");
sT = size(T, "c");
P_matrix = P'*ones(1, sT);
T_matrix = T*ones(sP, 1);
P_matrix + T_matrix
->
[P(1)+T(1) P(1)+T(2)
P(2)+T(1) P(2)+T(2)]
Now my question:
is there a more convenient way to do this,
and especially,
if I want to generate a f(P, T) matrix,
do I have to build P_matrix and T_matrix and perform
f(P_matrix, T_matrix)
or is there a way to get it directly from the P and T vectors?
Hi
I believe the most convinient think to do is to write a function.
Here is an example that works for the sum, but also any arbitrary
function f:
functiony=map_fun(P,T,f)sP=size(P,"c")sT=size(T,"c")P_=P'*ones(1,sT)T_=ones(sP,1)*Ty=f(P_,T_)endfunctionfunctiony=somme(a,b)y=a+bendfunctionfunctiony=power(a,b)y=a.^bendfunctionP=linspace(3,30,7)T=linspace(0,15,7)map_fun(P,T,somme)["+"string(P);string(T')string(map_fun(P,T,somme))]
!+ 3 7.5 12 16.5 21 25.5 30 !
! !
!0 3 5.5 8 10.5 13 15.5 18 !
! !
!2.5 7.5 10 12.5 15 17.5 20 22.5 !
! !
!5 12 14.5 17 19.5 22 24.5 27 !
! !
!7.5 16.5 19 21.5 24 26.5 29 31.5 !
! !
!10 21 23.5 26 28.5 31 33.5 36 !
! !
!12.5 25.5 28 30.5 33 35.5 38 40.5 !
! !
!15 30 32.5 35 37.5 40 42.5 45 !
["^"string(P);string(T')string(map_fun(P,T,power))]
!^ 3 7.5 12 16.5 21 25.5 30 !
! !
!0 1 15.588457 243 3787.9951 59049 920482.81 14348907 !
! !
!2.5 1 154.04697 23730.469 3655606.8 5.631D+08 8.675D+10 1.336D+13 !
! !
!5 1 498.83063 248832 1.241D+08 6.192D+10 3.089D+13 1.541D+16 !
! !
!7.5 1 1105.8847 1222981 1.352D+09 1.496D+12 1.654D+15 1.829D+18 !
! !
!10 1 2020.9159 4084101 8.254D+09 1.668D+13 3.371D+16 6.812D+19 !
! !
!12.5 1 3283.6015 10782039 3.540D+10 1.163D+14 3.817D+17 1.253D+21 !
! !
!15 1 4929.503 24300000 1.198D+11 5.905D+14 2.911D+18 1.435D+22 !
Cheer
Adrien Vogt-Schilb
Exported from Notepad++
_______________________________________________
users mailing list
[email protected]
http://lists.scilab.org/mailman/listinfo/users