Symbolic substitution: Subs command is substituting zeros with 'new' despite 'old' not being defined as zero - why?

4 次查看(过去 30 天)
I've got a simple piece of symbolic code below. It all seems to work well, except one strange thing happens. Vx1_before contains mostly zeros. For some reason, in the loop in the code, the subs command substitutes these zeros for the variable heavi_x1_9... the subs 'old' input is not zero, so really, it shouldn't act at all on these entries.
Precis: Upon running the code, most of Vx1_after contains the symbolic variable heavi_x1_9, although these entries should never have been substituted and should be zeros.
I hope someone can explain to me what is happening, why these elements of Vx1_before have been substituted despite not even containing the value to be substituted.
clear;
clc;
tx1=[0.06 0.06 0.06 0.06 0.06 0.144 0.144 0.144 0.144 0.144]
syms heavi_y5 heavi_x1_5 x y
Vx1_before=[heavi_y5*heavi_x1_5*((250*x)/21 - 12/7)^4*((125*y)/3 - 1)^4;-(500*heavi_y5*heavi_x1_5*y*((250*x)/21 - 12/7)^4*((125*y)/3 - 1)^3)/3;-4*heavi_y5*heavi_x1_5*((250*x)/21 - 5/7)*((250*x)/21 - 12/7)^3*((125*y)/3 - 1)^4;(2000*heavi_y5*heavi_x1_5*y*((250*x)/21 - 5/7)*((250*x)/21 - 12/7)^3*((125*y)/3 - 1)^3)/3;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0]
heaviside_vector_x1=[0;0;0;0;heaviside(x - 3/50) - heaviside(x - 18/125);0;0;0;0]
m1=length(tx1)-1;
for k=1:m1
Vx1_after=subs(Vx1_before,sprintf('heavi_x%d_%d', 1, k),heaviside_vector_x1(k));
end
Vx1_after

采纳的回答

Kobye
Kobye 2013-7-21
I have found the answer.
I am using Matlab R2012b. There is a bug in the code that switches the arguments old and new if subs(s,old,new) returns s. It is therefore necessary to give the input subs(s,old,new,0) instead of subs(s,old,new) in order to prevent this from happening.
This has been fixed in Matlab R2013b, which will no longer accept the subs(s,old,new,0) syntax. However, it is imperative to specify this in previous version or the function will switch the old and new as it pleases.
Relevant part of the subs description:
"subs(s,old,new,0) will not accept 0 in a future release. Use subs(s,old,new) instead.
In previous releases, subs(s,old,new,0) prevented switching the arguments old and new if subs(s,old,new) returned s. The subs function does not switch old and new anymore."

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by