【发布时间】:2019-05-23 14:56:52
【问题描述】:
我以前从未使用过 Matlab,我真的不知道如何修复代码。我需要绘制 log(1000 over k),k 从 1 到 1000。
y = @(x) log(nchoosek(1000,x));
fplot(y,[1 1000]);
错误:
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly
vectorize your function to return an output with the same size and shape as the input
arguments.
In matlab.graphics.function.FunctionLine>getFunction
In matlab.graphics.function.FunctionLine/updateFunction
In matlab.graphics.function.FunctionLine/set.Function_I
In matlab.graphics.function.FunctionLine/set.Function
In matlab.graphics.function.FunctionLine
In fplot>singleFplot (line 241)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 196)
In fplot>vectorizeFplot (line 196)
In fplot (line 166)
In P1 (line 5)
【问题讨论】:
-
nchoosek需要一个标量输入。您应该使用循环来分别计算每个k的结果。但话又说回来,我认为你的练习是为了让你不要使用nchoosek,因为中间值是巨大的,可能不适合双精度浮点数(或者至少给出错误的结果)。 log(n over k) 的方程是什么? -
根本不是重复的。这个问题使用
fplot(它接受一个匿名函数作为输入),而另一个问题使用plot(它接受数字向量)。此外,由于 OP 的方法涉及大量数字,这个问题存在潜在的数值精度问题 -
@LuisMendo 考虑到他的数据都是离散的,对于他来说,在这个应用程序中使用 plot 可能比 fplot 更有意义。
标签: matlab plot logarithm binomial-coefficients