在MATLAB中求余切导数时的意外结果

在MATLAB中求余切导数时的意外结果,matlab,math,wolframalpha,Matlab,Math,Wolframalpha,有人能解释为什么在MATLAB中求余切(cot)函数的导数会产生与wolframalpha不同的结果吗 这是我在MATLAB中的代码(带有结果注释): 这是wolframalpha的结果: 虽然第一个和最后一个结果都很好,但第二个结果我不明白。据我所知,余切导数是: 有人能澄清我的困惑吗 我使用的是MATLAB R2017a。MATLAB和理论解是等价的。 Matlab为您提供了以下信息: d/dz( cot(z) ) = -( cot(z)^2 + 1 ) 让我们从那里开始工作 = -(

有人能解释为什么在MATLAB中求余切(
cot
)函数的导数会产生与wolframalpha不同的结果吗

这是我在MATLAB中的代码(带有结果注释):

这是wolframalpha的结果:

虽然第一个和最后一个结果都很好,但第二个结果我不明白。据我所知,余切导数是:

有人能澄清我的困惑吗


我使用的是MATLAB R2017a。

MATLAB和理论解是等价的。

Matlab为您提供了以下信息:

d/dz( cot(z) ) = -( cot(z)^2 + 1 )
让我们从那里开始工作

= -( 1/tan(z)^2 + 1 )                     % By definition cot(z)=1/tan(z)
= -( cos(z)^2 / sin(z)^2 + 1 )            % Using tan(z) = sin(z)/cos(z)
= -( (1 - sin(z)^2) / sin(z)^2 + 1 )      % Using cos(z)^2 + sin(z)^2 = 1
= -( 1/sin(z)^2 - sin(z)^2/sin(z)^2 + 1 ) % Expanding the fraction
= -( 1/sin(z)^2 - 1 + 1 )                 % Simplifying
= -1/sin(z)^2                             % Expected result!
因此,正如您在最后所述,MATLAB结果与理论预期完全一致
d
在偏导数中被视为常数,因此保留为系数

如果你不喜欢手工操作,你可以让MATLAB帮你检查等价性

simplify( R_z - (-d/sin(z)^2) ) % = 0, so they are the same.
通过使用
rewrite
simplify
,您可以从MATLAB中获得一个预期的表单

 % rewrite the result R_z  in terms in the SIN function
 simplify(rewrite( R_z, 'sin' ) )
 % >> ans = -d/sin(z)^2
但是,请注意,这并没有很好的定义。从

数学表达式的简化不是一个明确定义的主题对于哪种形式的表达式最简单,没有普遍的概念

您最好使用上面所示的
simplify(…)=0
方法,然后根据需要将所需结果打印到屏幕上。如果您没有输出结果字符串,那么表达式的形式是不相关的,您可以继续


至于wolframalpha,我知道你的问题

但是,您可以再次注意到,您显示的第一个结果是等效的:

% Your result from wolfram alpha
syms x
diff(cot(x), x)
% = - cot(x)^2 - 1
% = - ( cot(x)^2 + 1 ) 
% This is the same as the MATLAB result, same reasoning follows
% Your result from wolfram alpha
syms x
diff(cot(x), x)
% = - cot(x)^2 - 1
% = - ( cot(x)^2 + 1 ) 
% This is the same as the MATLAB result, same reasoning follows