Why doesn't diff() recognise the second variable?

syms x(t) m1 m2
xr = x;
xl = 0;
T = (m1 * diff(xl)^2) / 2 + (m2 * (diff(xr)^2)) / 2 % why doesn't it recognise that xr is a function of time?
Why is the different from this?
T = (m1 * diff(xl,t)^2) / 2 + (m2 * (diff(xr,t)^2)) / 2 % gives the expected result
whereas if i do
diff(xr) % it gives me the correct result?
What is going on here?

 Risposta accettata

This works correctly in R2020a (Update 3):
syms x(t)
xr = x
diff(xr) % why doesn't it recognise that xr is a function of time?
T = diff(xr)
producing:
xr(t) =
x(t)
ans(t) =
diff(x(t), t)
T(t) =
diff(x(t), t)
I cannot reproduce the error.

4 Commenti

Star could you try this and confirm ?
T = (m1 * diff(xl)^2) / 2 + (m2 * (diff(xr)^2)) / 2
Sure!
syms x(t) m1 m2 xl(t)
xr = x;
T = (m1 * diff(xl)^2) / 2 + (m2 * (diff(xr)^2)) / 2
produces:
T(t) =
(m2*diff(x(t), t)^2)/2 + (m1*diff(xl(t), t)^2)/2
Note the additions to the syms call.
EDIT —
Adding the edited changes:
syms x(t) m1 m2
xr = x;
xl = 0;
T = (m1 * diff(xl)^2) / 2 + (m2 * (diff(xr)^2)) / 2
produces:
T(t) =
[ empty sym ]
most likely because the derivative of a scalar (without specifying the independent variable it is to be differentiated with respect to) does not exist:
Dxl = diff(xl)
produces:
Dxl =
[]
however:
Dxl = diff(xl,t)
produces:
Dxl =
0
.
Sorry for the mess Star, I had two tabs open used a wrong tab to submit the question, I mean for this one:
syms x(t) m1 m2
xr = x;
xl = 0;
T = (m1 * diff(xl)^2) / 2 + (m2 * (diff(xr)^2)) / 2 % it returns empty sym in my version :(
Produces:
T(t) =
[ empty sym ]
Edit: Ah now it's clear adding an empty array to another number gives an empty array, hence it was subtle and surprising.
Thank you Star!
As always, my pleasure!

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2018b

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by