Symbolic Math is reordering my subtraction
9 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(2 个)
John D'Errico
2021-2-25
编辑:John D'Errico
2021-2-25
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 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
2021-2-26
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
2021-2-26
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)
y__dot_ = diff(y)
y = x__dot_^2 - y__dot_^2
subs(y, {x__dot_, y__dot_}, {x__dot, y__dot})
Notice the last line there has
and that the
looks nicer. But...
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/532119/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/532124/image.png)
diff(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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/532134/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/532139/image.png)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!