Problem with diff(f, diff())

4 views (last 30 days)
Backtobasics
Backtobasics on 9 Sep 2017
Commented: Backtobasics on 11 Sep 2017
Hi there,
I want to differentiate a long equasion L with respect to thetaAdot and ran into a problem. I managed to break it down to the following:
Example 1:
syms x a
f(x, a)=3*x+2*a^2;
df=diff(f, a)
ans=4a -> perfectly fine
Example 2:
syms x a adot
adot=diff(a);
f(x, adot)=3*x+2*adot^2;
df=diff(f, adot)
-> Error
It seems MATLAB has a problem with the derivative but I cannot figure out why? Can you help me with this?
Thank you in advance!

Accepted Answer

Walter Roberson
Walter Roberson on 9 Sep 2017
Your first problem is that you did not declare a to be a symbolic function. diff() applied to a constant variable is going to yield 1, not a placeholder derivative.
syms a(x) adot
adot = diff(a);
but then you have
f(x, adot)=3*x+2*adot^2;
adot is now a function. It is not possible to define a function like f with a parameter that is a function. It is valid to define
f(x) = 3 * x + 2*adot(x)^2
Then you have
df=diff(f, adot)
which attempts take the derivative of f with respect to a function. diff() can only take derivatives with respect to variables. There is functionalDerivative() in newer versions of MATLAB, but the best it would be able to do would be to take the derivative with respect to the function a
>> functionalDerivative(f,a)
ans(x) =
-4*diff(a(x), x, x)
Perhaps you would prefer,
syms AD
f(x, AD) = subs(3 * x + 2*adot(x)^2, adot, AD)
subs(diff(f, AD), AD, adot)
The problem with this is that it assumes that adot and x are independent of each other, which is not the case.
  3 Comments
Walter Roberson
Walter Roberson on 10 Sep 2017
Search your code for
2*L0(ydot*cos(thetaA+theta0)
Notice you missed the * between the L0 and what follows.
Backtobasics
Backtobasics on 11 Sep 2017
Oh dear, thank you very much! I think I checked it a dozen times and didn't see this mistake! Now it works perfectly fine, thank you so so much! :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!