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.

답변 (3개)

Jeremy Kemmerer
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
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
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
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

카테고리

Help CenterFile Exchange에서 Assumptions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by