MATLAB Answers

# Symbolic Math is reordering my subtraction

조회 수: 26(최근 30일)
Vincent Bell 25 Feb 2021
답변: Walter Roberson 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!

댓글을 달려면 로그인하십시오.

### 답변(2개)

John D'Errico 25 Feb 2021
편집: John D'Errico 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표시숨기기 없음
Steven Lord 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.

댓글을 달려면 로그인하십시오.

Walter Roberson 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...
diff(ans,t)
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!