More symbolic trouble substituting

2 views (last 30 days)
Robert
Robert on 22 Oct 2016
Answered: Walter Roberson on 23 Oct 2016
Matlab will not substitute values as it is being instructed.
syms s k e c r
r = c + e
e = ((2s^3+4*s^2+1*s)*c)/k
c = r - e
Gplant = c/e
t = expand(c/r)
closed_loop = simplify(t)
Now if i ask it what closed_loop is i should simply have an expression in terms of S! Not every other letter!
But of course not because matlab has to make everything totally insane and overly difficult. Can someone tell me why this is not working

Answers (1)

Walter Roberson
Walter Roberson on 23 Oct 2016
Order matters.
When you define
r = c + e
then r gets assigned a value that includes a copy of what c was and a copy of what e was at the time of execution of the statement.
e = ((2s^3+4*s^2+1*s)*c)/k
is a syntax error. Add the obvious multiplication and you are assign to e a value that includes a copy of what s was and a copy of what c was and a copy of what k was at the time of execution. This assignment to e does not change the e that was used in the definition of r
You then have
c = r - e
This is clearly intended as a procedural step rather than establishing simultaneous equations.
You are mixing procedural statements with the expectation that giving a value to symbols changes past references to those symbols.
Consider,
a = 1
b = a + 2
a = 2
You do not expect b to now become 2 + 2, because b is not establishing a formula to be evaluated with some current value of a: b is a procedural assignment, where the current value of a is to be copied and used to create a specific value of b that is then untouched by the change to a.
Just so if you have
c = sym('c'); %this is what "syms c" really means
a = 1
b = a + c
c = 2
then you should not expect that b will become 1 + 2, because assignments to symbolic variables do not "reach back" and change previous references to the variables. If you want a change to be reflected, you need to use subs() .
You should always avoid assigning to a symbolic variable after you have used it in a previous expression, as it leads to exactly these kinds of messes. subs() instead:
c = sym('c'); %this is what "syms c" really means
a = 1
b = a + c
subs(b, c, 2)

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!