How can I define a sym variable, say t and sym functions say q1(t) and q2(t) such that I can create another function say f=q1*q2 and in one instance differentiate f w.r.t. t and in another differentiate f partially w.r.t q1 or q2.
13 次查看(过去 30 天)
显示 更早的评论
syms t
%define q1 and q2 as functions of t
q1(t) = sym('q1(t)');
q2(t) = sym('q2(t)');
%define new function
f = cos(q1(t)) - sin(q1(t))*sin(q2(t)) + cos(q1(t))*cos(q2(t));
diff(f,t) %This differentiation works but when I try
diff(f,q1) %This gives an error: All arguments, except for the first one, must not be symbolic functions.
Is there anyway to get around this.
What I really want is to find jacobian(f,q1,q2) but I still want to keep q1 and q2 as functions of t.
0 个评论
回答(3 个)
Jeremy Kemmerer
2015-5-6
As of the R2015a release, MATLAB includes a function 'functionalDerivative' in the Symbolic Math Toolbox which will enable you the compute this (partial) derivative:
syms t;
% define q1 and q2
q1(t) = sym('q1(t)');
q2(t) = sym('q2(t)');
% define a new function
f = cos(q1(t)) - sin(q1(t))*sin(q2(t)) + cos(q1(t))*cos(q2(t));
% differentiate f w.r.t. q1(t)
functionalDerivative(f,q1(t))
ans =
- sin(q1(t)) - cos(q1(t))*sin(q2(t)) - cos(q2(t))*sin(q1(t))
8 个评论
Walter Roberson
2018-3-18
You are not using the Symbolic Toolbox. Instead, you have the MATLAB Connector for Maple installed, which is set to invoke Maplesoft's Waterloo Maple 2017 to do symbolic computation. However, the license that you have for that is through a license server and that license server is not reachable.
If you were able to reach the license server for Maple, you would get an error because the Maple command syms for MATLAB does not permit functions to be defined that way. The Maple package also does not have functionalDerivative, at least not under that name.
You might possibly also be licensed for Mathworks Symbolic Toolbox. Use the command
ver symbolic
to see if "Symbolic Math Toolbox" shows up. If it does, then use the pathtool command to move the folder that mentions "maple" in its name to the bottom of the path and save the path, and then you would be able to use the Symbolic Toolbox
Radha Krishna Maddukuri
2015-5-5
Here is a simple example:
syms x
syms f g
f = x^2 + 2*x;
g = 2*x;
diff(f, g)
The expected answer is 'x+1'. However, you will get an error message instead. In this scenario, there are two possible workarounds:
1. If both functions are differentiable, use the Chain Rule:
% df(x)/dg(x) = (df(x)/dx) / (dg(x)/dx)
dfdg = diff(f, x) ./ diff(g, x);
2. If g(x) is an invertible function, use substitution:
% Create new variable gx (mathematically, gx == g, but gx is a syms variable, whereas g is a function)
% Express all instances of x in f(x) in terms of g
syms gx
fg = subs(f, x, solve('gx=2*x', x));
% Compute df/dg
dfdg = diff(fg, gx);
% Express df/dg in terms of x
dfdg = subs(dfdg, gx, 2*x);
0 个评论
Walter Roberson
2015-5-5
Differentiating with respect to an unknown function is not a supported operation because there can be dependencies that alter the derivatives. I gave an example of the failure in a previous answer
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Assumptions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!