matlab - 在 MATLAB 中求余切导数时出现意外结果

标签 matlab math wolframalpha

任何人都可以解释为什么在 MATLAB 中对余切 (cot) 函数求导会产生与 Wolframalpha 不同的结果吗?

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

syms v d z
F = v + d*(cot(z));
R_v = diff(F,v)     % = 1
R_z = diff(F,z)     % = -d*(cot(z)^2 + 1) 
R_d = diff(F,d)     % = cot(z)

这是 Wolframalpha 的结果:

WA

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

ddx cotx

谁能解答我的困惑?

我使用的是 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.

可以通过使用rewritesimplify从MATLAB获得预期的形式。

 % rewrite the result R_z  in terms in the SIN function
 simplify(rewrite( R_z, 'sin' ) )
 % >> ans = -d/sin(z)^2

但是请注意,这并没有明确定义。来自 simplify docs

Simplification of mathematical expression is not a clearly defined subject. There is no universal idea as to which form of an expression is simplest.

您最好使用上面所示的 simplify(...) = 0 方法,然后根据需要将所需结果打印到屏幕上。如果您不输出结果字符串,则表达式的形式无关紧要,您可以继续前进!


对于wolframalpha,我 cannot reproduce你的问题...

diff

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

% 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

关于matlab - 在 MATLAB 中求余切导数时出现意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48212788/

相关文章:

c - 在 C 中保存多维数组

JavaScript、RegEx - 如何获取实际找到的字符串

wolfram-mathematica - 为什么这个表达式替换不起作用?

matlab - 在结构体元胞数组中查找值

java - 保持 MATLAB 的 "pwd"和内部 "user.dir"同步

database - 整数压缩法

matlab - 如何在 MATLAB 和 WolframAlpha 中计算函数与自身的卷积?

wolfram-mathematica - 如何指定 Wolframalpha 图中哪个轴上的哪个变量?

matlab - 以分钟为单位绘制格式化时间 (min.sec)

algorithm - 在二维网格上找到最大的效果重叠区域