Subs does not do the job

7 visualizzazioni (ultimi 30 giorni)
Hello friends!
I do not understand why in my code (see bellow) the command 'subs' does not through ant error and at the same time does
not do anything. I did google it but unfrtunately was not able to figure out what is the root of the problem.
clc;
J=3;
EZh=sym('EZh',[J 1]);
e=sym('e',[J 1]);
syms z w
for j=1:J
A=expand((z-w)^j);
A=char(A);
A=replace(A,'w','e(1)');
for k=j:-1:2
A=replace(A,strcat(['z^',num2str(k)]),strcat(['e(',num2str(k),')']));
end
A=replace(A,'z','e(1)');
A=append('rho^',num2str(j),'*(',A,')');
EZh(j)=str2sym(A);
end
EZh
EZh=subs(EZh,e,{1;1;1});
EZh
Any help is greatly appreciated!
Babak
  2 Commenti
Torsten
Torsten il 24 Gen 2022
Don't you have to use [ ] instead of { } in the subs command ?
Mohammad Shojaei Arani
Mohammad Shojaei Arani il 24 Gen 2022
I do not care so much about it. I just want subs to work either with [] or with {}

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 24 Gen 2022
e=sym('e',[J 1]);
e would be a vector containing symbols named e1 e2 e3
A=replace(A,'w','e(1)');
That e(1) will not be treated as indexing when you str2sym() . When you have something of the form variable(value) at the symbolic level, that is always a function call -- so e(1) would be treated as a call to an unresolved function named e with parameter 1.
EZh=subs(EZh,e,{1;1;1});
The names to be substituted for are e1 e2 e3 . But those do not appear in EZh -- function calls e(1) e(2) e(3) are present instead.
The MATLAB level of symbolic function does not support indexing at the symbolic level. If you had coded
e = sym('e', [J 1])
f = e(2)*e(3)
then the e(2)*e(3) would be executed at the MATLAB level. MATLAB would have a symbolic object named e that was a reference to something that lived inside the symbolic engine, and MATLAB would ask that to be indexed at location 2, and the symbolic engine would respond with a reference into something that lived inside the symbolic engine (and which would display as e2) . Likewise e(3) evaluated at the symbolic engine would ask the symbolic engine to index and would get back a reference to something inside the symbolic engine (that would displays as e3 ) . Then MATLAB would have the two references to things inside the symbolic engine and it would execute the * operation, which would send the symbolic referneces to the symbolic engine along with the _mult operation. The symbolic engine would return a reference to soemthing that lived inside the symbolic enigne (and which displayed as e2*e3 )
Notice that in this flow, it is MATLAB that is driving the indexing requests.
If you str2sym('e(2)*e(3)') then when processing the quoted string, the symbolic engine has no idea that e is a vector that lives inside the MATLAB level. There is no vector named e at the symbolic level -- the assignment e=sym() is to a variable at the MATLAB level, not at the symbolic level. The symbolic level only has an anonymous vector containing e1 e2 e3 with no name for it.
You cannot get sym or str2sym to recognize () as indexing of MATLAB variables or of symbolic objects.
  2 Commenti
Walter Roberson
Walter Roberson il 24 Gen 2022
So here is what you can do:
J=3;
EZh=sym('EZh',[J 1]);
e=sym('e',[J 1]);
syms z w
for j=1:J
A=expand((z-w)^j);
A=char(A);
A=replace(A,'w','e(1)');
for k=j:-1:2
A=replace(A,strcat(['z^',num2str(k)]),strcat(['e(',num2str(k),')']));
end
A=replace(A,'z','e(1)');
A=append('rho^',num2str(j),'*(',A,')');
EZh(j)=str2sym(A);
end
EZh
EZh = 
EZh = mapSymType(EZh, 'e', @(X) e(double(children(X,1))))
EZh = 
EZh = subs(EZh, e, [1;1;1])
EZh = 
Mohammad Shojaei Arani
Mohammad Shojaei Arani il 24 Gen 2022
Thanks Walter!
Very difficult to solve by myself. I appreciate your help a lot!

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by