MATLAB Answers

Symbolic Math is reordering my subtraction

1 view (last 30 days)
Vincent Bell
Vincent Bell on 25 Feb 2021
Answered: Walter Roberson on 26 Feb 2021
I am using symbolic function to take the derivative. My funcitions are:
syms x_k(t) y_k(t) L
x_kD = diff(x_k,t)
y_kD = diff(y_k,t)
rRk = sqrt(x_k^2 + y_k^2)
rTk = sqrt(x_k^2 + (y_k-L)^2)
rRkD = simplify(diff(rRk,t))
rTkD = simplify(diff(rTk,t))
The problem is when you run the rRk term the syms math changes the order of subtraction around from y_k - L to L - y_k. This is throwing off the derivative as well where rTkD gets a minus sign in between the two terms in the numerator where is should be a positive. Is there a way to stop the switching of the subtraction in the variable of rTk and rTkD? Another thing that would be helpful is there a way to convert the time derivative that is output into the dot derivative form? Thank you in advance!

Answers (2)

John D'Errico
John D'Errico on 25 Feb 2021
Edited: John D'Errico on 25 Feb 2021
syms x_k(t) y_k(t) L
x_kD = diff(x_k,t)
x_kD(t) = 
y_kD = diff(y_k,t)
y_kD(t) = 
rRk = sqrt(x_k^2 + y_k^2)
rRk(t) = 
rTk = sqrt(x_k^2 + (y_k-L)^2)
rTk(t) = 
rRkD = simplify(diff(rRk,t))
rRkD(t) = 
rTkD = simplify(diff(rTk,t))
rTkD(t) = 
The swap in sign is irrelevant. This is true in mathematics:
a - b = - (b - a)
Well, it is true and always has been, unless something has changed in mathematics. However, you never know with the new math these days, and some boards of education.
As for a dot derivative form, MATLAB does not provide that form. It is purely a notational thing, so why do you feel you need it?
  1 Comment
Steven Lord
Steven Lord on 26 Feb 2021
Not only that, (a-b)^2 is a*a-a*b-b*a+b*b. (b-a)^2 is b*b-b*a-a*b+a*a. Same terms, different order.

Sign in to comment.

Walter Roberson
Walter Roberson on 26 Feb 2021
Livescript only, you can use __dot as part of the symbol name. However, you cannot get the rendered form to appear on the left side, and it will not be put in automatically.
syms x(t) x__dot(t) x__dot_ y(t) y__dot
x__dot_ = diff(x)
x__dot_(t) = 
y__dot_ = diff(y)
y__dot_(t) = 
y = x__dot_^2 - y__dot_^2
y(t) = 
subs(y, {x__dot_, y__dot_}, {x__dot, y__dot})
ans(t) = 
Notice the last line there has and that the looks nicer. But...
ans(t) = 
y disappeared, because (no ) is not considered to be a function of t. For computation purposes, you cannot have it both ways, displaying without the (t) but being computed on the same symbols as if (t) were present

Community Treasure Hunt

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

Start Hunting!