SUBS function not working properly - R2020a
Afficher commentaires plus anciens
Hi :)
I have a really complicated expression for which I am using partial derivatives. Using diff everything works like magic :)
However, when I try to use subs I have problems since it won't substitute corectly and I not able to obtain a final value. You can find below my code. Thank you!
syms X [4 1] ;
syms B [4 1]
syms LW [4 1] ;
syms IW [4 4] ;
f = LW'*tanh(IW*X+B);
D1 = diff(f,X1);
g1 = matlabFunction(D1);
IW1_1n = IWn(1,1);
IW1_2n = IWn(1,2);
IW1_3n = IWn(1,3);
IW1_4n = IWn(1,4);
IW2_1n = IWn(2,1);
IW2_2n = IWn(2,2);
IW2_3n = IWn(2,3);
IW2_4n = IWn(2,4);
IW3_1n = IWn(3,1);
IW3_2n = IWn(3,2);
IW3_3n = IWn(3,3);
IW3_4n = IWn(3,4);
IW4_1n = IWn(4,1);
IW4_2n = IWn(4,2);
IW4_3n = IWn(4,3);
IW4_4n = IWn(4,4);
LW1n = LWn(1,1);
LW2n = LWn(2,1);
LW3n = LWn(3,1);
LW4n = LWn(4,1);
B1n = Bn(1,1);
B2n = Bn(2,1);
B3n = Bn(3,1);
B4n = Bn(4,1);
X1n = 2;
X2n = 1;
X3n = 4;
X4n = 5;
subs(g1,{B1,B2,B3,B4,IW1_1,IW1_2,IW1_3,IW1_4,IW2_1,IW2_2,IW2_3,IW2_4,IW3_1,IW3_2,IW3_3,IW3_4,IW4_1,IW4_2,IW4_3,IW4_4,LW1,LW2,LW3,LW4,X1,X2,X3,X4},{B1n,B2n,B3n,B4n,IW1_1n,IW1_2n,IW1_3n,IW1_4n,IW2_1n,IW2_2n,IW2_3n,IW2_4n,IW3_1n,IW3_2n,IW3_3n,IW3_4n,IW4_1n,IW4_2n,IW4_3n,IW4_4n,LW1n,LW2n,LW3n,LW4n,X1n,X2n,X3n,X4n});
The partial deriative w.r.t X1 is g1:
val =
@(B1,B2,B3,B4,IW1_1,IW1_2,IW1_3,IW1_4,IW2_1,IW2_2,IW2_3,IW2_4,IW3_1,IW3_2,IW3_3,IW3_4,IW4_1,IW4_2,IW4_3,IW4_4,LW1,LW2,LW3,LW4,X1,X2,X3,X4)-IW1_1.*conj(LW1).*(tanh(B1+IW1_1.*X1+IW1_2.*X2+IW1_3.*X3+IW1_4.*X4).^2-1.0)-IW2_1.*conj(LW2).*(tanh(B2+IW2_1.*X1+IW2_2.*X2+IW2_3.*X3+IW2_4.*X4).^2-1.0)-IW3_1.*conj(LW3).*(tanh(B3+IW3_1.*X1+IW3_2.*X2+IW3_3.*X3+IW3_4.*X4).^2-1.0)-IW4_1.*conj(LW4).*(tanh(B4+IW4_1.*X1+IW4_2.*X2+IW4_3.*X3+IW4_4.*X4).^2-1.0)
And I get a sym answer:
val =
(30164863393232222223527442459987*tanh(13183424693662021/4503599627370496)^2)/162259276829213363391578010288128 + (26807362790507965786084996958647*tanh(27839816456922887/4503599627370496)^2)/649037107316853453566312041152512 - (1463215109866443988261242772121*tanh(18240652112712759/4503599627370496)^2)/5070602400912917605986812821504 - (17479489290048798515102214463497*tanh(18112203897295801/9007199254740992)^2)/81129638414606681695789005144064 + 179660632019858363938062023740869/649037107316853453566312041152512
I have no idea why it doesn't compute tanh and I don't know why my variables have those values and what's with those divisions. For example:
Bn = [1.99173113344310
0.726676064746315
0.219999881681849
-1.15370946128863];
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Common Operations dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!