简体   繁体   English

八度-圆函数的渐变无法正确绘制

[英]Octave - gradients of a circle function do not plot correctly

Question

Trying to follow Gradients, Gradient Plots and Tangent Planes . 尝试遵循渐变,渐变图和切平面

The gradient vectors of (X^2 + Y^2) do not show up correctly in Octave 4.2.0 on Windows. (X ^ 2 + Y ^ 2)的梯度向量在Windows的Octave 4.2.0中无法正确显示。 With the code, expected the gradients of a circle diverge from center outwards. 使用该代码,预期的圆的坡度从中心向外发散。 However the actual is diagonal. 但是实际是对角线的。

Please assist to understand what is wrong. 请协助了解问题所在。

syms x y
f1 = x^2 + y^2; 
gradf1 = jacobian(f1,[x,y]);

f1fun  = function_handle(f1);
f1xfun = function_handle(gradf1(1));
f1yfun = function_handle(gradf1(2));

[xx, yy] = meshgrid(-1:.1:1,-1:.1:1);

hold on

contour(xx, yy, f1fun(xx, yy), 10)
quiver(xx, yy, f1xfun(xx, yy), f1yfun(xx, yy), 0.5)
axis equal tight

hold off

Expected 预期

在此处输入图片说明

Actual 实际

在此处输入图片说明

I think you have a bug in your code and the call to quiver should be 我认为您的代码中有一个错误,因此调用quiver应该是

quiver(xx, yy, f1xfun(xx), f1yfun(yy), 0.5)

which then gives (with colormap("jet") ) 然后给出(带有colormap("jet")

来自GNU Octave的剧情

When you perform: 执行时:

f1xfun = function_handle(gradf1(1));
f1yfun = function_handle(gradf1(2));

The output is: 输出为:

f1xfun = 
  @(x) 2 * x        % note: single-argument function
f1yfun =
  @(y) 2 * y        % note: single-argument function

that is AS OPPOSED TO 相反

f1xfun = 
  @(x,y) 2 * x      % two-argument function
f1yfun = 
  @(x,y) 2 * y      % two-argument function

which is what you seem to think was happening. 您似乎以为这正在发生。 (ie the resulting functions actually only take a single input, not both x and y ). (即所得到的功能实际上只需要一个单一的输入,而不是两个xy )。

Therefore later on when you call f1yfun with two inputs, the second input (ie y ) is simply silently discarded , and you are essentially calculating 2*x in both axes, hence the diagonal arrows. 因此,稍后使用两个输入调用f1yfun时,第二个输入(即y )将被简单地静默丢弃 ,并且实际上是在两个轴上计算2*x ,因此是对角箭头。


tl;dr Your call to quiver should be: tl; dr您致电quiver的电话应该是:

 quiver(xx, yy, f1xfun(xx), f1yfun(yy), 0.5); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM