Force symbolic variable to be constant

조회 수: 23 (최근 30일)
Jose Eduardo
Jose Eduardo 2018년 3월 9일
답변: Rahul 2024년 8월 8일
I would like to know if there is a way to impose that a symbolic variable is constant with respect to differentiation, for example.
I have a symbolic variable (e.g. z) which depends on other symbolic variables (e.g. x and y), and it is part of an expression that I want to differentiate. However, I want to impose that z is effectively a constant, so even if it depends on x and y it should not be differenciated with respect to these variables.
I can not substitute x and y by their numerical values, so I need an alternative way to set z as a symbolic constant.
syms x,y,z
z = x + y;
(then I would apply the desired method)
diff(z,x) (and ideally this would be zero)
Thank you in advance

답변 (1개)

Rahul
Rahul 2024년 8월 8일
Hi Jose,
As of MATLAB R2024a, there is no direct functionality for treating a dependent variable or symbol as constant while performing an operation w.r.t. the associated dependent variables, like differentiation. However, a workaround for such a scenario could be initializing another symbol, which could then be treated as a constant in the expression, depending on the use case:
% Define symbolic variables
syms x y z z_d
% Define relations between variables
z = x + y;
% Define an example expression taking constant or independent variable z_d
expr = z_d*x*y;
% Differentiate express w.r.t. required symbol
const_expr = diff(expr, x)
While declaring an expression, z_d and z can be used depending on whether z has to be treated as a constant or variable respectively and later subs function could be used to interchange the symbols for the other case.
% Substitute dependent variable z instead of independent symbol z_d
var_expr = subs(const_expr, z_d, z)
% Perform some operation
...
% Simplify resulting expression to required number of steps
simple_expr = simplify(var_expr, 'Steps', 2)
This will allow you to interchangeably use constant or variable z in an expression and perform subsequent operations.
For further information regarding defining symbols and expressions or differentiation, you can refer to the Symbolic Math Toolbox documentations:
Hope this helps!

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by