如何在 Octave 上绘制函数微分图?
我从一开始就想学习三角学。 所以我可以通过将这些命令作为命令来绘制曲线。
x1 = linspace(-2*pi, 2*pi)
y1 = cos(x1)
plot(x1, y1)
但如果我尝试这样做,
x1 = linspace(-2*pi, 2*pi)
y2 = diff(cos(x1))
plot(x1, y1)
我会收到一条错误消息,指出
error: __plt2vv__: vector lengths must match error: called from __plt__>__plt2vv__ at line 487 column 5 __plt__>__plt2__ at line 247 column 14 __plt__ at line 112 column 18 plot at line 229 column 10
“但是,我确实获得了 y2 = diff(cos(x1)) 的值,由于此错误,我无法绘制任何内容。”
注意:我知道 sin(x)
的微分是 cos(x)
,我可以只使用 cos(x)
,但是不能'我这样做吗?
尝试了 diff(cos(90))
我得到了
ans = \[\](0x0)
I was trying to learn trigonometry from the beginning.
so I can plot a curve by giving these as commands.
x1 = linspace(-2*pi, 2*pi)
y1 = cos(x1)
plot(x1, y1)
But If I try to do this,
x1 = linspace(-2*pi, 2*pi)
y2 = diff(cos(x1))
plot(x1, y1)
I receive an error stating
error: __plt2vv__: vector lengths must match error: called from __plt__>__plt2vv__ at line 487 column 5 __plt__>__plt2__ at line 247 column 14 __plt__ at line 112 column 18 plot at line 229 column 10
However, I do get values for y2 = diff(cos(x1))
, I can't plot anything because of this error.
Note: I know the differentiation of sin(x)
is cos(x)
and I can just use cos(x)
, but can't I do it this way?
Tried diff(cos(90))
I got
ans = \[\](0x0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
diff
比其输入短一个元素,因为它是差异[v(2)-v(1), v(3)-v(2), ...]< /代码>。
您需要执行类似 y2 = [NaN, diff(cos(x1))]; 的操作来将数组填充到与 x1 相同的大小。
请注意,这当然会导致偏移,您需要一个 中心差分 方法而不是前向差分方法(如
diff
)来保持事物的良好对齐。diff
is one element shorter than its input, because it it the differences[v(2)-v(1), v(3)-v(2), ...]
.You need to do something like
y2 = [NaN, diff(cos(x1))];
to pad your array to be the same size asx1
.Note that of course this causes an offset, you would need a central differencing method instead of a forward difference method (like
diff
) to keep things well-aligned.