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.
显示 更早的评论
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.
回答(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
2015-5-6
I noticed in the last couple of days some people hoping to calculate Jacobian with respect to a parameter that is a function. Is there a way to extend this new functionalDerivative to handle that?
My mind is not all that sharp today; I do not see why my previous arguments about the operation not being consistent are not valid?
v a
2018-3-18
Hello I tried this method but there is an error popping out stating that Error in syms assignin('caller',varagin(1),sym(varagin(1)). So could you help me with this?
Walter Roberson
2018-3-18
If you are using R2018a or later, then sym('q1(t)') and sym('q2(t)') will not work. You should instead use
syms q1(t) q2(t)
Walter Roberson
2018-3-18
The code worked for me:
clc
clear all
close all
% define q1 and q2
syms q1(t) 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))
Please go up to the upper right hand corner of your editor window and click on the white downward-pointing triangle. In the menu that shows up, click on Undock . When you do that, more of the command window will become visible; please copy for us the entire error message, everything in red.
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
v a
2018-3-23
thank you so much I finally got it worked out
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);
Walter Roberson
2015-5-5
0 个投票
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
类别
在 帮助中心 和 File Exchange 中查找有关 Code Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!