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!

 Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Sept. de 2017

1 voto

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 comentarios

Backtobasics
Backtobasics el 10 de Sept. de 2017
Editada: Backtobasics el 10 de Sept. de 2017
Thank you very much for your explanation! I could transfer it to my example but for some reason, the "real" task still leads to errors. So here's what it is all about: I have to differentiate this equation first wrt to thetaAdot and then the result again wrt t.
My code now is
syms t theta0(t) thetaA(t) thetaB(t) x(t) y(t) m2 m1 m0 L2 L1 L0
thetaAdot=diff(thetaA, t);
thetaBdot=diff(thetaB, t);
theta0dot=diff(theta0, t);
xdot=diff(x, t); ydot=diff(y, t);
T=0.5*m2*(xdot^2+ydot^2)+0.5*m1*((L2-0.5*L1)^2*theta0dot^2+xdot^2+xdot^2+2*(L2-0.5*L1)*(ydot*cos(theta0)-xdot*sin(theta0)*theta0dot))+0.5*m0*((L0^2/4)*(thetaAdot+thetaBdot)^2+xdot^2+ydot^2+L0*(ydot*cos(thetaA+theta0)-xdot*sin(thetaA+theta0))*(thetaAdot+theta0dot))+0.5*m0*(L0^2*(thetaAdot+theta0dot)^2+0.25*(thetaBdot+thetaAdot+theta0dot)+0.5*(thetaAdot+theta0dot)*(thetaBdot+thetaAdot+theta0dot)*cos(thetaB)+xdot^2+ydot+2*L0(ydot*cos(thetaA+theta0)-xdot*sin(thetaA+theta0))*(thetaAdot+theta0dot)+L0*(ydot*cos(thetaB+thetaA+theta0)-xdot*sin(thetaB+thetaA+theta0))*(thetaBdot*thetaAdot+theta0dot));
D=functionalDerivative(T, thetaA);
I'm relatively new to MATLAB and I just cannot figure out where I make the mistake(s). I hope you can help me?
Walter Roberson
Walter Roberson el 10 de Sept. de 2017
Search your code for
2*L0(ydot*cos(thetaA+theta0)
Notice you missed the * between the L0 and what follows.
Backtobasics
Backtobasics el 11 de Sept. de 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! :)

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 9 de Sept. de 2017

Comentada:

el 11 de Sept. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by