Function 'subs': how use?

I have realized that the counts with this function 'subs' doing x(1) +1*x(2) and x(1) + 1*x(2) are differents, for example. The space in these expressions give me results differents.
Which the problem?
Which is correct? I think that the correct is x(1) +1*x(2) and not with the space.
Thanks.
JRC

Antworten (1)

Walter Roberson
Walter Roberson am 21 Aug. 2013

0 Stimmen

In the context of the [] operation,
x(1) +1*x(2)
would usually be interpreted as
(x(1)), (+1*x(2))
which is two entries.
x(1) + 1*x(2)
inside [] would be interpreted as one entry.
If you are not within the context of [] then the two should be the same.

5 Kommentare

JRC
JRC am 21 Aug. 2013
Is only one entry... but have results differents. I think that the results should be the same, but this don´t occurs.
The sentence is
J(N) = subs(J(N), {x(N),y(N)},{1*x(N-1) -1*u(N-1), 0.5*x(N-1)+2*y(N-1)});
and is different from
J(N) = subs(J(N), {x(N),y(N)},{1*x(N-1) - 1*u(N-1), 0.5*x(N-1)+2*y(N-1)});
With the others variables, don´t have the problem and i can change the space that the results keep on the same.
Thanks.
Walter Roberson
Walter Roberson am 21 Aug. 2013
The remarks I gave for [] apply to {} as well (I forgot about that)
Your
1*x(N-1) -1*u(N-1)
is being interpreted as
1*x(N-1), -1*u(N-1)
Within list-building operations, [] and {}, if you have two expressions separated by a string, and each of the expressions by itself could be interpreted as being a syntactically valid expression, then the space will be interpreted as horizontal concatenation. +1*x(2) is (unary plus 1) multipled by x(2) so that is a valid expression by itself, and x(1) is a valid expression by itself, so x(1) +1*x(2) is interpreted as horzcat(x(1), +1*x(2)). When either or both of the expressions cannot be interpreted as syntactically valid expressions then the space is not interpreted as indicating horizontal concatenation.
Which one is "right" depends on your purpose.
Advice: if you are using spacing at all, always use a space after a + or - that is used to indicate subtraction, and (so that you will find your code easier to read later), use a comma between expressions that you intend to be horizontally concatenated together. If you are machine-generating expressions as strings and for some reason the generated strings might have irregular spacing, then add () around terms that you intend to be a single expression.
[1 -4]
is two elements, not subtract(1,4) or add(1, unaryminus(4))
[(1 -4)]
is subtract(1,4)
JRC
JRC am 21 Aug. 2013
Thanks Walter...
Have a detail... this situation that speak is in the looping and with values symbolics...
This because i use the comand
x = [x; sym(['x' num2str(j)])]; u = [u; sym(['u' num2str(j)])];
for inicialize the problem and after i do the accounts with
J(N) = subs(J(N), {x(N),y(N)},{1*x(N-1) -1*u(N-1), 0.5*x(N-1)+2*y(N-1)});
After i come back the problem for matrix
eval(['x' num2str(j+1) '=' num2str(eval(x(j+1)))]); eval(['u' num2str(j) '=' num2str(eval(u(j)))]);
I think that in this process my accounts are right... only would like to be sure.
I use 1*x(N-1) -1*u(N-1) and not 1*x(N-1) - 1*u(N-1), but these values are symbolics... this way, i think that the two forms are right.
This is the process (basically).
Thanks again...
Walter Roberson
Walter Roberson am 21 Aug. 2013
Do not eval() MuPAD expressions, such as are returned by sym() or subs(). MuPAD and MATLAB use different syntax rules and different operators. If you want to convert a MuPAD expression into executable MATLAB form, use matlabFunction() on the MuPAD expression -- but if you do not need to execute at the MATLAB level, keep expressions in MuPAD form. If you have a MuPAD expression which consists entirely of constants and you want to convert it to double precision, use double() on the MuPAD expression.
Please see also http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F . It looks to me as if you would be better off storing values in a struct than using eval() to generate new variable names.
JRC
JRC am 21 Aug. 2013
Thanks Walter.

Melden Sie sich an, um zu kommentieren.

Gefragt:

JRC
am 21 Aug. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by